2018-02-21 08:51:30 +01:00
|
|
|
<?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);
|
|
|
|
|
2018-02-21 18:42:15 +01:00
|
|
|
namespace FireflyIII\Services\Internal\Update;
|
2018-02-21 08:51:30 +01:00
|
|
|
|
|
|
|
use FireflyIII\Models\Transaction;
|
2018-02-23 15:12:47 +01:00
|
|
|
use FireflyIII\Services\Internal\Support\TransactionServiceTrait;
|
2018-02-21 08:51:30 +01:00
|
|
|
use FireflyIII\User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TransactionUpdateService
|
|
|
|
*/
|
|
|
|
class TransactionUpdateService
|
|
|
|
{
|
2018-02-23 15:12:47 +01:00
|
|
|
use TransactionServiceTrait;
|
|
|
|
|
2018-02-21 08:51:30 +01:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
2018-02-23 15:12:47 +01:00
|
|
|
/**
|
|
|
|
* @param int $transactionId
|
|
|
|
*
|
|
|
|
* @return Transaction|null
|
|
|
|
*/
|
|
|
|
public function reconcile(int $transactionId): ?Transaction
|
2018-02-21 08:51:30 +01:00
|
|
|
{
|
2018-02-23 15:12:47 +01:00
|
|
|
$transaction = Transaction::find($transactionId);
|
2018-04-02 14:50:17 +02:00
|
|
|
if (null !== $transaction) {
|
2018-02-23 15:12:47 +01:00
|
|
|
$transaction->reconciled = true;
|
|
|
|
$transaction->save();
|
|
|
|
|
|
|
|
return $transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
2018-02-21 08:51:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Transaction $transaction
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Transaction
|
2018-07-26 06:27:52 +02:00
|
|
|
* @throws \FireflyIII\Exceptions\FireflyException
|
2018-02-21 08:51:30 +01:00
|
|
|
*/
|
|
|
|
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'];
|
2018-07-26 06:10:17 +02:00
|
|
|
$amount = (string)$data['amount'];
|
|
|
|
$account = null;
|
2018-02-21 08:51:30 +01:00
|
|
|
// update description:
|
|
|
|
$transaction->description = $description;
|
2018-02-23 15:12:47 +01:00
|
|
|
$foreignAmount = null;
|
2018-04-02 14:50:17 +02:00
|
|
|
if ((float)$transaction->amount < 0) {
|
2018-02-21 08:51:30 +01:00
|
|
|
// this is the source transaction.
|
|
|
|
$type = $this->accountType($journal, 'source');
|
|
|
|
$account = $this->findAccount($type, $data['source_id'], $data['source_name']);
|
2018-07-26 06:10:17 +02:00
|
|
|
$amount = app('steam')->negative($amount);
|
2018-04-02 14:50:17 +02:00
|
|
|
$foreignAmount = app('steam')->negative((string)$data['foreign_amount']);
|
2018-02-21 08:51:30 +01:00
|
|
|
}
|
|
|
|
|
2018-04-02 14:50:17 +02:00
|
|
|
if ((float)$transaction->amount > 0) {
|
2018-02-21 08:51:30 +01:00
|
|
|
// this is the destination transaction.
|
|
|
|
$type = $this->accountType($journal, 'destination');
|
|
|
|
$account = $this->findAccount($type, $data['destination_id'], $data['destination_name']);
|
2018-07-26 06:10:17 +02:00
|
|
|
$amount = app('steam')->positive($amount);
|
2018-04-02 14:50:17 +02:00
|
|
|
$foreignAmount = app('steam')->positive((string)$data['foreign_amount']);
|
2018-02-21 08:51:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the actual transaction:
|
|
|
|
$transaction->description = $description;
|
|
|
|
$transaction->amount = $amount;
|
|
|
|
$transaction->foreign_amount = null;
|
2018-07-26 06:27:52 +02:00
|
|
|
$transaction->transaction_currency_id = null === $currency ? $transaction->transaction_currency_id : $currency->id;
|
2018-02-21 08:51:30 +01:00
|
|
|
$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:
|
2018-07-26 06:27:52 +02:00
|
|
|
if (null !== $foreign && null !== $data['foreign_amount']) {
|
2018-02-21 08:51:30 +01:00
|
|
|
$this->setForeignCurrency($transaction, $foreign);
|
|
|
|
$this->setForeignAmount($transaction, $foreignAmount);
|
|
|
|
}
|
2018-07-26 06:27:52 +02:00
|
|
|
if (null === $foreign && null === $data['foreign_amount']) {
|
2018-02-21 08:51:30 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:21:28 +01:00
|
|
|
/**
|
|
|
|
* Update budget for a journal.
|
|
|
|
*
|
|
|
|
* @param Transaction $transaction
|
|
|
|
* @param int $budgetId
|
|
|
|
*
|
|
|
|
* @return Transaction
|
|
|
|
*/
|
|
|
|
public function updateBudget(Transaction $transaction, int $budgetId): Transaction
|
|
|
|
{
|
|
|
|
$budget = $this->findBudget($budgetId, null);
|
|
|
|
$this->setBudget($transaction, $budget);
|
|
|
|
|
|
|
|
return $transaction;
|
2018-02-21 08:51:30 +01:00
|
|
|
|
2018-02-23 16:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update category for a journal.
|
|
|
|
*
|
|
|
|
* @param Transaction $transaction
|
|
|
|
* @param string $category
|
|
|
|
*
|
|
|
|
* @return Transaction
|
|
|
|
*/
|
|
|
|
public function updateCategory(Transaction $transaction, string $category): Transaction
|
|
|
|
{
|
2018-03-29 19:01:47 +02:00
|
|
|
$found = $this->findCategory(0, $category);
|
|
|
|
$this->setCategory($transaction, $found);
|
2018-02-23 16:21:28 +01:00
|
|
|
|
2018-03-04 07:56:30 +01:00
|
|
|
return $transaction;
|
2018-02-23 16:21:28 +01:00
|
|
|
}
|
2018-03-05 19:35:58 +01:00
|
|
|
}
|