diff --git a/app/Http/Controllers/Transaction/IndexController.php b/app/Http/Controllers/Transaction/IndexController.php index 6229226676..ad0b2b3931 100644 --- a/app/Http/Controllers/Transaction/IndexController.php +++ b/app/Http/Controllers/Transaction/IndexController.php @@ -103,10 +103,11 @@ class IndexController extends Controller $firstJournal = $this->repository->firstNull(); $startPeriod = $firstJournal instanceof TransactionJournal ? $firstJournal->date : new Carbon(); $endPeriod = clone $end; - - // limit to 3 years for the time being. - if (now()->diffInYears($startPeriod, true) > 3) { - $startPeriod = now()->subYears(3); + $endPeriod->endOfDay(); + // limit to 6 years for the time being. + $max = 6; + if (now()->diffInYears($startPeriod, true) > $max) { + $startPeriod = now()->subYears($max); } $periods = $this->getTransactionPeriodOverview($objectType, $startPeriod, $endPeriod); diff --git a/app/Repositories/PeriodStatistic/PeriodStatisticRepository.php b/app/Repositories/PeriodStatistic/PeriodStatisticRepository.php index 450281696c..8160b679b1 100644 --- a/app/Repositories/PeriodStatistic/PeriodStatisticRepository.php +++ b/app/Repositories/PeriodStatistic/PeriodStatisticRepository.php @@ -49,6 +49,8 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U #[Override] public function allInRangeForPrefix(string $prefix, Carbon $start, Carbon $end): Collection { + Log::debug(sprintf('Collect all statistics where type starts with "%s"', $prefix)); + Log::debug(sprintf('Between %s and %s', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s'))); return $this->userGroup ->periodStatistics() ->where('type', 'LIKE', sprintf('%s%%', $prefix)) @@ -156,6 +158,7 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U int $count, string $amount ): PeriodStatistic { + Log::debug(sprintf('Store as type "%s"', sprintf('%s_%s', $prefix, $type))); $stat = new PeriodStatistic(); $stat->transaction_currency_id = $currencyId; $stat->user_group_id = $this->getUserGroup()->id; diff --git a/app/Support/Http/Controllers/PeriodOverview.php b/app/Support/Http/Controllers/PeriodOverview.php index 3b3dc9cf3a..c62f6d8526 100644 --- a/app/Support/Http/Controllers/PeriodOverview.php +++ b/app/Support/Http/Controllers/PeriodOverview.php @@ -37,7 +37,6 @@ use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\PeriodStatistic\PeriodStatisticRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface; -use FireflyIII\Support\CacheProperties; use FireflyIII\Support\Facades\Amount; use FireflyIII\Support\Facades\Navigation; use FireflyIII\Support\Facades\Steam; @@ -75,52 +74,13 @@ use Illuminate\Support\Str; */ trait PeriodOverview { - protected AccountRepositoryInterface $accountRepository; - protected CategoryRepositoryInterface $categoryRepository; - protected TagRepositoryInterface $tagRepository; - protected JournalRepositoryInterface $journalRepos; + protected AccountRepositoryInterface $accountRepository; + protected CategoryRepositoryInterface $categoryRepository; + protected TagRepositoryInterface $tagRepository; + protected JournalRepositoryInterface $journalRepos; protected PeriodStatisticRepositoryInterface $periodStatisticRepo; - private Collection $statistics; // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - // temp data holder - private array $transactions; // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder - - // temp data holder + private Collection $statistics; + private array $transactions; /** * This method returns "period entries", so nov-2015, dec-2015, etc. (this depends on the users session range) @@ -132,18 +92,18 @@ trait PeriodOverview protected function getAccountPeriodOverview(Account $account, Carbon $start, Carbon $end): array { Log::debug(sprintf('Now in getAccountPeriodOverview(#%d, %s %s)', $account->id, $start->format('Y-m-d H:i:s.u'), $end->format('Y-m-d H:i:s.u'))); - $this->accountRepository = app(AccountRepositoryInterface::class); + $this->accountRepository = app(AccountRepositoryInterface::class); $this->accountRepository->setUser($account->user); $this->periodStatisticRepo = app(PeriodStatisticRepositoryInterface::class); $range = Navigation::getViewRange(true); - [$start, $end] = $end < $start ? [$end, $start] : [$start, $end]; + [$start, $end] = $end < $start ? [$end, $start] : [$start, $end]; /** @var array $dates */ - $dates = Navigation::blockPeriods($start, $end, $range); - [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end); - $this->statistics = $this->periodStatisticRepo->allInRangeForModel($account, $start, $end); + $dates = Navigation::blockPeriods($start, $end, $range); + [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end); + $this->statistics = $this->periodStatisticRepo->allInRangeForModel($account, $start, $end); - $entries = []; + $entries = []; Log::debug(sprintf('Count of loops: %d', count($dates))); foreach ($dates as $currentDate) { $entries[] = $this->getSingleModelPeriod($account, $currentDate['period'], $currentDate['start'], $currentDate['end']); @@ -160,18 +120,18 @@ trait PeriodOverview */ protected function getCategoryPeriodOverview(Category $category, Carbon $start, Carbon $end): array { - $this->categoryRepository = app(CategoryRepositoryInterface::class); + $this->categoryRepository = app(CategoryRepositoryInterface::class); $this->categoryRepository->setUser($category->user); $this->periodStatisticRepo = app(PeriodStatisticRepositoryInterface::class); - $range = Navigation::getViewRange(true); - [$start, $end] = $end < $start ? [$end, $start] : [$start, $end]; + $range = Navigation::getViewRange(true); + [$start, $end] = $end < $start ? [$end, $start] : [$start, $end]; /** @var array $dates */ - $dates = Navigation::blockPeriods($start, $end, $range); - $entries = []; - [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end); - $this->statistics = $this->periodStatisticRepo->allInRangeForModel($category, $start, $end); + $dates = Navigation::blockPeriods($start, $end, $range); + $entries = []; + [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end); + $this->statistics = $this->periodStatisticRepo->allInRangeForModel($category, $start, $end); Log::debug(sprintf('Count of loops: %d', count($dates))); foreach ($dates as $currentDate) { @@ -193,13 +153,13 @@ trait PeriodOverview Log::debug(sprintf('Now in getNoModelPeriodOverview(%s, %s %s)', $model, $start->format('Y-m-d'), $end->format('Y-m-d'))); $this->periodStatisticRepo = app(PeriodStatisticRepositoryInterface::class); $range = Navigation::getViewRange(true); - [$start, $end] = $end < $start ? [$end, $start] : [$start, $end]; + [$start, $end] = $end < $start ? [$end, $start] : [$start, $end]; /** @var array $dates */ - $dates = Navigation::blockPeriods($start, $end, $range); - [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end); - $entries = []; - $this->statistics = $this->periodStatisticRepo->allInRangeForPrefix(sprintf('no_%s', $model), $start, $end); + $dates = Navigation::blockPeriods($start, $end, $range); + [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end); + $entries = []; + $this->statistics = $this->periodStatisticRepo->allInRangeForPrefix(sprintf('no_%s', $model), $start, $end); Log::debug(sprintf('Collected %d stats', $this->statistics->count())); foreach ($dates as $currentDate) { @@ -209,6 +169,7 @@ trait PeriodOverview return $entries; } + protected function getSingleModelPeriod(Model $model, string $period, Carbon $start, Carbon $end): array { Log::debug(sprintf('Now in getSingleModelPeriod(%s #%d, %s %s)', $model::class, $model->id, $start->format('Y-m-d'), $end->format('Y-m-d'))); @@ -224,7 +185,7 @@ trait PeriodOverview ]; $this->transactions = []; foreach ($types as $type) { - $set = $this->getSingleModelPeriodByType($model, $start, $end, $type); + $set = $this->getSingleModelPeriodByType($model, $start, $end, $type); $return['total_transactions'] += $set['count']; unset($set['count']); $return[$type] = $set; @@ -240,18 +201,18 @@ trait PeriodOverview */ protected function getTagPeriodOverview(Tag $tag, Carbon $start, Carbon $end): array { // period overview for tags. - $this->tagRepository = app(TagRepositoryInterface::class); + $this->tagRepository = app(TagRepositoryInterface::class); $this->tagRepository->setUser($tag->user); $this->periodStatisticRepo = app(PeriodStatisticRepositoryInterface::class); - $range = Navigation::getViewRange(true); - [$start, $end] = $end < $start ? [$end, $start] : [$start, $end]; + $range = Navigation::getViewRange(true); + [$start, $end] = $end < $start ? [$end, $start] : [$start, $end]; /** @var array $dates */ - $dates = Navigation::blockPeriods($start, $end, $range); - $entries = []; - [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end); - $this->statistics = $this->periodStatisticRepo->allInRangeForModel($tag, $start, $end); + $dates = Navigation::blockPeriods($start, $end, $range); + $entries = []; + [$start, $end] = $this->getPeriodFromBlocks($dates, $start, $end); + $this->statistics = $this->periodStatisticRepo->allInRangeForModel($tag, $start, $end); Log::debug(sprintf('Count of loops: %d', count($dates))); foreach ($dates as $currentDate) { @@ -266,65 +227,56 @@ trait PeriodOverview */ protected function getTransactionPeriodOverview(string $transactionType, Carbon $start, Carbon $end): array { - $range = Navigation::getViewRange(true); - $types = config(sprintf('firefly.transactionTypesByType.%s', $transactionType)); + $this->periodStatisticRepo = app(PeriodStatisticRepositoryInterface::class); + $range = Navigation::getViewRange(true); [$start, $end] = $end < $start ? [$end, $start] : [$start, $end]; - // properties for cache - $cache = new CacheProperties(); - $cache->addProperty($start); - $cache->addProperty($end); - $cache->addProperty('transactions-period-entries'); - $cache->addProperty($transactionType); - if ($cache->has()) { - return $cache->get(); - } - /** @var array $dates */ - $dates = Navigation::blockPeriods($start, $end, $range); - $entries = []; - $spent = []; - $earned = []; - $transferred = []; - // collect all journals in this period (regardless of type) - $collector = app(GroupCollectorInterface::class); - $collector->setTypes($types)->setRange($start, $end); - $genericSet = $collector->getExtractedJournals(); - $loops = 0; + $dates = Navigation::blockPeriods($start, $end, $range); + $entries = []; + $this->statistics = $this->periodStatisticRepo->allInRangeForPrefix('all_', $start, $end); + Log::debug(sprintf('Collected %d statistics', $this->statistics->count())); foreach ($dates as $currentDate) { - $title = Navigation::periodShow($currentDate['end'], $currentDate['period']); - - if ($loops < 10) { - // set to correct array - if ('expenses' === $transactionType || 'withdrawal' === $transactionType) { - $spent = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']); - } - if ('revenue' === $transactionType || 'deposit' === $transactionType) { - $earned = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']); - } - if ('transfer' === $transactionType || 'transfers' === $transactionType) { - $transferred = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']); - } - } - $entries[] = [ - 'title' => $title, - 'route' => route('transactions.index', [ - $transactionType, - $currentDate['start']->format('Y-m-d'), - $currentDate['end']->format('Y-m-d'), - ]), - 'total_transactions' => count($spent) + count($earned) + count($transferred), - 'spent' => $this->groupByCurrency($spent), - 'earned' => $this->groupByCurrency($earned), - 'transferred' => $this->groupByCurrency($transferred), - ]; - ++$loops; + $entries[] = $this->getGenericPeriod($transactionType, $currentDate['period'], $currentDate['start'], $currentDate['end']); } return $entries; } + protected function getGenericPeriod(string $type, string $period, Carbon $start, Carbon $end): array + { + $return = [ + 'title' => Navigation::periodShow($start, $period), + 'route' => route('transactions.index', [ + $type, + $start->format('Y-m-d'), + $end->format('Y-m-d'), + ]), + 'total_transactions' => 0, + ]; + $setTypes = [ + 'withdrawal' => 'spent', + 'expenses' => 'spent', + 'deposit' => 'earned', + 'revenue' => 'earned', + 'transfer' => 'transferred', + 'transfers' => 'transferred', + ]; + if (!array_key_exists($type, $setTypes)) { + throw new FireflyException(sprintf('[c] Cannot deal with type "%s"', $type)); + } + $setType = $setTypes[$type]; + + $this->transactions = []; + $set = $this->getSingleGenericPeriodByType($start, $end, $type); + $return['total_transactions'] += $set['count']; + $return[$setType] = $set; + + return $return; + } + + /** * Filter a list of journals by a set of dates, and then group them by currency. */ @@ -351,7 +303,7 @@ trait PeriodOverview } return $this->statistics->filter( - static fn (PeriodStatistic $statistic): bool => ( + static fn(PeriodStatistic $statistic): bool => ( $statistic->start->eq($start) && $statistic->end->eq($end) && str_starts_with($statistic->type, $prefix) @@ -366,9 +318,11 @@ trait PeriodOverview return new Collection(); } - + Log::debug(sprintf('Now in filterStatistics("%s")', $type)); return $this->statistics->filter( - static fn (PeriodStatistic $statistic): bool => $statistic->start->eq($start) && $statistic->end->eq($end) && $statistic->type === $type + static function (PeriodStatistic $statistic) use ($start, $end, $type): bool { + return $statistic->start->isSameSecond($start) && $statistic->end->isSameSecond($end) && $statistic->type === $type; + } ); } @@ -381,7 +335,7 @@ trait PeriodOverview $fits = $item['type'] === $type->value && $date >= $start && $date <= $end; if ($fits) { // if type is withdrawal, negative amount: - if (TransactionTypeEnum::WITHDRAWAL === $type && 1 === bccomp((string) $item['amount'], '0')) { + if (TransactionTypeEnum::WITHDRAWAL === $type && 1 === bccomp((string)$item['amount'], '0')) { $item['amount'] = Steam::negative($item['amount']); } $result[] = $item; @@ -398,12 +352,12 @@ trait PeriodOverview foreach ($this->transactions as $item) { $date = Carbon::parse($item['date']); if ($date >= $start && $date <= $end) { - if ('Transfer' === $item['type'] && 'away' === $direction && -1 === bccomp((string) $item['amount'], '0')) { + if ('Transfer' === $item['type'] && 'away' === $direction && -1 === bccomp((string)$item['amount'], '0')) { $result[] = $item; continue; } - if ('Transfer' === $item['type'] && 'in' === $direction && 1 === bccomp((string) $item['amount'], '0')) { + if ('Transfer' === $item['type'] && 'in' === $direction && 1 === bccomp((string)$item['amount'], '0')) { $result[] = $item; } } @@ -434,18 +388,21 @@ trait PeriodOverview private function getSingleModelPeriodByType(Model $model, Carbon $start, Carbon $end, string $type): array { Log::debug(sprintf( - 'Now in getSingleModelPeriodByType(%s #%d, %s %s, %s)', - $model::class, - $model->id, - $start->format('Y-m-d'), - $end->format('Y-m-d'), - $type - )); + 'Now in getSingleModelPeriodByType(%s #%d, %s %s, %s)', + $model::class, + $model->id, + $start->format('Y-m-d H:i:s.u'), + $end->format('Y-m-d H:i:s.u'), + $type + )); $statistics = $this->filterStatistics($start, $end, $type); // nothing found, regenerate them. if (0 === $statistics->count()) { - Log::debug(sprintf('Found nothing in this period for type "%s"', $type)); + Log::debug(sprintf('Found nothing between %s and %s for type "%s"', + $start->format('Y-m-d H:i:s.u'), + $end->format('Y-m-d H:i:s.u'), + $type)); if (0 === count($this->transactions)) { switch ($model::class) { default: @@ -470,7 +427,7 @@ trait PeriodOverview switch ($type) { default: - throw new FireflyException(sprintf('Cannot deal with category period type %s', $type)); + throw new FireflyException(sprintf('Cannot deal with type %s', $type)); case 'spent': $result = $this->filterTransactionsByType(TransactionTypeEnum::WITHDRAWAL, $start, $end); @@ -500,22 +457,64 @@ trait PeriodOverview return $grouped; } - $grouped = ['count' => 0]; + $grouped = ['count' => 0]; /** @var PeriodStatistic $statistic */ foreach ($statistics as $statistic) { - $id = (int) $statistic->transaction_currency_id; - $currency = Amount::getTransactionCurrencyById($id); - $grouped[$id] = [ - 'amount' => (string) $statistic->amount, - 'count' => (int) $statistic->count, + $id = (int)$statistic->transaction_currency_id; + $currency = Amount::getTransactionCurrencyById($id); + $grouped[$id] = [ + 'amount' => (string)$statistic->amount, + 'count' => (int)$statistic->count, 'currency_id' => $currency->id, 'currency_name' => $currency->name, 'currency_code' => $currency->code, 'currency_symbol' => $currency->symbol, 'currency_decimal_places' => $currency->decimal_places, ]; - $grouped['count'] += (int) $statistic->count; + $grouped['count'] += (int)$statistic->count; + } + + return $grouped; + } + + private function getSingleGenericPeriodByType(Carbon $start, Carbon $end, string $type): array + { + $filterType = sprintf('all_%s', $type); + $statistics = $this->filterStatistics($start, $end, $filterType); + $types = config(sprintf('firefly.transactionTypesByType.%s', $type)); + // nothing found, regenerate them. + if (0 === $statistics->count()) { + if (0 === count($this->transactions)) { + // get collection! + // collect all journals in this period (regardless of type) + $collector = app(GroupCollectorInterface::class); + $collector->setTypes($types)->setRange($start, $end); + $this->transactions = $collector->getExtractedJournals(); + Log::debug(sprintf('Going to group %d found journal(s)', count($types))); + } + + $grouped = $this->groupByCurrency($this->filterJournalsByDate($this->transactions, $start, $end)); + $this->saveGroupedForPrefix('all', $start, $end, $type, $grouped); + + return $grouped; + } + $grouped = ['count' => 0]; + + /** @var PeriodStatistic $statistic */ + foreach ($statistics as $statistic) { + $id = (int)$statistic->transaction_currency_id; + $currency = Amount::getTransactionCurrencyById($id); + $grouped[$id] = [ + 'amount' => (string)$statistic->amount, + 'count' => (int)$statistic->count, + 'currency_id' => $currency->id, + 'currency_name' => $currency->name, + 'currency_code' => $currency->code, + 'currency_symbol' => $currency->symbol, + 'currency_decimal_places' => $currency->decimal_places, + ]; + $grouped['count'] += (int)$statistic->count; } return $grouped; @@ -532,12 +531,12 @@ trait PeriodOverview switch ($model) { default: - throw new FireflyException(sprintf('Cannot deal with model of type "%s"', $model)); + throw new FireflyException(sprintf('[b] Cannot deal with model of type "%s"', $model)); case 'budget': // get all expenses without a budget. /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); + $collector = app(GroupCollectorInterface::class); $collector->setRange($start, $end)->withoutBudget()->withAccountInformation()->setTypes([TransactionTypeEnum::WITHDRAWAL->value]); $spent = $collector->getExtractedJournals(); $earned = []; @@ -548,23 +547,23 @@ trait PeriodOverview case 'category': // collect all expenses in this period: /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); + $collector = app(GroupCollectorInterface::class); $collector->withoutCategory(); $collector->setRange($start, $end); $collector->setTypes([TransactionTypeEnum::DEPOSIT->value]); - $earned = $collector->getExtractedJournals(); + $earned = $collector->getExtractedJournals(); // collect all income in this period: /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); + $collector = app(GroupCollectorInterface::class); $collector->withoutCategory(); $collector->setRange($start, $end); $collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value]); - $spent = $collector->getExtractedJournals(); + $spent = $collector->getExtractedJournals(); // collect all transfers in this period: /** @var GroupCollectorInterface $collector */ - $collector = app(GroupCollectorInterface::class); + $collector = app(GroupCollectorInterface::class); $collector->withoutCategory(); $collector->setRange($start, $end); $collector->setTypes([TransactionTypeEnum::TRANSFER->value]); @@ -575,10 +574,10 @@ trait PeriodOverview $groupedSpent = $this->groupByCurrency($spent); $groupedEarned = $this->groupByCurrency($earned); $groupedTransferred = $this->groupByCurrency($transferred); - $entry = ['title' => $title, 'route' => route(sprintf('%s.no-%s', Str::plural($model), $model), [ + $entry = ['title' => $title, 'route' => route(sprintf('%s.no-%s', Str::plural($model), $model), [ $start->format('Y-m-d'), $end->format('Y-m-d'), - ]), 'total_transactions' => count($spent), 'spent' => $groupedSpent, 'earned' => $groupedEarned, 'transferred' => $groupedTransferred]; + ]), 'total_transactions' => count($spent), 'spent' => $groupedSpent, 'earned' => $groupedEarned, 'transferred' => $groupedTransferred]; $this->saveGroupedForPrefix(sprintf('no_%s', $model), $start, $end, 'spent', $groupedSpent); $this->saveGroupedForPrefix(sprintf('no_%s', $model), $start, $end, 'earned', $groupedEarned); $this->saveGroupedForPrefix(sprintf('no_%s', $model), $start, $end, 'transferred', $groupedTransferred); @@ -587,30 +586,30 @@ trait PeriodOverview } Log::debug(sprintf('Found %d statistics in period %s - %s.', count($statistics), $start->format('Y-m-d'), $end->format('Y-m-d'))); - $entry = ['title' => $title, 'route' => route(sprintf('%s.no-%s', Str::plural($model), $model), [ + $entry = ['title' => $title, 'route' => route(sprintf('%s.no-%s', Str::plural($model), $model), [ $start->format('Y-m-d'), $end->format('Y-m-d'), - ]), 'total_transactions' => 0, 'spent' => [], 'earned' => [], 'transferred' => []]; - $grouped = []; + ]), 'total_transactions' => 0, 'spent' => [], 'earned' => [], 'transferred' => []]; + $grouped = []; /** @var PeriodStatistic $statistic */ foreach ($statistics as $statistic) { - $type = str_replace(sprintf('no_%s_', $model), '', $statistic->type); - $id = (int) $statistic->transaction_currency_id; - $currency = Amount::getTransactionCurrencyById($id); + $type = str_replace(sprintf('no_%s_', $model), '', $statistic->type); + $id = (int)$statistic->transaction_currency_id; + $currency = Amount::getTransactionCurrencyById($id); $grouped[$type]['count'] ??= 0; - $grouped[$type][$id] = [ - 'amount' => (string) $statistic->amount, - 'count' => (int) $statistic->count, + $grouped[$type][$id] = [ + 'amount' => (string)$statistic->amount, + 'count' => (int)$statistic->count, 'currency_id' => $currency->id, 'currency_name' => $currency->name, 'currency_code' => $currency->code, 'currency_symbol' => $currency->symbol, 'currency_decimal_places' => $currency->decimal_places, ]; - $grouped[$type]['count'] += (int) $statistic->count; + $grouped[$type]['count'] += (int)$statistic->count; } - $types = ['spent', 'earned', 'transferred']; + $types = ['spent', 'earned', 'transferred']; foreach ($types as $type) { if (array_key_exists($type, $grouped)) { $entry['total_transactions'] += $grouped[$type]['count']; @@ -632,16 +631,16 @@ trait PeriodOverview /** @var array $journal */ foreach ($journals as $journal) { - $currencyId = (int) $journal['currency_id']; - $currencyCode = $journal['currency_code']; - $currencyName = $journal['currency_name']; - $currencySymbol = $journal['currency_symbol']; - $currencyDecimalPlaces = $journal['currency_decimal_places']; - $foreignCurrencyId = $journal['foreign_currency_id']; - $amount = (string) ($journal['amount'] ?? '0'); + $currencyId = (int)$journal['currency_id']; + $currencyCode = $journal['currency_code']; + $currencyName = $journal['currency_name']; + $currencySymbol = $journal['currency_symbol']; + $currencyDecimalPlaces = $journal['currency_decimal_places']; + $foreignCurrencyId = $journal['foreign_currency_id']; + $amount = (string)($journal['amount'] ?? '0'); if ($this->convertToPrimary && $currencyId !== $this->primaryCurrency->id && $foreignCurrencyId !== $this->primaryCurrency->id) { - $amount = (string) ($journal['pc_amount'] ?? '0'); + $amount = (string)($journal['pc_amount'] ?? '0'); $currencyId = $this->primaryCurrency->id; $currencyCode = $this->primaryCurrency->code; $currencyName = $this->primaryCurrency->name; @@ -649,12 +648,12 @@ trait PeriodOverview $currencyDecimalPlaces = $this->primaryCurrency->decimal_places; } if ($this->convertToPrimary && $currencyId !== $this->primaryCurrency->id && $foreignCurrencyId === $this->primaryCurrency->id) { - $currencyId = (int) $foreignCurrencyId; + $currencyId = (int)$foreignCurrencyId; $currencyCode = $journal['foreign_currency_code']; $currencyName = $journal['foreign_currency_name']; $currencySymbol = $journal['foreign_currency_symbol']; $currencyDecimalPlaces = $journal['foreign_currency_decimal_places']; - $amount = (string) ($journal['foreign_amount'] ?? '0'); + $amount = (string)($journal['foreign_amount'] ?? '0'); } $return[$currencyId] ??= [ 'amount' => '0', @@ -666,7 +665,7 @@ trait PeriodOverview 'currency_decimal_places' => $currencyDecimalPlaces, ]; - $return[$currencyId]['amount'] = bcadd((string) $return[$currencyId]['amount'], $amount); + $return[$currencyId]['amount'] = bcadd((string)$return[$currencyId]['amount'], $amount); ++$return[$currencyId]['count']; ++$return['count']; } @@ -678,14 +677,14 @@ trait PeriodOverview { unset($array['count']); Log::debug(sprintf( - 'saveGroupedAsStatistics(%s #%d, %s, %s, "%s", array(%d))', - $model::class, - $model->id, - $start->format('Y-m-d'), - $end->format('Y-m-d'), - $type, - count($array) - )); + 'saveGroupedAsStatistics(%s #%d, %s, %s, "%s", array(%d))', + $model::class, + $model->id, + $start->format('Y-m-d'), + $end->format('Y-m-d'), + $type, + count($array) + )); foreach ($array as $entry) { $this->periodStatisticRepo->saveStatistic($model, $entry['currency_id'], $start, $end, $type, $entry['count'], $entry['amount']); } @@ -699,13 +698,13 @@ trait PeriodOverview { unset($array['count']); Log::debug(sprintf( - 'saveGroupedForPrefix("%s", %s, %s, "%s", array(%d))', - $prefix, - $start->format('Y-m-d'), - $end->format('Y-m-d'), - $type, - count($array) - )); + 'saveGroupedForPrefix("%s", %s, %s, "%s", array(%d))', + $prefix, + $start->format('Y-m-d'), + $end->format('Y-m-d'), + $type, + count($array) + )); foreach ($array as $entry) { $this->periodStatisticRepo->savePrefixedStatistic($prefix, $entry['currency_id'], $start, $end, $type, $entry['count'], $entry['amount']); }