Files
firefly-iii/app/Handlers/Observer/AccountObserver.php

95 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/*
* AccountObserver.php
* Copyright (c) 2023 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\Account;
2024-12-14 20:16:08 +01:00
use FireflyIII\Models\PiggyBank;
2024-12-20 05:20:37 +01:00
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
2024-12-28 07:35:20 +01:00
use FireflyIII\Support\Facades\Amount;
2024-12-20 05:20:37 +01:00
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use Illuminate\Support\Facades\Log;
/**
* Class AccountObserver
*/
class AccountObserver
{
2024-12-22 08:43:12 +01:00
public function created(Account $account): void
{
// Log::debug('Observe "created" of an account.');
2024-12-22 08:43:12 +01:00
$this->updateNativeAmount($account);
}
private function updateNativeAmount(Account $account): void
{
2024-12-28 07:35:20 +01:00
if(!Amount::convertToNative($account->user)) {
return;
}
2024-12-22 08:43:12 +01:00
$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.');
2024-12-22 08:43:12 +01:00
}
/**
* Also delete related objects.
*/
public function deleting(Account $account): void
{
// app('log')->debug('Observe "deleting" of an account.');
$account->accountMeta()->delete();
2024-12-14 20:16:08 +01:00
/** @var PiggyBank $piggy */
foreach ($account->piggyBanks()->get() as $piggy) {
2024-12-14 20:16:08 +01:00
$piggy->accounts()->detach($account);
}
foreach ($account->attachments()->get() as $attachment) {
$attachment->delete();
}
foreach ($account->transactions()->get() as $transaction) {
$transaction->delete();
}
$account->notes()->delete();
$account->locations()->delete();
}
2024-12-20 05:20:37 +01:00
public function updated(Account $account): void
{
// Log::debug('Observe "updated" of an account.');
2024-12-20 05:20:37 +01:00
$this->updateNativeAmount($account);
}
}