From 07b55bd71f3e7a17071c5b9290892878816d8fb5 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 19 Nov 2024 06:31:49 +0100 Subject: [PATCH] Fix https://github.com/firefly-iii/firefly-iii/issues/9477 --- .../Controllers/Chart/AccountController.php | 15 ++++---- .../Controllers/Chart/ReportController.php | 37 +++++++++++++++++-- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index 1c2a660b65..aad9c2fb66 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -40,6 +40,7 @@ use FireflyIII\Support\Http\Controllers\ChartGeneration; use FireflyIII\Support\Http\Controllers\DateCalculation; use Illuminate\Http\JsonResponse; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; /** * Class AccountController. @@ -300,13 +301,13 @@ class AccountController extends Controller $start = clone session('start', today(config('app.timezone'))->startOfMonth()); $end = clone session('end', today(config('app.timezone'))->endOfMonth()); $defaultSet = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET])->pluck('id')->toArray(); - app('log')->debug('Default set is ', $defaultSet); + Log::debug('Default set is ', $defaultSet); $frontpage = app('preferences')->get('frontpageAccounts', $defaultSet); $frontpageArray = !is_array($frontpage->data) ? [] : $frontpage->data; - app('log')->debug('Frontpage preference set is ', $frontpageArray); + Log::debug('Frontpage preference set is ', $frontpageArray); if (0 === count($frontpageArray)) { app('preferences')->set('frontpageAccounts', $defaultSet); - app('log')->debug('frontpage set is empty!'); + Log::debug('frontpage set is empty!'); } $accounts = $repository->getAccountsById($frontpageArray); @@ -414,7 +415,7 @@ class AccountController extends Controller */ private function periodByCurrency(Carbon $start, Carbon $end, Account $account, TransactionCurrency $currency): array { - app('log')->debug(sprintf('Now in periodByCurrency("%s", "%s", %s, "%s")', $start->format('Y-m-d'), $end->format('Y-m-d'), $account->id, $currency->code)); + Log::debug(sprintf('Now in periodByCurrency("%s", "%s", %s, "%s")', $start->format('Y-m-d'), $end->format('Y-m-d'), $account->id, $currency->code)); $locale = app('steam')->getLocale(); $step = $this->calculateStep($start, $end); $result = [ @@ -424,13 +425,13 @@ class AccountController extends Controller ]; $entries = []; $current = clone $start; - app('log')->debug(sprintf('Step is %s', $step)); + Log::debug(sprintf('Step is %s', $step)); // fix for issue https://github.com/firefly-iii/firefly-iii/issues/8041 // have to make sure this chart is always based on the balance at the END of the period. // This period depends on the size of the chart $current = app('navigation')->endOfX($current, $step, null); - app('log')->debug(sprintf('$current date is %s', $current->format('Y-m-d'))); + Log::debug(sprintf('$current date is %s', $current->format('Y-m-d'))); if ('1D' === $step) { // per day the entire period, balance for every day. $format = (string)trans('config.month_and_day_js', [], $locale); @@ -447,7 +448,7 @@ class AccountController extends Controller } if ('1W' === $step || '1M' === $step || '1Y' === $step) { while ($end >= $current) { - app('log')->debug(sprintf('Current is: %s', $current->format('Y-m-d'))); + Log::debug(sprintf('Current is: %s', $current->format('Y-m-d'))); $balance = (float)app('steam')->balance($account, $current, $currency); $label = app('navigation')->periodShow($current, $step); $entries[$label] = $balance; diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index 4783f01286..5db07dd1b5 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Http\Controllers\Chart; use Carbon\Carbon; +use FireflyIII\Enums\TransactionTypeEnum; use FireflyIII\Generator\Chart\Basic\GeneratorInterface; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Helpers\Report\NetWorthInterface; @@ -36,6 +37,7 @@ use FireflyIII\Support\Http\Controllers\BasicDataSupport; use FireflyIII\Support\Http\Controllers\ChartGeneration; use Illuminate\Http\JsonResponse; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Log; /** * Class ReportController. @@ -88,7 +90,7 @@ class ReportController extends Controller $includeNetWorth = $accountRepository->getMetaValue($account, 'include_net_worth'); $result = null === $includeNetWorth ? true : '1' === $includeNetWorth; if (false === $result) { - app('log')->debug(sprintf('Will not include "%s" in net worth charts.', $account->name)); + Log::debug(sprintf('Will not include "%s" in net worth charts.', $account->name)); } return $result; @@ -136,6 +138,7 @@ class ReportController extends Controller */ public function operations(Collection $accounts, Carbon $start, Carbon $end): JsonResponse { + $end->endOfDay(); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty('chart.report.operations'); @@ -146,7 +149,8 @@ class ReportController extends Controller // return response()->json($cache->get()); } - app('log')->debug('Going to do operations for accounts ', $accounts->pluck('id')->toArray()); + Log::debug('Going to do operations for accounts ', $accounts->pluck('id')->toArray()); + Log::debug(sprintf('Period: %s to %s', $start->toW3cString(), $end->toW3cString())); $format = app('navigation')->preferredCarbonFormat($start, $end); $titleFormat = app('navigation')->preferredCarbonLocalizedFormat($start, $end); $preferredRange = app('navigation')->preferredRangeFormat($start, $end); @@ -158,7 +162,13 @@ class ReportController extends Controller $collector = app(GroupCollectorInterface::class); $collector->setRange($start, $end)->withAccountInformation(); $collector->setXorAccounts($accounts); - $collector->setTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::RECONCILIATION, TransactionType::TRANSFER]); + $collector->setTypes( + [ + TransactionTypeEnum::WITHDRAWAL, + TransactionTypeEnum::DEPOSIT, + TransactionTypeEnum::RECONCILIATION, + TransactionTypeEnum::TRANSFER + ]); $journals = $collector->getExtractedJournals(); // loop. group by currency and by period. @@ -184,15 +194,23 @@ class ReportController extends Controller // deposit = incoming // transfer or reconcile or opening balance, and these accounts are the destination. - if (TransactionType::DEPOSIT === $journal['transaction_type_type'] || ((TransactionType::TRANSFER === $journal['transaction_type_type'] || TransactionType::RECONCILIATION === $journal['transaction_type_type'] || TransactionType::OPENING_BALANCE === $journal['transaction_type_type']) && in_array($journal['destination_account_id'], $ids, true))) { + if ( + TransactionTypeEnum::DEPOSIT->value === $journal['transaction_type_type'] || + (( + TransactionTypeEnum::TRANSFER->value === $journal['transaction_type_type'] || + TransactionTypeEnum::RECONCILIATION->value === $journal['transaction_type_type'] || + TransactionTypeEnum::OPENING_BALANCE->value === $journal['transaction_type_type']) && + in_array($journal['destination_account_id'], $ids, true))) { $key = 'earned'; } $data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount); } // loop this data, make chart bars for each currency: + Log::debug('Looping data'); /** @var array $currency */ foreach ($data as $currency) { + Log::debug(sprintf('Now processing currency "%s"', $currency['currency_name'])); $income = [ 'label' => (string)trans('firefly.box_earned_in_currency', ['currency' => $currency['currency_name']]), 'type' => 'bar', @@ -214,12 +232,15 @@ class ReportController extends Controller // loop all possible periods between $start and $end $currentStart = clone $start; $currentEnd = clone $end; + Log::debug(sprintf('START current start and end: %s and %s', $currentStart->toW3cString(), $currentEnd->toW3cString())); // #8374. Sloppy fix for yearly charts. Not really interested in a better fix with v2 layout and all. if ('1Y' === $preferredRange) { $currentEnd = app('navigation')->endOfPeriod($currentEnd, $preferredRange); } + Log::debug('Start of sub-loop'); while ($currentStart <= $currentEnd) { + Log::debug(sprintf('Current start: %s', $currentStart->toW3cString())); $key = $currentStart->format($format); $title = $currentStart->isoFormat($titleFormat); // #8663 make sure the period exists in the data previously collected. @@ -227,12 +248,20 @@ class ReportController extends Controller $income['entries'][$title] = app('steam')->bcround($currency[$key]['earned'] ?? '0', $currency['currency_decimal_places']); $expense['entries'][$title] = app('steam')->bcround($currency[$key]['spent'] ?? '0', $currency['currency_decimal_places']); } + // #9477 if the period is not in the data, add it with zero values. + if (!array_key_exists($key, $currency)) { + $income['entries'][$title] = '0'; + $expense['entries'][$title] = '0'; + + } $currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0); } + Log::debug('End of sub-loop'); $chartData[] = $income; $chartData[] = $expense; } + Log::debug('End of loop'); $data = $this->generator->multiSet($chartData); $cache->store($data);