mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
Fix charts and balances.
This commit is contained in:
@@ -116,7 +116,7 @@ class AccountController extends Controller
|
||||
];
|
||||
// TODO this code is also present in the V2 chart account controller so this method is due to be deprecated.
|
||||
$currentStart = clone $start;
|
||||
$range = app('steam')->finalAccountBalanceInRange($account, $start, clone $end);
|
||||
$range = app('steam')->finalAccountBalanceInRange($account, $start, clone $end, $this->convertToNative);
|
||||
// 2022-10-11 this method no longer converts to float.
|
||||
$previous = array_values($range)[0];
|
||||
while ($currentStart <= $end) {
|
||||
|
@@ -55,6 +55,7 @@ abstract class Controller extends BaseController
|
||||
/** @var array<int, string> */
|
||||
protected array $allowedSort;
|
||||
protected ParameterBag $parameters;
|
||||
protected bool $convertToNative = false;
|
||||
|
||||
/**
|
||||
* Controller constructor.
|
||||
@@ -68,7 +69,9 @@ abstract class Controller extends BaseController
|
||||
$this->parameters = $this->getParameters();
|
||||
if (auth()->check()) {
|
||||
$language = app('steam')->getLanguage();
|
||||
$this->convertToNative = app('preferences')->get('convert_to_native', false)->data;
|
||||
app()->setLocale($language);
|
||||
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
|
@@ -118,7 +118,7 @@ class AccountController extends Controller
|
||||
'native_entries' => [],
|
||||
];
|
||||
$currentStart = clone $params['start'];
|
||||
$range = app('steam')->finalAccountBalanceInRange($account, $params['start'], clone $params['end'], $currency);
|
||||
$range = app('steam')->finalAccountBalanceInRange($account, $params['start'], clone $params['end'], $this->convertToNative);
|
||||
|
||||
$previous = array_values($range)[0]['balance'];
|
||||
$previousNative = array_values($range)[0]['native_balance'];
|
||||
|
@@ -57,6 +57,7 @@ class Controller extends BaseController
|
||||
protected const string CONTENT_TYPE = 'application/vnd.api+json';
|
||||
protected array $acceptedRoles = [UserRoleEnum::READ_ONLY];
|
||||
protected ParameterBag $parameters;
|
||||
protected bool $convertToNative = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
@@ -30,7 +30,6 @@ use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
|
||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
@@ -418,21 +417,128 @@ class AccountController extends Controller
|
||||
*/
|
||||
public function period(Account $account, Carbon $start, Carbon $end): JsonResponse
|
||||
{
|
||||
Log::debug('Now in period()');
|
||||
$chartData = [];
|
||||
$cache = new CacheProperties();
|
||||
$cache->addProperty('chart.account.period');
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($end);
|
||||
$cache->addProperty($this->convertToNative);
|
||||
$cache->addProperty($account->id);
|
||||
if ($cache->has()) {
|
||||
return response()->json($cache->get());
|
||||
// return response()->json($cache->get());
|
||||
}
|
||||
$currencies = $this->accountRepository->getUsedCurrencies($account);
|
||||
|
||||
// if the account is not expense or revenue, just use the account's default currency.
|
||||
if (!in_array($account->accountType->type, [AccountType::REVENUE, AccountType::EXPENSE], true)) {
|
||||
$currencies = [$this->accountRepository->getAccountCurrency($account) ?? app('amount')->getDefaultCurrency()];
|
||||
// collect and filter balances for the entire period.
|
||||
$step = $this->calculateStep($start, $end);
|
||||
Log::debug(sprintf('Step is %s', $step));
|
||||
$locale = app('steam')->getLocale();
|
||||
$return = [];
|
||||
// fix for issue https://github.com/firefly-iii/firefly-iii/issues/8041
|
||||
// have to make sure this chart is always based on the balance at the END of the period.
|
||||
// This period depends on the size of the chart
|
||||
$current = clone $start;
|
||||
$current = app('navigation')->endOfX($current, $step, null);
|
||||
$format = (string) trans('config.month_and_day_js', [], $locale);
|
||||
$accountCurrency = $this->accountRepository->getAccountCurrency($account);
|
||||
|
||||
Log::debug('One');
|
||||
$range = Steam::finalAccountBalanceInRange($account, $start, $end, $this->convertToNative);
|
||||
Log::debug('Two');
|
||||
$range = Steam::filterAccountBalances($range, $account, $this->convertToNative, $accountCurrency);
|
||||
Log::debug('Three');
|
||||
$previous = array_values($range)[0];
|
||||
$accountCurrency = $accountCurrency ?? $this->defaultCurrency; // do this AFTER getting the balances.
|
||||
while ($end >= $current) {
|
||||
$theDate = $current->format('Y-m-d');
|
||||
// each day contains multiple balances, and this may even be different over time.
|
||||
$momentBalance = $range[$theDate] ?? $previous;
|
||||
$return = $this->updateChartKeys($return, $momentBalance);
|
||||
|
||||
// process each balance thing.
|
||||
foreach($momentBalance as $key => $amount) {
|
||||
$label = $current->isoFormat($format);
|
||||
$return[$key]['entries'][$label] = $amount;
|
||||
}
|
||||
$current = app('navigation')->addPeriod($current, $step, 0);
|
||||
// here too, to fix #8041, the data is corrected to the end of the period.
|
||||
$current = app('navigation')->endOfX($current, $step, null);
|
||||
$previous = $momentBalance;
|
||||
}
|
||||
// second loop (yes) to create nice array with info! Yay!
|
||||
$chartData = [];
|
||||
foreach($return as $key => $info) {
|
||||
if(3 === strlen($key)) {
|
||||
// assume it's a currency:
|
||||
$setCurrency = $this->currencyRepository->findByCode($key);
|
||||
$info['currency_symbol'] = $setCurrency->symbol;
|
||||
$info['currency_code'] = $setCurrency->code;
|
||||
$info['label'] = sprintf('%s (%s)', $account->name, $setCurrency->symbol);
|
||||
}
|
||||
if('balance' === $key) {
|
||||
$info['currency_symbol'] = $accountCurrency->symbol;
|
||||
$info['currency_code'] = $accountCurrency->code;
|
||||
$info['label'] = sprintf('%s (%s)', $account->name, $accountCurrency->symbol);
|
||||
}
|
||||
if('native_balance' === $key) {
|
||||
$info['currency_symbol'] = $this->defaultCurrency->symbol;
|
||||
$info['currency_code'] = $this->defaultCurrency->code;
|
||||
$info['label'] = sprintf('%s (%s) (%s)', $account->name, (string)trans('firefly.sum'), $this->defaultCurrency->symbol);
|
||||
}
|
||||
$chartData[] = $info;
|
||||
}
|
||||
|
||||
$data = $this->generator->multiSet($chartData);
|
||||
$cache->store($data);
|
||||
|
||||
return response()->json($data);
|
||||
|
||||
|
||||
|
||||
var_dump($chartData);exit;
|
||||
|
||||
|
||||
|
||||
|
||||
$result = [
|
||||
'label' => sprintf('%s (%s)', $account->name, $currency->symbol),
|
||||
'currency_symbol' => $currency->symbol,
|
||||
'currency_code' => $currency->code,
|
||||
];
|
||||
$entries = [];
|
||||
$current = clone $start;
|
||||
|
||||
|
||||
Log::debug(sprintf('$current date is %s', $current->format('Y-m-d')));
|
||||
if ('1D' === $step) {
|
||||
// per day the entire period, balance for every day.
|
||||
$format = (string) trans('config.month_and_day_js', [], $locale);
|
||||
$range = app('steam')->finalAccountBalanceInRange($account, $start, $end, $this->convertToNative);
|
||||
$previous = array_values($range)[0];
|
||||
while ($end >= $current) {
|
||||
$theDate = $current->format('Y-m-d');
|
||||
$balance = $range[$theDate]['balance'] ?? $previous;
|
||||
$label = $current->isoFormat($format);
|
||||
$entries[$label] = (float) $balance;
|
||||
$previous = $balance;
|
||||
$current->addDay();
|
||||
}
|
||||
}
|
||||
if ('1W' === $step || '1M' === $step || '1Y' === $step) {
|
||||
while ($end >= $current) {
|
||||
Log::debug(sprintf('Current is: %s', $current->format('Y-m-d')));
|
||||
$balance = Steam::finalAccountBalance($account, $current)[$currency->code] ?? '0';
|
||||
$label = app('navigation')->periodShow($current, $step);
|
||||
$entries[$label] = $balance;
|
||||
$current = app('navigation')->addPeriod($current, $step, 0);
|
||||
// here too, to fix #8041, the data is corrected to the end of the period.
|
||||
$current = app('navigation')->endOfX($current, $step, null);
|
||||
}
|
||||
}
|
||||
$result['entries'] = $entries;
|
||||
|
||||
return $result;
|
||||
|
||||
|
||||
/** @var TransactionCurrency $currency */
|
||||
foreach ($currencies as $currency) {
|
||||
@@ -470,7 +576,7 @@ class AccountController extends Controller
|
||||
if ('1D' === $step) {
|
||||
// per day the entire period, balance for every day.
|
||||
$format = (string) trans('config.month_and_day_js', [], $locale);
|
||||
$range = app('steam')->finalAccountBalanceInRange($account, $start, $end);
|
||||
$range = app('steam')->finalAccountBalanceInRange($account, $start, $end, $this->convertToNative);
|
||||
$previous = array_values($range)[0];
|
||||
while ($end >= $current) {
|
||||
$theDate = $current->format('Y-m-d');
|
||||
@@ -633,4 +739,14 @@ class AccountController extends Controller
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
private function updateChartKeys(array $array, array $balances): array
|
||||
{
|
||||
foreach (array_keys($balances) as $key) {
|
||||
$array[$key] ??= [
|
||||
'key' => $key,
|
||||
];
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
@@ -69,11 +69,12 @@ class Steam
|
||||
return $sum;
|
||||
}
|
||||
|
||||
public function finalAccountBalanceInRange(Account $account, Carbon $start, Carbon $end): array
|
||||
public function finalAccountBalanceInRange(Account $account, Carbon $start, Carbon $end, bool $convertToNative): array
|
||||
{
|
||||
// expand period.
|
||||
$start->subDay()->startOfDay();
|
||||
$end->addDay()->endOfDay();
|
||||
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();
|
||||
@@ -89,14 +90,23 @@ class Steam
|
||||
$formatted = $start->format('Y-m-d');
|
||||
$startBalance = $this->finalAccountBalance($account, $start);
|
||||
$defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($account->user->userGroup);
|
||||
$currency = $this->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$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 = [
|
||||
$currency->id => $currency,
|
||||
$defaultCurrency->id => $defaultCurrency,
|
||||
];
|
||||
$startBalance[$defaultCurrency->code] ??= '0';
|
||||
|
||||
|
||||
$startBalance[$currency->code] ??= '0';
|
||||
$balances[$formatted] = $startBalance;
|
||||
Log::debug('Final start balance: ', $startBalance);
|
||||
|
||||
|
||||
// sums up the balance changes per day, for foreign, native and normal amounts.
|
||||
@@ -118,38 +128,66 @@ class Steam
|
||||
\DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'),
|
||||
\DB::raw('SUM(transactions.native_amount) AS modified_native'),
|
||||
]
|
||||
)
|
||||
;
|
||||
);
|
||||
|
||||
$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);
|
||||
|
||||
// add "modified" to amount if the currency id matches the account currency id.
|
||||
if ($entry->transaction_currency_id === $currency->id) {
|
||||
$currentBalance['balance'] = bcadd($currentBalance['balance'], $modified);
|
||||
$currentBalance[$currency->code] = bcadd($currentBalance[$currency->code], $modified);
|
||||
}
|
||||
// find currency of this entry.
|
||||
$currencies[$entry->transaction_currency_id] = $currencies[$entry->transaction_currency_id] ?? TransactionCurrency::find($entry->transaction_currency_id);
|
||||
$entryCurrency = $currencies[$entry->transaction_currency_id];
|
||||
|
||||
// always add the native balance, even if it ends up at zero.
|
||||
Log::debug(sprintf('Processing transaction(s) on date %s', $carbon->format('Y-m-d H:i:s')));
|
||||
|
||||
// if convert to native, if NOT convert to native.
|
||||
if($convertToNative) {
|
||||
Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $modified, $foreignModified, $nativeModified));
|
||||
// add to native balance.
|
||||
$currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);
|
||||
|
||||
// add modified foreign to the array
|
||||
if (null !== $entry->foreign_currency_id) {
|
||||
$foreignId = $entry->foreign_currency_id;
|
||||
$currencies[$foreignId] ??= TransactionCurrency::find($foreignId);
|
||||
$foreignCurrency = $currencies[$foreignId];
|
||||
$currentBalance[$foreignCurrency->code] ??= '0';
|
||||
$currentBalance[$foreignCurrency->code] = bcadd($currentBalance[$foreignCurrency->code], $foreignModified);
|
||||
if($entry->foreign_currency_id === $defaultCurrency->id) {
|
||||
$currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $foreignModified);
|
||||
}
|
||||
// add to balance if is the same.
|
||||
if($entry->transaction_currency_id === $accountCurrency?->id) {
|
||||
$currentBalance['balance'] = bcadd($currentBalance['balance'], $modified);
|
||||
}
|
||||
// add currency balance
|
||||
$currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code], $modified);
|
||||
}
|
||||
if(!$convertToNative) {
|
||||
Log::debug(sprintf('Amount is %s %s, foreign amount is %s, native amount is %s', $entryCurrency->code, $modified, $foreignModified, $nativeModified));
|
||||
// add to balance, as expected.
|
||||
$currentBalance['balance'] = bcadd($currentBalance['balance'] ?? '0', $modified);
|
||||
// add to GBP, as expected.
|
||||
$currentBalance[$entryCurrency->code] = bcadd($currentBalance[$entryCurrency->code], $modified);
|
||||
}
|
||||
|
||||
// // add "modified" to amount if the currency id matches the account currency id.
|
||||
// if ($entry->transaction_currency_id === $currency->id) {
|
||||
// $currentBalance['balance'] = bcadd($currentBalance['balance'], $modified);
|
||||
// $currentBalance[$currency->code] = bcadd($currentBalance[$currency->code], $modified);
|
||||
// }
|
||||
//
|
||||
// // always add the native balance, even if it ends up at zero.
|
||||
// $currentBalance['native_balance'] = bcadd($currentBalance['native_balance'], $nativeModified);
|
||||
|
||||
// DO NOT add modified foreign to the array
|
||||
// if (null !== $entry->foreign_currency_id) {
|
||||
// $foreignId = $entry->foreign_currency_id;
|
||||
// $currencies[$foreignId] ??= TransactionCurrency::find($foreignId);
|
||||
// $foreignCurrency = $currencies[$foreignId];
|
||||
// $currentBalance[$foreignCurrency->code] ??= '0';
|
||||
// $currentBalance[$foreignCurrency->code] = bcadd($currentBalance[$foreignCurrency->code], $foreignModified);
|
||||
// }
|
||||
$balances[$carbon->format('Y-m-d')] = $currentBalance;
|
||||
Log::debug('Updated entry',$currentBalance);
|
||||
}
|
||||
$cache->store($balances);
|
||||
|
||||
@@ -291,8 +329,7 @@ class Steam
|
||||
->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')
|
||||
;
|
||||
->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']);
|
||||
@@ -304,8 +341,7 @@ class Steam
|
||||
->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')
|
||||
;
|
||||
->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, $return['native_balance']));
|
||||
@@ -316,8 +352,7 @@ class Steam
|
||||
->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')
|
||||
;
|
||||
->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, $sum, $return['native_balance']));
|
||||
@@ -328,8 +363,7 @@ class Steam
|
||||
->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 the account has no own currency preference, drop balance in favor of native balance
|
||||
@@ -351,19 +385,25 @@ class Steam
|
||||
$return['native_balance'] = $sum;
|
||||
unset($return['balance']);
|
||||
}
|
||||
|
||||
return array_merge($return, $others);
|
||||
$final = array_merge($return, $others);
|
||||
Log::debug('Return is', $final);
|
||||
return $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
|
||||
{
|
||||
Log::debug(sprintf('filterAccountBalances(#%d)', $account->id));
|
||||
$return = [];
|
||||
foreach ($total as $key => $value) {
|
||||
$return[$key] = $this->filterAccountBalance($value, $account, $convertToNative, $currency);
|
||||
}
|
||||
Log::debug(sprintf('end of filterAccountBalances(#%d)', $account->id));
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function filterAccountBalance(array $set, Account $account, bool $convertToNative, ?TransactionCurrency $currency = null): array {
|
||||
public function filterAccountBalance(array $set, Account $account, bool $convertToNative, ?TransactionCurrency $currency = null): array
|
||||
{
|
||||
Log::debug(sprintf('filterAccountBalance(#%d)', $account->id), $set);
|
||||
if (0 === count($set)) {
|
||||
Log::debug(sprintf('Return empty array for account #%d', $account->id));
|
||||
return [];
|
||||
@@ -388,7 +428,7 @@ class Steam
|
||||
if (!$convertToNative) {
|
||||
if (null === $currency) {
|
||||
Log::debug(sprintf('Unset native_balance and make defaultCurrency balance the balance for account #%d', $account->id));
|
||||
$set['balance'] = $set[$this->defaultCurrency->code] ?? '0';
|
||||
$set['balance'] = $set[$defaultCurrency->code] ?? '0';
|
||||
unset($set['native_balance'], $set[$defaultCurrency->code]);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user