Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Merge branch 'v6.2' of github.com:firefly-iii/firefly-iii into v6.2
James Cole33531244aa80
diff --cc app/Http/Controllers/Chart/AccountController.phpindex dc835a5bb8,ddb5bd7295..d425816335--- a/app/Http/Controllers/Chart/AccountController.php+++ b/app/Http/Controllers/Chart/AccountController.php@@@ -430,69 -430,43 +430,73 @@@ class AccountController extends Control}// collect and filter balances for the entire period.- $step = $this->calculateStep($start, $end);+ $step = $this->calculateStep($start, $end);Log::debug(sprintf('Step is %s', $step));- $locale = app('steam')->getLocale();- $return = [];+ $locale = app('steam')->getLocale();+ $return = [];++// fix for issue https://github.com/firefly-iii/firefly-iii/issues/8041// have to make sure this chart is always based on the balance at the END of the period.// This period depends on the size of the chart- $current = clone $start;- $current = app('navigation')->endOfX($current, $step, null);- $format = (string) trans('config.month_and_day_js', [], $locale);- $accountCurrency = $this->accountRepository->getAccountCurrency($account);+ $current = clone $start;+ $current = app('navigation')->endOfX($current, $step, null);+ $format = (string) trans('config.month_and_day_js', [], $locale);+ $accountCurrency = $this->accountRepository->getAccountCurrency($account);Log::debug('One');- $range = Steam::finalAccountBalanceInRange($account, $start, $end, $this->convertToNative);+ $range = Steam::finalAccountBalanceInRange($account, $start, $end, $this->convertToNative);Log::debug('Two');- $range = Steam::filterAccountBalances($range, $account, $this->convertToNative, $accountCurrency);+ $range = Steam::filterAccountBalances($range, $account, $this->convertToNative, $accountCurrency);Log::debug('Three');- $previous = array_values($range)[0];- $accountCurrency ??= $this->defaultCurrency; // do this AFTER getting the balances.- while ($end >= $current) {- $theDate = $current->format('Y-m-d');- // each day contains multiple balances, and this may even be different over time.- $momentBalance = $range[$theDate] ?? $previous;- $return = $this->updateChartKeys($return, $momentBalance);++ // temp, get end balance.+ Log::debug('temp get end balance');+ Steam::finalAccountBalance($account, $end);+ Log::debug('END temp get end balance done');++ $previous = array_values($range)[0];+ $accountCurrency = $accountCurrency ?? $this->defaultCurrency; // do this AFTER getting the balances.+ Log::debug('Start chart loop.');++ $newRange = [];+ $expectedIndex = 0;+ Log::debug('Balances exist at:');+ foreach ($range as $key => $value) {+ $newRange[] = ['date' => $key, 'info' => $value];+ Log::debug(sprintf(' - %s', $key));+ }+ $carbon = Carbon::createFromFormat('Y-m-d', $newRange[0]['date']);+ while ($end->gte($current)) {+ $momentBalance = $previous;+ $theDate = $current->format('Y-m-d');+ while ($carbon->lte($current) && array_key_exists($expectedIndex, $newRange)) {+ $momentBalance = $newRange[$expectedIndex]['info'];+ Log::debug(sprintf('Expected index is %d!, date is %s, current is %s', $expectedIndex, $carbon->format('Y-m-d'), $current->format('Y-m-d')));+ $carbon = Carbon::createFromFormat('Y-m-d', $newRange[$expectedIndex]['date']);+ $expectedIndex++;+ }++ $return = $this->updateChartKeys($return, $momentBalance);+ $previous = $momentBalance;++ Log::debug(sprintf('Now at %s', $theDate), $momentBalance);++// process each balance thing.foreach ($momentBalance as $key => $amount) {$label = $current->isoFormat($format);$return[$key]['entries'][$label] = $amount;}- $current = app('navigation')->addPeriod($current, $step, 0);+ $current = app('navigation')->addPeriod($current, $step, 0);// here too, to fix #8041, the data is corrected to the end of the period.- $current = app('navigation')->endOfX($current, $step, null);- $previous = $momentBalance;+ $current = app('navigation')->endOfX($current, $step, null);+ Log::debug(sprintf('Next moment is %s', $current->format('Y-m-d')));++}+ Log::debug('End of chart loop.');// second loop (yes) to create nice array with info! Yay!- $chartData = [];+ $chartData = [];++foreach ($return as $key => $info) {if (3 === strlen($key)) {// assume it's a currency:diff --cc app/Support/Steam.phpindex 528cf29e5d,721c6af480..c075cf3ade--- a/app/Support/Steam.php+++ b/app/Support/Steam.php@@@ -111,27 -110,28 +111,27 @@@ class Stea// sums up the balance changes per day, for foreign, native and normal amounts.- $set = $account->transactions()- ->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')- ->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s'))- ->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s'))- ->groupBy('transaction_journals.date')- ->groupBy('transactions.transaction_currency_id')- ->groupBy('transactions.foreign_currency_id')- ->orderBy('transaction_journals.date', 'ASC')- ->whereNull('transaction_journals.deleted_at')- ->get(- [ // @phpstan-ignore-line- 'transaction_journals.date',- 'transactions.transaction_currency_id',- \DB::raw('SUM(transactions.amount) AS modified'),- 'transactions.foreign_currency_id',- \DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'),- \DB::raw('SUM(transactions.native_amount) AS modified_native'),- ]- )- ;+ $set = $account->transactions()+ ->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')+ ->where('transaction_journals.date', '>=', $start->format('Y-m-d H:i:s'))+ ->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s'))+ ->groupBy('transaction_journals.date')+ ->groupBy('transactions.transaction_currency_id')+ ->groupBy('transactions.foreign_currency_id')+ ->orderBy('transaction_journals.date', 'ASC')+ ->whereNull('transaction_journals.deleted_at')+ ->get(+ [ // @phpstan-ignore-line+ 'transaction_journals.date',+ 'transactions.transaction_currency_id',+ DB::raw('SUM(transactions.amount) AS modified'),+ 'transactions.foreign_currency_id',+ DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'),+ DB::raw('SUM(transactions.native_amount) AS modified_native'),+ ]+ );- $currentBalance = $startBalance;+ $currentBalance = $startBalance;/** @var Transaction $entry */foreach ($set as $entry) {@@@ -148,42 -148,49 +148,42 @@@Log::debug(sprintf('Processing transaction(s) on date %s', $carbon->format('Y-m-d H:i:s')));// if convert to native, if NOT convert to native.- if ($convertToNative) {- Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $modified, $foreignModified, $nativeModified));+ if($convertToNative) {+ Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $this->bcround($modified,2), $this->bcround($foreignModified,2), $this->bcround($nativeModified,2)));+ // if the currency is the default currency add to native balance + currency balance+ if($entry->transaction_currency_id === $defaultCurrency->id) {+ Log::debug('Add amount to native.');+ $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $modified);+ }+// add to native balance.- $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);- if ($entry->foreign_currency_id === $defaultCurrency->id) {- $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $foreignModified);+ if($entry->foreign_currency_id !== $defaultCurrency->id) {+ // this check is not necessary, because if the foreign currency is the same as the default currency, the native amount is zero.+ // so adding this would mean nothing.+ $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);+ }+ if($entry->foreign_currency_id === $defaultCurrency->id) {+ $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $foreignModified);}// add to balance if is the same.- if($entry->transaction_currency_id === $accountCurrency?->id) {- $currentBalance['balance'] = bcadd($currentBalance['balance'], $modified);+ if ($entry->transaction_currency_id === $accountCurrency?->id) {+ $currentBalance['balance'] = bcadd($currentBalance['balance'], $modified);}// add currency balance- $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code], $modified);+ $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified);}- if(!$convertToNative) {+ if (!$convertToNative) {Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $modified, $foreignModified, $nativeModified));// add to balance, as expected.- $currentBalance['balance'] = bcadd($currentBalance['balance'] ?? '0', $modified);+ $currentBalance['balance'] = bcadd($currentBalance['balance'] ?? '0', $modified);// add to GBP, as expected.- $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code], $modified);+ $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified);}-- // // add "modified" to amount if the currency id matches the account currency id.- // if ($entry->transaction_currency_id === $currency->id) {- // $currentBalance['balance'] = bcadd($currentBalance['balance'], $modified);- // $currentBalance[$currency->code] = bcadd($currentBalance[$currency->code], $modified);- // }- //- // // always add the native balance, even if it ends up at zero.- // $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);-- // DO NOT add modified foreign to the array- // if (null !== $entry->foreign_currency_id) {- // $foreignId = $entry->foreign_currency_id;- // $currencies[$foreignId] ??= TransactionCurrency::find($foreignId);- // $foreignCurrency = $currencies[$foreignId];- // $currentBalance[$foreignCurrency->code] ??= '0';- // $currentBalance[$foreignCurrency->code] = bcadd($currentBalance[$foreignCurrency->code], $foreignModified);- // }$balances[$carbon->format('Y-m-d')] = $currentBalance;- Log::debug('Updated entry',$currentBalance);+ Log::debug('Updated entry', $currentBalance);}$cache->store($balances);+ Log::debug('End of method');return $balances;}@@@ -332,24 -340,26 +333,26 @@@// native balance$return['native_balance'] = (string) $account->transactions()- ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')- ->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))- ->whereNot('transactions.transaction_currency_id', $native->id)- ->sum('transactions.native_amount');+ ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')+ ->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))+ ->whereNot('transactions.transaction_currency_id', $native->id)+ ->sum('transactions.native_amount')+ ;// plus native virtual balance.$return['native_balance'] = bcadd('' === (string) $account->native_virtual_balance ? '0' : $account->native_virtual_balance, $return['native_balance']);- Log::debug(sprintf('native_balance is (all transactions to %s) %s (with virtual balance)', $native->code, $return['native_balance']));+ Log::debug(sprintf('native_balance is (all transactions to %s) %s (with virtual balance)', $native->code,$this->bcround( $return['native_balance'])));// plus foreign transactions in THIS currency.$sum = (string) $account->transactions()- ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')- ->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))- ->whereNot('transactions.transaction_currency_id', $native->id)- ->where('transactions.foreign_currency_id', $native->id)- ->sum('transactions.foreign_amount');+ ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')+ ->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))+ ->whereNot('transactions.transaction_currency_id', $native->id)+ ->where('transactions.foreign_currency_id', $native->id)+ ->sum('transactions.foreign_amount')+ ;$return['native_balance'] = bcadd($return['native_balance'], $sum);- Log::debug(sprintf('Foreign amount transactions add (%s only) %s, total native_balance is now %s', $native->code, $sum, $return['native_balance']));+ Log::debug(sprintf('Foreign amount transactions add (%s only) %s, total native_balance is now %s', $native->code, $this->bcround($sum), $this->bcround($return['native_balance'])));}// balance(s) in other (all) currencies.