mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
134 lines
4.4 KiB
PHP
134 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* TransactionUpdateService.php
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
*
|
|
* This file is part of Firefly III.
|
|
*
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Firefly III 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 General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace FireflyIII\Services\Internal\Update;
|
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
use FireflyIII\Models\Transaction;
|
|
use FireflyIII\Services\Internal\Support\TransactionServiceTrait;
|
|
use FireflyIII\User;
|
|
|
|
/**
|
|
* Class TransactionUpdateService
|
|
*/
|
|
class TransactionUpdateService
|
|
{
|
|
use TransactionServiceTrait;
|
|
|
|
/** @var User */
|
|
private $user;
|
|
|
|
|
|
/**
|
|
* @param int $transactionId
|
|
*
|
|
* @return Transaction|null
|
|
*/
|
|
public function reconcile(int $transactionId): ?Transaction
|
|
{
|
|
$transaction = Transaction::find($transactionId);
|
|
if (!is_null($transaction)) {
|
|
$transaction->reconciled = true;
|
|
$transaction->save();
|
|
|
|
return $transaction;
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
*/
|
|
public function setUser(User $user): void
|
|
{
|
|
$this->user = $user;
|
|
}
|
|
|
|
/**
|
|
* @param Transaction $transaction
|
|
* @param array $data
|
|
*
|
|
* @return Transaction
|
|
* @throws FireflyException
|
|
*/
|
|
public function update(Transaction $transaction, array $data): Transaction
|
|
{
|
|
$currency = $this->findCurrency($data['currency_id'], $data['currency_code']);
|
|
$journal = $transaction->transactionJournal;
|
|
$description = $journal->description === $data['description'] ? null : $data['description'];
|
|
|
|
// update description:
|
|
$transaction->description = $description;
|
|
$foreignAmount = null;
|
|
if (floatval($transaction->amount) < 0) {
|
|
// this is the source transaction.
|
|
$type = $this->accountType($journal, 'source');
|
|
$account = $this->findAccount($type, $data['source_id'], $data['source_name']);
|
|
$amount = app('steam')->negative(strval($data['amount']));
|
|
$foreignAmount = app('steam')->negative(strval($data['foreign_amount']));
|
|
}
|
|
|
|
if (floatval($transaction->amount) > 0) {
|
|
// this is the destination transaction.
|
|
$type = $this->accountType($journal, 'destination');
|
|
$account = $this->findAccount($type, $data['destination_id'], $data['destination_name']);
|
|
$amount = app('steam')->positive(strval($data['amount']));
|
|
$foreignAmount = app('steam')->positive(strval($data['foreign_amount']));
|
|
}
|
|
|
|
// update the actual transaction:
|
|
$transaction->description = $description;
|
|
$transaction->amount = $amount;
|
|
$transaction->foreign_amount = null;
|
|
$transaction->transaction_currency_id = $currency->id;
|
|
$transaction->account_id = $account->id;
|
|
$transaction->reconciled = $data['reconciled'];
|
|
$transaction->save();
|
|
|
|
// set foreign currency
|
|
$foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']);
|
|
// set foreign amount:
|
|
if (!is_null($data['foreign_amount'])) {
|
|
$this->setForeignCurrency($transaction, $foreign);
|
|
$this->setForeignAmount($transaction, $foreignAmount);
|
|
}
|
|
if (is_null($data['foreign_amount'])) {
|
|
$this->setForeignCurrency($transaction, null);
|
|
$this->setForeignAmount($transaction, null);
|
|
}
|
|
|
|
// set budget:
|
|
$budget = $this->findBudget($data['budget_id'], $data['budget_name']);
|
|
$this->setBudget($transaction, $budget);
|
|
|
|
// set category
|
|
$category = $this->findCategory($data['category_id'], $data['category_name']);
|
|
$this->setCategory($transaction, $category);
|
|
|
|
return $transaction;
|
|
}
|
|
|
|
|
|
} |