mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-26 05:26:17 +00:00
Support multi-currency accounts better, matches the old method.
This commit is contained in:
@@ -241,7 +241,8 @@ class ShowController extends Controller
|
|||||||
Log::debug(sprintf('showAll: Call accountsBalancesOptimized with date/time "%s"', $end->toIso8601String()));
|
Log::debug(sprintf('showAll: Call accountsBalancesOptimized with date/time "%s"', $end->toIso8601String()));
|
||||||
|
|
||||||
// 2025-10-08 replace finalAccountBalance with accountsBalancesOptimized.
|
// 2025-10-08 replace finalAccountBalance with accountsBalancesOptimized.
|
||||||
// $balances = Steam::filterAccountBalance(Steam::finalAccountBalance($account, $end), $account, $this->convertToPrimary, $accountCurrency);
|
// $balances = Steam::finalAccountBalance($account, $end);
|
||||||
|
// $balances = Steam::filterAccountBalance($balances, $account, $this->convertToPrimary, $accountCurrency);
|
||||||
$balances = Steam::accountsBalancesOptimized(new Collection()->push($account), $end)[$account->id];
|
$balances = Steam::accountsBalancesOptimized(new Collection()->push($account), $end)[$account->id];
|
||||||
|
|
||||||
return view(
|
return view(
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ class Steam
|
|||||||
->groupBy(['transactions.account_id', 'transaction_currencies.code'])
|
->groupBy(['transactions.account_id', 'transaction_currencies.code'])
|
||||||
->get(['transactions.account_id', 'transaction_currencies.code', DB::raw('SUM(transactions.amount) as sum_of_amount')])->toArray();
|
->get(['transactions.account_id', 'transaction_currencies.code', DB::raw('SUM(transactions.amount) as sum_of_amount')])->toArray();
|
||||||
|
|
||||||
|
Log::debug('Array of sums: ', $arrayOfSums);
|
||||||
/** @var Account $account */
|
/** @var Account $account */
|
||||||
foreach ($accounts as $account) {
|
foreach ($accounts as $account) {
|
||||||
$return = [
|
$return = [
|
||||||
@@ -73,19 +74,19 @@ class Steam
|
|||||||
$currency = $currencies[$account->id];
|
$currency = $currencies[$account->id];
|
||||||
|
|
||||||
// second array
|
// second array
|
||||||
$accountSum = array_filter($arrayOfSums, fn($entry) => $entry['account_id'] === $account->id);
|
$accountSums = array_filter($arrayOfSums, fn($entry) => $entry['account_id'] === $account->id);
|
||||||
if (0 === count($accountSum)) {
|
if (0 === count($accountSums)) {
|
||||||
$result[$account->id] = $return;
|
$result[$account->id] = $return;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$accountSum = array_values($accountSum)[0];
|
$sumsByCode = [];
|
||||||
$sumOfAmount = (string)$accountSum['sum_of_amount'];
|
foreach($accountSums as $accountSum) {
|
||||||
$sumOfAmount = $this->floatalize('' === $sumOfAmount ? '0' : $sumOfAmount);
|
// $accountSum = array_values($accountSum)[0];
|
||||||
$sumsByCode = [
|
$sumOfAmount = (string)$accountSum['sum_of_amount'];
|
||||||
$accountSum['code'] => $sumOfAmount,
|
$sumOfAmount = $this->floatalize('' === $sumOfAmount ? '0' : $sumOfAmount);
|
||||||
];
|
$sumsByCode[$accountSum['code']] = $sumOfAmount;
|
||||||
|
}
|
||||||
// Log::debug('All balances are (joined)', $others);
|
// Log::debug('All balances are (joined)', $others);
|
||||||
// if there is no request to convert, take this as "balance" and "pc_balance".
|
// if there is no request to convert, take this as "balance" and "pc_balance".
|
||||||
$return['balance'] = $sumsByCode[$currency->code] ?? '0';
|
$return['balance'] = $sumsByCode[$currency->code] ?? '0';
|
||||||
@@ -93,6 +94,7 @@ class Steam
|
|||||||
unset($return['pc_balance']);
|
unset($return['pc_balance']);
|
||||||
// Log::debug(sprintf('Set balance to %s, unset pc_balance', $return['balance']));
|
// Log::debug(sprintf('Set balance to %s, unset pc_balance', $return['balance']));
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
|
// if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
|
||||||
if ($convertToPrimary) {
|
if ($convertToPrimary) {
|
||||||
$return['pc_balance'] = $this->convertAllBalances($sumsByCode, $primary, $date);
|
$return['pc_balance'] = $this->convertAllBalances($sumsByCode, $primary, $date);
|
||||||
@@ -116,7 +118,7 @@ class Steam
|
|||||||
}
|
}
|
||||||
$final = array_merge($return, $sumsByCode);
|
$final = array_merge($return, $sumsByCode);
|
||||||
$result[$account->id] = $final;
|
$result[$account->id] = $final;
|
||||||
// Log::debug('Final balance is', $final);
|
Log::debug(sprintf('Final balance for account #%d is',$account->id), $final);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
@@ -306,7 +308,7 @@ class Steam
|
|||||||
$cache->addProperty($date);
|
$cache->addProperty($date);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
Log::debug(sprintf('CACHED finalAccountBalance(#%d, %s, inclusive:%s)', $account->id, $date->format('Y-m-d H:i:s'), var_export($inclusive, true)));
|
Log::debug(sprintf('CACHED finalAccountBalance(#%d, %s, inclusive:%s)', $account->id, $date->format('Y-m-d H:i:s'), var_export($inclusive, true)));
|
||||||
return $cache->get();
|
// return $cache->get();
|
||||||
}
|
}
|
||||||
Log::debug(sprintf('finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s')));
|
Log::debug(sprintf('finalAccountBalance(#%d, %s)', $account->id, $date->format('Y-m-d H:i:s')));
|
||||||
if (null === $convertToPrimary) {
|
if (null === $convertToPrimary) {
|
||||||
@@ -321,7 +323,6 @@ class Steam
|
|||||||
$accountCurrency = $account->meta['currency'];
|
$accountCurrency = $account->meta['currency'];
|
||||||
}
|
}
|
||||||
if (!$currencyPresent) {
|
if (!$currencyPresent) {
|
||||||
|
|
||||||
$accountCurrency = $this->getAccountCurrency($account);
|
$accountCurrency = $this->getAccountCurrency($account);
|
||||||
}
|
}
|
||||||
$hasCurrency = null !== $accountCurrency;
|
$hasCurrency = null !== $accountCurrency;
|
||||||
@@ -337,7 +338,7 @@ class Steam
|
|||||||
->where('transaction_journals.date', $inclusive ? '<=': '<', $date->format('Y-m-d H:i:s'))
|
->where('transaction_journals.date', $inclusive ? '<=': '<', $date->format('Y-m-d H:i:s'))
|
||||||
->get(['transaction_currencies.code', 'transactions.amount'])->toArray();
|
->get(['transaction_currencies.code', 'transactions.amount'])->toArray();
|
||||||
$others = $this->groupAndSumTransactions($array, 'code', 'amount');
|
$others = $this->groupAndSumTransactions($array, 'code', 'amount');
|
||||||
// Log::debug('All balances are (joined)', $others);
|
Log::debug('All balances are (joined)', $others);
|
||||||
// if there is no request to convert, take this as "balance" and "pc_balance".
|
// if there is no request to convert, take this as "balance" and "pc_balance".
|
||||||
$return['balance'] = $others[$currency->code] ?? '0';
|
$return['balance'] = $others[$currency->code] ?? '0';
|
||||||
if (!$convertToPrimary) {
|
if (!$convertToPrimary) {
|
||||||
@@ -346,7 +347,7 @@ class Steam
|
|||||||
}
|
}
|
||||||
// if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
|
// if there is a request to convert, convert to "pc_balance" and use "balance" for whichever amount is in the primary currency.
|
||||||
if ($convertToPrimary) {
|
if ($convertToPrimary) {
|
||||||
$return['pc_balance'] = $this->convertAllBalances($others, $primary, $date); // todo sum all and convert.
|
$return['pc_balance'] = $this->convertAllBalances($others, $primary, $date);
|
||||||
// Log::debug(sprintf('Set pc_balance to %s', $return['pc_balance']));
|
// Log::debug(sprintf('Set pc_balance to %s', $return['pc_balance']));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,7 +367,7 @@ class Steam
|
|||||||
// Log::debug(sprintf('Virtual balance makes the (primary currency) total %s', $return['balance']));
|
// Log::debug(sprintf('Virtual balance makes the (primary currency) total %s', $return['balance']));
|
||||||
}
|
}
|
||||||
$final = array_merge($return, $others);
|
$final = array_merge($return, $others);
|
||||||
// Log::debug('Final balance is', $final);
|
Log::debug('Final balance is', $final);
|
||||||
$cache->store($final);
|
$cache->store($final);
|
||||||
|
|
||||||
return $final;
|
return $final;
|
||||||
|
|||||||
Reference in New Issue
Block a user