Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Also include delete event in new event triggers.
James Coleccf33f1db6aa
diff --git a/app/Events/Model/TransactionGroup/DestroyedSingleTransactionGroup.php b/app/Events/Model/TransactionGroup/DestroyedSingleTransactionGroup.phpindex 936ea39a18..1790e5b3d5 100644--- a/app/Events/Model/TransactionGroup/DestroyedSingleTransactionGroup.php+++ b/app/Events/Model/TransactionGroup/DestroyedSingleTransactionGroup.php@@ -25,7 +25,6 @@ declare(strict_types=1);namespace FireflyIII\Events\Model\TransactionGroup;use FireflyIII\Events\Event;-use FireflyIII\Models\TransactionGroup;use Illuminate\Queue\SerializesModels;class DestroyedSingleTransactionGroup extends Event@@ -35,7 +34,5 @@ class DestroyedSingleTransactionGroup extends Event/*** Create a new event instance.*/- public function __construct(- public TransactionGroup $transactionGroup- ) {}+ public function __construct(public TransactionGroupEventFlags $flags, public TransactionGroupEventObjects $objects) {}}diff --git a/app/Listeners/Model/TransactionGroup/ProcessesDestroyedTransactionGroup.php b/app/Listeners/Model/TransactionGroup/ProcessesDestroyedTransactionGroup.phpindex e500592ba2..741eb2b6e2 100644--- a/app/Listeners/Model/TransactionGroup/ProcessesDestroyedTransactionGroup.php+++ b/app/Listeners/Model/TransactionGroup/ProcessesDestroyedTransactionGroup.php@@ -36,37 +36,25 @@ use Illuminate\Support\Facades\Log;class ProcessesDestroyedTransactionGroup implements ShouldQueue{+ use SupportsGroupProcessingTrait;public function handle(DestroyedSingleTransactionGroup $event): void{- $this->triggerWebhooks($event);- $this->updateRunningBalance($event);- }-- private function triggerWebhooks(DestroyedSingleTransactionGroup $destroyedGroupEvent): void- {- Log::debug('DestroyedTransactionGroup:triggerWebhooks');- $group = $destroyedGroupEvent->transactionGroup;- $user = $group->user;+ Log::debug(sprintf('User called %s', get_class($event)));- /** @var MessageGeneratorInterface $engine */- $engine = app(MessageGeneratorInterface::class);- $engine->setUser($user);- $engine->setObjects(new Collection()->push($group));- $engine->setTrigger(WebhookTrigger::DESTROY_TRANSACTION);- $engine->generateMessages();- Log::debug(sprintf('send event WebhookMessagesRequestSending from %s', __METHOD__));- event(new WebhookMessagesRequestSending());- }+ if (!$event->flags->recalculateCredit) {+ Log::debug(sprintf('Will NOT recalculate credit for %d journal(s)', $event->objects->transactionJournals->count()));+ }+ if (!$event->flags->fireWebhooks) {+ Log::debug(sprintf('Will NOT fire webhooks for %d journal(s)', $event->objects->transactionJournals->count()));+ }- private function updateRunningBalance(DestroyedSingleTransactionGroup $event): void- {- if (false === FireflyConfig::get('use_running_balance', config('firefly.feature_flags.running_balance_column'))->data) {- return;+ if ($event->flags->recalculateCredit) {+ $this->recalculateCredit($event->objects->accounts);}- Log::debug(__METHOD__);- $group = $event->transactionGroup;- foreach ($group->transactionJournals as $journal) {- AccountBalanceCalculator::recalculateForJournal($journal);+ if ($event->flags->fireWebhooks) {+ $this->fireWebhooks($event->objects->transactionGroups, WebhookTrigger::DESTROY_TRANSACTION);}+ $this->removePeriodStatistics($event->objects);+ $this->recalculateRunningBalance($event->objects);}}diff --git a/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php b/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.phpindex d881bb8435..4899f7d970 100644--- a/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php+++ b/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php@@ -68,7 +68,7 @@ class ProcessesNewTransactionGroup implements ShouldQueue$this->recalculateCredit($event->objects->accounts);}if ($event->flags->fireWebhooks) {- $this->fireWebhooks($journals, WebhookTrigger::STORE_TRANSACTION);+ $this->fireWebhooks($event->objects->transactionGroups, WebhookTrigger::STORE_TRANSACTION);}$this->removePeriodStatistics($event->objects);$this->recalculateRunningBalance($event->objects);diff --git a/app/Listeners/Model/TransactionGroup/ProcessesUpdatedTransactionGroup.php b/app/Listeners/Model/TransactionGroup/ProcessesUpdatedTransactionGroup.phpindex 0a0aecf189..8f4f431d74 100644--- a/app/Listeners/Model/TransactionGroup/ProcessesUpdatedTransactionGroup.php+++ b/app/Listeners/Model/TransactionGroup/ProcessesUpdatedTransactionGroup.php@@ -60,7 +60,7 @@ class ProcessesUpdatedTransactionGroup$this->recalculateCredit($event->objects->accounts);}if ($event->flags->fireWebhooks) {- $this->fireWebhooks($event->objects->transactionJournals, WebhookTrigger::UPDATE_TRANSACTION);+ $this->fireWebhooks($event->objects->transactionGroups, WebhookTrigger::UPDATE_TRANSACTION);}$this->removePeriodStatistics($event->objects);$this->recalculateRunningBalance($event->objects);diff --git a/app/Listeners/Model/TransactionGroup/SupportsGroupProcessingTrait.php b/app/Listeners/Model/TransactionGroup/SupportsGroupProcessingTrait.phpindex 0f3fba5736..78db6c443f 100644--- a/app/Listeners/Model/TransactionGroup/SupportsGroupProcessingTrait.php+++ b/app/Listeners/Model/TransactionGroup/SupportsGroupProcessingTrait.php@@ -36,15 +36,12 @@ trait SupportsGroupProcessingTrait$object->recalculate();}- private function fireWebhooks(Collection $journals, WebhookTrigger $trigger): void+ private function fireWebhooks(Collection $groups, WebhookTrigger $trigger): void{- // collect transaction groups by set ids.- $groups = TransactionGroup::whereIn('id', array_unique($journals->pluck('transaction_group_id')->toArray()))->get();-Log::debug(__METHOD__);- /** @var TransactionJournal $first */- $first = $journals->first();+ /** @var TransactionGroup $first */+ $first = $groups->first();$user = $first->user;/** @var MessageGeneratorInterface $engine */diff --git a/app/Services/Internal/Destroy/TransactionGroupDestroyService.php b/app/Services/Internal/Destroy/TransactionGroupDestroyService.phpindex e9031efec0..8f6c903993 100644--- a/app/Services/Internal/Destroy/TransactionGroupDestroyService.php+++ b/app/Services/Internal/Destroy/TransactionGroupDestroyService.php@@ -25,6 +25,8 @@ declare(strict_types=1);namespace FireflyIII\Services\Internal\Destroy;use FireflyIII\Events\Model\TransactionGroup\DestroyedSingleTransactionGroup;+use FireflyIII\Events\Model\TransactionGroup\TransactionGroupEventFlags;+use FireflyIII\Events\Model\TransactionGroup\TransactionGroupEventObjects;use FireflyIII\Models\TransactionGroup;use Illuminate\Support\Facades\Log;@@ -36,6 +38,7 @@ class TransactionGroupDestroyServicepublic function destroy(TransactionGroup $transactionGroup): void{Log::debug(sprintf('Now in %s', __METHOD__));+ $objects = TransactionGroupEventObjects::collectFromTransactionGroup($transactionGroup);/** @var JournalDestroyService $service */$service = app(JournalDestroyService::class);@@ -44,6 +47,7 @@ class TransactionGroupDestroyService}$transactionGroup->delete();// trigger just after destruction- event(new DestroyedSingleTransactionGroup($transactionGroup));+ $flags = new TransactionGroupEventFlags();+ event(new DestroyedSingleTransactionGroup($flags, $objects));}}