From d3b2748c8f387caf57c0d08b9417f2b05cd4132e Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 12 Feb 2025 06:38:21 +0100 Subject: [PATCH] Ok this fix for #9797 goes out to @nebster9k who is delightfully stubborn and absolutely correct. --- .../Controllers/Chart/AccountController.php | 5 +---- app/Support/Steam.php | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index 649d973c3e..3f46253fac 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -426,7 +426,6 @@ class AccountController extends Controller $end->endOfDay(); // 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'))); - $chartData = []; $cache = new CacheProperties(); $cache->addProperty('chart.account.period'); $cache->addProperty($start); @@ -455,9 +454,7 @@ class AccountController extends Controller $range = Steam::filterAccountBalances($range, $account, $this->convertToNative, $accountCurrency); // temp, get end balance. - Log::debug('temp get end balance'); Log::debug(sprintf('period: Call finalAccountBalance with date/time "%s"', $end->toIso8601String())); - // correct Steam::finalAccountBalance($account, $end); Log::debug('END temp get end balance done'); @@ -510,7 +507,7 @@ class AccountController extends Controller $chartData = []; foreach ($return as $key => $info) { - if (3 === strlen($key)) { + if ('balance' !== $key && 'native_balance' !== $key) { // assume it's a currency: $setCurrency = $this->currencyRepository->findByCode($key); $info['currency_symbol'] = $setCurrency->symbol; diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 23820d14a7..ba1d360276 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -76,8 +76,15 @@ class Steam $balances = []; $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); $accountCurrency = $this->getAccountCurrency($account); $hasCurrency = null !== $accountCurrency; @@ -141,6 +148,8 @@ class Steam $entryCurrency = $currencies[$entry->transaction_currency_id]; 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] = bcadd($sumOfDay, $currentBalance[$entryCurrency->code]); @@ -149,9 +158,14 @@ class Steam $currentBalance['balance'] = bcadd($currentBalance['balance'], $sumOfDay); } // 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) { $nativeSumOfDay = $converter->convert($entryCurrency, $nativeCurrency, $carbon, $sumOfDay); $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeSumOfDay); + if($currency->id === $entryCurrency->id) { + $currentBalance['balance'] = bcadd($currentBalance['balance'], $sumOfDay); + } + } // just set it. $balances[$carbonKey] = $currentBalance; @@ -361,8 +375,8 @@ class Steam $defaultCurrency = app('amount')->getNativeCurrency(); if ($convertToNative) { if ($defaultCurrency->id === $currency?->id) { - Log::debug(sprintf('Unset "native_balance" and [%s] for account #%d', $defaultCurrency->code, $account->id)); - unset($set['native_balance'], $set[$defaultCurrency->code]); + Log::debug(sprintf('Unset [%s] for account #%d (no longer unset "native_balance")', $defaultCurrency->code, $account->id)); + unset($set[$defaultCurrency->code]); } // todo rethink this logic. if (null !== $currency && $defaultCurrency->id !== $currency->id) {