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

@@ -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);
}
}
}

View File

@@ -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;
@@ -42,8 +43,7 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U
->where('start', $start)
->where('end', $end)
->whereIn('type', $types)
->get()
;
->get();
}
public function findPeriodStatistic(Model $model, Carbon $start, Carbon $end, string $type): Collection
@@ -52,8 +52,7 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U
->where('start', $start)
->where('end', $end)
->where('type', $type)
->get()
;
->get();
}
public function saveStatistic(Model $model, int $currencyId, Carbon $start, Carbon $end, string $type, int $count, string $amount): PeriodStatistic
@@ -101,8 +100,7 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U
{
return $this->userGroup->periodStatistics()
->where('type', 'LIKE', sprintf('%s%%', $prefix))
->where('start', '>=', $start)->where('end', '<=', $end)->get()
;
->where('start', '>=', $start)->where('end', '<=', $end)->get();
}
#[Override]
@@ -133,4 +131,10 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface, U
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 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;
}

View File

@@ -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();
}