Fix currency collection.

This commit is contained in:
James Cole
2025-08-07 19:37:36 +02:00
parent eb6d585bb2
commit 85337c53d4
3 changed files with 31 additions and 16 deletions

View File

@@ -29,6 +29,8 @@ class BudgetLimitEnrichment implements EnrichmentInterface
private Collection $budgets; private Collection $budgets;
private array $expenses = []; private array $expenses = [];
private array $pcExpenses = []; private array $pcExpenses = [];
private array $currencyIds = [];
private array $currencies = [];
private bool $convertToPrimary = true; private bool $convertToPrimary = true;
private TransactionCurrency $primaryCurrency; private TransactionCurrency $primaryCurrency;
@@ -42,6 +44,7 @@ class BudgetLimitEnrichment implements EnrichmentInterface
{ {
$this->collection = $collection; $this->collection = $collection;
$this->collectIds(); $this->collectIds();
$this->collectCurrencies();
$this->collectNotes(); $this->collectNotes();
$this->collectBudgets(); $this->collectBudgets();
$this->appendCollectedData(); $this->appendCollectedData();
@@ -76,18 +79,22 @@ class BudgetLimitEnrichment implements EnrichmentInterface
/** @var BudgetLimit $limit */ /** @var BudgetLimit $limit */
foreach ($this->collection as $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 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', BudgetLimit::class)->get(['notes.noteable_id', 'notes.text'])->toArray() ->where('noteable_type', BudgetLimit::class)->get(['notes.noteable_id', 'notes.text'])->toArray();
;
foreach ($notes as $note) { foreach ($notes as $note) {
$this->notes[(int)$note['noteable_id']] = (string)$note['text']; $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) { $this->collection = $this->collection->map(function (BudgetLimit $item) {
$id = (int)$item->id; $id = (int)$item->id;
$currencyId = (int)$item->transaction_currency_id;
if (0 === $currencyId) {
$currencyId = $this->primaryCurrency->id;
}
$meta = [ $meta = [
'notes' => $this->notes[$id] ?? null, 'notes' => $this->notes[$id] ?? null,
'spent' => $this->expenses[$id] ?? [], 'spent' => $this->expenses[$id] ?? [],
'pc_spent' => $this->pcExpenses[$id] ?? [], 'pc_spent' => $this->pcExpenses[$id] ?? [],
'currency' => $this->currencies[$currencyId],
]; ];
$item->meta = $meta; $item->meta = $meta;
@@ -114,9 +126,9 @@ class BudgetLimitEnrichment implements EnrichmentInterface
$budgetIds = $this->collection->pluck('budget_id')->unique()->toArray(); $budgetIds = $this->collection->pluck('budget_id')->unique()->toArray();
$this->budgets = Budget::whereIn('id', $budgetIds)->get(); $this->budgets = Budget::whereIn('id', $budgetIds)->get();
$repository = app(OperationsRepository::class); $repository = app(OperationsRepository::class);
$repository->setUser($this->user); $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 */ /** @var BudgetLimit $budgetLimit */
foreach ($this->collection as $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;
}
}
} }

View File

@@ -173,7 +173,7 @@ class RecurringEnrichment implements EnrichmentInterface
$this->transactions[$id][$transactionId] = [ $this->transactions[$id][$transactionId] = [
'id' => (string)$transactionId, 'id' => (string)$transactionId,
'recurrence_id' => $id, //'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,

View File

@@ -64,10 +64,7 @@ class BudgetLimitTransformer extends AbstractTransformer
public function transform(BudgetLimit $budgetLimit): array public function transform(BudgetLimit $budgetLimit): array
{ {
$currency = $budgetLimit->transactionCurrency; $currency = $budgetLimit->meta['currency'];
if (null === $currency) {
$currency = $this->primaryCurrency;
}
$amount = Steam::bcround($budgetLimit->amount, $currency->decimal_places); $amount = Steam::bcround($budgetLimit->amount, $currency->decimal_places);
$pcAmount = null; $pcAmount = null;
if ($this->convertToPrimary && $currency->id === $this->primaryCurrency->id) { if ($this->convertToPrimary && $currency->id === $this->primaryCurrency->id) {
@@ -76,9 +73,6 @@ class BudgetLimitTransformer extends AbstractTransformer
if ($this->convertToPrimary && $currency->id !== $this->primaryCurrency->id) { if ($this->convertToPrimary && $currency->id !== $this->primaryCurrency->id) {
$pcAmount = Steam::bcround($budgetLimit->native_amount, $this->primaryCurrency->decimal_places); $pcAmount = Steam::bcround($budgetLimit->native_amount, $this->primaryCurrency->decimal_places);
} }
// TODO fix currency collection.
// TODO fix documentation.a
return [ return [
'id' => (string)$budgetLimit->id, 'id' => (string)$budgetLimit->id,
'created_at' => $budgetLimit->created_at->toAtomString(), 'created_at' => $budgetLimit->created_at->toAtomString(),