From 4a901a1b420d594fad86d9e6ab4b61c504652ded Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 17 Nov 2025 19:01:38 +0100 Subject: [PATCH] Add a bunch of debug #11247 --- .../Controllers/Recurring/EditController.php | 2 +- .../Enrichments/RecurringEnrichment.php | 162 +++++++++--------- 2 files changed, 83 insertions(+), 81 deletions(-) diff --git a/app/Http/Controllers/Recurring/EditController.php b/app/Http/Controllers/Recurring/EditController.php index f0cdb8b4da..dbbd7c2947 100644 --- a/app/Http/Controllers/Recurring/EditController.php +++ b/app/Http/Controllers/Recurring/EditController.php @@ -174,7 +174,7 @@ class EditController extends Controller public function update(RecurrenceFormRequest $request, Recurrence $recurrence) { $data = $request->getAll(); - $this->repository->update($recurrence, $data); + $recurrence = $this->repository->update($recurrence, $data); $request->session()->flash('success', (string) trans('firefly.updated_recurrence', ['title' => $recurrence->title])); Log::channel('audit')->info(sprintf('Updated recurrence #%d.', $recurrence->id), $data); diff --git a/app/Support/JsonApi/Enrichments/RecurringEnrichment.php b/app/Support/JsonApi/Enrichments/RecurringEnrichment.php index 1f881ddbc5..0143aa2602 100644 --- a/app/Support/JsonApi/Enrichments/RecurringEnrichment.php +++ b/app/Support/JsonApi/Enrichments/RecurringEnrichment.php @@ -51,28 +51,28 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; - use function Safe\json_decode; class RecurringEnrichment implements EnrichmentInterface { - private array $accounts = []; + private array $accounts = []; private Collection $collection; // private array $transactionTypeIds = []; // private array $transactionTypes = []; - private readonly bool $convertToPrimary; - private array $currencies = []; - private array $currencyIds = []; - private array $destinationAccountIds = []; - private array $foreignCurrencyIds = []; - private array $ids = []; - private string $language = 'en_US'; - private array $notes = []; + private readonly bool $convertToPrimary; + private array $currencies = []; + private array $currencyIds = []; + private array $destinationAccountIds = []; + private array $foreignCurrencyIds = []; + private array $ids = []; + private string $language = 'en_US'; + private array $notes = []; private readonly TransactionCurrency $primaryCurrency; - private array $recurrenceIds = []; - private array $repetitions = []; - private array $sourceAccountIds = []; - private array $transactions = []; + private array $recurrenceIds = []; + private array $recurrenceByTransaction = []; + private array $repetitions = []; + private array $sourceAccountIds = []; + private array $transactions = []; private User $user; private UserGroup $userGroup; @@ -84,6 +84,7 @@ class RecurringEnrichment implements EnrichmentInterface public function enrich(Collection $collection): Collection { + Log::debug(__METHOD__); $this->collection = $collection; $this->collectIds(); $this->collectRepetitions(); @@ -98,7 +99,7 @@ class RecurringEnrichment implements EnrichmentInterface return $this->collection; } - public function enrichSingle(array|Model $model): array|Model + public function enrichSingle(array | Model $model): array | Model { Log::debug(__METHOD__); $collection = new Collection()->push($model); @@ -132,7 +133,7 @@ class RecurringEnrichment implements EnrichmentInterface return (string)trans('firefly.recurring_monthly', ['dayOfMonth' => $repetition->repetition_moment, 'skip' => $repetition->repetition_skip - 1], $this->language); } if ('ndom' === $repetition->repetition_type) { - $parts = explode(',', $repetition->repetition_moment); + $parts = explode(',', $repetition->repetition_moment); // first part is number of week, second is weekday. $dayOfWeek = trans(sprintf('config.dow_%s', $parts[1]), [], $this->language); if ($repetition->repetition_skip > 0) { @@ -149,7 +150,7 @@ class RecurringEnrichment implements EnrichmentInterface } // $diffInYears = (int)$today->diffInYears($repDate, true); // $repDate->addYears($diffInYears); // technically not necessary. - $string = $repDate->isoFormat((string)trans('config.month_and_day_no_year_js')); + $string = $repDate->isoFormat((string)trans('config.month_and_day_no_year_js')); return (string)trans('firefly.recurring_yearly', ['date' => $string], $this->language); } @@ -172,8 +173,8 @@ class RecurringEnrichment implements EnrichmentInterface private function appendCollectedData(): void { $this->collection = $this->collection->map(function (Recurrence $item): Recurrence { - $id = (int)$item->id; - $meta = [ + $id = (int)$item->id; + $meta = [ 'notes' => $this->notes[$id] ?? null, 'repetitions' => array_values($this->repetitions[$id] ?? []), 'transactions' => $this->processTransactions(array_values($this->transactions[$id] ?? [])), @@ -194,6 +195,7 @@ class RecurringEnrichment implements EnrichmentInterface foreach ($accounts as $account) { $id = (int)$account->id; $this->accounts[$id] = $account; + Log::debug(sprintf('Collected account #%d', $id)); } } @@ -279,6 +281,7 @@ class RecurringEnrichment implements EnrichmentInterface foreach ($currencies as $currency) { $id = (int)$currency->id; $this->currencies[$id] = $currency; + Log::debug(sprintf('Collected currency #%d', $id)); } } @@ -287,29 +290,23 @@ class RecurringEnrichment implements EnrichmentInterface /** @var Recurrence $recurrence */ foreach ($this->collection as $recurrence) { $id = (int)$recurrence->id; - // $typeId = (int)$recurrence->transaction_type_id; $this->ids[] = $id; - // $this->transactionTypeIds[$id] = $typeId; + Log::debug(sprintf('Collected recurrence id #%d', $id)); } $this->ids = array_unique($this->ids); - - // collect transaction types. - // $transactionTypes = TransactionType::whereIn('id', array_unique($this->transactionTypeIds))->get(); - // foreach ($transactionTypes as $transactionType) { - // $id = (int)$transactionType->id; - // $this->transactionTypes[$id] = TransactionTypeEnum::from($transactionType->type); - // } + Log::debug('Final array:', $this->ids); } private function collectNotes(): void { $notes = Note::query()->whereIn('noteable_id', $this->ids) - ->whereNotNull('notes.text') - ->where('notes.text', '!=', '') - ->where('noteable_type', Recurrence::class)->get(['notes.noteable_id', 'notes.text'])->toArray() - ; + ->whereNotNull('notes.text') + ->where('notes.text', '!=', '') + ->where('noteable_type', Recurrence::class)->get(['notes.id', 'notes.noteable_id', 'notes.text'])->toArray(); foreach ($notes as $note) { - $this->notes[(int)$note['noteable_id']] = (string)$note['text']; + $notableId = (int)$note['noteable_id']; + $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))); } @@ -337,26 +334,27 @@ class RecurringEnrichment implements EnrichmentInterface Log::debug('Start of enrichment: collectRepetitions()'); $repository = app(RecurringRepositoryInterface::class); $repository->setUserGroup($this->userGroup); - $set = RecurrenceRepetition::whereIn('recurrence_id', $this->ids)->get(); + $set = RecurrenceRepetition::whereIn('recurrence_id', $this->ids)->get(); /** @var RecurrenceRepetition $repetition */ foreach ($set as $repetition) { - $recurrence = $this->collection->filter(fn (Recurrence $item): bool => (int)$item->id === (int)$repetition->recurrence_id)->first(); - $fromDate = clone ($recurrence->latest_date ?? $recurrence->first_date); - $id = (int)$repetition->recurrence_id; - $repId = (int)$repetition->id; - $this->repetitions[$id] ??= []; + $recurrence = $this->collection->filter(fn(Recurrence $item): bool => (int)$item->id === (int)$repetition->recurrence_id)->first(); + $fromDate = clone($recurrence->latest_date ?? $recurrence->first_date); + $recurrenceId = (int)$repetition->recurrence_id; + $repId = (int)$repetition->id; + $this->repetitions[$recurrenceId] ??= []; + Log::debug(sprintf('Collected repetition #%d of recurrence #%d.', $repId, $recurrenceId)); // get the (future) occurrences for this specific type of repetition: - $amount = 'daily' === $repetition->repetition_type ? 9 : 5; - $set = $repository->getXOccurrencesSince($repetition, $fromDate, now(config('app.timezone')), $amount); - $occurrences = []; + $amount = 'daily' === $repetition->repetition_type ? 9 : 5; + $set = $repository->getXOccurrencesSince($repetition, $fromDate, now(config('app.timezone')), $amount); + $occurrences = []; /** @var Carbon $carbon */ foreach ($set as $carbon) { $occurrences[] = $carbon->toAtomString(); } - $this->repetitions[$id][$repId] = [ + $this->repetitions[$recurrenceId][$repId] = [ 'id' => (string)$repId, 'created_at' => $repetition->created_at->toAtomString(), 'updated_at' => $repetition->updated_at->toAtomString(), @@ -373,8 +371,11 @@ class RecurringEnrichment implements EnrichmentInterface private function collectTransactionMetaData(): void { - $ids = array_keys($this->transactions); - $meta = RecurrenceTransactionMeta::whereNull('deleted_at')->whereIn('rt_id', $ids)->get(); + $rtIds = []; + foreach ($this->ids as $recurrenceId) { + $rtIds = array_merge($rtIds, array_keys($this->transactions[$recurrenceId])); + } + $meta = RecurrenceTransactionMeta::whereNull('deleted_at')->whereIn('rt_id', $rtIds)->get(); // other meta-data to be collected: $billIds = []; $piggyBankIds = []; @@ -386,10 +387,11 @@ class RecurringEnrichment implements EnrichmentInterface $transactionId = (int)$entry->rt_id; // this should refer to another array, were rtIds can be used to find the recurrence. - $recurrenceId = $this->recurrenceIds[$transactionId] ?? 0; - $name = (string)($entry->name ?? ''); + $recurrenceId = $this->recurrenceByTransaction[$transactionId] ?? 0; + Log::debug(sprintf('Collecting meta data entry #%d for recurrence transaction #%d, for recurrence #%d ', $id, $transactionId, $recurrenceId)); + $name = (string)($entry->name ?? ''); if (0 === $recurrenceId) { - Log::error(sprintf('Could not find recurrence ID for recurrence transaction ID %d', $transactionId)); + Log::error(sprintf('Could not find recurrence ID for recurrence transaction ID #%d', $transactionId)); continue; } @@ -487,16 +489,16 @@ class RecurringEnrichment implements EnrichmentInterface /** @var RecurrenceTransaction $transaction */ foreach ($set as $transaction) { - $id = (int)$transaction->recurrence_id; - $transactionId = (int)$transaction->id; - $this->recurrenceIds[$transactionId] = $id; - $this->transactions[$id] ??= []; - $amount = $transaction->amount; - $foreignAmount = $transaction->foreign_amount; + $recurrenceId = (int)$transaction->recurrence_id; + $transactionId = (int)$transaction->id; + $this->recurrenceByTransaction[$transactionId] = $recurrenceId; + $this->transactions[$recurrenceId] ??= []; + $amount = $transaction->amount; + $foreignAmount = $transaction->foreign_amount; + Log::debug(sprintf('Collected transaction #%d of recurrence #%d', $transactionId, $recurrenceId)); - $this->transactions[$id][$transactionId] = [ + $this->transactions[$recurrenceId][$transactionId] = [ 'id' => (string)$transactionId, - // 'recurrence_id' => $id, 'transaction_currency_id' => (int)$transaction->transaction_currency_id, 'foreign_currency_id' => null === $transaction->foreign_currency_id ? null : (int)$transaction->foreign_currency_id, 'source_id' => (int)$transaction->source_id, @@ -518,7 +520,7 @@ class RecurringEnrichment implements EnrichmentInterface 'subscription_name' => null, ]; - // collect all kinds of meta data to be collected later. + // collect all kinds of meta-data to be collected later. $this->currencyIds[$transactionId] = (int)$transaction->transaction_currency_id; $this->sourceAccountIds[$transactionId] = (int)$transaction->source_id; $this->destinationAccountIds[$transactionId] = (int)$transaction->destination_id; @@ -531,8 +533,8 @@ class RecurringEnrichment implements EnrichmentInterface private function getLanguage(): void { /** @var Preference $preference */ - $preference = Preferences::getForUser($this->user, 'language', config('firefly.default_language', 'en_US')); - $language = $preference->data; + $preference = Preferences::getForUser($this->user, 'language', config('firefly.default_language', 'en_US')); + $language = $preference->data; if (is_array($language)) { $language = 'en_US'; } @@ -545,9 +547,9 @@ class RecurringEnrichment implements EnrichmentInterface $return = []; $converter = new ExchangeRateConverter(); foreach ($transactions as $transaction) { - $currencyId = $transaction['transaction_currency_id']; - $pcAmount = null; - $pcForeignAmount = null; + $currencyId = $transaction['transaction_currency_id']; + $pcAmount = null; + $pcForeignAmount = null; // set the same amount in the primary currency, if both are the same anyway. if ($this->convertToPrimary && $currencyId === (int)$this->primaryCurrency->id) { $pcAmount = $transaction['amount']; @@ -563,26 +565,26 @@ class RecurringEnrichment implements EnrichmentInterface } } - $transaction['pc_amount'] = $pcAmount; - $transaction['pc_foreign_amount'] = $pcForeignAmount; + $transaction['pc_amount'] = $pcAmount; + $transaction['pc_foreign_amount'] = $pcForeignAmount; - $sourceId = $transaction['source_id']; - $transaction['source_name'] = $this->accounts[$sourceId]->name; - $transaction['source_iban'] = $this->accounts[$sourceId]->iban; - $transaction['source_type'] = $this->accounts[$sourceId]->accountType->type; - $transaction['source_id'] = (string)$transaction['source_id']; + $sourceId = $transaction['source_id']; + $transaction['source_name'] = $this->accounts[$sourceId]->name; + $transaction['source_iban'] = $this->accounts[$sourceId]->iban; + $transaction['source_type'] = $this->accounts[$sourceId]->accountType->type; + $transaction['source_id'] = (string)$transaction['source_id']; - $destId = $transaction['destination_id']; - $transaction['destination_name'] = $this->accounts[$destId]->name; - $transaction['destination_iban'] = $this->accounts[$destId]->iban; - $transaction['destination_type'] = $this->accounts[$destId]->accountType->type; - $transaction['destination_id'] = (string)$transaction['destination_id']; + $destId = $transaction['destination_id']; + $transaction['destination_name'] = $this->accounts[$destId]->name; + $transaction['destination_iban'] = $this->accounts[$destId]->iban; + $transaction['destination_type'] = $this->accounts[$destId]->accountType->type; + $transaction['destination_id'] = (string)$transaction['destination_id']; - $transaction['currency_id'] = (string)$currencyId; - $transaction['currency_name'] = $this->currencies[$currencyId]->name; - $transaction['currency_code'] = $this->currencies[$currencyId]->code; - $transaction['currency_symbol'] = $this->currencies[$currencyId]->symbol; - $transaction['currency_decimal_places'] = $this->currencies[$currencyId]->decimal_places; + $transaction['currency_id'] = (string)$currencyId; + $transaction['currency_name'] = $this->currencies[$currencyId]->name; + $transaction['currency_code'] = $this->currencies[$currencyId]->code; + $transaction['currency_symbol'] = $this->currencies[$currencyId]->symbol; + $transaction['currency_decimal_places'] = $this->currencies[$currencyId]->decimal_places; $transaction['primary_currency_id'] = (string)$this->primaryCurrency->id; $transaction['primary_currency_name'] = $this->primaryCurrency->name; @@ -604,7 +606,7 @@ class RecurringEnrichment implements EnrichmentInterface $transaction['foreign_currency_decimal_places'] = $this->currencies[$currencyId]->decimal_places; } unset($transaction['transaction_currency_id']); - $return[] = $transaction; + $return[] = $transaction; } return $return;