Remove statistics when creating a new journal.

This commit is contained in:
James Cole
2025-09-26 19:38:26 +02:00
parent 8b09cfb8c9
commit 8f24ac4fcd
3 changed files with 32 additions and 8 deletions

View File

@@ -28,6 +28,7 @@ use FireflyIII\Events\RequestedSendWebhookMessages;
use FireflyIII\Events\StoredTransactionGroup; use FireflyIII\Events\StoredTransactionGroup;
use FireflyIII\Generator\Webhook\MessageGeneratorInterface; use FireflyIII\Generator\Webhook\MessageGeneratorInterface;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\PeriodStatistic\PeriodStatisticRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface; use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\Services\Internal\Support\CreditRecalculateService; use FireflyIII\Services\Internal\Support\CreditRecalculateService;
use FireflyIII\TransactionRules\Engine\RuleEngineInterface; use FireflyIII\TransactionRules\Engine\RuleEngineInterface;
@@ -36,6 +37,8 @@ use Illuminate\Support\Facades\Log;
/** /**
* Class StoredGroupEventHandler * Class StoredGroupEventHandler
*
* TODO migrate to observer?
*/ */
class StoredGroupEventHandler class StoredGroupEventHandler
{ {
@@ -44,6 +47,7 @@ class StoredGroupEventHandler
$this->processRules($event); $this->processRules($event);
$this->recalculateCredit($event); $this->recalculateCredit($event);
$this->triggerWebhooks($event); $this->triggerWebhooks($event);
$this->removePeriodStatistics($event);
} }
/** /**
@@ -94,6 +98,19 @@ class StoredGroupEventHandler
$object->recalculate(); $object->recalculate();
} }
private function removePeriodStatistics(StoredTransactionGroup $event): void
{
/** @var PeriodStatisticRepositoryInterface $repository */
$repository = app(PeriodStatisticRepositoryInterface::class);
/** @var TransactionJournal $journal */
foreach ($event->transactionGroup->transactionJournals as $journal) {
$source = $journal->transactions()->where('amount', '<', '0')->first();
$dest = $journal->transactions()->where('amount', '>', '0')->first();
$repository->deleteStatisticsForModel($source->account, $journal->date);
$repository->deleteStatisticsForModel($dest->account, $journal->date);
}
}
/** /**
* This method processes all webhooks that respond to the "stored transaction group" trigger (100) * This method processes all webhooks that respond to the "stored transaction group" trigger (100)
*/ */

View File

@@ -74,4 +74,9 @@ class PeriodStatisticRepository implements PeriodStatisticRepositoryInterface
{ {
return $model->primaryPeriodStatistics()->where('start', '>=', $start)->where('end', '<=', $end)->get(); return $model->primaryPeriodStatistics()->where('start', '>=', $start)->where('end', '<=', $end)->get();
} }
public function deleteStatisticsForModel(Model $model, Carbon $date): void
{
$model->primaryPeriodStatistics()->where('start', '<=', $date)->where('end', '>=', $date)->delete();
}
} }

View File

@@ -37,4 +37,6 @@ interface PeriodStatisticRepositoryInterface
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;
public function allInRangeForModel(Model $model, Carbon $start, Carbon $end): Collection; public function allInRangeForModel(Model $model, Carbon $start, Carbon $end): Collection;
public function deleteStatisticsForModel(Model $model, Carbon $date): void;
} }