diff --git a/app/Handlers/Events/StoredGroupEventHandler.php b/app/Handlers/Events/StoredGroupEventHandler.php index 50ce62f742..f01331a15c 100644 --- a/app/Handlers/Events/StoredGroupEventHandler.php +++ b/app/Handlers/Events/StoredGroupEventHandler.php @@ -62,14 +62,14 @@ class StoredGroupEventHandler } Log::debug('Now in StoredGroupEventHandler::processRules()'); - $journals = $storedGroupEvent->transactionGroup->transactionJournals; - $array = []; + $journals = $storedGroupEvent->transactionGroup->transactionJournals; + $array = []; /** @var TransactionJournal $journal */ foreach ($journals as $journal) { $array[] = $journal->id; } - $journalIds = implode(',', $array); + $journalIds = implode(',', $array); Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds)); // collect rules: @@ -78,10 +78,10 @@ class StoredGroupEventHandler // add the groups to the rule engine. // it should run the rules in the group and cancel the group if necessary. - $groups = $ruleGroupRepository->getRuleGroupsWithRules('store-journal'); + $groups = $ruleGroupRepository->getRuleGroupsWithRules('store-journal'); // create and fire rule engine. - $newRuleEngine = app(RuleEngineInterface::class); + $newRuleEngine = app(RuleEngineInterface::class); $newRuleEngine->setUser($storedGroupEvent->transactionGroup->user); $newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]); $newRuleEngine->setRuleGroups($groups); @@ -90,7 +90,7 @@ class StoredGroupEventHandler private function recalculateCredit(StoredTransactionGroup $event): void { - $group = $event->transactionGroup; + $group = $event->transactionGroup; /** @var CreditRecalculateService $object */ $object = app(CreditRecalculateService::class); @@ -109,12 +109,24 @@ class StoredGroupEventHandler $dest = $journal->transactions()->where('amount', '>', '0')->first(); $repository->deleteStatisticsForModel($source->account, $journal->date); $repository->deleteStatisticsForModel($dest->account, $journal->date); - foreach ($journal->categories as $category) { + $categories = $journal->categories; + $tags = $journal->tags; + $budgets = $journal->budgets; + foreach ($categories as $category) { $repository->deleteStatisticsForModel($category, $journal->date); } - foreach ($journal->tags as $tag) { + foreach ($tags as $tag) { $repository->deleteStatisticsForModel($tag, $journal->date); } + foreach ($budgets as $budget) { + $repository->deleteStatisticsForModel($budget, $journal->date); + } + if (0 === $categories->count()) { + $repository->deleteStatisticsForPrefix($journal->userGroup, 'no_category', $journal->date); + } + if (0 === $budgets->count()) { + $repository->deleteStatisticsForPrefix($journal->userGroup, 'no_budget', $journal->date); + } } } @@ -124,14 +136,14 @@ class StoredGroupEventHandler private function triggerWebhooks(StoredTransactionGroup $storedGroupEvent): void { Log::debug(__METHOD__); - $group = $storedGroupEvent->transactionGroup; + $group = $storedGroupEvent->transactionGroup; if (false === $storedGroupEvent->fireWebhooks) { Log::info(sprintf('Will not fire webhooks for transaction group #%d', $group->id)); return; } - $user = $group->user; + $user = $group->user; /** @var MessageGeneratorInterface $engine */ $engine = app(MessageGeneratorInterface::class); diff --git a/app/Repositories/PeriodStatistic/PeriodStatisticRepository.php b/app/Repositories/PeriodStatistic/PeriodStatisticRepository.php index ad294b9fcb..8763009535 100644 --- a/app/Repositories/PeriodStatistic/PeriodStatisticRepository.php +++ b/app/Repositories/PeriodStatistic/PeriodStatisticRepository.php @@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\PeriodStatistic; use Carbon\Carbon; use FireflyIII\Models\PeriodStatistic; +use FireflyIII\Models\UserGroup; use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface; use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; use Illuminate\Database\Eloquent\Model; @@ -39,26 +40,24 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U public function findPeriodStatistics(Model $model, Carbon $start, Carbon $end, array $types): Collection { return $model->primaryPeriodStatistics() - ->where('start', $start) - ->where('end', $end) - ->whereIn('type', $types) - ->get() - ; + ->where('start', $start) + ->where('end', $end) + ->whereIn('type', $types) + ->get(); } public function findPeriodStatistic(Model $model, Carbon $start, Carbon $end, string $type): Collection { return $model->primaryPeriodStatistics() - ->where('start', $start) - ->where('end', $end) - ->where('type', $type) - ->get() - ; + ->where('start', $start) + ->where('end', $end) + ->where('type', $type) + ->get(); } public function saveStatistic(Model $model, int $currencyId, Carbon $start, Carbon $end, string $type, int $count, string $amount): PeriodStatistic { - $stat = new PeriodStatistic(); + $stat = new PeriodStatistic(); $stat->primaryStatable()->associate($model); $stat->transaction_currency_id = $currencyId; $stat->user_group_id = $this->getUserGroup()->id; @@ -72,16 +71,16 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U $stat->save(); Log::debug(sprintf( - 'Saved #%d [currency #%d, Model %s #%d, %s to %s, %d, %s] as new statistic.', - $stat->id, - $model::class, - $model->id, - $stat->transaction_currency_id, - $stat->start->toW3cString(), - $stat->end->toW3cString(), - $count, - $amount - )); + 'Saved #%d [currency #%d, Model %s #%d, %s to %s, %d, %s] as new statistic.', + $stat->id, + $model::class, + $model->id, + $stat->transaction_currency_id, + $stat->start->toW3cString(), + $stat->end->toW3cString(), + $count, + $amount + )); return $stat; } @@ -100,9 +99,8 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U public function allInRangeForPrefix(string $prefix, Carbon $start, Carbon $end): Collection { return $this->userGroup->periodStatistics() - ->where('type', 'LIKE', sprintf('%s%%', $prefix)) - ->where('start', '>=', $start)->where('end', '<=', $end)->get() - ; + ->where('type', 'LIKE', sprintf('%s%%', $prefix)) + ->where('start', '>=', $start)->where('end', '<=', $end)->get(); } #[Override] @@ -121,16 +119,22 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U $stat->save(); Log::debug(sprintf( - 'Saved #%d [currency #%d, type "%s", %s to %s, %d, %s] as new statistic.', - $stat->id, - $stat->transaction_currency_id, - $stat->type, - $stat->start->toW3cString(), - $stat->end->toW3cString(), - $count, - $amount - )); + 'Saved #%d [currency #%d, type "%s", %s to %s, %d, %s] as new statistic.', + $stat->id, + $stat->transaction_currency_id, + $stat->type, + $stat->start->toW3cString(), + $stat->end->toW3cString(), + $count, + $amount + )); return $stat; } + + #[\Override] + public function deleteStatisticsForPrefix(UserGroup $userGroup, string $prefix, Carbon $date): void + { + $userGroup->periodStatistics()->where('start', '<=', $date)->where('end', '>=', $date)->where('type', 'LIKE', sprintf('%s%%', $prefix))->delete(); + } } diff --git a/app/Repositories/PeriodStatistic/PeriodStatisticRepositoryInterface.php b/app/Repositories/PeriodStatistic/PeriodStatisticRepositoryInterface.php index 22f17d8f2d..c4a9574d29 100644 --- a/app/Repositories/PeriodStatistic/PeriodStatisticRepositoryInterface.php +++ b/app/Repositories/PeriodStatistic/PeriodStatisticRepositoryInterface.php @@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\PeriodStatistic; use Carbon\Carbon; use FireflyIII\Models\PeriodStatistic; +use FireflyIII\Models\UserGroup; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; @@ -43,4 +44,6 @@ interface PeriodStatisticRepositoryInterface public function allInRangeForPrefix(string $prefix, Carbon $start, Carbon $end): Collection; public function deleteStatisticsForModel(Model $model, Carbon $date): void; + + public function deleteStatisticsForPrefix(UserGroup $userGroup, string $prefix, Carbon $date): void; } diff --git a/app/Support/Http/Controllers/PeriodOverview.php b/app/Support/Http/Controllers/PeriodOverview.php index b639eff171..1fc9dc3fd4 100644 --- a/app/Support/Http/Controllers/PeriodOverview.php +++ b/app/Support/Http/Controllers/PeriodOverview.php @@ -325,7 +325,7 @@ trait PeriodOverview private function filterStatistics(Carbon $start, Carbon $end, string $type): Collection { if (0 === $this->statistics->count()) { - Log::warning('Have no statistic to filter!'); + Log::debug('Have no statistic to filter!'); return new Collection(); } @@ -338,7 +338,7 @@ trait PeriodOverview private function filterPrefixedStatistics(Carbon $start, Carbon $end, string $prefix): Collection { if (0 === $this->statistics->count()) { - Log::warning('Have no statistic to filter!'); + Log::debug('Have no statistic to filter!'); return new Collection(); }