2023-09-21 11:29:09 +02:00
|
|
|
<?php
|
2023-09-21 11:32:56 +02:00
|
|
|
|
2023-09-21 11:29:09 +02:00
|
|
|
/*
|
|
|
|
|
* TransactionObserver.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/>.
|
|
|
|
|
*/
|
2023-09-21 11:32:56 +02:00
|
|
|
declare(strict_types=1);
|
2023-09-21 11:29:09 +02:00
|
|
|
|
|
|
|
|
namespace FireflyIII\Handlers\Observer;
|
|
|
|
|
|
2026-02-03 05:42:50 +01:00
|
|
|
use FireflyIII\Handlers\ExchangeRate\ConversionParameters;
|
|
|
|
|
use FireflyIII\Handlers\ExchangeRate\ConvertsAmountToPrimaryAmount;
|
2023-09-21 11:29:09 +02:00
|
|
|
use FireflyIII\Models\Transaction;
|
2024-07-29 19:51:04 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-09-21 11:29:09 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class TransactionObserver
|
|
|
|
|
*/
|
|
|
|
|
class TransactionObserver
|
|
|
|
|
{
|
2024-12-21 11:33:58 +01:00
|
|
|
public static bool $recalculate = true;
|
2024-12-21 12:27:07 +01:00
|
|
|
|
2024-12-22 08:43:12 +01:00
|
|
|
public function created(Transaction $transaction): void
|
|
|
|
|
{
|
2026-02-03 18:58:24 +01:00
|
|
|
Log::debug(sprintf('Observed creation of Transaction #%d.', $transaction->id));
|
2025-07-31 20:55:30 +02:00
|
|
|
$this->updatePrimaryCurrencyAmount($transaction);
|
2024-12-22 08:43:12 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-02 20:08:37 +01:00
|
|
|
public function updated(Transaction $transaction): void
|
|
|
|
|
{
|
|
|
|
|
$this->updatePrimaryCurrencyAmount($transaction);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 20:55:30 +02:00
|
|
|
private function updatePrimaryCurrencyAmount(Transaction $transaction): void
|
2024-12-21 12:27:07 +01:00
|
|
|
{
|
2026-02-03 05:42:50 +01:00
|
|
|
// convert "amount" to "native_amount"
|
|
|
|
|
$params = new ConversionParameters();
|
|
|
|
|
$params->user = $transaction->transactionJournal->user;
|
|
|
|
|
$params->model = $transaction;
|
|
|
|
|
$params->originalCurrency = $transaction->transactionCurrency;
|
|
|
|
|
$params->amountField = 'amount';
|
|
|
|
|
$params->date = $transaction->transactionJournal->date;
|
|
|
|
|
$params->primaryAmountField = 'native_amount';
|
|
|
|
|
ConvertsAmountToPrimaryAmount::convert($params);
|
2024-12-20 05:31:16 +01:00
|
|
|
|
2026-02-03 05:42:50 +01:00
|
|
|
// convert "foreign_amount" to "native_foreign_amount"
|
|
|
|
|
$params = new ConversionParameters();
|
|
|
|
|
$params->user = $transaction->transactionJournal->user;
|
|
|
|
|
$params->model = $transaction;
|
|
|
|
|
$params->originalCurrency = $transaction->foreignCurrency;
|
|
|
|
|
$params->date = $transaction->transactionJournal->date;
|
|
|
|
|
$params->amountField = 'foreign_amount';
|
|
|
|
|
$params->primaryAmountField = 'native_foreign_amount';
|
|
|
|
|
ConvertsAmountToPrimaryAmount::convert($params);
|
2024-05-12 06:24:11 +02:00
|
|
|
}
|
2023-09-21 11:29:09 +02:00
|
|
|
}
|