mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Add more column conversions.
This commit is contained in:
@@ -51,6 +51,6 @@ class BudgetLimitObserver
|
||||
$budgetLimit->native_amount = $converter->convert($budgetLimit->transactionCurrency, $userCurrency, today(), $budgetLimit->amount);
|
||||
}
|
||||
$budgetLimit->saveQuietly();
|
||||
Log::debug('Bill native amounts are updated.');
|
||||
Log::debug('Budget limit native amounts are updated.');
|
||||
}
|
||||
}
|
||||
|
59
app/Handlers/Observer/PiggyBankEventObserver.php
Normal file
59
app/Handlers/Observer/PiggyBankEventObserver.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\AvailableBudget;
|
||||
use FireflyIII\Models\PiggyBankEvent;
|
||||
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PiggyBankEventObserver
|
||||
{
|
||||
public function updated(PiggyBankEvent $event): void
|
||||
{
|
||||
Log::debug('Observe "updated" of a piggy bank event.');
|
||||
$this->updateNativeAmount($event);
|
||||
}
|
||||
|
||||
public function created(PiggyBankEvent $event): void
|
||||
{
|
||||
Log::debug('Observe "created" of a piggy bank event.');
|
||||
$this->updateNativeAmount($event);
|
||||
}
|
||||
|
||||
private function updateNativeAmount(PiggyBankEvent $event): void
|
||||
{
|
||||
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($event->piggyBank->accounts()->first()->user->userGroup);
|
||||
$event->native_amount = null;
|
||||
if ($event->piggyBank->transactionCurrency->id !== $userCurrency->id) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$event->native_amount = $converter->convert($event->piggyBank->transactionCurrency, $userCurrency, today(), $event->amount);
|
||||
}
|
||||
$event->saveQuietly();
|
||||
Log::debug('Piggy bank event native amount is updated.');
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -26,24 +26,37 @@ namespace FireflyIII\Handlers\Observer;
|
||||
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\PiggyBankRepetition;
|
||||
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Class PiggyBankObserver
|
||||
*/
|
||||
class PiggyBankObserver
|
||||
{
|
||||
public function updated(PiggyBank $piggyBank): void
|
||||
{
|
||||
Log::debug('Observe "updated" of a piggy bank.');
|
||||
$this->updateNativeAmount($piggyBank);
|
||||
}
|
||||
|
||||
public function created(PiggyBank $piggyBank): void
|
||||
{
|
||||
app('log')->debug('Observe "created" of a piggy bank. DO NOTHING.');
|
||||
Log::debug('Observe "created" of a piggy bank.');
|
||||
$this->updateNativeAmount($piggyBank);
|
||||
}
|
||||
|
||||
// $repetition = new PiggyBankRepetition();
|
||||
// $repetition->piggyBank()->associate($piggyBank);
|
||||
// $repetition->start_date = $piggyBank->start_date;
|
||||
// $repetition->start_date_tz = $piggyBank->start_date->format('e');
|
||||
// $repetition->target_date = $piggyBank->target_date;
|
||||
// $repetition->target_date_tz = $piggyBank->target_date?->format('e');
|
||||
// $repetition->current_amount = '0';
|
||||
// $repetition->save();
|
||||
private function updateNativeAmount(PiggyBank $piggyBank): void
|
||||
{
|
||||
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($piggyBank->accounts()->first()->user->userGroup);
|
||||
$piggyBank->native_target_amount = null;
|
||||
if ($piggyBank->transactionCurrency->id !== $userCurrency->id) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$piggyBank->native_target_amount = $converter->convert($piggyBank->transactionCurrency, $userCurrency, today(), $piggyBank->target_amount);
|
||||
}
|
||||
$piggyBank->saveQuietly();
|
||||
Log::debug('Piggy bank native target amount is updated.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Handlers\Observer;
|
||||
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
|
||||
use FireflyIII\Support\Models\AccountBalanceCalculator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
@@ -47,6 +48,7 @@ class TransactionObserver
|
||||
AccountBalanceCalculator::recalculateForJournal($transaction->transactionJournal);
|
||||
}
|
||||
}
|
||||
$this->updateNativeAmount($transaction);
|
||||
}
|
||||
|
||||
public function created(Transaction $transaction): void
|
||||
@@ -58,5 +60,27 @@ class TransactionObserver
|
||||
AccountBalanceCalculator::recalculateForJournal($transaction->transactionJournal);
|
||||
}
|
||||
}
|
||||
$this->updateNativeAmount($transaction);
|
||||
}
|
||||
|
||||
private function updateNativeAmount(Transaction $transaction): void {
|
||||
$userCurrency = app('amount')->getDefaultCurrencyByUserGroup($transaction->transactionJournal->user->userGroup);
|
||||
$transaction->native_amount = null;
|
||||
$transaction->native_foreign_amount = null;
|
||||
// first normal amount
|
||||
if ($transaction->transactionCurrency->id !== $userCurrency->id) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$transaction->native_amount = $converter->convert($transaction->transactionCurrency, $userCurrency, $transaction->transactionJournal->date, $transaction->amount);
|
||||
}
|
||||
// then foreign amount
|
||||
if ($transaction->foreignCurrency?->id !== $userCurrency->id && null !== $transaction->foreign_amount) {
|
||||
$converter = new ExchangeRateConverter();
|
||||
$converter->setIgnoreSettings(true);
|
||||
$transaction->native_foreign_amount = $converter->convert($transaction->foreignCurrency, $userCurrency, $transaction->transactionJournal->date, $transaction->foreign_amount);
|
||||
}
|
||||
|
||||
$transaction->saveQuietly();
|
||||
Log::debug('Transaction native amounts are updated.');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user