Remove stats from empty objects.

This commit is contained in:
James Cole
2025-10-03 05:53:50 +02:00
parent e55af7186c
commit ca364dc877
4 changed files with 64 additions and 45 deletions

View File

@@ -62,14 +62,14 @@ class StoredGroupEventHandler
} }
Log::debug('Now in StoredGroupEventHandler::processRules()'); Log::debug('Now in StoredGroupEventHandler::processRules()');
$journals = $storedGroupEvent->transactionGroup->transactionJournals; $journals = $storedGroupEvent->transactionGroup->transactionJournals;
$array = []; $array = [];
/** @var TransactionJournal $journal */ /** @var TransactionJournal $journal */
foreach ($journals as $journal) { foreach ($journals as $journal) {
$array[] = $journal->id; $array[] = $journal->id;
} }
$journalIds = implode(',', $array); $journalIds = implode(',', $array);
Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds)); Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds));
// collect rules: // collect rules:
@@ -78,10 +78,10 @@ class StoredGroupEventHandler
// add the groups to the rule engine. // add the groups to the rule engine.
// it should run the rules in the group and cancel the group if necessary. // 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. // create and fire rule engine.
$newRuleEngine = app(RuleEngineInterface::class); $newRuleEngine = app(RuleEngineInterface::class);
$newRuleEngine->setUser($storedGroupEvent->transactionGroup->user); $newRuleEngine->setUser($storedGroupEvent->transactionGroup->user);
$newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]); $newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]);
$newRuleEngine->setRuleGroups($groups); $newRuleEngine->setRuleGroups($groups);
@@ -90,7 +90,7 @@ class StoredGroupEventHandler
private function recalculateCredit(StoredTransactionGroup $event): void private function recalculateCredit(StoredTransactionGroup $event): void
{ {
$group = $event->transactionGroup; $group = $event->transactionGroup;
/** @var CreditRecalculateService $object */ /** @var CreditRecalculateService $object */
$object = app(CreditRecalculateService::class); $object = app(CreditRecalculateService::class);
@@ -109,12 +109,24 @@ class StoredGroupEventHandler
$dest = $journal->transactions()->where('amount', '>', '0')->first(); $dest = $journal->transactions()->where('amount', '>', '0')->first();
$repository->deleteStatisticsForModel($source->account, $journal->date); $repository->deleteStatisticsForModel($source->account, $journal->date);
$repository->deleteStatisticsForModel($dest->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); $repository->deleteStatisticsForModel($category, $journal->date);
} }
foreach ($journal->tags as $tag) { foreach ($tags as $tag) {
$repository->deleteStatisticsForModel($tag, $journal->date); $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 private function triggerWebhooks(StoredTransactionGroup $storedGroupEvent): void
{ {
Log::debug(__METHOD__); Log::debug(__METHOD__);
$group = $storedGroupEvent->transactionGroup; $group = $storedGroupEvent->transactionGroup;
if (false === $storedGroupEvent->fireWebhooks) { if (false === $storedGroupEvent->fireWebhooks) {
Log::info(sprintf('Will not fire webhooks for transaction group #%d', $group->id)); Log::info(sprintf('Will not fire webhooks for transaction group #%d', $group->id));
return; return;
} }
$user = $group->user; $user = $group->user;
/** @var MessageGeneratorInterface $engine */ /** @var MessageGeneratorInterface $engine */
$engine = app(MessageGeneratorInterface::class); $engine = app(MessageGeneratorInterface::class);

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\PeriodStatistic;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\PeriodStatistic; use FireflyIII\Models\PeriodStatistic;
use FireflyIII\Models\UserGroup;
use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface; use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
use Illuminate\Database\Eloquent\Model; 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 public function findPeriodStatistics(Model $model, Carbon $start, Carbon $end, array $types): Collection
{ {
return $model->primaryPeriodStatistics() return $model->primaryPeriodStatistics()
->where('start', $start) ->where('start', $start)
->where('end', $end) ->where('end', $end)
->whereIn('type', $types) ->whereIn('type', $types)
->get() ->get();
;
} }
public function findPeriodStatistic(Model $model, Carbon $start, Carbon $end, string $type): Collection public function findPeriodStatistic(Model $model, Carbon $start, Carbon $end, string $type): Collection
{ {
return $model->primaryPeriodStatistics() return $model->primaryPeriodStatistics()
->where('start', $start) ->where('start', $start)
->where('end', $end) ->where('end', $end)
->where('type', $type) ->where('type', $type)
->get() ->get();
;
} }
public function saveStatistic(Model $model, int $currencyId, Carbon $start, Carbon $end, string $type, int $count, string $amount): PeriodStatistic 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->primaryStatable()->associate($model);
$stat->transaction_currency_id = $currencyId; $stat->transaction_currency_id = $currencyId;
$stat->user_group_id = $this->getUserGroup()->id; $stat->user_group_id = $this->getUserGroup()->id;
@@ -72,16 +71,16 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U
$stat->save(); $stat->save();
Log::debug(sprintf( Log::debug(sprintf(
'Saved #%d [currency #%d, Model %s #%d, %s to %s, %d, %s] as new statistic.', 'Saved #%d [currency #%d, Model %s #%d, %s to %s, %d, %s] as new statistic.',
$stat->id, $stat->id,
$model::class, $model::class,
$model->id, $model->id,
$stat->transaction_currency_id, $stat->transaction_currency_id,
$stat->start->toW3cString(), $stat->start->toW3cString(),
$stat->end->toW3cString(), $stat->end->toW3cString(),
$count, $count,
$amount $amount
)); ));
return $stat; return $stat;
} }
@@ -100,9 +99,8 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U
public function allInRangeForPrefix(string $prefix, Carbon $start, Carbon $end): Collection public function allInRangeForPrefix(string $prefix, Carbon $start, Carbon $end): Collection
{ {
return $this->userGroup->periodStatistics() return $this->userGroup->periodStatistics()
->where('type', 'LIKE', sprintf('%s%%', $prefix)) ->where('type', 'LIKE', sprintf('%s%%', $prefix))
->where('start', '>=', $start)->where('end', '<=', $end)->get() ->where('start', '>=', $start)->where('end', '<=', $end)->get();
;
} }
#[Override] #[Override]
@@ -121,16 +119,22 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U
$stat->save(); $stat->save();
Log::debug(sprintf( Log::debug(sprintf(
'Saved #%d [currency #%d, type "%s", %s to %s, %d, %s] as new statistic.', 'Saved #%d [currency #%d, type "%s", %s to %s, %d, %s] as new statistic.',
$stat->id, $stat->id,
$stat->transaction_currency_id, $stat->transaction_currency_id,
$stat->type, $stat->type,
$stat->start->toW3cString(), $stat->start->toW3cString(),
$stat->end->toW3cString(), $stat->end->toW3cString(),
$count, $count,
$amount $amount
)); ));
return $stat; 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();
}
} }

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\PeriodStatistic;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Models\PeriodStatistic; use FireflyIII\Models\PeriodStatistic;
use FireflyIII\Models\UserGroup;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -43,4 +44,6 @@ interface PeriodStatisticRepositoryInterface
public function allInRangeForPrefix(string $prefix, Carbon $start, Carbon $end): Collection; public function allInRangeForPrefix(string $prefix, Carbon $start, Carbon $end): Collection;
public function deleteStatisticsForModel(Model $model, Carbon $date): void; public function deleteStatisticsForModel(Model $model, Carbon $date): void;
public function deleteStatisticsForPrefix(UserGroup $userGroup, string $prefix, Carbon $date): void;
} }

View File

@@ -325,7 +325,7 @@ trait PeriodOverview
private function filterStatistics(Carbon $start, Carbon $end, string $type): Collection private function filterStatistics(Carbon $start, Carbon $end, string $type): Collection
{ {
if (0 === $this->statistics->count()) { if (0 === $this->statistics->count()) {
Log::warning('Have no statistic to filter!'); Log::debug('Have no statistic to filter!');
return new Collection(); return new Collection();
} }
@@ -338,7 +338,7 @@ trait PeriodOverview
private function filterPrefixedStatistics(Carbon $start, Carbon $end, string $prefix): Collection private function filterPrefixedStatistics(Carbon $start, Carbon $end, string $prefix): Collection
{ {
if (0 === $this->statistics->count()) { if (0 === $this->statistics->count()) {
Log::warning('Have no statistic to filter!'); Log::debug('Have no statistic to filter!');
return new Collection(); return new Collection();
} }