mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Clean up several endpoints with complex code.
This commit is contained in:
parent
05f1819f7d
commit
9f25880a59
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
178
app/Support/Report/Summarizer/TransactionSummarizer.php
Normal file
178
app/Support/Report/Summarizer/TransactionSummarizer.php
Normal file
@ -0,0 +1,178 @@
|
||||
<?php
|
||||
/*
|
||||
* TransactionSummarizer.php
|
||||
* Copyright (c) 2024 james@firefly-iii.org.
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Report\Summarizer;
|
||||
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Support\Facades\Amount;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class TransactionSummarizer
|
||||
{
|
||||
private User $user;
|
||||
private TransactionCurrency $default;
|
||||
private bool $convertToNative = false;
|
||||
|
||||
public function __construct(?User $user = null)
|
||||
{
|
||||
if (null !== $user) {
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user