From 5f9f621fa6aea8e94858ba8033030696a296cc83 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 1 Aug 2023 12:28:11 +0200 Subject: [PATCH] Small chart fixes --- .../Controllers/Chart/AccountController.php | 5 +++++ .../Controllers/Chart/BalanceController.php | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/Api/V2/Controllers/Chart/AccountController.php b/app/Api/V2/Controllers/Chart/AccountController.php index 868a446ecd..4101cf5d72 100644 --- a/app/Api/V2/Controllers/Chart/AccountController.php +++ b/app/Api/V2/Controllers/Chart/AccountController.php @@ -27,6 +27,7 @@ namespace FireflyIII\Api\V2\Controllers\Chart; use Carbon\Carbon; use FireflyIII\Api\V2\Controllers\Controller; use FireflyIII\Api\V2\Request\Generic\DateRequest; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use FireflyIII\Models\TransactionCurrency; @@ -67,11 +68,15 @@ class AccountController extends Controller * * The native currency is the preferred currency on the page /currencies. * + * If a transaction has foreign currency = native currency, the foreign amount will be used, no conversion + * will take place. + * * @param DateRequest $request * * @return JsonResponse * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface + * @throws FireflyException */ public function dashboard(DateRequest $request): JsonResponse { diff --git a/app/Api/V2/Controllers/Chart/BalanceController.php b/app/Api/V2/Controllers/Chart/BalanceController.php index 0a6a4cd4c0..31c74f51ce 100644 --- a/app/Api/V2/Controllers/Chart/BalanceController.php +++ b/app/Api/V2/Controllers/Chart/BalanceController.php @@ -24,6 +24,7 @@ namespace FireflyIII\Api\V2\Controllers\Chart; use Carbon\Carbon; use FireflyIII\Api\V2\Controllers\Controller; use FireflyIII\Api\V2\Request\Chart\BalanceChartRequest; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collector\GroupCollectorInterface; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionType; @@ -63,11 +64,13 @@ class BalanceController extends Controller * Currency is up to the account/transactions in question, but conversion to the default * currency is possible. * - * + * If the transaction being processed is already in native currency OR if the + * foreign amount is in the native currency, the amount will not be converted. * * @param BalanceChartRequest $request * * @return JsonResponse + * @throws FireflyException */ public function balance(BalanceChartRequest $request): JsonResponse { @@ -175,6 +178,12 @@ class BalanceController extends Controller $rate = $converter->getCurrencyRate($currency, $default, $journal['date']); $amountConverted = bcmul($amount, $rate); + // perhaps transaction already has the foreign amount in the native currency. + if ((int)$journal['foreign_currency_id'] === (int)$default->id) { + $amountConverted = $journal['foreign_amount'] ?? '0'; + $amountConverted = 'earned' === $key ? app('steam')->positive($amountConverted) : app('steam')->negative($amountConverted); + } + // add normal entry $data[$currencyId][$period][$key] = bcadd($data[$currencyId][$period][$key], $amount); @@ -188,7 +197,7 @@ class BalanceController extends Controller foreach ($data as $currency) { // income and expense array prepped: $income = [ - 'label' => sprintf('earned-%s', $currency['currency_code']), + 'label' => 'earned', 'currency_id' => $currency['currency_id'], 'currency_symbol' => $currency['currency_symbol'], 'currency_code' => $currency['currency_code'], @@ -204,7 +213,7 @@ class BalanceController extends Controller 'native_entries' => [], ]; $expense = [ - 'label' => sprintf('spent-%s', $currency['currency_code']), + 'label' => 'spent', 'currency_id' => $currency['currency_id'], 'currency_symbol' => $currency['currency_symbol'], 'currency_code' => $currency['currency_code'], @@ -230,8 +239,8 @@ class BalanceController extends Controller $expense['entries'][$label] = app('steam')->bcround(($currency[$key]['spent'] ?? '0'), $currency['currency_decimal_places']); // converted entries - $income['converted_entries'][$label] = app('steam')->bcround(($currency[$key]['converted_earned'] ?? '0'), $currency['native_decimal_places']); - $expense['converted_entries'][$label] = app('steam')->bcround(($currency[$key]['converted_spent'] ?? '0'), $currency['native_decimal_places']); + $income['native_entries'][$label] = app('steam')->bcround(($currency[$key]['native_earned'] ?? '0'), $currency['native_decimal_places']); + $expense['native_entries'][$label] = app('steam')->bcround(($currency[$key]['native_spent'] ?? '0'), $currency['native_decimal_places']); // next loop $currentStart = app('navigation')->addPeriod($currentStart, $preferredRange, 0);