Fix chart

This commit is contained in:
James Cole
2023-08-01 09:42:59 +02:00
parent 8504d55f17
commit 8162b22d43
2 changed files with 71 additions and 42 deletions

View File

@@ -29,6 +29,7 @@ use FireflyIII\Api\V2\Controllers\Controller;
use FireflyIII\Api\V2\Request\Generic\DateRequest; use FireflyIII\Api\V2\Request\Generic\DateRequest;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType; use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Administration\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Administration\Account\AccountRepositoryInterface;
use FireflyIII\Support\Http\Api\ConvertsExchangeRates; use FireflyIII\Support\Http\Api\ConvertsExchangeRates;
use FireflyIII\User; use FireflyIII\User;
@@ -90,6 +91,7 @@ class AccountController extends Controller
// user's preferences // user's preferences
$defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray(); $defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray();
$frontPage = app('preferences')->get('frontPageAccounts', $defaultSet); $frontPage = app('preferences')->get('frontPageAccounts', $defaultSet);
/** @var TransactionCurrency $default */
$default = app('amount')->getDefaultCurrency(); $default = app('amount')->getDefaultCurrency();
$accounts = $this->repository->getAccountsById($frontPage->data); $accounts = $this->repository->getAccountsById($frontPage->data);
$chartData = []; $chartData = [];
@@ -113,11 +115,11 @@ class AccountController extends Controller
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
'currency_decimal_places' => $currency->decimal_places, 'currency_decimal_places' => $currency->decimal_places,
// the default currency of the user (may be the same!) // the default currency of the user (could be the same!)
'native_id' => $default->id, 'native_id' => (int)$default->id,
'native_code' => $default->code, 'native_code' => $default->code,
'native_symbol' => $default->symbol, 'native_symbol' => $default->symbol,
'native_decimal_places' => $default->decimal_places, 'native_decimal_places' => (int)$default->decimal_places,
'start_date' => $start->toAtomString(), 'start_date' => $start->toAtomString(),
'end_date' => $end->toAtomString(), 'end_date' => $end->toAtomString(),
'entries' => [], 'entries' => [],

View File

@@ -109,6 +109,11 @@ class BalanceController extends Controller
'currency_code' => $default->code, 'currency_code' => $default->code,
'currency_name' => $default->name, 'currency_name' => $default->name,
'currency_decimal_places' => (int)$default->decimal_places, 'currency_decimal_places' => (int)$default->decimal_places,
'native_id' => $defaultCurrencyId,
'native_symbol' => $default->symbol,
'native_code' => $default->code,
'native_name' => $default->name,
'native_decimal_places' => (int)$default->decimal_places,
]; ];
@@ -126,24 +131,28 @@ class BalanceController extends Controller
// set the array with monetary info, if it does not exist. // set the array with monetary info, if it does not exist.
$data[$currencyId] = $data[$currencyId] ?? [ $data[$currencyId] = $data[$currencyId] ?? [
'currency_id' => $currencyId, 'currency_id' => $currencyId,
'currency_symbol' => $currencySymbol, 'currency_symbol' => $journal['currency_symbol'],
'currency_code' => $currencyCode, 'currency_code' => $journal['currency_code'],
'currency_name' => $currencyName, 'currency_name' => $journal['currency_name'],
'currency_decimal_places' => $currencyDecimalPlaces, 'currency_decimal_places' => $journal['currency_decimal_places'],
// native currency info (could be the same)
'native_id' => (int)$default->id,
'native_code' => $default->code,
'native_symbol' => $default->symbol,
'native_decimal_places' => (int)$default->decimal_places,
]; ];
// set the array with spent/earned in this $period, if it does not exist. // set the array (in monetary info) with spent/earned in this $period, if it does not exist.
$data[$currencyId][$period] = $data[$currencyId][$period] ?? [ $data[$currencyId][$period] = $data[$currencyId][$period] ?? [
'period' => $period, 'period' => $period,
'spent' => '0', 'spent' => '0',
'earned' => '0', 'earned' => '0',
'spent_converted' => '0', 'converted_spent' => '0',
'earned_converted' => '0', 'converted_earned' => '0',
]; ];
// is this amount in- our outgoing? // is this journal's amount in- our outgoing?
$key = 'spent'; $key = 'spent';
$amount = app('steam')->negative($journal['amount']); $amount = app('steam')->negative($journal['amount']);
$amountConverted = $amount;
// deposit = incoming // deposit = incoming
// transfer or reconcile or opening balance, and these accounts are the destination. // transfer or reconcile or opening balance, and these accounts are the destination.
if ( if (
@@ -161,27 +170,33 @@ class BalanceController extends Controller
) { ) {
$key = 'earned'; $key = 'earned';
$amount = app('steam')->positive($journal['amount']); $amount = app('steam')->positive($journal['amount']);
$amountConverted = $amount;
} }
// if configured convert the amount, convert the amount to $default // get conversion rate
if ($convert) {
$rate = $converter->getCurrencyRate($currency, $default, $journal['date']); $rate = $converter->getCurrencyRate($currency, $default, $journal['date']);
$amountConverted = bcmul($amount, $rate); $amountConverted = bcmul($amount, $rate);
}
// add normal entry
$data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount); $data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount);
$convertedKey = sprintf('%s_converted', $key);
// add converted entry
$convertedKey = sprintf('converted_%s', $key);
$data[$currencyId][$period][$convertedKey] = bcadd($data[$currencyId][$period][$convertedKey], $amountConverted); $data[$currencyId][$period][$convertedKey] = bcadd($data[$currencyId][$period][$convertedKey], $amountConverted);
} }
// loop this data, make chart bars for each currency: // loop this data, make chart bars for each currency:
/** @var array $currency */ /** @var array $currency */
foreach ($data as $currency) { foreach ($data as $currency) {
// income and expense array prepped:
$income = [ $income = [
'label' => 'earned', 'label' => 'earned',
'currency_id' => $currency['currency_id'], 'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'], 'currency_symbol' => $currency['currency_symbol'],
'currency_code' => $currency['currency_code'], 'currency_code' => $currency['currency_code'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'native_id' => $currency['native_id'],
'native_symbol' => $currency['native_symbol'],
'native_code' => $currency['native_code'],
'native_decimal_places' => $currency['native_decimal_places'],
'entries' => [], 'entries' => [],
'converted_entries' => [], 'converted_entries' => [],
]; ];
@@ -190,6 +205,11 @@ class BalanceController extends Controller
'currency_id' => $currency['currency_id'], 'currency_id' => $currency['currency_id'],
'currency_symbol' => $currency['currency_symbol'], 'currency_symbol' => $currency['currency_symbol'],
'currency_code' => $currency['currency_code'], 'currency_code' => $currency['currency_code'],
'currency_decimal_places' => $currency['currency_decimal_places'],
'native_id' => $currency['native_id'],
'native_symbol' => $currency['native_symbol'],
'native_code' => $currency['native_code'],
'native_decimal_places' => $currency['native_decimal_places'],
'entries' => [], 'entries' => [],
'converted_entries' => [], 'converted_entries' => [],
@@ -199,8 +219,15 @@ class BalanceController extends Controller
while ($currentStart <= $end) { while ($currentStart <= $end) {
$key = $currentStart->format($format); $key = $currentStart->format($format);
$title = $currentStart->isoFormat($titleFormat); $title = $currentStart->isoFormat($titleFormat);
// normal entries
$income['entries'][$title] = app('steam')->bcround(($currency[$key]['earned'] ?? '0'), $currency['currency_decimal_places']); $income['entries'][$title] = app('steam')->bcround(($currency[$key]['earned'] ?? '0'), $currency['currency_decimal_places']);
$expense['entries'][$title] = app('steam')->bcround(($currency[$key]['spent'] ?? '0'), $currency['currency_decimal_places']); $expense['entries'][$title] = app('steam')->bcround(($currency[$key]['spent'] ?? '0'), $currency['currency_decimal_places']);
// converted entries
$income['converted_entries'][$title] = app('steam')->bcround(($currency[$key]['converted_earned'] ?? '0'), $currency['native_decimal_places']);
$expense['converted_entries'][$title] = app('steam')->bcround(($currency[$key]['converted_spent'] ?? '0'), $currency['native_decimal_places']);
// next loop
$currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0); $currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0);
} }