This commit is contained in:
James Cole
2025-02-04 19:43:53 +01:00
parent 8f6eefb5e7
commit 6ada5fa560

View File

@@ -28,10 +28,11 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use FireflyIII\Support\Facades\Amount;
/** /**
* Class Steam. * Class Steam.
@@ -114,8 +115,7 @@ class Steam
DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'), DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'),
DB::raw('SUM(transactions.native_amount) AS modified_native'), DB::raw('SUM(transactions.native_amount) AS modified_native'),
] ]
) );
;
$currentBalance = $startBalance; $currentBalance = $startBalance;
@@ -300,7 +300,7 @@ class Steam
$cache->addProperty($account->id); $cache->addProperty($account->id);
$cache->addProperty($date); $cache->addProperty($date);
if ($cache->has()) { if ($cache->has()) {
return $cache->get(); //return $cache->get();
} }
Log::debug(sprintf('Now in finalAccountBalance(#%d, "%s", "%s")', $account->id, $account->name, $date->format('Y-m-d H:i:s'))); Log::debug(sprintf('Now in finalAccountBalance(#%d, "%s", "%s")', $account->id, $account->name, $date->format('Y-m-d H:i:s')));
@@ -310,61 +310,47 @@ class Steam
$accountCurrency = $this->getAccountCurrency($account); $accountCurrency = $this->getAccountCurrency($account);
$hasCurrency = null !== $accountCurrency; $hasCurrency = null !== $accountCurrency;
$currency = $hasCurrency ? $accountCurrency : $native; $currency = $hasCurrency ? $accountCurrency : $native;
$return = []; $return = [
'balance' => '0',
// first, the "balance", as described earlier. 'native_balance' => '0',
if ($convertToNative) { ];
// normal balance
$return['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'))
->where('transactions.transaction_currency_id', $native->id)
->sum('transactions.amount')
;
// plus virtual balance, if the account has a virtual_balance in the native currency
if ($native->id === $accountCurrency?->id) {
$return['balance'] = bcadd('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance, $return['balance']);
}
// Log::debug(sprintf('balance is (%s only) %s (with virtual balance)', $native->code, $this->bcround($return['balance'], 2)));
// 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')
;
// 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, $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')
;
$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, $this->bcround($sum), $this->bcround($return['native_balance'])));
}
// balance(s) in other (all) currencies. // balance(s) in other (all) currencies.
$array = $account->transactions() $array = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id') ->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s')) ->where('transaction_journals.date', '<=', $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 the account has no own currency preference, drop balance in favor of native balance // if there is no request to convert, take this as "balance" and "native_balance".
if ($hasCurrency && !$convertToNative) { if (!$convertToNative) {
$return['balance'] = $others[$currency->code] ?? '0'; $return['balance'] = $others[$currency->code] ?? '0';
$return['native_balance'] = $others[$currency->code] ?? '0'; $return['native_balance'] = $others[$currency->code] ?? '0';
// Log::debug(sprintf('Set balance + native_balance to %s', $return['balance'])); Log::debug(sprintf('Set balance + native_balance to %s', $return['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) {
$return['balance'] = $others[$native->code] ?? '0';
$return['native_balance'] = $this->convertAllBalances($others, $native, $date);// todo sum all and convert.
Log::debug(sprintf('Set balance to %s and native_balance to %s', $return['balance'], $return['native_balance']));
}
// either way, the balance is always combined with the virtual balance:
$virtualBalance = (string) ('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance);
$return['balance'] = bcadd($return['balance'], $virtualBalance);
Log::debug(sprintf('Virtual balance makes the total %s', $return['balance']));
if ($convertToNative) {
// the native balance is combined with a converted virtual_balance:
$converter = new ExchangeRateConverter();
$nativeVirtualBalance = $converter->convert($currency, $native, $date, $virtualBalance);
$return['native_balance'] = bcadd($nativeVirtualBalance, $return['native_balance']);
Log::debug(sprintf('Native virtual balance makes the native total %s', $return['native_balance']));
}
if (!$convertToNative) {
// if not, also increase the native balance for consistency.
$return['native_balance'] = bcadd($return['native_balance'], $virtualBalance);
Log::debug(sprintf('Virtual balance makes the (native) total %s', $return['balance']));
} }
// if the currency is the same as the native currency, set the native_balance to the balance for consistency. // if the currency is the same as the native currency, set the native_balance to the balance for consistency.
@@ -372,18 +358,20 @@ class Steam
// $return['native_balance'] = $return['balance']; // $return['native_balance'] = $return['balance'];
// } // }
if (!$hasCurrency && array_key_exists('balance', $return)) { // if (!$hasCurrency && array_key_exists('balance', $return)) {
// Log::debug('Account has no currency preference, dropping balance in favor of native balance.'); // // Log::debug('Account has no currency preference, dropping balance in favor of native balance.');
$sum = bcadd($return['balance'], $return['native_balance']); // $sum = bcadd($return['balance'], $return['native_balance']);
// Log::debug(sprintf('%s + %s = %s', $return['balance'], $return['native_balance'], $sum)); // // Log::debug(sprintf('%s + %s = %s', $return['balance'], $return['native_balance'], $sum));
$return['native_balance'] = $sum; // $return['native_balance'] = $sum;
unset($return['balance']); // unset($return['balance']);
} // }
$final = array_merge($return, $others); $final = array_merge($return, $others);
// Log::debug('Return is', $final); // // Log::debug('Return is', $final);
$cache->store($final);
return array_merge($return, $others); $cache->store($final);
return $final;
//return array_merge($return, $others);
// Log::debug('Return is', $final); // Log::debug('Return is', $final);
} }
@@ -687,4 +675,19 @@ class Steam
return $amount; return $amount;
} }
private function convertAllBalances(array $others, TransactionCurrency $native, Carbon $date): string {
$total = '0';
$converter = new ExchangeRateConverter();
foreach($others as $key => $amount) {
$currency = TransactionCurrency::where('code', $key)->first();
if(null === $currency) {
continue;
}
$current = $converter->convert($currency, $native, $date, $amount);
Log::debug(sprintf('Convert %s %s to %s %s', $currency->code, $amount, $native->code, $current));
$total = bcadd($current, $total);
}
return $total;
}
} }