Add a bunch of debug #11247

This commit is contained in:
James Cole
2025-11-17 19:01:38 +01:00
parent a8fffc25c0
commit 4a901a1b42
2 changed files with 83 additions and 81 deletions

View File

@@ -174,7 +174,7 @@ class EditController extends Controller
public function update(RecurrenceFormRequest $request, Recurrence $recurrence) public function update(RecurrenceFormRequest $request, Recurrence $recurrence)
{ {
$data = $request->getAll(); $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])); $request->session()->flash('success', (string) trans('firefly.updated_recurrence', ['title' => $recurrence->title]));
Log::channel('audit')->info(sprintf('Updated recurrence #%d.', $recurrence->id), $data); Log::channel('audit')->info(sprintf('Updated recurrence #%d.', $recurrence->id), $data);

View File

@@ -51,28 +51,28 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use function Safe\json_decode; use function Safe\json_decode;
class RecurringEnrichment implements EnrichmentInterface class RecurringEnrichment implements EnrichmentInterface
{ {
private array $accounts = []; private array $accounts = [];
private Collection $collection; private Collection $collection;
// private array $transactionTypeIds = []; // private array $transactionTypeIds = [];
// private array $transactionTypes = []; // private array $transactionTypes = [];
private readonly bool $convertToPrimary; private readonly bool $convertToPrimary;
private array $currencies = []; private array $currencies = [];
private array $currencyIds = []; private array $currencyIds = [];
private array $destinationAccountIds = []; private array $destinationAccountIds = [];
private array $foreignCurrencyIds = []; private array $foreignCurrencyIds = [];
private array $ids = []; private array $ids = [];
private string $language = 'en_US'; private string $language = 'en_US';
private array $notes = []; private array $notes = [];
private readonly TransactionCurrency $primaryCurrency; private readonly TransactionCurrency $primaryCurrency;
private array $recurrenceIds = []; private array $recurrenceIds = [];
private array $repetitions = []; private array $recurrenceByTransaction = [];
private array $sourceAccountIds = []; private array $repetitions = [];
private array $transactions = []; private array $sourceAccountIds = [];
private array $transactions = [];
private User $user; private User $user;
private UserGroup $userGroup; private UserGroup $userGroup;
@@ -84,6 +84,7 @@ class RecurringEnrichment implements EnrichmentInterface
public function enrich(Collection $collection): Collection public function enrich(Collection $collection): Collection
{ {
Log::debug(__METHOD__);
$this->collection = $collection; $this->collection = $collection;
$this->collectIds(); $this->collectIds();
$this->collectRepetitions(); $this->collectRepetitions();
@@ -98,7 +99,7 @@ class RecurringEnrichment implements EnrichmentInterface
return $this->collection; return $this->collection;
} }
public function enrichSingle(array|Model $model): array|Model public function enrichSingle(array | Model $model): array | Model
{ {
Log::debug(__METHOD__); Log::debug(__METHOD__);
$collection = new Collection()->push($model); $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); return (string)trans('firefly.recurring_monthly', ['dayOfMonth' => $repetition->repetition_moment, 'skip' => $repetition->repetition_skip - 1], $this->language);
} }
if ('ndom' === $repetition->repetition_type) { if ('ndom' === $repetition->repetition_type) {
$parts = explode(',', $repetition->repetition_moment); $parts = explode(',', $repetition->repetition_moment);
// first part is number of week, second is weekday. // first part is number of week, second is weekday.
$dayOfWeek = trans(sprintf('config.dow_%s', $parts[1]), [], $this->language); $dayOfWeek = trans(sprintf('config.dow_%s', $parts[1]), [], $this->language);
if ($repetition->repetition_skip > 0) { if ($repetition->repetition_skip > 0) {
@@ -149,7 +150,7 @@ class RecurringEnrichment implements EnrichmentInterface
} }
// $diffInYears = (int)$today->diffInYears($repDate, true); // $diffInYears = (int)$today->diffInYears($repDate, true);
// $repDate->addYears($diffInYears); // technically not necessary. // $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); return (string)trans('firefly.recurring_yearly', ['date' => $string], $this->language);
} }
@@ -172,8 +173,8 @@ class RecurringEnrichment implements EnrichmentInterface
private function appendCollectedData(): void private function appendCollectedData(): void
{ {
$this->collection = $this->collection->map(function (Recurrence $item): Recurrence { $this->collection = $this->collection->map(function (Recurrence $item): Recurrence {
$id = (int)$item->id; $id = (int)$item->id;
$meta = [ $meta = [
'notes' => $this->notes[$id] ?? null, 'notes' => $this->notes[$id] ?? null,
'repetitions' => array_values($this->repetitions[$id] ?? []), 'repetitions' => array_values($this->repetitions[$id] ?? []),
'transactions' => $this->processTransactions(array_values($this->transactions[$id] ?? [])), 'transactions' => $this->processTransactions(array_values($this->transactions[$id] ?? [])),
@@ -194,6 +195,7 @@ class RecurringEnrichment implements EnrichmentInterface
foreach ($accounts as $account) { foreach ($accounts as $account) {
$id = (int)$account->id; $id = (int)$account->id;
$this->accounts[$id] = $account; $this->accounts[$id] = $account;
Log::debug(sprintf('Collected account #%d', $id));
} }
} }
@@ -279,6 +281,7 @@ class RecurringEnrichment implements EnrichmentInterface
foreach ($currencies as $currency) { foreach ($currencies as $currency) {
$id = (int)$currency->id; $id = (int)$currency->id;
$this->currencies[$id] = $currency; $this->currencies[$id] = $currency;
Log::debug(sprintf('Collected currency #%d', $id));
} }
} }
@@ -287,29 +290,23 @@ class RecurringEnrichment implements EnrichmentInterface
/** @var Recurrence $recurrence */ /** @var Recurrence $recurrence */
foreach ($this->collection as $recurrence) { foreach ($this->collection as $recurrence) {
$id = (int)$recurrence->id; $id = (int)$recurrence->id;
// $typeId = (int)$recurrence->transaction_type_id;
$this->ids[] = $id; $this->ids[] = $id;
// $this->transactionTypeIds[$id] = $typeId; Log::debug(sprintf('Collected recurrence id #%d', $id));
} }
$this->ids = array_unique($this->ids); $this->ids = array_unique($this->ids);
Log::debug('Final array:', $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);
// }
} }
private function collectNotes(): void private function collectNotes(): void
{ {
$notes = Note::query()->whereIn('noteable_id', $this->ids) $notes = Note::query()->whereIn('noteable_id', $this->ids)
->whereNotNull('notes.text') ->whereNotNull('notes.text')
->where('notes.text', '!=', '') ->where('notes.text', '!=', '')
->where('noteable_type', Recurrence::class)->get(['notes.noteable_id', 'notes.text'])->toArray() ->where('noteable_type', Recurrence::class)->get(['notes.id', 'notes.noteable_id', 'notes.text'])->toArray();
;
foreach ($notes as $note) { 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))); 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()'); Log::debug('Start of enrichment: collectRepetitions()');
$repository = app(RecurringRepositoryInterface::class); $repository = app(RecurringRepositoryInterface::class);
$repository->setUserGroup($this->userGroup); $repository->setUserGroup($this->userGroup);
$set = RecurrenceRepetition::whereIn('recurrence_id', $this->ids)->get(); $set = RecurrenceRepetition::whereIn('recurrence_id', $this->ids)->get();
/** @var RecurrenceRepetition $repetition */ /** @var RecurrenceRepetition $repetition */
foreach ($set as $repetition) { foreach ($set as $repetition) {
$recurrence = $this->collection->filter(fn (Recurrence $item): bool => (int)$item->id === (int)$repetition->recurrence_id)->first(); $recurrence = $this->collection->filter(fn(Recurrence $item): bool => (int)$item->id === (int)$repetition->recurrence_id)->first();
$fromDate = clone ($recurrence->latest_date ?? $recurrence->first_date); $fromDate = clone($recurrence->latest_date ?? $recurrence->first_date);
$id = (int)$repetition->recurrence_id; $recurrenceId = (int)$repetition->recurrence_id;
$repId = (int)$repetition->id; $repId = (int)$repetition->id;
$this->repetitions[$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: // get the (future) occurrences for this specific type of repetition:
$amount = 'daily' === $repetition->repetition_type ? 9 : 5; $amount = 'daily' === $repetition->repetition_type ? 9 : 5;
$set = $repository->getXOccurrencesSince($repetition, $fromDate, now(config('app.timezone')), $amount); $set = $repository->getXOccurrencesSince($repetition, $fromDate, now(config('app.timezone')), $amount);
$occurrences = []; $occurrences = [];
/** @var Carbon $carbon */ /** @var Carbon $carbon */
foreach ($set as $carbon) { foreach ($set as $carbon) {
$occurrences[] = $carbon->toAtomString(); $occurrences[] = $carbon->toAtomString();
} }
$this->repetitions[$id][$repId] = [ $this->repetitions[$recurrenceId][$repId] = [
'id' => (string)$repId, 'id' => (string)$repId,
'created_at' => $repetition->created_at->toAtomString(), 'created_at' => $repetition->created_at->toAtomString(),
'updated_at' => $repetition->updated_at->toAtomString(), 'updated_at' => $repetition->updated_at->toAtomString(),
@@ -373,8 +371,11 @@ class RecurringEnrichment implements EnrichmentInterface
private function collectTransactionMetaData(): void private function collectTransactionMetaData(): void
{ {
$ids = array_keys($this->transactions); $rtIds = [];
$meta = RecurrenceTransactionMeta::whereNull('deleted_at')->whereIn('rt_id', $ids)->get(); 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: // other meta-data to be collected:
$billIds = []; $billIds = [];
$piggyBankIds = []; $piggyBankIds = [];
@@ -386,10 +387,11 @@ class RecurringEnrichment implements EnrichmentInterface
$transactionId = (int)$entry->rt_id; $transactionId = (int)$entry->rt_id;
// this should refer to another array, were rtIds can be used to find the recurrence. // this should refer to another array, were rtIds can be used to find the recurrence.
$recurrenceId = $this->recurrenceIds[$transactionId] ?? 0; $recurrenceId = $this->recurrenceByTransaction[$transactionId] ?? 0;
$name = (string)($entry->name ?? ''); 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) { 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; continue;
} }
@@ -487,16 +489,16 @@ class RecurringEnrichment implements EnrichmentInterface
/** @var RecurrenceTransaction $transaction */ /** @var RecurrenceTransaction $transaction */
foreach ($set as $transaction) { foreach ($set as $transaction) {
$id = (int)$transaction->recurrence_id; $recurrenceId = (int)$transaction->recurrence_id;
$transactionId = (int)$transaction->id; $transactionId = (int)$transaction->id;
$this->recurrenceIds[$transactionId] = $id; $this->recurrenceByTransaction[$transactionId] = $recurrenceId;
$this->transactions[$id] ??= []; $this->transactions[$recurrenceId] ??= [];
$amount = $transaction->amount; $amount = $transaction->amount;
$foreignAmount = $transaction->foreign_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, 'id' => (string)$transactionId,
// 'recurrence_id' => $id,
'transaction_currency_id' => (int)$transaction->transaction_currency_id, 'transaction_currency_id' => (int)$transaction->transaction_currency_id,
'foreign_currency_id' => null === $transaction->foreign_currency_id ? null : (int)$transaction->foreign_currency_id, 'foreign_currency_id' => null === $transaction->foreign_currency_id ? null : (int)$transaction->foreign_currency_id,
'source_id' => (int)$transaction->source_id, 'source_id' => (int)$transaction->source_id,
@@ -518,7 +520,7 @@ class RecurringEnrichment implements EnrichmentInterface
'subscription_name' => null, '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->currencyIds[$transactionId] = (int)$transaction->transaction_currency_id;
$this->sourceAccountIds[$transactionId] = (int)$transaction->source_id; $this->sourceAccountIds[$transactionId] = (int)$transaction->source_id;
$this->destinationAccountIds[$transactionId] = (int)$transaction->destination_id; $this->destinationAccountIds[$transactionId] = (int)$transaction->destination_id;
@@ -531,8 +533,8 @@ class RecurringEnrichment implements EnrichmentInterface
private function getLanguage(): void private function getLanguage(): void
{ {
/** @var Preference $preference */ /** @var Preference $preference */
$preference = Preferences::getForUser($this->user, 'language', config('firefly.default_language', 'en_US')); $preference = Preferences::getForUser($this->user, 'language', config('firefly.default_language', 'en_US'));
$language = $preference->data; $language = $preference->data;
if (is_array($language)) { if (is_array($language)) {
$language = 'en_US'; $language = 'en_US';
} }
@@ -545,9 +547,9 @@ class RecurringEnrichment implements EnrichmentInterface
$return = []; $return = [];
$converter = new ExchangeRateConverter(); $converter = new ExchangeRateConverter();
foreach ($transactions as $transaction) { foreach ($transactions as $transaction) {
$currencyId = $transaction['transaction_currency_id']; $currencyId = $transaction['transaction_currency_id'];
$pcAmount = null; $pcAmount = null;
$pcForeignAmount = null; $pcForeignAmount = null;
// set the same amount in the primary currency, if both are the same anyway. // set the same amount in the primary currency, if both are the same anyway.
if ($this->convertToPrimary && $currencyId === (int)$this->primaryCurrency->id) { if ($this->convertToPrimary && $currencyId === (int)$this->primaryCurrency->id) {
$pcAmount = $transaction['amount']; $pcAmount = $transaction['amount'];
@@ -563,26 +565,26 @@ class RecurringEnrichment implements EnrichmentInterface
} }
} }
$transaction['pc_amount'] = $pcAmount; $transaction['pc_amount'] = $pcAmount;
$transaction['pc_foreign_amount'] = $pcForeignAmount; $transaction['pc_foreign_amount'] = $pcForeignAmount;
$sourceId = $transaction['source_id']; $sourceId = $transaction['source_id'];
$transaction['source_name'] = $this->accounts[$sourceId]->name; $transaction['source_name'] = $this->accounts[$sourceId]->name;
$transaction['source_iban'] = $this->accounts[$sourceId]->iban; $transaction['source_iban'] = $this->accounts[$sourceId]->iban;
$transaction['source_type'] = $this->accounts[$sourceId]->accountType->type; $transaction['source_type'] = $this->accounts[$sourceId]->accountType->type;
$transaction['source_id'] = (string)$transaction['source_id']; $transaction['source_id'] = (string)$transaction['source_id'];
$destId = $transaction['destination_id']; $destId = $transaction['destination_id'];
$transaction['destination_name'] = $this->accounts[$destId]->name; $transaction['destination_name'] = $this->accounts[$destId]->name;
$transaction['destination_iban'] = $this->accounts[$destId]->iban; $transaction['destination_iban'] = $this->accounts[$destId]->iban;
$transaction['destination_type'] = $this->accounts[$destId]->accountType->type; $transaction['destination_type'] = $this->accounts[$destId]->accountType->type;
$transaction['destination_id'] = (string)$transaction['destination_id']; $transaction['destination_id'] = (string)$transaction['destination_id'];
$transaction['currency_id'] = (string)$currencyId; $transaction['currency_id'] = (string)$currencyId;
$transaction['currency_name'] = $this->currencies[$currencyId]->name; $transaction['currency_name'] = $this->currencies[$currencyId]->name;
$transaction['currency_code'] = $this->currencies[$currencyId]->code; $transaction['currency_code'] = $this->currencies[$currencyId]->code;
$transaction['currency_symbol'] = $this->currencies[$currencyId]->symbol; $transaction['currency_symbol'] = $this->currencies[$currencyId]->symbol;
$transaction['currency_decimal_places'] = $this->currencies[$currencyId]->decimal_places; $transaction['currency_decimal_places'] = $this->currencies[$currencyId]->decimal_places;
$transaction['primary_currency_id'] = (string)$this->primaryCurrency->id; $transaction['primary_currency_id'] = (string)$this->primaryCurrency->id;
$transaction['primary_currency_name'] = $this->primaryCurrency->name; $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; $transaction['foreign_currency_decimal_places'] = $this->currencies[$currencyId]->decimal_places;
} }
unset($transaction['transaction_currency_id']); unset($transaction['transaction_currency_id']);
$return[] = $transaction; $return[] = $transaction;
} }
return $return; return $return;