mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Rename variable for consistency.
This commit is contained in:
parent
7e4fece63d
commit
6499b5eaab
@ -41,8 +41,8 @@ class Steam
|
|||||||
{
|
{
|
||||||
public function getAccountCurrency(Account $account): ?TransactionCurrency
|
public function getAccountCurrency(Account $account): ?TransactionCurrency
|
||||||
{
|
{
|
||||||
$type = $account->accountType->type;
|
$type = $account->accountType->type;
|
||||||
$list = config('firefly.valid_currency_account_types');
|
$list = config('firefly.valid_currency_account_types');
|
||||||
|
|
||||||
// return null if not in this list.
|
// return null if not in this list.
|
||||||
if (!in_array($type, $list, true)) {
|
if (!in_array($type, $list, true)) {
|
||||||
@ -64,74 +64,73 @@ class Steam
|
|||||||
Log::debug(sprintf('finalAccountBalanceInRange(#%d, %s, %s)', $account->id, $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
|
Log::debug(sprintf('finalAccountBalanceInRange(#%d, %s, %s)', $account->id, $start->format('Y-m-d H:i:s'), $end->format('Y-m-d H:i:s')));
|
||||||
|
|
||||||
// set up cache
|
// set up cache
|
||||||
$cache = new CacheProperties();
|
$cache = new CacheProperties();
|
||||||
$cache->addProperty($account->id);
|
$cache->addProperty($account->id);
|
||||||
$cache->addProperty('final-balance-in-range');
|
$cache->addProperty('final-balance-in-range');
|
||||||
$cache->addProperty($start);
|
$cache->addProperty($start);
|
||||||
$cache->addProperty($end);
|
$cache->addProperty($end);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
// return $cache->get();
|
return $cache->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
$balances = [];
|
$balances = [];
|
||||||
$formatted = $start->format('Y-m-d');
|
$formatted = $start->format('Y-m-d');
|
||||||
$startBalance = $this->finalAccountBalance($account, $start);
|
$startBalance = $this->finalAccountBalance($account, $start);
|
||||||
$defaultCurrency = 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;
|
||||||
$currency = $accountCurrency ?? $defaultCurrency;
|
$currency = $accountCurrency ?? $nativeCurrency;
|
||||||
Log::debug(sprintf('Currency is %s', $currency->code));
|
Log::debug(sprintf('Currency is %s', $currency->code));
|
||||||
if (!$hasCurrency) {
|
if (!$hasCurrency) {
|
||||||
Log::debug(sprintf('Also set start balance in %s', $defaultCurrency->code));
|
Log::debug(sprintf('Also set start balance in %s', $nativeCurrency->code));
|
||||||
$startBalance[$defaultCurrency->code] ??= '0';
|
$startBalance[$nativeCurrency->code] ??= '0';
|
||||||
}
|
}
|
||||||
$currencies = [
|
$currencies = [
|
||||||
$currency->id => $currency,
|
$currency->id => $currency,
|
||||||
$defaultCurrency->id => $defaultCurrency,
|
$nativeCurrency->id => $nativeCurrency,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
$startBalance[$currency->code] ??= '0';
|
$startBalance[$currency->code] ??= '0';
|
||||||
$balances[$formatted] = $startBalance;
|
$balances[$formatted] = $startBalance;
|
||||||
Log::debug('Final start balance: ', $startBalance);
|
Log::debug('Final start balance: ', $startBalance);
|
||||||
|
|
||||||
|
|
||||||
// sums up the balance changes per day, for foreign, native and normal amounts.
|
// sums up the balance changes per day, for foreign, native and normal amounts.
|
||||||
$set = $account->transactions()
|
$set = $account->transactions()
|
||||||
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
->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', '>=', $start->format('Y-m-d H:i:s'))
|
||||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s'))
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d H:i:s'))
|
||||||
->groupBy('transaction_journals.date')
|
->groupBy('transaction_journals.date')
|
||||||
->groupBy('transactions.transaction_currency_id')
|
->groupBy('transactions.transaction_currency_id')
|
||||||
->orderBy('transaction_journals.date', 'ASC')
|
->orderBy('transaction_journals.date', 'ASC')
|
||||||
->whereNull('transaction_journals.deleted_at')
|
->whereNull('transaction_journals.deleted_at')
|
||||||
->get(
|
->get(
|
||||||
[ // @phpstan-ignore-line
|
[ // @phpstan-ignore-line
|
||||||
'transaction_journals.date',
|
'transaction_journals.date',
|
||||||
'transactions.transaction_currency_id',
|
'transactions.transaction_currency_id',
|
||||||
DB::raw('SUM(transactions.amount) AS sum_of_day'),
|
DB::raw('SUM(transactions.amount) AS sum_of_day'),
|
||||||
]
|
]
|
||||||
)
|
);
|
||||||
;
|
|
||||||
|
|
||||||
$currentBalance = $startBalance;
|
$currentBalance = $startBalance;
|
||||||
$converter = new ExchangeRateConverter();
|
$converter = new ExchangeRateConverter();
|
||||||
|
|
||||||
|
|
||||||
/** @var Transaction $entry */
|
/** @var Transaction $entry */
|
||||||
foreach ($set as $entry) {
|
foreach ($set as $entry) {
|
||||||
// normal, native and foreign amount
|
// normal, native and foreign amount
|
||||||
$carbon = new Carbon($entry->date, $entry->date_tz);
|
$carbon = new Carbon($entry->date, $entry->date_tz);
|
||||||
$sumOfDay = (string) (null === $entry->sum_of_day ? '0' : $entry->sum_of_day);
|
$sumOfDay = (string) (null === $entry->sum_of_day ? '0' : $entry->sum_of_day);
|
||||||
|
|
||||||
// find currency of this entry.
|
// find currency of this entry.
|
||||||
$currencies[$entry->transaction_currency_id] ??= TransactionCurrency::find($entry->transaction_currency_id);
|
$currencies[$entry->transaction_currency_id] ??= TransactionCurrency::find($entry->transaction_currency_id);
|
||||||
|
|
||||||
/** @var TransactionCurrency $entryCurrency */
|
/** @var TransactionCurrency $entryCurrency */
|
||||||
$entryCurrency = $currencies[$entry->transaction_currency_id];
|
$entryCurrency = $currencies[$entry->transaction_currency_id];
|
||||||
|
|
||||||
Log::debug(sprintf('Processing transaction(s) on date %s', $carbon->format('Y-m-d H:i:s')));
|
Log::debug(sprintf('Processing transaction(s) on date %s', $carbon->format('Y-m-d H:i:s')));
|
||||||
$currentBalance[$entryCurrency->code] ??= '0';
|
$currentBalance[$entryCurrency->code] ??= '0';
|
||||||
$currentBalance[$entryCurrency->code] = bcadd($sumOfDay, $currentBalance[$entryCurrency->code]);
|
$currentBalance[$entryCurrency->code] = bcadd($sumOfDay, $currentBalance[$entryCurrency->code]);
|
||||||
|
|
||||||
// if not convert to native, add the amount to "balance" and "native_balance" alike:
|
// if not convert to native, add the amount to "balance" and "native_balance" alike:
|
||||||
@ -141,10 +140,10 @@ class Steam
|
|||||||
}
|
}
|
||||||
// if convert to native add the converted amount to native balance.
|
// if convert to native add the converted amount to native balance.
|
||||||
if ($convertToNative) {
|
if ($convertToNative) {
|
||||||
$nativeSumOfDay = $converter->convert($entryCurrency, $defaultCurrency, $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);
|
||||||
// only add to "balance" if it has the correct currency code (the same as "native")
|
// 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);
|
$currentBalance['balance'] = bcadd($currentBalance['balance'], $sumOfDay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -186,7 +185,7 @@ class Steam
|
|||||||
// $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified);
|
// $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
$balances[$carbon->format('Y-m-d')] = $currentBalance;
|
$balances[$carbon->format('Y-m-d')] = $currentBalance;
|
||||||
Log::debug('Updated entry', $currentBalance);
|
Log::debug('Updated entry', $currentBalance);
|
||||||
}
|
}
|
||||||
$cache->store($balances);
|
$cache->store($balances);
|
||||||
@ -224,10 +223,10 @@ class Steam
|
|||||||
// Log::debug(sprintf('Trying bcround("%s",%d)', $number, $precision));
|
// Log::debug(sprintf('Trying bcround("%s",%d)', $number, $precision));
|
||||||
if (str_contains($number, '.')) {
|
if (str_contains($number, '.')) {
|
||||||
if ('-' !== $number[0]) {
|
if ('-' !== $number[0]) {
|
||||||
return bcadd($number, '0.'.str_repeat('0', $precision).'5', $precision);
|
return bcadd($number, '0.' . str_repeat('0', $precision) . '5', $precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bcsub($number, '0.'.str_repeat('0', $precision).'5', $precision);
|
return bcsub($number, '0.' . str_repeat('0', $precision) . '5', $precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $number;
|
return $number;
|
||||||
@ -294,28 +293,18 @@ class Steam
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the balance of an account at exact moment given. Array with at least one value.
|
* 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
|
* If the user has $convertToNative:
|
||||||
* THAT currency.
|
* "native_balance": balance in the user's native balance, with all amounts converted to native.
|
||||||
* "native_balance" the balance according to the "native_amount" + "native_foreign_amount" fields.
|
* "EUR": balance in EUR (or whatever currencies the account has balance in)
|
||||||
* "ABC" the balance in this particular currency code (may repeat for each found currency).
|
|
||||||
*
|
*
|
||||||
* 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
|
public function finalAccountBalance(Account $account, Carbon $date): array
|
||||||
{
|
{
|
||||||
$cache = new CacheProperties();
|
$cache = new CacheProperties();
|
||||||
$cache->addProperty($account->id);
|
$cache->addProperty($account->id);
|
||||||
$cache->addProperty($date);
|
$cache->addProperty($date);
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
@ -330,17 +319,15 @@ class Steam
|
|||||||
$hasCurrency = null !== $accountCurrency;
|
$hasCurrency = null !== $accountCurrency;
|
||||||
$currency = $hasCurrency ? $accountCurrency : $native;
|
$currency = $hasCurrency ? $accountCurrency : $native;
|
||||||
$return = [
|
$return = [
|
||||||
// 'balance' => '0',
|
|
||||||
'native_balance' => '0',
|
'native_balance' => '0',
|
||||||
];
|
];
|
||||||
// balance(s) in other (all) currencies.
|
// balance(s) in 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 there is no request to convert, take this as "balance" and "native_balance".
|
// if there is no request to convert, take this as "balance" and "native_balance".
|
||||||
if (!$convertToNative) {
|
if (!$convertToNative) {
|
||||||
@ -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 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) {
|
||||||
$return['balance'] = $others[$native->code] ?? '0';
|
|
||||||
$return['native_balance'] = $this->convertAllBalances($others, $native, $date); // todo sum all and convert.
|
$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:
|
// either way, the balance is always combined with the virtual balance:
|
||||||
$virtualBalance = (string) ('' === (string) $account->virtual_balance ? '0' : $account->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) {
|
if ($convertToNative) {
|
||||||
// the native balance is combined with a converted virtual_balance:
|
// the native balance is combined with a converted virtual_balance:
|
||||||
@ -372,28 +356,10 @@ class Steam
|
|||||||
$return['native_balance'] = bcadd($return['native_balance'], $virtualBalance);
|
$return['native_balance'] = bcadd($return['native_balance'], $virtualBalance);
|
||||||
Log::debug(sprintf('Virtual balance makes the (native) total %s', $return['native_balance']));
|
Log::debug(sprintf('Virtual balance makes the (native) total %s', $return['native_balance']));
|
||||||
}
|
}
|
||||||
|
$final = array_merge($return, $others);
|
||||||
// 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);
|
$cache->store($final);
|
||||||
|
|
||||||
return $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
|
public function filterAccountBalances(array $total, Account $account, bool $convertToNative, ?TransactionCurrency $currency = null): array
|
||||||
@ -496,15 +462,15 @@ class Steam
|
|||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
|
|
||||||
$set = auth()->user()->transactions()
|
$set = auth()->user()->transactions()
|
||||||
->whereIn('transactions.account_id', $accounts)
|
->whereIn('transactions.account_id', $accounts)
|
||||||
->groupBy(['transactions.account_id', 'transaction_journals.user_id'])
|
->groupBy(['transactions.account_id', 'transaction_journals.user_id'])
|
||||||
->get(['transactions.account_id', \DB::raw('MAX(transaction_journals.date) AS max_date')]) // @phpstan-ignore-line
|
->get(['transactions.account_id', \DB::raw('MAX(transaction_journals.date) AS max_date')]) // @phpstan-ignore-line
|
||||||
;
|
;
|
||||||
|
|
||||||
/** @var Transaction $entry */
|
/** @var Transaction $entry */
|
||||||
foreach ($set as $entry) {
|
foreach ($set as $entry) {
|
||||||
$date = new Carbon($entry->max_date, config('app.timezone'));
|
$date = new Carbon($entry->max_date, config('app.timezone'));
|
||||||
$date->setTimezone(config('app.timezone'));
|
$date->setTimezone(config('app.timezone'));
|
||||||
$list[(int) $entry->account_id] = $date;
|
$list[(int) $entry->account_id] = $date;
|
||||||
}
|
}
|
||||||
@ -579,9 +545,9 @@ class Steam
|
|||||||
public function getSafeUrl(string $unknownUrl, string $safeUrl): string
|
public function getSafeUrl(string $unknownUrl, string $safeUrl): string
|
||||||
{
|
{
|
||||||
// Log::debug(sprintf('getSafeUrl(%s, %s)', $unknownUrl, $safeUrl));
|
// Log::debug(sprintf('getSafeUrl(%s, %s)', $unknownUrl, $safeUrl));
|
||||||
$returnUrl = $safeUrl;
|
$returnUrl = $safeUrl;
|
||||||
$unknownHost = parse_url($unknownUrl, PHP_URL_HOST);
|
$unknownHost = parse_url($unknownUrl, PHP_URL_HOST);
|
||||||
$safeHost = parse_url($safeUrl, PHP_URL_HOST);
|
$safeHost = parse_url($safeUrl, PHP_URL_HOST);
|
||||||
|
|
||||||
if (null !== $unknownHost && $unknownHost === $safeHost) {
|
if (null !== $unknownHost && $unknownHost === $safeHost) {
|
||||||
$returnUrl = $unknownUrl;
|
$returnUrl = $unknownUrl;
|
||||||
@ -618,7 +584,7 @@ class Steam
|
|||||||
*/
|
*/
|
||||||
public function floatalize(string $value): string
|
public function floatalize(string $value): string
|
||||||
{
|
{
|
||||||
$value = strtoupper($value);
|
$value = strtoupper($value);
|
||||||
if (!str_contains($value, 'E')) {
|
if (!str_contains($value, 'E')) {
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
@ -706,9 +672,9 @@ class Steam
|
|||||||
if (null === $currency) {
|
if (null === $currency) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$current = $converter->convert($currency, $native, $date, $amount);
|
$current = $converter->convert($currency, $native, $date, $amount);
|
||||||
Log::debug(sprintf('Convert %s %s to %s %s', $currency->code, $amount, $native->code, $current));
|
Log::debug(sprintf('Convert %s %s to %s %s', $currency->code, $amount, $native->code, $current));
|
||||||
$total = bcadd($current, $total);
|
$total = bcadd($current, $total);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $total;
|
return $total;
|
||||||
|
Loading…
Reference in New Issue
Block a user