Rename variable for consistency.

This commit is contained in:
James Cole 2025-02-05 06:03:57 +01:00
parent 7e4fece63d
commit 6499b5eaab
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80

View File

@ -70,24 +70,24 @@ class Steam
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
// return $cache->get();
return $cache->get();
}
$balances = [];
$formatted = $start->format('Y-m-d');
$startBalance = $this->finalAccountBalance($account, $start);
$defaultCurrency = app('amount')->getNativeCurrencyByUserGroup($account->user->userGroup);
$nativeCurrency = app('amount')->getNativeCurrencyByUserGroup($account->user->userGroup);
$accountCurrency = $this->getAccountCurrency($account);
$hasCurrency = null !== $accountCurrency;
$currency = $accountCurrency ?? $defaultCurrency;
$currency = $accountCurrency ?? $nativeCurrency;
Log::debug(sprintf('Currency is %s', $currency->code));
if (!$hasCurrency) {
Log::debug(sprintf('Also set start balance in %s', $defaultCurrency->code));
$startBalance[$defaultCurrency->code] ??= '0';
Log::debug(sprintf('Also set start balance in %s', $nativeCurrency->code));
$startBalance[$nativeCurrency->code] ??= '0';
}
$currencies = [
$currency->id => $currency,
$defaultCurrency->id => $defaultCurrency,
$nativeCurrency->id => $nativeCurrency,
];
@ -111,8 +111,7 @@ class Steam
'transactions.transaction_currency_id',
DB::raw('SUM(transactions.amount) AS sum_of_day'),
]
)
;
);
$currentBalance = $startBalance;
$converter = new ExchangeRateConverter();
@ -141,10 +140,10 @@ class Steam
}
// if convert to native add the converted amount to native balance.
if ($convertToNative) {
$nativeSumOfDay = $converter->convert($entryCurrency, $defaultCurrency, $carbon, $sumOfDay);
$nativeSumOfDay = $converter->convert($entryCurrency, $nativeCurrency, $carbon, $sumOfDay);
$currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeSumOfDay);
// only add to "balance" if it has the correct currency code (the same as "native")
if ($defaultCurrency->code === $entryCurrency->code) {
if ($nativeCurrency->code === $entryCurrency->code) {
$currentBalance['balance'] = bcadd($currentBalance['balance'], $sumOfDay);
}
}
@ -294,24 +293,14 @@ class Steam
/**
* Returns the balance of an account at exact moment given. Array with at least one value.
* Always returns:
* "native_balance": balance in the user's native balance.
* "EUR": balance in EUR (or whatever currencies the account has balance in)
*
* "balance" the balance in whatever currency the account has, so the sum of all transaction that happen to have
* THAT currency.
* "native_balance" the balance according to the "native_amount" + "native_foreign_amount" fields.
* "ABC" the balance in this particular currency code (may repeat for each found currency).
* If the user has $convertToNative:
* "native_balance": balance in the user's native balance, with all amounts converted to native.
* "EUR": balance in EUR (or whatever currencies the account has balance in)
*
* Het maakt niet uit of de native currency wel of niet gelijk is aan de account currency.
* Optelsom zou hetzelfde moeten zijn. Als het EUR is en de rekening ook is native_amount 0.
* Zo niet is amount 0 en native_amount het bedrag.
*
* Eerst een som van alle transacties in de native currency. Alle EUR bij elkaar opgeteld.
* Om te weten wat er nog meer op de rekening gebeurt, pak alles waar currency niet EUR is, en de foreign ook niet,
* en tel native_amount erbij op.
* Daarna pak je alle transacties waar currency niet EUR is, en de foreign wel, en tel foreign_amount erbij op.
*
* Wil je niks weten van native currencies, pak je:
*
* Eerst een som van alle transacties gegroepeerd op currency. Einde.
*/
public function finalAccountBalance(Account $account, Carbon $date): array
{
@ -330,16 +319,14 @@ class Steam
$hasCurrency = null !== $accountCurrency;
$currency = $hasCurrency ? $accountCurrency : $native;
$return = [
// 'balance' => '0',
'native_balance' => '0',
];
// balance(s) in other (all) currencies.
// balance(s) in all currencies.
$array = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transactions.transaction_currency_id')
->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');
Log::debug('All balances are (joined)', $others);
// if there is no request to convert, take this as "balance" and "native_balance".
@ -350,15 +337,12 @@ class Steam
}
// 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']));
Log::debug(sprintf('Set native_balance to %s', $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:
@ -372,28 +356,10 @@ class Steam
$return['native_balance'] = bcadd($return['native_balance'], $virtualBalance);
Log::debug(sprintf('Virtual balance makes the (native) total %s', $return['native_balance']));
}
// if the currency is the same as the native currency, set the native_balance to the balance for consistency.
// if($currency->id === $native->id) {
// $return['native_balance'] = $return['balance'];
// }
// if (!$hasCurrency && array_key_exists('balance', $return)) {
// // Log::debug('Account has no currency preference, dropping balance in favor of native balance.');
// $sum = bcadd($return['balance'], $return['native_balance']);
// // Log::debug(sprintf('%s + %s = %s', $return['balance'], $return['native_balance'], $sum));
// $return['native_balance'] = $sum;
// unset($return['balance']);
// }
$final = array_merge($return, $others);
// // Log::debug('Return is', $final);
$cache->store($final);
return $final;
// return array_merge($return, $others);
// Log::debug('Return is', $final);
}
public function filterAccountBalances(array $total, Account $account, bool $convertToNative, ?TransactionCurrency $currency = null): array