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)
{
$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);

View File

@@ -51,7 +51,6 @@ 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
@@ -70,6 +69,7 @@ class RecurringEnrichment implements EnrichmentInterface
private array $notes = [];
private readonly TransactionCurrency $primaryCurrency;
private array $recurrenceIds = [];
private array $recurrenceByTransaction = [];
private array $repetitions = [];
private array $sourceAccountIds = [];
private array $transactions = [];
@@ -84,6 +84,7 @@ class RecurringEnrichment implements EnrichmentInterface
public function enrich(Collection $collection): Collection
{
Log::debug(__METHOD__);
$this->collection = $collection;
$this->collectIds();
$this->collectRepetitions();
@@ -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,18 +290,11 @@ 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
@@ -306,10 +302,11 @@ class RecurringEnrichment implements EnrichmentInterface
$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()
;
->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)));
}
@@ -343,9 +340,10 @@ class RecurringEnrichment implements EnrichmentInterface
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;
$recurrenceId = (int)$repetition->recurrence_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:
$amount = 'daily' === $repetition->repetition_type ? 9 : 5;
@@ -356,7 +354,7 @@ class RecurringEnrichment implements EnrichmentInterface
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;
$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;
$recurrenceId = (int)$transaction->recurrence_id;
$transactionId = (int)$transaction->id;
$this->recurrenceIds[$transactionId] = $id;
$this->transactions[$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;