mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-22 03:53:37 +00:00
Merge branch 'develop' of github.com:firefly-iii/firefly-iii into develop
# Conflicts: # app/Support/Steam.php
This commit is contained in:
@@ -41,8 +41,8 @@ class Steam
|
||||
{
|
||||
public function getAccountCurrency(Account $account): ?TransactionCurrency
|
||||
{
|
||||
$type = $account->accountType->type;
|
||||
$list = config('firefly.valid_currency_account_types');
|
||||
$type = $account->accountType->type;
|
||||
$list = config('firefly.valid_currency_account_types');
|
||||
|
||||
// return null if not in this list.
|
||||
if (!in_array($type, $list, true)) {
|
||||
@@ -64,7 +64,7 @@ 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')));
|
||||
|
||||
// set up cache
|
||||
$cache = new CacheProperties();
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty($account->id);
|
||||
$cache->addProperty('final-balance-in-range');
|
||||
$cache->addProperty($start);
|
||||
@@ -73,26 +73,26 @@ class Steam
|
||||
// return $cache->get();
|
||||
}
|
||||
|
||||
$balances = [];
|
||||
$formatted = $start->format('Y-m-d');
|
||||
$startBalance = $this->finalAccountBalance($account, $start);
|
||||
$defaultCurrency = app('amount')->getNativeCurrencyByUserGroup($account->user->userGroup);
|
||||
$accountCurrency = $this->getAccountCurrency($account);
|
||||
$hasCurrency = null !== $accountCurrency;
|
||||
$currency = $accountCurrency ?? $defaultCurrency;
|
||||
$balances = [];
|
||||
$formatted = $start->format('Y-m-d');
|
||||
$startBalance = $this->finalAccountBalance($account, $start);
|
||||
$defaultCurrency = app('amount')->getNativeCurrencyByUserGroup($account->user->userGroup);
|
||||
$accountCurrency = $this->getAccountCurrency($account);
|
||||
$hasCurrency = null !== $accountCurrency;
|
||||
$currency = $accountCurrency ?? $defaultCurrency;
|
||||
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';
|
||||
}
|
||||
$currencies = [
|
||||
$currencies = [
|
||||
$currency->id => $currency,
|
||||
$defaultCurrency->id => $defaultCurrency,
|
||||
];
|
||||
|
||||
|
||||
$startBalance[$currency->code] ??= '0';
|
||||
$balances[$formatted] = $startBalance;
|
||||
$balances[$formatted] = $startBalance;
|
||||
Log::debug('Final start balance: ', $startBalance);
|
||||
|
||||
|
||||
@@ -116,6 +116,7 @@ class Steam
|
||||
$currentBalance = $startBalance;
|
||||
$converter = new ExchangeRateConverter();
|
||||
|
||||
|
||||
/** @var Transaction $entry */
|
||||
foreach ($set as $entry) {
|
||||
// normal, native and foreign amount
|
||||
@@ -126,7 +127,7 @@ class Steam
|
||||
$currencies[$entry->transaction_currency_id] ??= TransactionCurrency::find($entry->transaction_currency_id);
|
||||
|
||||
/** @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')));
|
||||
$currentBalance[$entryCurrency->code] ??= '0';
|
||||
@@ -147,7 +148,6 @@ class Steam
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// // if convert to native, if NOT convert to native.
|
||||
// if ($convertToNative) {
|
||||
// // convert for this day to native
|
||||
@@ -184,6 +184,7 @@ class Steam
|
||||
// // add to GBP, as expected.
|
||||
// $currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code] ?? '0', $modified);
|
||||
// }
|
||||
|
||||
$balances[$carbon->format('Y-m-d')] = $currentBalance;
|
||||
Log::debug('Updated entry', $currentBalance);
|
||||
}
|
||||
@@ -222,10 +223,10 @@ class Steam
|
||||
// Log::debug(sprintf('Trying bcround("%s",%d)', $number, $precision));
|
||||
if (str_contains($number, '.')) {
|
||||
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;
|
||||
@@ -313,31 +314,32 @@ class Steam
|
||||
*/
|
||||
public function finalAccountBalance(Account $account, Carbon $date): array
|
||||
{
|
||||
$cache = new CacheProperties();
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty($account->id);
|
||||
$cache->addProperty($date);
|
||||
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')));
|
||||
|
||||
$native = Amount::getNativeCurrencyByUserGroup($account->user->userGroup);
|
||||
$convertToNative = Amount::convertToNative($account->user);
|
||||
$accountCurrency = $this->getAccountCurrency($account);
|
||||
$hasCurrency = null !== $accountCurrency;
|
||||
$currency = $hasCurrency ? $accountCurrency : $native;
|
||||
$return = [
|
||||
$native = Amount::getNativeCurrencyByUserGroup($account->user->userGroup);
|
||||
$convertToNative = Amount::convertToNative($account->user);
|
||||
$accountCurrency = $this->getAccountCurrency($account);
|
||||
$hasCurrency = null !== $accountCurrency;
|
||||
$currency = $hasCurrency ? $accountCurrency : $native;
|
||||
$return = [
|
||||
'balance' => '0',
|
||||
'native_balance' => '0',
|
||||
];
|
||||
// balance(s) in other (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();
|
||||
$others = $this->groupAndSumTransactions($array, 'code', 'amount');
|
||||
$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()
|
||||
;
|
||||
$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".
|
||||
if (!$convertToNative) {
|
||||
@@ -348,7 +350,7 @@ 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.
|
||||
$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']));
|
||||
}
|
||||
|
||||
@@ -375,20 +377,21 @@ class Steam
|
||||
// $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);
|
||||
// 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);
|
||||
// return array_merge($return, $others);
|
||||
// Log::debug('Return is', $final);
|
||||
}
|
||||
|
||||
@@ -492,15 +495,15 @@ class Steam
|
||||
{
|
||||
$list = [];
|
||||
|
||||
$set = auth()->user()->transactions()
|
||||
->whereIn('transactions.account_id', $accounts)
|
||||
->groupBy(['transactions.account_id', 'transaction_journals.user_id'])
|
||||
->get(['transactions.account_id', \DB::raw('MAX(transaction_journals.date) AS max_date')]) // @phpstan-ignore-line
|
||||
$set = auth()->user()->transactions()
|
||||
->whereIn('transactions.account_id', $accounts)
|
||||
->groupBy(['transactions.account_id', 'transaction_journals.user_id'])
|
||||
->get(['transactions.account_id', \DB::raw('MAX(transaction_journals.date) AS max_date')]) // @phpstan-ignore-line
|
||||
;
|
||||
|
||||
/** @var Transaction $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'));
|
||||
$list[(int) $entry->account_id] = $date;
|
||||
}
|
||||
@@ -575,9 +578,9 @@ class Steam
|
||||
public function getSafeUrl(string $unknownUrl, string $safeUrl): string
|
||||
{
|
||||
// Log::debug(sprintf('getSafeUrl(%s, %s)', $unknownUrl, $safeUrl));
|
||||
$returnUrl = $safeUrl;
|
||||
$unknownHost = parse_url($unknownUrl, PHP_URL_HOST);
|
||||
$safeHost = parse_url($safeUrl, PHP_URL_HOST);
|
||||
$returnUrl = $safeUrl;
|
||||
$unknownHost = parse_url($unknownUrl, PHP_URL_HOST);
|
||||
$safeHost = parse_url($safeUrl, PHP_URL_HOST);
|
||||
|
||||
if (null !== $unknownHost && $unknownHost === $safeHost) {
|
||||
$returnUrl = $unknownUrl;
|
||||
@@ -614,7 +617,7 @@ class Steam
|
||||
*/
|
||||
public function floatalize(string $value): string
|
||||
{
|
||||
$value = strtoupper($value);
|
||||
$value = strtoupper($value);
|
||||
if (!str_contains($value, 'E')) {
|
||||
return $value;
|
||||
}
|
||||
@@ -693,18 +696,20 @@ class Steam
|
||||
return $amount;
|
||||
}
|
||||
|
||||
private function convertAllBalances(array $others, TransactionCurrency $native, Carbon $date): string {
|
||||
$total = '0';
|
||||
$converter = new ExchangeRateConverter();
|
||||
foreach($others as $key => $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) {
|
||||
if (null === $currency) {
|
||||
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));
|
||||
$total = bcadd($current, $total);
|
||||
$total = bcadd($current, $total);
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user