Ok this fix for #9797 goes out to @nebster9k who is delightfully stubborn and absolutely correct.

This commit is contained in:
James Cole
2025-02-12 06:38:21 +01:00
parent 1b4655fd70
commit d3b2748c8f
2 changed files with 19 additions and 8 deletions

View File

@@ -426,7 +426,6 @@ class AccountController extends Controller
$end->endOfDay(); $end->endOfDay();
// TODO not sure if these date ranges will work as expected. // TODO not sure if these date ranges will work as expected.
Log::debug(sprintf('Now in period("%s", "%s")', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s'))); Log::debug(sprintf('Now in period("%s", "%s")', $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
$chartData = [];
$cache = new CacheProperties(); $cache = new CacheProperties();
$cache->addProperty('chart.account.period'); $cache->addProperty('chart.account.period');
$cache->addProperty($start); $cache->addProperty($start);
@@ -455,9 +454,7 @@ class AccountController extends Controller
$range = Steam::filterAccountBalances($range, $account, $this->convertToNative, $accountCurrency); $range = Steam::filterAccountBalances($range, $account, $this->convertToNative, $accountCurrency);
// temp, get end balance. // temp, get end balance.
Log::debug('temp get end balance');
Log::debug(sprintf('period: Call finalAccountBalance with date/time "%s"', $end->toIso8601String())); Log::debug(sprintf('period: Call finalAccountBalance with date/time "%s"', $end->toIso8601String()));
// correct
Steam::finalAccountBalance($account, $end); Steam::finalAccountBalance($account, $end);
Log::debug('END temp get end balance done'); Log::debug('END temp get end balance done');
@@ -510,7 +507,7 @@ class AccountController extends Controller
$chartData = []; $chartData = [];
foreach ($return as $key => $info) { foreach ($return as $key => $info) {
if (3 === strlen($key)) { if ('balance' !== $key && 'native_balance' !== $key) {
// assume it's a currency: // assume it's a currency:
$setCurrency = $this->currencyRepository->findByCode($key); $setCurrency = $this->currencyRepository->findByCode($key);
$info['currency_symbol'] = $setCurrency->symbol; $info['currency_symbol'] = $setCurrency->symbol;

View File

@@ -76,8 +76,15 @@ class Steam
$balances = []; $balances = [];
$formatted = $start->format('Y-m-d'); $formatted = $start->format('Y-m-d');
Log::debug(sprintf('finalAccountBalanceInRange: Call finalAccountBalance with date/time "%s"', $start->toIso8601String())); /*
$startBalance = $this->finalAccountBalance($account, $start); * To make sure the start balance is correct, we need to get the balance at the exact end of the previous day.
* Since we just did "startOfDay" we can do subDay()->endOfDay() to get the correct moment.
* THAT will be the start balance.
*/
$request = clone $start;
$request->subDay()->endOfDay();
Log::debug(sprintf('finalAccountBalanceInRange: Call finalAccountBalance with date/time "%s"', $request->toIso8601String()));
$startBalance = $this->finalAccountBalance($account, $request);
$nativeCurrency = app('amount')->getNativeCurrencyByUserGroup($account->user->userGroup); $nativeCurrency = app('amount')->getNativeCurrencyByUserGroup($account->user->userGroup);
$accountCurrency = $this->getAccountCurrency($account); $accountCurrency = $this->getAccountCurrency($account);
$hasCurrency = null !== $accountCurrency; $hasCurrency = null !== $accountCurrency;
@@ -141,6 +148,8 @@ class Steam
$entryCurrency = $currencies[$entry->transaction_currency_id]; $entryCurrency = $currencies[$entry->transaction_currency_id];
Log::debug(sprintf('Processing transaction(s) on moment %s', $carbon->format('Y-m-d H:i:s'))); Log::debug(sprintf('Processing transaction(s) on moment %s', $carbon->format('Y-m-d H:i:s')));
// add amount to current balance in currency code.
$currentBalance[$entryCurrency->code] ??= '0'; $currentBalance[$entryCurrency->code] ??= '0';
$currentBalance[$entryCurrency->code] = bcadd($sumOfDay, $currentBalance[$entryCurrency->code]); $currentBalance[$entryCurrency->code] = bcadd($sumOfDay, $currentBalance[$entryCurrency->code]);
@@ -149,9 +158,14 @@ class Steam
$currentBalance['balance'] = bcadd($currentBalance['balance'], $sumOfDay); $currentBalance['balance'] = bcadd($currentBalance['balance'], $sumOfDay);
} }
// if convert to native add the converted amount to "native_balance". // if convert to native add the converted amount to "native_balance".
// if there is a request to convert, convert to "native_balance" and use "balance" for whichever amount is in the native currency.
if ($convertToNative) { if ($convertToNative) {
$nativeSumOfDay = $converter->convert($entryCurrency, $nativeCurrency, $carbon, $sumOfDay); $nativeSumOfDay = $converter->convert($entryCurrency, $nativeCurrency, $carbon, $sumOfDay);
$currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeSumOfDay); $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeSumOfDay);
if($currency->id === $entryCurrency->id) {
$currentBalance['balance'] = bcadd($currentBalance['balance'], $sumOfDay);
}
} }
// just set it. // just set it.
$balances[$carbonKey] = $currentBalance; $balances[$carbonKey] = $currentBalance;
@@ -361,8 +375,8 @@ class Steam
$defaultCurrency = app('amount')->getNativeCurrency(); $defaultCurrency = app('amount')->getNativeCurrency();
if ($convertToNative) { if ($convertToNative) {
if ($defaultCurrency->id === $currency?->id) { if ($defaultCurrency->id === $currency?->id) {
Log::debug(sprintf('Unset "native_balance" and [%s] for account #%d', $defaultCurrency->code, $account->id)); Log::debug(sprintf('Unset [%s] for account #%d (no longer unset "native_balance")', $defaultCurrency->code, $account->id));
unset($set['native_balance'], $set[$defaultCurrency->code]); unset($set[$defaultCurrency->code]);
} }
// todo rethink this logic. // todo rethink this logic.
if (null !== $currency && $defaultCurrency->id !== $currency->id) { if (null !== $currency && $defaultCurrency->id !== $currency->id) {