Skip to content

Firefly III: a personal finances manager

Fix convert to primary amount.

James Cole60026bbcba12
diff --git a/app/Handlers/ExchangeRate/ConversionParameters.php b/app/Handlers/ExchangeRate/ConversionParameters.php
new file mode 100644
index 0000000000..e13cf041e0
--- /dev/null
+++ b/app/Handlers/ExchangeRate/ConversionParameters.php
@@ -0,0 +1,43 @@
+<?php
+/*
+ * ConversionParameters.php
+ * Copyright (c) 2026 [email protected]
+ *
+ * This file is part of Firefly III (https://github.com/firefly-iii).
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+namespace FireflyIII\Handlers\ExchangeRate;
+
+use Carbon\Carbon;
+use FireflyIII\Models\TransactionCurrency;
+use FireflyIII\User;
+use Illuminate\Database\Eloquent\Model;
+
+class ConversionParameters
+{
+ public User $user;
+ public Model $model;
+ public TransactionCurrency $originalCurrency;
+ public string $amountField;
+ public string $primaryAmountField;
+ public Carbon $date;
+
+ public function __construct()
+ {
+ $this->date = now();
+ }
+
+}
diff --git a/app/Handlers/ExchangeRate/ConvertsAmountToPrimaryAmount.php b/app/Handlers/ExchangeRate/ConvertsAmountToPrimaryAmount.php
index 346a1ca83d..febe4d1d75 100644
--- a/app/Handlers/ExchangeRate/ConvertsAmountToPrimaryAmount.php
+++ b/app/Handlers/ExchangeRate/ConvertsAmountToPrimaryAmount.php
@@ -21,41 +21,44 @@
namespace FireflyIII\Handlers\ExchangeRate;
-use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Support\Facades\Amount;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
-use FireflyIII\User;
-use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
class ConvertsAmountToPrimaryAmount
{
- public static function convert(User $user, Model $model, TransactionCurrency $originalCurrency, string $amountField, string $primaryAmountField): void
+ public static function convert(ConversionParameters $params): void
{
- if (!Amount::convertToPrimary($user)) {
+ if (!Amount::convertToPrimary($params->user)) {
Log::debug(sprintf('User does not want to do conversion, no need to convert %s and store it in field %s.', $amountField, $primaryAmountField));
return;
}
- $primaryCurrency = Amount::getPrimaryCurrencyByUserGroup($user->userGroup);
- Log::debug(sprintf('Will convert amount in field %s from %s to %s and store it in %s', $originalCurrency->code, $primaryCurrency->code, $amountField, $primaryAmountField));
- if ($originalCurrency->id === $primaryCurrency->id) {
+ $primaryCurrency = Amount::getPrimaryCurrencyByUserGroup($params->user->userGroup);
+ Log::debug(sprintf('Will convert amount in field %s from %s to %s and store it in %s', $params->originalCurrency->code, $primaryCurrency->code, $params->amountField, $params->primaryAmountField));
+ if ($params->originalCurrency->id === $primaryCurrency->id) {
Log::debug('Both currencies are the same, do nothing.');
return;
}
+ $amountField = $params->amountField;
+ $primaryAmountField = $params->primaryAmountField;
+
+
// field is empty or zero, do nothing.
- $amount = (string)$model->$amountField;
- if ('' === $amount || 0 !== bccomp($amount, '0')) {
+ $amount = (string)$params->model->$amountField;
+ if ('' === $amount || 0 === bccomp($amount, '0')) {
Log::debug(sprintf('Amount "%s" in field "%s" cannot be used, do nothing.', $amount, $amountField));
- $model->$amountField = null;
- $model->$primaryAmountField = null;
- $model->saveQuietly();
+ $params->model->$amountField = null;
+ $params->model->$primaryAmountField = null;
+ $params->model->saveQuietly();
return;
}
$converter = new ExchangeRateConverter();
- $converter->setUserGroup($user->userGroup);
+ $newAmount = $converter->convert($params->originalCurrency, $primaryCurrency, now(), $amount);
+ $converter->setUserGroup($params->user->userGroup);
$converter->setIgnoreSettings(true);
- $model->$primaryAmountField = $converter->convert($originalCurrency, $primaryCurrency, today(), $amount);
- $model->saveQuietly();
+ $params->model->$primaryAmountField = $newAmount;
+ $params->model->saveQuietly();
+ Log::debug(sprintf('Converted field "%s" of %s #%d from %s %s to %s %s (in field "%s")', $amountField, get_class($params->model), $params->model->id, $params->originalCurrency->code, $amount, $primaryCurrency->code, $newAmount, $primaryAmountField));
}
}
diff --git a/app/Listeners/Model/Account/UpdatesAccountInformation.php b/app/Listeners/Model/Account/UpdatesAccountInformation.php
index dc53625173..b75bdf2505 100644
--- a/app/Listeners/Model/Account/UpdatesAccountInformation.php
+++ b/app/Listeners/Model/Account/UpdatesAccountInformation.php
@@ -26,17 +26,17 @@ namespace FireflyIII\Listeners\Model\Account;
use FireflyIII\Events\Model\Account\CreatedNewAccount;
use FireflyIII\Events\Model\Account\UpdatedExistingAccount;
+use FireflyIII\Handlers\ExchangeRate\ConversionParameters;
use FireflyIII\Handlers\ExchangeRate\ConvertsAmountToPrimaryAmount;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Services\Internal\Support\CreditRecalculateService;
-use FireflyIII\Support\Facades\Amount;
-use FireflyIII\Support\Http\Api\ExchangeRateConverter;
+use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
-class UpdatesAccountInformation
+class UpdatesAccountInformation implements ShouldQueue
{
- public function handle(CreatedNewAccount|UpdatedExistingAccount $event): void
+ public function handle(CreatedNewAccount | UpdatedExistingAccount $event): void
{
$this->recalculateCredit($event->account);
$this->updateVirtualBalance($event->account);
@@ -44,7 +44,7 @@ class UpdatesAccountInformation
private function recalculateCredit(Account $account): void
{
- Log::debug('Will call CreditRecalculateService because a new account was created.');
+ Log::debug('Will call CreditRecalculateService because a new account was created or updated.');
/** @var CreditRecalculateService $object */
$object = app(CreditRecalculateService::class);
@@ -54,42 +54,21 @@ class UpdatesAccountInformation
private function updateVirtualBalance(Account $account): void
{
- $repository = app(AccountRepositoryInterface::class);
- $currency = $repository->getAccountCurrency($account);
+ Log::debug('Will updateVirtualBalance');
+ $repository = app(AccountRepositoryInterface::class);
+ $currency = $repository->getAccountCurrency($account);
- if(null !== $currency) {
- // moet dit alleen als het NIET null is?
- ConvertsAmountToPrimaryAmount::convert($account->user, $account, $currency, 'virtual_balance', 'native_virtual_balance');
-
- }
- return;
-
- if (!Amount::convertToPrimary($account->user)) {
- Log::debug('After account creation, no need to convert virtual balance.');
-
- return;
+ if (null !== $currency) {
+ // only when the account has a currency, because that is the only way for the
+ // account to have a virtual balance.
+ $params = new ConversionParameters();
+ $params->user = $account->user;
+ $params->model = $account;
+ $params->originalCurrency = $currency;
+ $params->amountField = 'virtual_balance';
+ $params->primaryAmountField = 'native_virtual_balance';
+ ConvertsAmountToPrimaryAmount::convert($params);
+ Log::debug('Account primary currency virtual balance is updated.');
}
- Log::debug('After account creation, convert virtual balance.');
- $userCurrency = Amount::getPrimaryCurrencyByUserGroup($account->user->userGroup);
- $repository = app(AccountRepositoryInterface::class);
- $currency = $repository->getAccountCurrency($account);
- if (
- null !== $currency
- && $currency->id !== $userCurrency->id
- && '' !== (string) $account->virtual_balance
- && 0 !== bccomp($account->virtual_balance, '0')
- ) {
- $converter = new ExchangeRateConverter();
- $converter->setUserGroup($account->user->userGroup);
- $converter->setIgnoreSettings(true);
- $account->native_virtual_balance = $converter->convert($currency, $userCurrency, today(), $account->virtual_balance);
- }
- if ('' === (string) $account->virtual_balance || 0 === bccomp($account->virtual_balance, '0')) {
- $account->virtual_balance = null;
- $account->native_virtual_balance = null;
- }
- $account->saveQuietly();
-
- // Log::debug('Account primary currency virtual balance is updated.');
}
}