diff --git a/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php index 4ba98ba113..81097dd552 100644 --- a/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php +++ b/app/Generator/Chart/Account/ChartJsAccountChartGenerator.php @@ -3,10 +3,8 @@ namespace FireflyIII\Generator\Chart\Account; use Carbon\Carbon; -use Config; use FireflyIII\Models\Account; use Illuminate\Support\Collection; -use Preferences; use Steam; /** @@ -90,20 +88,21 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator public function frontpage(Collection $accounts, Carbon $start, Carbon $end) { // language: - $format = trans('config.month_and_day'); - $data = [ + $format = trans('config.month_and_day'); + $data = [ 'count' => 0, 'labels' => [], 'datasets' => [], ]; - $current = clone $start; + $current = clone $start; while ($current <= $end) { $data['labels'][] = $current->formatLocalized($format); $current->addDay(); } + foreach ($accounts as $account) { - $set = [ + $set = [ 'label' => $account->name, 'fillColor' => 'rgba(220,220,220,0.2)', 'strokeColor' => 'rgba(220,220,220,1)', @@ -113,9 +112,15 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator 'pointHighlightStroke' => 'rgba(220,220,220,1)', 'data' => [], ]; - $current = clone $start; + $current = clone $start; + $range = Steam::balanceInRange($account, $start, $end); + $previous = array_values($range)[0]; while ($current <= $end) { - $set['data'][] = Steam::balance($account, $current); + $format = $current->format('Y-m-d'); + $balance = isset($range[$format]) ? $range[$format] : $previous; + + $set['data'][] = $balance; + $previous = $balance; $current->addDay(); } $data['datasets'][] = $set; @@ -135,7 +140,7 @@ class ChartJsAccountChartGenerator implements AccountChartGenerator public function single(Account $account, Carbon $start, Carbon $end) { // language: - $format = trans('config.month_and_day'); + $format = trans('config.month_and_day'); $data = [ 'count' => 1, diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 2a5d64d07c..8a1118ac6f 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -72,6 +72,54 @@ class Steam return round($balance, 2); } + /** + * Gets the balance for the given account during the whole range, using this format: + * + * [yyyy-mm-dd] => 123,2 + * + * @param Account $account + * @param Carbon $start + * @param Carbon $end + */ + public function balanceInRange(Account $account, Carbon $start, Carbon $end) + { + // abuse chart properties: + $cache = new CacheProperties; + $cache->addProperty($account->id); + $cache->addProperty('balance-in-range'); + $cache->addProperty($start); + $cache->addProperty($end); + if ($cache->has()) { + return $cache->get(); // @codeCoverageIgnore + } + + $balances = []; + $start->subDay(); + $end->addDay(); + $startBalance = $this->balance($account, $start); + $balances[$start->format('Y-m-d')] = $startBalance; + $start->addDay(); + + // query! + $set = $account->transactions() + ->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') + ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) + ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) + ->groupBy('transaction_journals.date') + ->get(['transaction_journals.date', DB::Raw('SUM(`transactions`.`amount`) as `modified`')]); + $currentBalance = $startBalance; + foreach ($set as $entry) { + $currentBalance = bcadd($currentBalance, $entry->modified); + $balances[$entry->date] = $currentBalance; + } + + $cache->store($balances); + + return $balances; + + + } + /** * * @param array $ids