From 8162b22d43ad9f50c97c36ecb760597b5ee4ea7d Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 1 Aug 2023 09:42:59 +0200 Subject: [PATCH] Fix chart --- .../Controllers/Chart/AccountController.php | 14 +-- .../Controllers/Chart/BalanceController.php | 99 ++++++++++++------- 2 files changed, 71 insertions(+), 42 deletions(-) diff --git a/app/Api/V2/Controllers/Chart/AccountController.php b/app/Api/V2/Controllers/Chart/AccountController.php index 3927147fc7..1cc084f4ec 100644 --- a/app/Api/V2/Controllers/Chart/AccountController.php +++ b/app/Api/V2/Controllers/Chart/AccountController.php @@ -29,6 +29,7 @@ use FireflyIII\Api\V2\Controllers\Controller; use FireflyIII\Api\V2\Request\Generic\DateRequest; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; +use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Administration\Account\AccountRepositoryInterface; use FireflyIII\Support\Http\Api\ConvertsExchangeRates; use FireflyIII\User; @@ -90,9 +91,10 @@ class AccountController extends Controller // user's preferences $defaultSet = $this->repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT])->pluck('id')->toArray(); $frontPage = app('preferences')->get('frontPageAccounts', $defaultSet); - $default = app('amount')->getDefaultCurrency(); - $accounts = $this->repository->getAccountsById($frontPage->data); - $chartData = []; + /** @var TransactionCurrency $default */ + $default = app('amount')->getDefaultCurrency(); + $accounts = $this->repository->getAccountsById($frontPage->data); + $chartData = []; if (!(is_array($frontPage->data) && count($frontPage->data) > 0)) { $frontPage->data = $defaultSet; @@ -113,11 +115,11 @@ class AccountController extends Controller 'currency_symbol' => $currency->symbol, 'currency_decimal_places' => $currency->decimal_places, - // the default currency of the user (may be the same!) - 'native_id' => $default->id, + // the default currency of the user (could be the same!) + 'native_id' => (int)$default->id, 'native_code' => $default->code, 'native_symbol' => $default->symbol, - 'native_decimal_places' => $default->decimal_places, + 'native_decimal_places' => (int)$default->decimal_places, 'start_date' => $start->toAtomString(), 'end_date' => $end->toAtomString(), 'entries' => [], diff --git a/app/Api/V2/Controllers/Chart/BalanceController.php b/app/Api/V2/Controllers/Chart/BalanceController.php index 4d21b0bb79..18a8e85729 100644 --- a/app/Api/V2/Controllers/Chart/BalanceController.php +++ b/app/Api/V2/Controllers/Chart/BalanceController.php @@ -109,6 +109,11 @@ class BalanceController extends Controller 'currency_code' => $default->code, 'currency_name' => $default->name, '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. $data[$currencyId] = $data[$currencyId] ?? [ 'currency_id' => $currencyId, - 'currency_symbol' => $currencySymbol, - 'currency_code' => $currencyCode, - 'currency_name' => $currencyName, - 'currency_decimal_places' => $currencyDecimalPlaces, + 'currency_symbol' => $journal['currency_symbol'], + 'currency_code' => $journal['currency_code'], + 'currency_name' => $journal['currency_name'], + '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] ?? [ 'period' => $period, 'spent' => '0', 'earned' => '0', - 'spent_converted' => '0', - 'earned_converted' => '0', + 'converted_spent' => '0', + 'converted_earned' => '0', ]; - // is this amount in- our outgoing? - $key = 'spent'; - $amount = app('steam')->negative($journal['amount']); - $amountConverted = $amount; + // is this journal's amount in- our outgoing? + $key = 'spent'; + $amount = app('steam')->negative($journal['amount']); // deposit = incoming // transfer or reconcile or opening balance, and these accounts are the destination. if ( @@ -159,49 +168,67 @@ class BalanceController extends Controller && in_array($journal['destination_account_id'], $ids, true) ) ) { - $key = 'earned'; - $amount = app('steam')->positive($journal['amount']); - $amountConverted = $amount; - } - // if configured convert the amount, convert the amount to $default - if ($convert) { - $rate = $converter->getCurrencyRate($currency, $default, $journal['date']); - $amountConverted = bcmul($amount, $rate); + $key = 'earned'; + $amount = app('steam')->positive($journal['amount']); } + // get conversion rate + $rate = $converter->getCurrencyRate($currency, $default, $journal['date']); + $amountConverted = bcmul($amount, $rate); - $data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount); - $convertedKey = sprintf('%s_converted', $key); + // add normal entry + $data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount); + + // add converted entry + $convertedKey = sprintf('converted_%s', $key); $data[$currencyId][$period][$convertedKey] = bcadd($data[$currencyId][$period][$convertedKey], $amountConverted); } // loop this data, make chart bars for each currency: /** @var array $currency */ foreach ($data as $currency) { + // income and expense array prepped: $income = [ - 'label' => 'earned', - 'currency_id' => $currency['currency_id'], - 'currency_symbol' => $currency['currency_symbol'], - 'currency_code' => $currency['currency_code'], - 'entries' => [], - 'converted_entries' => [], + 'label' => 'earned', + 'currency_id' => $currency['currency_id'], + 'currency_symbol' => $currency['currency_symbol'], + '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' => [], + 'converted_entries' => [], ]; $expense = [ - 'label' => 'spent', - 'currency_id' => $currency['currency_id'], - 'currency_symbol' => $currency['currency_symbol'], - 'currency_code' => $currency['currency_code'], - 'entries' => [], - 'converted_entries' => [], + 'label' => 'spent', + 'currency_id' => $currency['currency_id'], + 'currency_symbol' => $currency['currency_symbol'], + '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' => [], + 'converted_entries' => [], ]; // loop all possible periods between $start and $end, and add them to the correct dataset. $currentStart = clone $start; while ($currentStart <= $end) { - $key = $currentStart->format($format); - $title = $currentStart->isoFormat($titleFormat); + $key = $currentStart->format($format); + $title = $currentStart->isoFormat($titleFormat); + // normal entries $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']); - $currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0); + + // 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); } $chartData[] = $income;