Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Add new function.
James Colebd2a746b8a19
diff --git a/app/Handlers/ExchangeRate/ConvertsAmountToPrimaryAmount.php b/app/Handlers/ExchangeRate/ConvertsAmountToPrimaryAmount.phpnew file mode 100644index 0000000000..346a1ca83d--- /dev/null+++ b/app/Handlers/ExchangeRate/ConvertsAmountToPrimaryAmount.php@@ -0,0 +1,61 @@+<?php+/*+ * ConvertsAmountToPrimaryAmount.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 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+ {+ if (!Amount::convertToPrimary($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) {+ Log::debug('Both currencies are the same, do nothing.');+ return;+ }+ // field is empty or zero, do nothing.+ $amount = (string)$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();+ return;+ }+ $converter = new ExchangeRateConverter();+ $converter->setUserGroup($user->userGroup);+ $converter->setIgnoreSettings(true);+ $model->$primaryAmountField = $converter->convert($originalCurrency, $primaryCurrency, today(), $amount);+ $model->saveQuietly();+ }++}diff --git a/app/Listeners/Model/Account/UpdatesAccountInformation.php b/app/Listeners/Model/Account/UpdatesAccountInformation.phpindex 4ba85a5410..e6ae85acdd 100644--- a/app/Listeners/Model/Account/UpdatesAccountInformation.php+++ b/app/Listeners/Model/Account/UpdatesAccountInformation.php@@ -23,6 +23,7 @@ namespace FireflyIII\Listeners\Model\Account;use FireflyIII\Events\Model\Account\CreatedNewAccount;use FireflyIII\Events\Model\Account\UpdatedExistingAccount;+use FireflyIII\Handlers\ExchangeRate\ConvertsAmountToPrimaryAmount;use FireflyIII\Models\Account;use FireflyIII\Repositories\Account\AccountRepositoryInterface;use FireflyIII\Services\Internal\Support\CreditRecalculateService;@@ -49,6 +50,16 @@ class UpdatesAccountInformationprivate function updateVirtualBalance(Account $account): void{+ $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;