2024-12-29 18:32:02 +01:00
|
|
|
<?php
|
2024-12-30 04:12:18 +01:00
|
|
|
|
2024-12-29 18:32:02 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
2025-01-19 11:54:40 +01:00
|
|
|
$this->default = Amount::getNativeCurrencyByUserGroup($user->userGroup);
|
2024-12-29 18:32:02 +01:00
|
|
|
$this->convertToNative = Amount::convertToNative($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function groupByCurrencyId(array $journals, string $method = 'negative'): array
|
|
|
|
{
|
2025-02-11 16:27:18 +01:00
|
|
|
Log::debug(sprintf('Now in groupByCurrencyId(array, "%s")', $method));
|
2024-12-29 18:32:02 +01:00
|
|
|
$array = [];
|
|
|
|
foreach ($journals as $journal) {
|
2025-02-17 04:11:50 +01:00
|
|
|
$field = 'amount';
|
2024-12-29 18:32:02 +01:00
|
|
|
|
|
|
|
// grab default currency information.
|
2025-02-17 04:11:50 +01:00
|
|
|
$currencyId = (int) $journal['currency_id'];
|
|
|
|
$currencyName = $journal['currency_name'];
|
|
|
|
$currencySymbol = $journal['currency_symbol'];
|
|
|
|
$currencyCode = $journal['currency_code'];
|
|
|
|
$currencyDecimalPlaces = $journal['currency_decimal_places'];
|
2025-02-11 16:27:18 +01:00
|
|
|
|
|
|
|
// prepare foreign currency info:
|
2025-02-17 04:11:50 +01:00
|
|
|
$foreignCurrencyId = 0;
|
2025-02-15 14:10:54 +01:00
|
|
|
$foreignCurrencyName = null;
|
|
|
|
$foreignCurrencySymbol = null;
|
|
|
|
$foreignCurrencyCode = null;
|
|
|
|
$foreignCurrencyDecimalPlaces = null;
|
2025-02-11 16:27:18 +01:00
|
|
|
|
2024-12-29 18:32:02 +01:00
|
|
|
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'];
|
|
|
|
}
|
|
|
|
}
|
2024-12-30 04:12:18 +01:00
|
|
|
if (!$this->convertToNative) {
|
2025-02-11 16:27:18 +01:00
|
|
|
Log::debug(sprintf('Journal #%d also includes foreign amount (foreign is %s)', $journal['transaction_journal_id'], $journal['foreign_currency_code']));
|
|
|
|
// use foreign amount?
|
2025-02-12 10:50:48 +01:00
|
|
|
$foreignCurrencyId = (int) $journal['foreign_currency_id'];
|
|
|
|
if (0 !== $foreignCurrencyId) {
|
2025-02-11 16:27:18 +01:00
|
|
|
$foreignCurrencyName = $journal['foreign_currency_name'];
|
|
|
|
$foreignCurrencySymbol = $journal['foreign_currency_symbol'];
|
|
|
|
$foreignCurrencyCode = $journal['foreign_currency_code'];
|
|
|
|
$foreignCurrencyDecimalPlaces = $journal['foreign_currency_decimal_places'];
|
|
|
|
}
|
2024-12-29 18:32:02 +01:00
|
|
|
}
|
2025-02-11 16:27:18 +01:00
|
|
|
|
|
|
|
// first process normal amount
|
2025-02-17 04:11:50 +01:00
|
|
|
$amount = (string) ($journal[$field] ?? '0');
|
2024-12-30 04:12:18 +01:00
|
|
|
$array[$currencyId] ??= [
|
2024-12-29 18:32:02 +01:00
|
|
|
'sum' => '0',
|
|
|
|
'currency_id' => $currencyId,
|
|
|
|
'currency_name' => $currencyName,
|
|
|
|
'currency_symbol' => $currencySymbol,
|
|
|
|
'currency_code' => $currencyCode,
|
|
|
|
'currency_decimal_places' => $currencyDecimalPlaces,
|
|
|
|
];
|
2025-02-11 16:27:18 +01:00
|
|
|
|
2025-01-04 07:39:16 +01:00
|
|
|
if ('positive' === $method) {
|
2025-01-03 14:56:06 +01:00
|
|
|
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->positive($amount));
|
|
|
|
}
|
2025-01-04 07:39:16 +01:00
|
|
|
if ('negative' === $method) {
|
2025-01-03 14:56:06 +01:00
|
|
|
$array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->negative($amount));
|
|
|
|
}
|
2025-02-11 16:27:18 +01:00
|
|
|
|
|
|
|
// then process foreign amount, if it exists.
|
2025-02-12 10:50:48 +01:00
|
|
|
if (0 !== $foreignCurrencyId) {
|
|
|
|
$amount = (string) ($journal['foreign_amount'] ?? '0');
|
2025-02-11 16:27:18 +01:00
|
|
|
$array[$foreignCurrencyId] ??= [
|
|
|
|
'sum' => '0',
|
|
|
|
'currency_id' => $foreignCurrencyId,
|
|
|
|
'currency_name' => $foreignCurrencyName,
|
|
|
|
'currency_symbol' => $foreignCurrencySymbol,
|
|
|
|
'currency_code' => $foreignCurrencyCode,
|
|
|
|
'currency_decimal_places' => $foreignCurrencyDecimalPlaces,
|
|
|
|
];
|
|
|
|
|
|
|
|
if ('positive' === $method) {
|
|
|
|
$array[$foreignCurrencyId]['sum'] = bcadd($array[$foreignCurrencyId]['sum'], app('steam')->positive($amount));
|
|
|
|
}
|
|
|
|
if ('negative' === $method) {
|
|
|
|
$array[$foreignCurrencyId]['sum'] = bcadd($array[$foreignCurrencyId]['sum'], app('steam')->negative($amount));
|
|
|
|
}
|
|
|
|
}
|
2025-02-12 10:50:48 +01:00
|
|
|
|
2025-01-03 14:56:06 +01:00
|
|
|
// $array[$currencyId]['sum'] = bcadd($array[$currencyId]['sum'], app('steam')->{$method}($amount));
|
2024-12-30 15:35:26 +01:00
|
|
|
// Log::debug(sprintf('Journal #%d adds amount %s %s', $journal['transaction_journal_id'], $currencyCode, $amount));
|
2024-12-29 18:32:02 +01:00
|
|
|
}
|
|
|
|
Log::debug('End of sumExpenses.', $array);
|
2024-12-30 04:12:18 +01:00
|
|
|
|
2024-12-29 18:32:02 +01:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2024-12-30 04:12:18 +01:00
|
|
|
public function groupByDirection(array $journals, string $method, string $direction): array
|
|
|
|
{
|
2024-12-29 18:32:02 +01:00
|
|
|
|
|
|
|
$array = [];
|
|
|
|
$idKey = sprintf('%s_account_id', $direction);
|
|
|
|
$nameKey = sprintf('%s_account_name', $direction);
|
|
|
|
$convertToNative = Amount::convertToNative($this->user);
|
2025-01-19 11:54:40 +01:00
|
|
|
$default = Amount::getNativeCurrencyByUserGroup($this->user->userGroup);
|
2024-12-29 18:32:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
2024-12-30 04:12:18 +01:00
|
|
|
$key = sprintf('%s-%s', $journal[$idKey], $currencyId);
|
2024-12-29 18:32:02 +01:00
|
|
|
// 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.
|
2024-12-30 04:12:18 +01:00
|
|
|
$array[$key]['sum'] = bcadd($array[$key]['sum'], app('steam')->{$method}((string) ($journal[$field] ?? '0'))); // @phpstan-ignore-line
|
2024-12-29 18:32:02 +01:00
|
|
|
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']);
|
2024-12-30 04:12:18 +01:00
|
|
|
$array[$key] ??= [
|
2024-12-29 18:32:02 +01:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}
|