From 9f25880a59744c3211e623018c5bd9898ad3aaf1 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 29 Dec 2024 18:32:02 +0100 Subject: [PATCH] Clean up several endpoints with complex code. --- .../Account/OperationsRepository.php | 107 +---------- .../Budget/NoBudgetRepository.php | 58 +----- .../Budget/OperationsRepository.php | 57 +----- .../Category/NoCategoryRepository.php | 47 +---- .../Category/OperationsRepository.php | 54 +----- .../Summarizer/TransactionSummarizer.php | 178 ++++++++++++++++++ app/Transformers/AccountTransformer.php | 2 +- 7 files changed, 198 insertions(+), 305 deletions(-) create mode 100644 app/Support/Report/Summarizer/TransactionSummarizer.php diff --git a/app/Repositories/Account/OperationsRepository.php b/app/Repositories/Account/OperationsRepository.php index b214e45d76..5f0bdca8bf 100644 --- a/app/Repositories/Account/OperationsRepository.php +++ b/app/Repositories/Account/OperationsRepository.php @@ -29,6 +29,7 @@ use FireflyIII\Enums\TransactionTypeEnum; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Support\Facades\Amount; +use FireflyIII\Support\Report\Summarizer\TransactionSummarizer; use FireflyIII\User; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Support\Collection; @@ -219,44 +220,8 @@ class OperationsRepository implements OperationsRepositoryInterface private function groupByCurrency(array $journals, string $direction): array { - $array = []; - $convertToNative = Amount::convertToNative($this->user); - $default = Amount::getDefaultCurrencyByUserGroup($this->user->userGroup); - - foreach ($journals as $journal) { - // currency - $currencyId = $journal['currency_id']; - $currencyName = $journal['currency_name']; - $currencySymbol = $journal['currency_symbol']; - $currencyCode = $journal['currency_code']; - $currencyDecimalPlaces = $journal['currency_decimal_places']; - $field = $convertToNative && $currencyId !== $default->id ? 'native_amount' : 'amount'; - - // perhaps use default currency instead? - if ($convertToNative && $journal['currency_id'] !== $default->id) { - $currencyId = $default->id; - $currencyName = $default->name; - $currencySymbol = $default->symbol; - $currencyCode = $default->code; - $currencyDecimalPlaces = $default->decimal_places; - } - // use foreign amount when the foreign currency IS the default currency. - if ($convertToNative && $journal['currency_id'] !== $default->id && $default->id === $journal['foreign_currency_id']) { - $field = 'foreign_amount'; - } - - $array[$currencyId] ??= [ - 'sum' => '0', - 'currency_id' => $currencyId, - 'currency_name' => $currencyName, - 'currency_symbol' => $currencySymbol, - 'currency_code' => $currencyCode, - 'currency_decimal_places' => $currencyDecimalPlaces, - ]; - $array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->{$direction}($journal[$field])); // @phpstan-ignore-line - } - - return $array; + $summarizer = new TransactionSummarizer($this->user); + return $summarizer->groupByCurrencyId($journals, $direction); } /** @@ -277,70 +242,8 @@ class OperationsRepository implements OperationsRepositoryInterface private function groupByDirection(array $journals, string $direction, string $method): array { - $array = []; - $idKey = sprintf('%s_account_id', $direction); - $nameKey = sprintf('%s_account_name', $direction); - $convertToNative = Amount::convertToNative($this->user); - $default = Amount::getDefaultCurrencyByUserGroup($this->user->userGroup); - Log::debug(sprintf('groupByDirection(array, %s, %s).', $direction, $method)); - foreach ($journals as $journal) { - // currency - $currencyId = $journal['currency_id']; - $currencyName = $journal['currency_name']; - $currencySymbol = $journal['currency_symbol']; - $currencyCode = $journal['currency_code']; - $currencyDecimalPlaces = $journal['currency_decimal_places']; - $field = $convertToNative && $currencyId !== $default->id ? 'native_amount' : 'amount'; - - // perhaps use default currency instead? - if ($convertToNative && $journal['currency_id'] !== $default->id) { - $currencyId = $default->id; - $currencyName = $default->name; - $currencySymbol = $default->symbol; - $currencyCode = $default->code; - $currencyDecimalPlaces = $default->decimal_places; - } - // use foreign amount when the foreign currency IS the default currency. - if ($convertToNative && $journal['currency_id'] !== $default->id && $default->id === $journal['foreign_currency_id']) { - $field = 'foreign_amount'; - } - $key = sprintf('%s-%s', $journal[$idKey], $currencyId); - // sum it all up or create a new array. - $array[$key] ??= [ - 'id' => $journal[$idKey], - 'name' => $journal[$nameKey], - 'sum' => '0', - 'currency_id' => $currencyId, - 'currency_name' => $currencyName, - 'currency_symbol' => $currencySymbol, - 'currency_code' => $currencyCode, - 'currency_decimal_places' => $currencyDecimalPlaces, - ]; - - // add the data from the $field to the array. - $array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string) ($journal[$field] ?? '0'))); // @phpstan-ignore-line - Log::debug(sprintf('Field for transaction #%d is "%s" (%s). Sum: %s', $journal['transaction_group_id'], $currencyCode, $field, $array[$key]['sum'])); - - // also do foreign amount, but only when convertToNative is false (otherwise we have it already) - // or when convertToNative is true and the foreign currency is ALSO not the default currency. - if ((!$convertToNative || $journal['foreign_currency_id'] !== $default->id) && 0 !== (int) $journal['foreign_currency_id']) { - Log::debug(sprintf('Use foreign amount from transaction #%d: %s %s. Sum: %s', $journal['transaction_group_id'], $currencyCode, $journal['foreign_amount'], $array[$key]['sum'])); - $key = sprintf('%s-%s', $journal[$idKey], $journal['foreign_currency_id']); - $array[$key] ??= [ - 'id' => $journal[$idKey], - 'name' => $journal[$nameKey], - 'sum' => '0', - 'currency_id' => $journal['foreign_currency_id'], - 'currency_name' => $journal['foreign_currency_name'], - 'currency_symbol' => $journal['foreign_currency_symbol'], - 'currency_code' => $journal['foreign_currency_code'], - 'currency_decimal_places' => $journal['foreign_currency_decimal_places'], - ]; - $array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string) $journal['foreign_amount'])); // @phpstan-ignore-line - } - } - - return $array; + $summarizer = new TransactionSummarizer($this->user); + return $summarizer->groupByDirection($journals, $method, $direction); } /** diff --git a/app/Repositories/Budget/NoBudgetRepository.php b/app/Repositories/Budget/NoBudgetRepository.php index 7630d50881..21dcae270f 100644 --- a/app/Repositories/Budget/NoBudgetRepository.php +++ b/app/Repositories/Budget/NoBudgetRepository.php @@ -25,10 +25,12 @@ declare(strict_types=1); namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; +use FireflyIII\Enums\TransactionTypeEnum; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionType; use FireflyIII\Support\Facades\Amount; +use FireflyIII\Support\Report\Summarizer\TransactionSummarizer; use FireflyIII\User; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Support\Collection; @@ -50,7 +52,7 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface $collector = app(GroupCollectorInterface::class); $collector->setAccounts($accounts)->setRange($start, $end); - $collector->setTypes([TransactionType::WITHDRAWAL]); + $collector->setTypes([TransactionTypeEnum::WITHDRAWAL->value]); $collector->withoutBudget(); $journals = $collector->getExtractedJournals(); $data = []; @@ -103,57 +105,7 @@ class NoBudgetRepository implements NoBudgetRepositoryInterface $collector->withoutBudget(); $collector->withBudgetInformation(); $journals = $collector->getExtractedJournals(); - $array = []; - $convertToNative = Amount::convertToNative($this->user); - $default = Amount::getDefaultCurrency(); - - foreach ($journals as $journal) { - // same as in the other methods. - $amount = '0'; - $currencyId = (int) $journal['currency_id']; - $currencyName = $journal['currency_name']; - $currencySymbol = $journal['currency_symbol']; - $currencyCode = $journal['currency_code']; - $currencyDecimalPlaces = $journal['currency_decimal_places']; - - if ($convertToNative) { - $useNative = $default->id !== (int) $journal['currency_id']; - $amount = Amount::getAmountFromJournal($journal); - if ($useNative) { - Log::debug(sprintf('Journal #%d switches to native amount (original is %s)', $journal['transaction_journal_id'], $journal['currency_code'])); - $currencyId = $default->id; - $currencyName = $default->name; - $currencySymbol = $default->symbol; - $currencyCode = $default->code; - $currencyDecimalPlaces = $default->decimal_places; - } - } - if (!$convertToNative) { - $amount = $journal['amount']; - // if the amount is not in $currency (but should be), use the foreign_amount if that one is correct. - // otherwise, ignore the transaction all together. - if (null !== $currency && $currencyId !== $currency->id && $currency->id === (int) $journal['foreign_currency_id']) { - Log::debug(sprintf('Journal #%d switches to foreign amount because it matches native.', $journal['transaction_journal_id'])); - $amount = $journal['foreign_amount']; - $currencyId = (int) $journal['foreign_currency_id']; - $currencyName = $journal['foreign_currency_name']; - $currencySymbol = $journal['foreign_currency_symbol']; - $currencyCode = $journal['foreign_currency_code']; - $currencyDecimalPlaces = $journal['foreign_currency_decimal_places']; - } - } - - $array[$currencyId] ??= [ - 'sum' => '0', - 'currency_id' => $currencyId, - 'currency_name' => $currencyName, - 'currency_symbol' => $currencySymbol, - 'currency_code' => $currencyCode, - 'currency_decimal_places' => $currencyDecimalPlaces, - ]; - $array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($amount)); - } - - return $array; + $summarizer = new TransactionSummarizer($this->user); + return $summarizer->groupByCurrencyId($journals); } } diff --git a/app/Repositories/Budget/OperationsRepository.php b/app/Repositories/Budget/OperationsRepository.php index 75404c40c8..eb3e82688c 100644 --- a/app/Repositories/Budget/OperationsRepository.php +++ b/app/Repositories/Budget/OperationsRepository.php @@ -33,6 +33,7 @@ use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Support\Facades\Amount; +use FireflyIII\Support\Report\Summarizer\TransactionSummarizer; use FireflyIII\User; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Support\Collection; @@ -222,10 +223,6 @@ class OperationsRepository implements OperationsRepositoryInterface $subset = $repository->getAccountsByType(config('firefly.valid_liabilities')); $selection = new Collection(); - // default currency information for native stuff. - $convertToNative = Amount::convertToNative($this->user); - $default = Amount::getDefaultCurrency(); - /** @var Account $account */ foreach ($subset as $account) { if ('credit' === $repository->getMetaValue($account, 'liability_direction')) { @@ -258,55 +255,7 @@ class OperationsRepository implements OperationsRepositoryInterface if (null !== $currency) { Log::debug('STOP looking for transactions in the foreign currency.'); } - $array = []; - - foreach ($journals as $journal) { - // TODO same as in category::sumexpenses - $amount = '0'; - $currencyId = (int) $journal['currency_id']; - $currencyName = $journal['currency_name']; - $currencySymbol = $journal['currency_symbol']; - $currencyCode = $journal['currency_code']; - $currencyDecimalPlaces = $journal['currency_decimal_places']; - if ($convertToNative) { - $useNative = $default->id !== (int) $journal['currency_id']; - $amount = Amount::getAmountFromJournal($journal); - if ($useNative) { - Log::debug(sprintf('Journal #%d switches to native amount (original is %s)', $journal['transaction_journal_id'], $journal['currency_code'])); - $currencyId = $default->id; - $currencyName = $default->name; - $currencySymbol = $default->symbol; - $currencyCode = $default->code; - $currencyDecimalPlaces = $default->decimal_places; - } - } - if (!$convertToNative) { - $amount = $journal['amount']; - // if the amount is not in $currency (but should be), use the foreign_amount if that one is correct. - // otherwise, ignore the transaction all together. - if (null !== $currency && $currencyId !== $currency->id && $currency->id === (int) $journal['foreign_currency_id']) { - Log::debug(sprintf('Journal #%d switches to foreign amount because it matches native.', $journal['transaction_journal_id'])); - $amount = $journal['foreign_amount']; - $currencyId = (int) $journal['foreign_currency_id']; - $currencyName = $journal['foreign_currency_name']; - $currencySymbol = $journal['foreign_currency_symbol']; - $currencyCode = $journal['foreign_currency_code']; - $currencyDecimalPlaces = $journal['foreign_currency_decimal_places']; - } - } - $array[$currencyId] ??= [ - 'sum' => '0', - 'currency_id' => $currencyId, - 'currency_name' => $currencyName, - 'currency_symbol' => $currencySymbol, - 'currency_code' => $currencyCode, - 'currency_decimal_places' => $currencyDecimalPlaces, - ]; - $array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($amount)); - Log::debug(sprintf('Journal #%d adds amount %s %s', $journal['transaction_journal_id'], $currencyCode, $amount)); - } - Log::debug('End of sumExpenses.', $array); - - return $array; + $summarizer = new TransactionSummarizer($this->user); + return $summarizer->groupByCurrencyId($journals); } } diff --git a/app/Repositories/Category/NoCategoryRepository.php b/app/Repositories/Category/NoCategoryRepository.php index 810fc178b3..0d62a6b979 100644 --- a/app/Repositories/Category/NoCategoryRepository.php +++ b/app/Repositories/Category/NoCategoryRepository.php @@ -28,6 +28,7 @@ use Carbon\Carbon; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\TransactionType; use FireflyIII\Support\Facades\Amount; +use FireflyIII\Support\Report\Summarizer\TransactionSummarizer; use FireflyIII\User; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Support\Collection; @@ -152,50 +153,8 @@ class NoCategoryRepository implements NoCategoryRepositoryInterface $collector->setAccounts($accounts); } $journals = $collector->getExtractedJournals(); - $array = []; - // default currency information for native stuff. - $convertToNative = Amount::convertToNative($this->user); - $default = Amount::getDefaultCurrency(); - - foreach ($journals as $journal) { - // Almost the same as in \FireflyIII\Repositories\Budget\OperationsRepository::sumExpenses - $amount = '0'; - $currencyId = (int) $journal['currency_id']; - $currencyName = $journal['currency_name']; - $currencySymbol = $journal['currency_symbol']; - $currencyCode = $journal['currency_code']; - $currencyDecimalPlaces = $journal['currency_decimal_places']; - if ($convertToNative) { - $useNative = $default->id !== (int) $journal['currency_id']; - $amount = Amount::getAmountFromJournal($journal); - if ($useNative) { - $currencyId = $default->id; - $currencyName = $default->name; - $currencySymbol = $default->symbol; - $currencyCode = $default->code; - $currencyDecimalPlaces = $default->decimal_places; - } - Log::debug(sprintf('[a] Add amount %s %s', $currencyCode, $amount)); - } - if (!$convertToNative) { - // ignore the amount in foreign currency. - Log::debug(sprintf('[b] Add amount %s %s', $currencyCode, $journal['amount'])); - $amount = $journal['amount']; - } - - - $array[$currencyId] ??= [ - 'sum' => '0', - 'currency_id' => (string) $currencyId, - 'currency_name' => $currencyName, - 'currency_symbol' => $currencySymbol, - 'currency_code' => $currencyCode, - 'currency_decimal_places' => $currencyDecimalPlaces, - ]; - $array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($amount)); - } - - return $array; + $summarizer = new TransactionSummarizer($this->user); + return $summarizer->groupByCurrencyId($journals); } /** diff --git a/app/Repositories/Category/OperationsRepository.php b/app/Repositories/Category/OperationsRepository.php index 6d4615b3f4..dbe1ff6660 100644 --- a/app/Repositories/Category/OperationsRepository.php +++ b/app/Repositories/Category/OperationsRepository.php @@ -29,6 +29,7 @@ use FireflyIII\Enums\TransactionTypeEnum; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\TransactionType; use FireflyIII\Support\Facades\Amount; +use FireflyIII\Support\Report\Summarizer\TransactionSummarizer; use FireflyIII\User; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Support\Collection; @@ -330,9 +331,6 @@ class OperationsRepository implements OperationsRepositoryInterface $collector = app(GroupCollectorInterface::class); $collector->setUser($this->user)->setRange($start, $end)->setTypes([TransactionTypeEnum::WITHDRAWAL->value]); - // default currency information for native stuff. - $convertToNative = Amount::convertToNative($this->user); - $default = Amount::getDefaultCurrency(); if (null !== $accounts && $accounts->count() > 0) { $collector->setAccounts($accounts); } @@ -342,54 +340,8 @@ class OperationsRepository implements OperationsRepositoryInterface $collector->setCategories($categories); $collector->withCategoryInformation(); $journals = $collector->getExtractedJournals(); - $array = []; - - Log::debug(sprintf('Collected %d journals', count($journals))); - - foreach ($journals as $journal) { - // Almost the same as in \FireflyIII\Repositories\Budget\OperationsRepository::sumExpenses - $amount = '0'; - $currencyId = (int) $journal['currency_id']; - $currencyName = $journal['currency_name']; - $currencySymbol = $journal['currency_symbol']; - $currencyCode = $journal['currency_code']; - $currencyDecimalPlaces = $journal['currency_decimal_places']; - if ($convertToNative) { - $amount = Amount::getAmountFromJournal($journal); - if ($default->id !== (int) $journal['currency_id'] && $default->id !== (int) $journal['foreign_currency_id']) { - $currencyId = $default->id; - $currencyName = $default->name; - $currencySymbol = $default->symbol; - $currencyCode = $default->code; - $currencyDecimalPlaces = $default->decimal_places; - } - if ($default->id !== (int) $journal['currency_id'] && $default->id === (int) $journal['foreign_currency_id']) { - $currencyId = $journal['foreign_currency_id']; - $currencyName = $journal['foreign_currency_name']; - $currencySymbol = $journal['foreign_currency_symbol']; - $currencyCode = $journal['foreign_currency_code']; - $currencyDecimalPlaces = $journal['foreign_currency_decimal_places']; - } - Log::debug(sprintf('[a] Add amount %s %s', $currencyCode, $amount)); - } - if (!$convertToNative) { - // ignore the amount in foreign currency. - Log::debug(sprintf('[b] Add amount %s %s', $currencyCode, $journal['amount'])); - $amount = $journal['amount']; - } - - $array[$currencyId] ??= [ - 'sum' => '0', - 'currency_id' => (string) $currencyId, - 'currency_name' => $currencyName, - 'currency_symbol' => $currencySymbol, - 'currency_code' => $currencyCode, - 'currency_decimal_places' => $currencyDecimalPlaces, - ]; - $array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($amount)); - } - - return $array; + $summarizer = new TransactionSummarizer($this->user); + return $summarizer->groupByCurrencyId($journals); } /** diff --git a/app/Support/Report/Summarizer/TransactionSummarizer.php b/app/Support/Report/Summarizer/TransactionSummarizer.php new file mode 100644 index 0000000000..d6cbbf04f6 --- /dev/null +++ b/app/Support/Report/Summarizer/TransactionSummarizer.php @@ -0,0 +1,178 @@ +setUser($user); + } + } + + public function setUser(User $user): void + { + $this->user = $user; + $this->default = Amount::getDefaultCurrencyByUserGroup($user->userGroup); + $this->convertToNative = Amount::convertToNative($user); + } + + public function groupByCurrencyId(array $journals, string $method = 'negative'): array + { + $array = []; + foreach ($journals as $journal) { + $field = 'amount'; + + // grab default currency information. + $currencyId = (int) $journal['currency_id']; + $currencyName = $journal['currency_name']; + $currencySymbol = $journal['currency_symbol']; + $currencyCode = $journal['currency_code']; + $currencyDecimalPlaces = $journal['currency_decimal_places']; + if ($this->convertToNative) { + // if convert to native, use the native amount yes or no? + $useNative = $this->default->id !== (int) $journal['currency_id']; + $useForeign = $this->default->id === (int) $journal['foreign_currency_id']; + if ($useNative) { + Log::debug(sprintf('Journal #%d switches to native amount (original is %s)', $journal['transaction_journal_id'], $journal['currency_code'])); + $field = 'native_amount'; + $currencyId = $this->default->id; + $currencyName = $this->default->name; + $currencySymbol = $this->default->symbol; + $currencyCode = $this->default->code; + $currencyDecimalPlaces = $this->default->decimal_places; + } + if ($useForeign) { + Log::debug(sprintf('Journal #%d switches to foreign amount (foreign is %s)', $journal['transaction_journal_id'], $journal['foreign_currency_code'])); + $field = 'foreign_amount'; + $currencyId = (int) $journal['foreign_currency_id']; + $currencyName = $journal['foreign_currency_name']; + $currencySymbol = $journal['foreign_currency_symbol']; + $currencyCode = $journal['foreign_currency_code']; + $currencyDecimalPlaces = $journal['foreign_currency_decimal_places']; + } + } + if(!$this->convertToNative) { + // default to the normal amount, but also + } + $amount = (string) ($journal[$field] ?? '0'); + $array[$currencyId] ??= [ + 'sum' => '0', + 'currency_id' => $currencyId, + 'currency_name' => $currencyName, + 'currency_symbol' => $currencySymbol, + 'currency_code' => $currencyCode, + 'currency_decimal_places' => $currencyDecimalPlaces, + ]; + $array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->$method($amount)); + Log::debug(sprintf('Journal #%d adds amount %s %s', $journal['transaction_journal_id'], $currencyCode, $amount)); + } + Log::debug('End of sumExpenses.', $array); + return $array; + } + + public function groupByDirection(array $journals, string $method, string $direction): array { + + $array = []; + $idKey = sprintf('%s_account_id', $direction); + $nameKey = sprintf('%s_account_name', $direction); + $convertToNative = Amount::convertToNative($this->user); + $default = Amount::getDefaultCurrencyByUserGroup($this->user->userGroup); + + + + + + Log::debug(sprintf('groupByDirection(array, %s, %s).', $direction, $method)); + foreach ($journals as $journal) { + // currency + $currencyId = $journal['currency_id']; + $currencyName = $journal['currency_name']; + $currencySymbol = $journal['currency_symbol']; + $currencyCode = $journal['currency_code']; + $currencyDecimalPlaces = $journal['currency_decimal_places']; + $field = $convertToNative && $currencyId !== $default->id ? 'native_amount' : 'amount'; + + // perhaps use default currency instead? + if ($convertToNative && $journal['currency_id'] !== $default->id) { + $currencyId = $default->id; + $currencyName = $default->name; + $currencySymbol = $default->symbol; + $currencyCode = $default->code; + $currencyDecimalPlaces = $default->decimal_places; + } + // use foreign amount when the foreign currency IS the default currency. + if ($convertToNative && $journal['currency_id'] !== $default->id && $default->id === $journal['foreign_currency_id']) { + $field = 'foreign_amount'; + } + $key = sprintf('%s-%s', $journal[$idKey], $currencyId); + // sum it all up or create a new array. + $array[$key] ??= [ + 'id' => $journal[$idKey], + 'name' => $journal[$nameKey], + 'sum' => '0', + 'currency_id' => $currencyId, + 'currency_name' => $currencyName, + 'currency_symbol' => $currencySymbol, + 'currency_code' => $currencyCode, + 'currency_decimal_places' => $currencyDecimalPlaces, + ]; + + // add the data from the $field to the array. + $array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string) ($journal[$field] ?? '0'))); // @phpstan-ignore-line + Log::debug(sprintf('Field for transaction #%d is "%s" (%s). Sum: %s', $journal['transaction_group_id'], $currencyCode, $field, $array[$key]['sum'])); + + // also do foreign amount, but only when convertToNative is false (otherwise we have it already) + // or when convertToNative is true and the foreign currency is ALSO not the default currency. + if ((!$convertToNative || $journal['foreign_currency_id'] !== $default->id) && 0 !== (int) $journal['foreign_currency_id']) { + Log::debug(sprintf('Use foreign amount from transaction #%d: %s %s. Sum: %s', $journal['transaction_group_id'], $currencyCode, $journal['foreign_amount'], $array[$key]['sum'])); + $key = sprintf('%s-%s', $journal[$idKey], $journal['foreign_currency_id']); + $array[$key] ??= [ + 'id' => $journal[$idKey], + 'name' => $journal[$nameKey], + 'sum' => '0', + 'currency_id' => $journal['foreign_currency_id'], + 'currency_name' => $journal['foreign_currency_name'], + 'currency_symbol' => $journal['foreign_currency_symbol'], + 'currency_code' => $journal['foreign_currency_code'], + 'currency_decimal_places' => $journal['foreign_currency_decimal_places'], + ]; + $array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string) $journal['foreign_amount'])); // @phpstan-ignore-line + } + } + + return $array; + } + +} diff --git a/app/Transformers/AccountTransformer.php b/app/Transformers/AccountTransformer.php index 70212444b4..a322dd9cbd 100644 --- a/app/Transformers/AccountTransformer.php +++ b/app/Transformers/AccountTransformer.php @@ -58,7 +58,7 @@ class AccountTransformer extends AbstractTransformer // get account type: $fullType = $account->accountType->type; - $accountType = (string) config(sprintf('firefly.shortNamesByFullName.%s', $fullType)); + $accountType = (string) config(sprintf( 'firefly.shortNamesByFullName.%s', $fullType)); $liabilityType = (string) config(sprintf('firefly.shortLiabilityNameByFullName.%s', $fullType)); $liabilityType = '' === $liabilityType ? null : strtolower($liabilityType); $liabilityDirection = $this->repository->getMetaValue($account, 'liability_direction');