mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-22 12:11:19 +00:00
Code cleanup
This commit is contained in:
@@ -35,6 +35,31 @@ use Illuminate\Support\Facades\Log;
|
||||
*/
|
||||
class AccountObserver
|
||||
{
|
||||
public function created(Account $account): void
|
||||
{
|
||||
Log::debug('Observe "created" 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.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Also delete related objects.
|
||||
*/
|
||||
@@ -57,34 +82,9 @@ class AccountObserver
|
||||
$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.');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user