mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-26 13:36:15 +00:00
Add observers for amounts.
This commit is contained in:
@@ -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.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user