mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Add observers for amounts.
This commit is contained in:
@@ -26,6 +26,9 @@ namespace FireflyIII\Handlers\Observer;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class AccountObserver
|
||||
@@ -53,4 +56,37 @@ class AccountObserver
|
||||
$account->notes()->delete();
|
||||
$account->locations()->delete();
|
||||
}
|
||||
|
||||
public function created(Account $account): void
|
||||
{
|
||||
Log::debug('Observe "created" of an account.');
|
||||
$this->updateNativeAmount($account);
|
||||
}
|
||||
|
||||
public function updated(Account $account): void
|
||||
{
|
||||
Log::debug('Observe "updated" of an account.');
|
||||
$this->updateNativeAmount($account);
|
||||
}
|
||||
|
||||
private function updateNativeAmount(Account $account): void
|
||||
{
|
||||
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($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->setIgnoreSettings(true);
|
||||
$account->native_virtual_balance = $converter->convert($currency, $userCurrency, today(), $account->virtual_balance);
|
||||
|
||||
}
|
||||
if ('' === (string) $account->virtual_balance || ('' !== (string) $account->virtual_balance && 0 === bccomp($account->virtual_balance, '0'))) {
|
||||
$account->virtual_balance = null;
|
||||
$account->native_virtual_balance = null;
|
||||
}
|
||||
$account->saveQuietly();
|
||||
Log::debug('Account native virtual balance is updated.');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
58
app/Handlers/Observer/AutoBudgetObserver.php
Normal file
58
app/Handlers/Observer/AutoBudgetObserver.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/*
|
||||
* AutoBudgetObserver.php
|
||||
* Copyright (c) 2024 james@firefly-iii.org.
|
||||
*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Handlers\Observer;
|
||||
|
||||
use FireflyIII\Models\AutoBudget;
|
||||
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class AutoBudgetObserver
|
||||
{
|
||||
public function updated(AutoBudget $autoBudget): void
|
||||
{
|
||||
Log::debug('Observe "updated" of an auto budget.');
|
||||
$this->updateNativeAmount($autoBudget);
|
||||
}
|
||||
|
||||
public function created(AutoBudget $autoBudget): void
|
||||
{
|
||||
Log::debug('Observe "created" of an auto budget.');
|
||||
$this->updateNativeAmount($autoBudget);
|
||||
}
|
||||
|
||||
private function updateNativeAmount(AutoBudget $autoBudget): void
|
||||
{
|
||||
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($autoBudget->budget->user->userGroup);
|
||||
$autoBudget->native_amount =null;
|
||||
if ($autoBudget->transactionCurrency->id !== $userCurrency->id) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$autoBudget->native_amount = $converter->convert($autoBudget->transactionCurrency, $userCurrency, today(), $autoBudget->amount);
|
||||
}
|
||||
$autoBudget->saveQuietly();
|
||||
Log::debug('Auto budget native amount is updated.');
|
||||
}
|
||||
|
||||
|
||||
}
|
59
app/Handlers/Observer/AvailableBudgetObserver.php
Normal file
59
app/Handlers/Observer/AvailableBudgetObserver.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/*
|
||||
* AutoBudgetObserver.php
|
||||
* Copyright (c) 2024 james@firefly-iii.org.
|
||||
*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Handlers\Observer;
|
||||
|
||||
use FireflyIII\Models\AutoBudget;
|
||||
use FireflyIII\Models\AvailableBudget;
|
||||
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class AvailableBudgetObserver
|
||||
{
|
||||
public function updated(AvailableBudget $availableBudget): void
|
||||
{
|
||||
Log::debug('Observe "updated" of an available budget.');
|
||||
$this->updateNativeAmount($availableBudget);
|
||||
}
|
||||
|
||||
public function created(AvailableBudget $availableBudget): void
|
||||
{
|
||||
Log::debug('Observe "created" of an available budget.');
|
||||
$this->updateNativeAmount($availableBudget);
|
||||
}
|
||||
|
||||
private function updateNativeAmount(AvailableBudget $availableBudget): void
|
||||
{
|
||||
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($availableBudget->user->userGroup);
|
||||
$availableBudget->native_amount = null;
|
||||
if ($availableBudget->transactionCurrency->id !== $userCurrency->id) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$availableBudget->native_amount = $converter->convert($availableBudget->transactionCurrency, $userCurrency, today(), $availableBudget->amount);
|
||||
}
|
||||
$availableBudget->saveQuietly();
|
||||
Log::debug('Available budget native amount is updated.');
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -24,6 +24,8 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Handlers\Observer;
|
||||
|
||||
use FireflyIII\Models\Bill;
|
||||
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class BillObserver
|
||||
@@ -38,4 +40,31 @@ class BillObserver
|
||||
}
|
||||
$bill->notes()->delete();
|
||||
}
|
||||
|
||||
public function updated(Bill $bill): void
|
||||
{
|
||||
Log::debug('Observe "updated" of a bill.');
|
||||
$this->updateNativeAmount($bill);
|
||||
}
|
||||
|
||||
public function created(Bill $bill): void
|
||||
{
|
||||
Log::debug('Observe "created" of a bill.');
|
||||
$this->updateNativeAmount($bill);
|
||||
}
|
||||
|
||||
private function updateNativeAmount(Bill $bill): void
|
||||
{
|
||||
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($bill->user->userGroup);
|
||||
$bill->native_amount_min = null;
|
||||
$bill->native_amount_max = null;
|
||||
if ($bill->transactionCurrency->id !== $userCurrency->id) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$bill->native_amount_min = $converter->convert($bill->transactionCurrency, $userCurrency, today(), $bill->amount_min);
|
||||
$bill->native_amount_max = $converter->convert($bill->transactionCurrency, $userCurrency, today(), $bill->amount_max);
|
||||
}
|
||||
$bill->saveQuietly();
|
||||
Log::debug('Bill native amounts are updated.');
|
||||
}
|
||||
}
|
||||
|
56
app/Handlers/Observer/BudgetLimitObserver.php
Normal file
56
app/Handlers/Observer/BudgetLimitObserver.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/*
|
||||
* BudgetLimitObserver.php
|
||||
* Copyright (c) 2024 james@firefly-iii.org.
|
||||
*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Handlers\Observer;
|
||||
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class BudgetLimitObserver
|
||||
{
|
||||
public function updated(BudgetLimit $budgetLimit): void
|
||||
{
|
||||
Log::debug('Observe "updated" of a budget limit.');
|
||||
$this->updateNativeAmount($budgetLimit);
|
||||
}
|
||||
|
||||
public function created(BudgetLimit $budgetLimit): void
|
||||
{
|
||||
Log::debug('Observe "created" of a budget limit.');
|
||||
$this->updateNativeAmount($budgetLimit);
|
||||
}
|
||||
|
||||
private function updateNativeAmount(BudgetLimit $budgetLimit): void
|
||||
{
|
||||
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($budgetLimit->budget->user->userGroup);
|
||||
$budgetLimit->native_amount = null;
|
||||
if ($budgetLimit->transactionCurrency->id !== $userCurrency->id) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$budgetLimit->native_amount = $converter->convert($budgetLimit->transactionCurrency, $userCurrency, today(), $budgetLimit->amount);
|
||||
}
|
||||
$budgetLimit->saveQuietly();
|
||||
Log::debug('Bill native amounts are updated.');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user