From 85337c53d4a0d1cabc1557d3ddb31785517ff129 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 7 Aug 2025 19:37:36 +0200 Subject: [PATCH] Fix currency collection. --- .../Enrichments/BudgetLimitEnrichment.php | 37 +++++++++++++++---- .../Enrichments/RecurringEnrichment.php | 2 +- app/Transformers/BudgetLimitTransformer.php | 8 +--- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/app/Support/JsonApi/Enrichments/BudgetLimitEnrichment.php b/app/Support/JsonApi/Enrichments/BudgetLimitEnrichment.php index bacb95bed7..58593f321f 100644 --- a/app/Support/JsonApi/Enrichments/BudgetLimitEnrichment.php +++ b/app/Support/JsonApi/Enrichments/BudgetLimitEnrichment.php @@ -29,6 +29,8 @@ class BudgetLimitEnrichment implements EnrichmentInterface private Collection $budgets; private array $expenses = []; private array $pcExpenses = []; + private array $currencyIds = []; + private array $currencies = []; private bool $convertToPrimary = true; private TransactionCurrency $primaryCurrency; @@ -42,6 +44,7 @@ class BudgetLimitEnrichment implements EnrichmentInterface { $this->collection = $collection; $this->collectIds(); + $this->collectCurrencies(); $this->collectNotes(); $this->collectBudgets(); $this->appendCollectedData(); @@ -76,18 +79,22 @@ class BudgetLimitEnrichment implements EnrichmentInterface /** @var BudgetLimit $limit */ foreach ($this->collection as $limit) { - $this->ids[] = (int)$limit->id; + $id = (int)$limit->id; + $this->ids[] = $id; + if (0 !== (int)$limit->transaction_currency_id) { + $this->currencyIds[$id] = (int)$limit->transaction_currency_id; + } } - $this->ids = array_unique($this->ids); + $this->ids = array_unique($this->ids); + $this->currencyIds = array_unique($this->currencyIds); } private function collectNotes(): void { $notes = Note::query()->whereIn('noteable_id', $this->ids) - ->whereNotNull('notes.text') - ->where('notes.text', '!=', '') - ->where('noteable_type', BudgetLimit::class)->get(['notes.noteable_id', 'notes.text'])->toArray() - ; + ->whereNotNull('notes.text') + ->where('notes.text', '!=', '') + ->where('noteable_type', BudgetLimit::class)->get(['notes.noteable_id', 'notes.text'])->toArray(); foreach ($notes as $note) { $this->notes[(int)$note['noteable_id']] = (string)$note['text']; } @@ -98,10 +105,15 @@ class BudgetLimitEnrichment implements EnrichmentInterface { $this->collection = $this->collection->map(function (BudgetLimit $item) { $id = (int)$item->id; + $currencyId = (int)$item->transaction_currency_id; + if (0 === $currencyId) { + $currencyId = $this->primaryCurrency->id; + } $meta = [ 'notes' => $this->notes[$id] ?? null, 'spent' => $this->expenses[$id] ?? [], 'pc_spent' => $this->pcExpenses[$id] ?? [], + 'currency' => $this->currencies[$currencyId], ]; $item->meta = $meta; @@ -114,9 +126,9 @@ class BudgetLimitEnrichment implements EnrichmentInterface $budgetIds = $this->collection->pluck('budget_id')->unique()->toArray(); $this->budgets = Budget::whereIn('id', $budgetIds)->get(); - $repository = app(OperationsRepository::class); + $repository = app(OperationsRepository::class); $repository->setUser($this->user); - $expenses = $repository->collectExpenses($this->start, $this->end, null, $this->budgets, null); + $expenses = $repository->collectExpenses($this->start, $this->end, null, $this->budgets, null); /** @var BudgetLimit $budgetLimit */ foreach ($this->collection as $budgetLimit) { @@ -133,4 +145,13 @@ class BudgetLimitEnrichment implements EnrichmentInterface } } } + + private function collectCurrencies(): void + { + $this->currencies[$this->primaryCurrency->id] = $this->primaryCurrency; + $currencies = TransactionCurrency::whereIn('id', $this->currencyIds)->whereNot('id', $this->primaryCurrency->id)->get(); + foreach ($currencies as $currency) { + $this->currencies[(int)$currency->id] = $currency; + } + } } diff --git a/app/Support/JsonApi/Enrichments/RecurringEnrichment.php b/app/Support/JsonApi/Enrichments/RecurringEnrichment.php index ba8bf9540f..9a8284758f 100644 --- a/app/Support/JsonApi/Enrichments/RecurringEnrichment.php +++ b/app/Support/JsonApi/Enrichments/RecurringEnrichment.php @@ -173,7 +173,7 @@ class RecurringEnrichment implements EnrichmentInterface $this->transactions[$id][$transactionId] = [ 'id' => (string)$transactionId, - 'recurrence_id' => $id, + //'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, diff --git a/app/Transformers/BudgetLimitTransformer.php b/app/Transformers/BudgetLimitTransformer.php index e43d34f647..9655110254 100644 --- a/app/Transformers/BudgetLimitTransformer.php +++ b/app/Transformers/BudgetLimitTransformer.php @@ -64,10 +64,7 @@ class BudgetLimitTransformer extends AbstractTransformer public function transform(BudgetLimit $budgetLimit): array { - $currency = $budgetLimit->transactionCurrency; - if (null === $currency) { - $currency = $this->primaryCurrency; - } + $currency = $budgetLimit->meta['currency']; $amount = Steam::bcround($budgetLimit->amount, $currency->decimal_places); $pcAmount = null; if ($this->convertToPrimary && $currency->id === $this->primaryCurrency->id) { @@ -76,9 +73,6 @@ class BudgetLimitTransformer extends AbstractTransformer if ($this->convertToPrimary && $currency->id !== $this->primaryCurrency->id) { $pcAmount = Steam::bcround($budgetLimit->native_amount, $this->primaryCurrency->decimal_places); } - // TODO fix currency collection. - // TODO fix documentation.a - return [ 'id' => (string)$budgetLimit->id, 'created_at' => $budgetLimit->created_at->toAtomString(),