From 76b8ff18b06d82c127f2e5eb2d6615b0a3d32ce8 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 24 Jan 2026 18:52:07 +0100 Subject: [PATCH] Transaction events for #11544 --- .../Models/Transaction/StoreController.php | 12 ++++-- .../CreatedSingleTransactionGroup.php | 40 +++++++++++++++++++ ...php => CreatedTransactionGroupInBatch.php} | 2 +- .../TransactionGroupEventFlags.php | 30 ++++++++++++++ app/Events/StoredTransactionGroup.php | 6 +-- .../Events/StoredGroupEventHandler.php | 4 +- app/Handlers/Observer/TransactionObserver.php | 9 ++--- app/Helpers/Collector/GroupCollector.php | 2 +- .../Transaction/CreateController.php | 7 +++- app/Jobs/CreateRecurringTransactions.php | 8 +++- .../ProcessesNewTransactionGroup.php | 39 ++++++++++++++++++ app/Providers/EventServiceProvider.php | 3 +- .../JsonApi/Enrichments/AccountEnrichment.php | 9 ++--- .../JsonApi/Enrichments/BudgetEnrichment.php | 2 +- .../Enrichments/BudgetLimitEnrichment.php | 2 +- .../Enrichments/CategoryEnrichment.php | 2 +- .../Enrichments/PiggyBankEnrichment.php | 2 +- .../Enrichments/RecurringEnrichment.php | 2 +- .../Enrichments/SubscriptionEnrichment.php | 2 +- .../TransactionGroupEnrichment.php | 4 +- app/Support/Request/ConvertsDataTypes.php | 2 +- app/Support/Steam.php | 11 ++--- 22 files changed, 152 insertions(+), 48 deletions(-) create mode 100644 app/Events/Model/TransactionGroup/CreatedSingleTransactionGroup.php rename app/Events/Model/TransactionGroup/{CreatedTransactionGroupBatch.php => CreatedTransactionGroupInBatch.php} (95%) create mode 100644 app/Events/Model/TransactionGroup/TransactionGroupEventFlags.php create mode 100644 app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php diff --git a/app/Api/V1/Controllers/Models/Transaction/StoreController.php b/app/Api/V1/Controllers/Models/Transaction/StoreController.php index bfecbcd58e..42665dae50 100644 --- a/app/Api/V1/Controllers/Models/Transaction/StoreController.php +++ b/app/Api/V1/Controllers/Models/Transaction/StoreController.php @@ -27,7 +27,8 @@ namespace FireflyIII\Api\V1\Controllers\Models\Transaction; use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Requests\Models\Transaction\StoreRequest; use FireflyIII\Enums\UserRoleEnum; -use FireflyIII\Events\StoredTransactionGroup; +use FireflyIII\Events\Model\TransactionGroup\CreatedSingleTransactionGroup; +use FireflyIII\Events\Model\TransactionGroup\TransactionGroupEventFlags; use FireflyIII\Exceptions\DuplicateTransactionException; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\GroupCollectorInterface; @@ -109,9 +110,12 @@ class StoreController extends Controller throw new ValidationException($validator); } Preferences::mark(); - $applyRules = $data['apply_rules'] ?? true; - $fireWebhooks = $data['fire_webhooks'] ?? true; - event(new StoredTransactionGroup($transactionGroup, $applyRules, $fireWebhooks)); + $flags = new TransactionGroupEventFlags(); + $flags->applyRules = $data['apply_rules'] ?? true; + $flags->fireWebhooks = $data['fire_webhooks'] ?? true; + $flags->batchSubmission = $data['batch_submission'] ?? false; + Log::debug('dingflofbips'); + event(new CreatedSingleTransactionGroup($transactionGroup, $flags)); $manager = $this->getManager(); diff --git a/app/Events/Model/TransactionGroup/CreatedSingleTransactionGroup.php b/app/Events/Model/TransactionGroup/CreatedSingleTransactionGroup.php new file mode 100644 index 0000000000..7c08c2090f --- /dev/null +++ b/app/Events/Model/TransactionGroup/CreatedSingleTransactionGroup.php @@ -0,0 +1,40 @@ +. + */ + +namespace FireflyIII\Events\Model\TransactionGroup; + +use FireflyIII\Events\Event; +use FireflyIII\Models\TransactionGroup; +use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Log; + +class CreatedSingleTransactionGroup extends Event +{ + use SerializesModels; + + /** + * Create a new event instance. + */ + public function __construct(public TransactionGroup $transactionGroup, public TransactionGroupEventFlags $flags) { + Log::debug(__METHOD__); + } +} + diff --git a/app/Events/Model/TransactionGroup/CreatedTransactionGroupBatch.php b/app/Events/Model/TransactionGroup/CreatedTransactionGroupInBatch.php similarity index 95% rename from app/Events/Model/TransactionGroup/CreatedTransactionGroupBatch.php rename to app/Events/Model/TransactionGroup/CreatedTransactionGroupInBatch.php index bbbca184d5..c9ac4f9b06 100644 --- a/app/Events/Model/TransactionGroup/CreatedTransactionGroupBatch.php +++ b/app/Events/Model/TransactionGroup/CreatedTransactionGroupInBatch.php @@ -25,7 +25,7 @@ use FireflyIII\Events\Event; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Collection; -class CreatedTransactionGroupBatch extends Event +class CreatedTransactionGroupInBatch extends Event { use SerializesModels; diff --git a/app/Events/Model/TransactionGroup/TransactionGroupEventFlags.php b/app/Events/Model/TransactionGroup/TransactionGroupEventFlags.php new file mode 100644 index 0000000000..f68e2f83a5 --- /dev/null +++ b/app/Events/Model/TransactionGroup/TransactionGroupEventFlags.php @@ -0,0 +1,30 @@ +. + */ + +namespace FireflyIII\Events\Model\TransactionGroup; + +class TransactionGroupEventFlags +{ + public bool $applyRules = true; + public bool $fireWebhooks = true; + public bool $batchSubmission = false; + +} diff --git a/app/Events/StoredTransactionGroup.php b/app/Events/StoredTransactionGroup.php index 743018ea2f..15c0d42b7b 100644 --- a/app/Events/StoredTransactionGroup.php +++ b/app/Events/StoredTransactionGroup.php @@ -37,9 +37,5 @@ class StoredTransactionGroup extends Event /** * Create a new event instance. */ - public function __construct( - public TransactionGroup $transactionGroup, - public bool $applyRules, - public bool $fireWebhooks - ) {} + public function __construct(public TransactionGroup $transactionGroup, public bool $applyRules, public bool $fireWebhooks) {} } diff --git a/app/Handlers/Events/StoredGroupEventHandler.php b/app/Handlers/Events/StoredGroupEventHandler.php index f1101731ca..dddf285924 100644 --- a/app/Handlers/Events/StoredGroupEventHandler.php +++ b/app/Handlers/Events/StoredGroupEventHandler.php @@ -55,8 +55,8 @@ class StoredGroupEventHandler public function triggerRulesManually(TriggeredStoredTransactionGroup $event): void { - $newEvent = new StoredTransactionGroup($event->transactionGroup, true, false); - $this->processRules($newEvent, $event->ruleGroup); +// $newEvent = new StoredTransactionGroup($event->transactionGroup, true, false); +// $this->processRules($newEvent, $event->ruleGroup); } /** diff --git a/app/Handlers/Observer/TransactionObserver.php b/app/Handlers/Observer/TransactionObserver.php index 97f81bf08b..2509a73f39 100644 --- a/app/Handlers/Observer/TransactionObserver.php +++ b/app/Handlers/Observer/TransactionObserver.php @@ -40,13 +40,10 @@ class TransactionObserver public function created(Transaction $transaction): void { + return; + Log::debug('Observe "created" of a transaction.'); - if ( - true === FireflyConfig::get('use_running_balance', config('firefly.feature_flags.running_balance_column'))->data && ( - 1 === bccomp($transaction->amount, '0') - && self::$recalculate - ) - ) { + if (true === FireflyConfig::get('use_running_balance', config('firefly.feature_flags.running_balance_column'))->data && (1 === bccomp($transaction->amount, '0') && self::$recalculate)) { Log::debug('Trigger recalculateForJournal'); $journal = $transaction->transactionJournal; if ($journal instanceof TransactionJournal) { diff --git a/app/Helpers/Collector/GroupCollector.php b/app/Helpers/Collector/GroupCollector.php index bf9fcacb31..a96a010d30 100644 --- a/app/Helpers/Collector/GroupCollector.php +++ b/app/Helpers/Collector/GroupCollector.php @@ -442,7 +442,7 @@ class GroupCollector implements GroupCollectorInterface } $result = $this->query->get($this->fields); // $this->dumpQueryInLogs(); - Log::debug(sprintf('Count of result is %d', $result->count())); + // Log::debug(sprintf('Count of result is %d', $result->count())); // now to parse this into an array. $collection = $this->parseArray($result); diff --git a/app/Http/Controllers/Transaction/CreateController.php b/app/Http/Controllers/Transaction/CreateController.php index d4afadafbe..6a43da707d 100644 --- a/app/Http/Controllers/Transaction/CreateController.php +++ b/app/Http/Controllers/Transaction/CreateController.php @@ -24,7 +24,8 @@ declare(strict_types=1); namespace FireflyIII\Http\Controllers\Transaction; -use FireflyIII\Events\StoredTransactionGroup; +use FireflyIII\Events\Model\TransactionGroup\CreatedSingleTransactionGroup; +use FireflyIII\Events\Model\TransactionGroup\TransactionGroupEventFlags; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\TransactionGroup; use FireflyIII\Repositories\Account\AccountRepositoryInterface; @@ -76,7 +77,9 @@ class CreateController extends Controller $newGroup = $service->cloneGroup($group); // event! - event(new StoredTransactionGroup($newGroup, true, true)); + $flags = new TransactionGroupEventFlags(); + event(new CreatedSingleTransactionGroup($group, $flags)); + // event(new StoredTransactionGroup($newGroup, true, true)); Preferences::mark(); diff --git a/app/Jobs/CreateRecurringTransactions.php b/app/Jobs/CreateRecurringTransactions.php index c9282f00c2..cf912bf643 100644 --- a/app/Jobs/CreateRecurringTransactions.php +++ b/app/Jobs/CreateRecurringTransactions.php @@ -25,8 +25,9 @@ declare(strict_types=1); namespace FireflyIII\Jobs; use Carbon\Carbon; +use FireflyIII\Events\Model\TransactionGroup\CreatedSingleTransactionGroup; +use FireflyIII\Events\Model\TransactionGroup\TransactionGroupEventFlags; use FireflyIII\Events\Model\TransactionGroup\TransactionGroupsRequestedReporting; -use FireflyIII\Events\StoredTransactionGroup; use FireflyIII\Exceptions\DuplicateTransactionException; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Recurrence; @@ -383,7 +384,10 @@ class CreateRecurringTransactions implements ShouldQueue Log::info(sprintf('Created new transaction group #%d', $group->id)); // trigger event: - event(new StoredTransactionGroup($group, $recurrence->apply_rules, true)); + $flags = new TransactionGroupEventFlags(); + $flags->applyRules = $recurrence->apply_rules; + event(new CreatedSingleTransactionGroup($group, $flags)); + // event(new StoredTransactionGroup($group, $recurrence->apply_rules, true)); $this->groups->push($group); // update recurring thing: diff --git a/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php b/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php new file mode 100644 index 0000000000..98ac2ba3f8 --- /dev/null +++ b/app/Listeners/Model/TransactionGroup/ProcessesNewTransactionGroup.php @@ -0,0 +1,39 @@ +. + */ + +namespace FireflyIII\Listeners\Model\TransactionGroup; + +use FireflyIII\Events\Model\TransactionGroup\CreatedSingleTransactionGroup; +use Illuminate\Support\Facades\Log; + +class ProcessesNewTransactionGroup +{ + public function handle(CreatedSingleTransactionGroup $event): void + { + Log::debug(sprintf('In ProcessesNewTransactionGroup::handle(#%d)', $event->transactionGroup->id)); + if (true === $event->flags->batchSubmission) { + Log::debug(sprintf('Will do nothing for group #%d because it is part of a batch.', $event->transactionGroup->id)); + return; + } + Log::debug(sprintf('Will join group #%d with all other open transaction groups and process them.', $event->transactionGroup->id)); + } + +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index aa21d6caa1..0fe1df6603 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -27,7 +27,6 @@ use FireflyIII\Events\DestroyedTransactionGroup; use FireflyIII\Events\Model\TransactionGroup\TriggeredStoredTransactionGroup; use FireflyIII\Events\Preferences\UserGroupChangedPrimaryCurrency; use FireflyIII\Events\StoredAccount; -use FireflyIII\Events\StoredTransactionGroup; use FireflyIII\Events\UpdatedAccount; use FireflyIII\Events\UpdatedTransactionGroup; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -44,7 +43,7 @@ class EventServiceProvider extends ServiceProvider protected $listen = [ // is a Transaction Journal related event. - StoredTransactionGroup::class => ['FireflyIII\Handlers\Events\StoredGroupEventHandler@runAllHandlers'], + // StoredTransactionGroup::class => ['FireflyIII\Handlers\Events\StoredGroupEventHandler@runAllHandlers'], TriggeredStoredTransactionGroup::class => ['FireflyIII\Handlers\Events\StoredGroupEventHandler@triggerRulesManually'], // is a Transaction Journal related event. UpdatedTransactionGroup::class => ['FireflyIII\Handlers\Events\UpdatedGroupEventHandler@runAllHandlers'], diff --git a/app/Support/JsonApi/Enrichments/AccountEnrichment.php b/app/Support/JsonApi/Enrichments/AccountEnrichment.php index a44823e2e9..2cec606a89 100644 --- a/app/Support/JsonApi/Enrichments/AccountEnrichment.php +++ b/app/Support/JsonApi/Enrichments/AccountEnrichment.php @@ -223,10 +223,7 @@ class AccountEnrichment implements EnrichmentInterface // $finalBalance = Steam::finalAccountBalance($item, $date, $this->primaryCurrency, $this->convertToPrimary); $finalBalance = $this->balances[$id]; $balanceDifference = $this->getBalanceDifference($id, $currency); - Log::debug( - sprintf('Call finalAccountBalance(%s) with date/time "%s"', var_export($this->convertToPrimary, true), $date->toIso8601String()), - $finalBalance - ); + // Log::debug(sprintf('Call finalAccountBalance(%s) with date/time "%s"', var_export($this->convertToPrimary, true), $date->toIso8601String()), $finalBalance); // collect current balances: $currentBalance = Steam::bcround($finalBalance[$currency->code] ?? '0', $currency->decimal_places); @@ -329,7 +326,7 @@ class AccountEnrichment implements EnrichmentInterface 'zoom_level' => (int) $location['zoom_level'], ]; } - Log::debug(sprintf('Enrich with %d locations(s)', count($this->locations))); +// Log::debug(sprintf('Enrich with %d locations(s)', count($this->locations))); } private function collectMetaData(): void @@ -385,7 +382,7 @@ class AccountEnrichment implements EnrichmentInterface foreach ($notes as $note) { $this->notes[(int) $note['noteable_id']] = (string) $note['text']; } - Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); +// Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); } private function collectObjectGroups(): void diff --git a/app/Support/JsonApi/Enrichments/BudgetEnrichment.php b/app/Support/JsonApi/Enrichments/BudgetEnrichment.php index 12386b34b0..4daf51c0d8 100644 --- a/app/Support/JsonApi/Enrichments/BudgetEnrichment.php +++ b/app/Support/JsonApi/Enrichments/BudgetEnrichment.php @@ -183,7 +183,7 @@ class BudgetEnrichment implements EnrichmentInterface foreach ($notes as $note) { $this->notes[(int) $note['noteable_id']] = (string) $note['text']; } - Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); +// Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); } private function collectObjectGroups(): void diff --git a/app/Support/JsonApi/Enrichments/BudgetLimitEnrichment.php b/app/Support/JsonApi/Enrichments/BudgetLimitEnrichment.php index 7834c19f67..237522af10 100644 --- a/app/Support/JsonApi/Enrichments/BudgetLimitEnrichment.php +++ b/app/Support/JsonApi/Enrichments/BudgetLimitEnrichment.php @@ -191,7 +191,7 @@ class BudgetLimitEnrichment implements EnrichmentInterface foreach ($notes as $note) { $this->notes[(int) $note['noteable_id']] = (string) $note['text']; } - Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); +// Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); } private function filterToBudget(array $expenses, int $budget): array diff --git a/app/Support/JsonApi/Enrichments/CategoryEnrichment.php b/app/Support/JsonApi/Enrichments/CategoryEnrichment.php index 04f334946d..07ab2ce2c1 100644 --- a/app/Support/JsonApi/Enrichments/CategoryEnrichment.php +++ b/app/Support/JsonApi/Enrichments/CategoryEnrichment.php @@ -132,7 +132,7 @@ class CategoryEnrichment implements EnrichmentInterface foreach ($notes as $note) { $this->notes[(int) $note['noteable_id']] = (string) $note['text']; } - Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); +// Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); } private function collectTransactions(): void diff --git a/app/Support/JsonApi/Enrichments/PiggyBankEnrichment.php b/app/Support/JsonApi/Enrichments/PiggyBankEnrichment.php index 8cb9e0cebc..66c5e8ad8e 100644 --- a/app/Support/JsonApi/Enrichments/PiggyBankEnrichment.php +++ b/app/Support/JsonApi/Enrichments/PiggyBankEnrichment.php @@ -262,7 +262,7 @@ class PiggyBankEnrichment implements EnrichmentInterface foreach ($notes as $note) { $this->notes[(int) $note['noteable_id']] = (string) $note['text']; } - Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); +// Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); } private function collectObjectGroups(): void diff --git a/app/Support/JsonApi/Enrichments/RecurringEnrichment.php b/app/Support/JsonApi/Enrichments/RecurringEnrichment.php index 47efd78c75..6b89bf5665 100644 --- a/app/Support/JsonApi/Enrichments/RecurringEnrichment.php +++ b/app/Support/JsonApi/Enrichments/RecurringEnrichment.php @@ -327,7 +327,7 @@ class RecurringEnrichment implements EnrichmentInterface $this->notes[$notableId] = (string) $note['text']; Log::debug(sprintf('Collected note #%d for recurrence #%d', $note['id'], $notableId)); } - Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); +// Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); } private function collectPiggyBankInfo(array $piggyBankIds): void diff --git a/app/Support/JsonApi/Enrichments/SubscriptionEnrichment.php b/app/Support/JsonApi/Enrichments/SubscriptionEnrichment.php index d66f0cd2cd..7fb7defd72 100644 --- a/app/Support/JsonApi/Enrichments/SubscriptionEnrichment.php +++ b/app/Support/JsonApi/Enrichments/SubscriptionEnrichment.php @@ -205,7 +205,7 @@ class SubscriptionEnrichment implements EnrichmentInterface foreach ($notes as $note) { $this->notes[(int) $note['noteable_id']] = (string) $note['text']; } - Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); +// Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); } private function collectObjectGroups(): void diff --git a/app/Support/JsonApi/Enrichments/TransactionGroupEnrichment.php b/app/Support/JsonApi/Enrichments/TransactionGroupEnrichment.php index 54ed349c65..bdc1474b6a 100644 --- a/app/Support/JsonApi/Enrichments/TransactionGroupEnrichment.php +++ b/app/Support/JsonApi/Enrichments/TransactionGroupEnrichment.php @@ -206,7 +206,7 @@ class TransactionGroupEnrichment implements EnrichmentInterface 'zoom_level' => (int) $location['zoom_level'], ]; } - Log::debug(sprintf('Enrich with %d locations(s)', count($this->locations))); +// Log::debug(sprintf('Enrich with %d locations(s)', count($this->locations))); } private function collectMetaData(): void @@ -242,7 +242,7 @@ class TransactionGroupEnrichment implements EnrichmentInterface foreach ($notes as $note) { $this->notes[(int) $note['noteable_id']] = (string) $note['text']; } - Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); +// Log::debug(sprintf('Enrich with %d note(s)', count($this->notes))); } private function collectTags(): void diff --git a/app/Support/Request/ConvertsDataTypes.php b/app/Support/Request/ConvertsDataTypes.php index 901ea52926..5b711b5052 100644 --- a/app/Support/Request/ConvertsDataTypes.php +++ b/app/Support/Request/ConvertsDataTypes.php @@ -398,7 +398,7 @@ trait ConvertsDataTypes protected function getCarbonDate(string $field): ?Carbon { $data = (string) $this->get($field); - Log::debug(sprintf('Date string is "%s"', $data)); + //Log::debug(sprintf('Date string is "%s"', $data)); if ('' === $data) { return null; diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 5e05ebcde1..3fe8d426a4 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -59,12 +59,7 @@ class Steam ?bool $convertToPrimary = null, bool $inclusive = true ): array { - Log::debug(sprintf( - 'accountsBalancesOptimized: Called for %d account(s) with date/time "%s" (inclusive: %s)', - $accounts->count(), - $date->toIso8601String(), - var_export($inclusive, true) - )); + // Log::debug(sprintf('accountsBalancesOptimized: Called for %d account(s) with date/time "%s" (inclusive: %s)', $accounts->count(), $date->toIso8601String(), var_export($inclusive, true))); $result = []; $convertToPrimary ??= Amount::convertToPrimary(); $primary ??= Amount::getPrimaryCurrency(); @@ -80,7 +75,7 @@ class Steam ->toArray() ; - Log::debug('Array of sums: ', $arrayOfSums); + // Log::debug('Array of sums: ', $arrayOfSums); /** @var Account $account */ foreach ($accounts as $account) { @@ -136,7 +131,7 @@ class Steam } $final = array_merge($return, $sumsByCode); $result[$account->id] = $final; - Log::debug(sprintf('Final balance for account #%d is', $account->id), $final); + // Log::debug(sprintf('Final balance for account #%d is', $account->id), $final); } return $result;