mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 01:42:32 +00:00
Convert more charts.
This commit is contained in:
@@ -38,8 +38,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)) {
|
||||
@@ -74,7 +74,7 @@ class Steam
|
||||
$end->addDay()->endOfDay();
|
||||
|
||||
// set up cache
|
||||
$cache = new CacheProperties();
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty($account->id);
|
||||
$cache->addProperty('final-balance-in-range');
|
||||
$cache->addProperty($start);
|
||||
@@ -83,52 +83,51 @@ class Steam
|
||||
// return $cache->get();
|
||||
}
|
||||
|
||||
$balances = [];
|
||||
$formatted = $start->format('Y-m-d');
|
||||
$startBalance = $this->finalAccountBalance($account, $start);
|
||||
$defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
|
||||
$currency = $this->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$currencies = [
|
||||
$balances = [];
|
||||
$formatted = $start->format('Y-m-d');
|
||||
$startBalance = $this->finalAccountBalance($account, $start);
|
||||
$defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
|
||||
$currency = $this->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$currencies = [
|
||||
$currency->id => $currency,
|
||||
$defaultCurrency->id => $defaultCurrency,
|
||||
];
|
||||
$startBalance[$defaultCurrency->code] ??= '0';
|
||||
$startBalance[$currency->code] ??= '0';
|
||||
$balances[$formatted] = $startBalance;
|
||||
$balances[$formatted] = $startBalance;
|
||||
|
||||
|
||||
// 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) {
|
||||
|
||||
// normal, native and foreign amount
|
||||
$carbon = new Carbon($entry->date, $entry->date_tz);
|
||||
$modified = (string) (null === $entry->modified ? '0' : $entry->modified);
|
||||
$foreignModified = (string) (null === $entry->modified_foreign ? '0' : $entry->modified_foreign);
|
||||
$nativeModified = (string) (null === $entry->modified_native ? '0' : $entry->modified_native);
|
||||
$carbon = new Carbon($entry->date, $entry->date_tz);
|
||||
$modified = (string) (null === $entry->modified ? '0' : $entry->modified);
|
||||
$foreignModified = (string) (null === $entry->modified_foreign ? '0' : $entry->modified_foreign);
|
||||
$nativeModified = (string) (null === $entry->modified_native ? '0' : $entry->modified_native);
|
||||
|
||||
// add "modified" to amount if the currency id matches the account currency id.
|
||||
if ($entry->transaction_currency_id === $currency->id) {
|
||||
@@ -137,7 +136,7 @@ class Steam
|
||||
}
|
||||
|
||||
// always add the native balance, even if it ends up at zero.
|
||||
$currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);
|
||||
$currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);
|
||||
|
||||
// add modified foreign to the array
|
||||
if (null !== $entry->foreign_currency_id) {
|
||||
@@ -183,10 +182,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;
|
||||
@@ -261,47 +260,60 @@ class Steam
|
||||
*/
|
||||
public function finalAccountBalance(Account $account, Carbon $date): array
|
||||
{
|
||||
$native = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
|
||||
$currency = $this->getAccountCurrency($account) ?? $native;
|
||||
$return = [
|
||||
$native = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
|
||||
$accountCurrency = $this->getAccountCurrency($account);
|
||||
$hasCurrency = null !== $accountCurrency;
|
||||
$currency = $accountCurrency ?? $native;
|
||||
if (!$hasCurrency) {
|
||||
Log::debug('Gave account fake currency.');
|
||||
// fake currency
|
||||
$native = new TransactionCurrency();
|
||||
}
|
||||
$return = [
|
||||
'native_balance' => '0',
|
||||
];
|
||||
Log::debug(sprintf('Now in finalAccountBalance("%s", "%s")', $account->name, $date->format('Y-m-d H:i:s')));
|
||||
// first, the "balance", as described earlier.
|
||||
$array = $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', $currency->id)
|
||||
->get(['transactions.amount'])->toArray()
|
||||
;
|
||||
->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', $currency->id)
|
||||
->get(['transactions.amount'])->toArray();
|
||||
$return['balance'] = $this->sumTransactions($array, 'amount');
|
||||
// Log::debug(sprintf('balance is %s', $return['balance']));
|
||||
//Log::debug(sprintf('balance is %s', $return['balance']));
|
||||
// add virtual balance:
|
||||
$return['balance'] = bcadd('' === (string) $account->virtual_balance ? '0' : $account->virtual_balance, $return['balance']);
|
||||
// Log::debug(sprintf('balance is %s (with virtual balance)', $return['balance']));
|
||||
Log::debug(sprintf('balance is %s (with virtual balance)', $return['balance']));
|
||||
|
||||
// then, native balance (if necessary(
|
||||
if ($native->id !== $currency->id) {
|
||||
Log::debug('Will grab native balance for transactions on this account.');
|
||||
$array = $account->transactions()
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))
|
||||
->get(['transactions.native_amount'])->toArray()
|
||||
;
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->where('transaction_journals.date', '<=', $date->format('Y-m-d H:i:s'))
|
||||
->get(['transactions.native_amount'])->toArray();
|
||||
$return['native_balance'] = $this->sumTransactions($array, 'native_amount');
|
||||
// Log::debug(sprintf('native_balance is %s', $return['native_balance']));
|
||||
// Log::debug(sprintf('native_balance is %s', $return['native_balance']));
|
||||
$return['native_balance'] = bcadd('' === (string) $account->native_virtual_balance ? '0' : $account->native_virtual_balance, $return['native_balance']);
|
||||
// Log::debug(sprintf('native_balance is %s (with virtual balance)', $return['native_balance']));
|
||||
Log::debug(sprintf('native_balance is %s (with virtual balance)', $return['native_balance']));
|
||||
}
|
||||
|
||||
// balance(s) in other 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');
|
||||
// Log::debug('All others are (joined)', $others);
|
||||
$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');
|
||||
|
||||
// if the account has no own currency preference, drop balance in favor of native balance
|
||||
if (!$hasCurrency) {
|
||||
Log::debug('Account has no currency preference, dropping balance in favor of native balance.');
|
||||
$return['native_balance'] = bcadd($return['balance'], $return['native_balance']);
|
||||
unset($return['balance']);
|
||||
}
|
||||
|
||||
Log::debug('All others are (joined)', $others);
|
||||
|
||||
return array_merge($return, $others);
|
||||
}
|
||||
@@ -343,17 +355,17 @@ 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;
|
||||
$list[(int) $entry->account_id] = $date;
|
||||
}
|
||||
|
||||
return $list;
|
||||
@@ -426,9 +438,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;
|
||||
@@ -465,7 +477,7 @@ class Steam
|
||||
*/
|
||||
public function floatalize(string $value): string
|
||||
{
|
||||
$value = strtoupper($value);
|
||||
$value = strtoupper($value);
|
||||
if (!str_contains($value, 'E')) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user