2018-02-16 09:43:25 -06:00
|
|
|
<?php
|
2018-05-11 03:08:34 -05:00
|
|
|
|
2018-02-16 09:43:25 -06:00
|
|
|
/**
|
|
|
|
* TransactionFactory.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/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-11 03:08:34 -05:00
|
|
|
declare(strict_types=1);
|
2018-02-16 09:43:25 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Factory;
|
|
|
|
|
|
|
|
|
2019-03-07 22:47:51 -06:00
|
|
|
use FireflyIII\Models\Account;
|
2018-02-16 09:43:25 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
2019-03-07 22:47:51 -06:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2018-02-16 09:43:25 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\User;
|
2019-03-18 10:52:49 -05:00
|
|
|
use Illuminate\Database\QueryException;
|
2018-05-10 02:10:16 -05:00
|
|
|
use Log;
|
2018-02-16 09:43:25 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TransactionFactory
|
|
|
|
*/
|
|
|
|
class TransactionFactory
|
|
|
|
{
|
2019-03-07 22:47:51 -06:00
|
|
|
/** @var TransactionJournal */
|
|
|
|
private $journal;
|
2019-06-04 13:42:11 -05:00
|
|
|
/** @var Account */
|
|
|
|
private $account;
|
|
|
|
/** @var TransactionCurrency */
|
|
|
|
private $currency;
|
|
|
|
/** @var TransactionCurrency */
|
|
|
|
private $foreignCurrency;
|
2018-12-03 08:57:15 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
2019-06-04 13:42:11 -05:00
|
|
|
/** @var bool */
|
|
|
|
private $reconciled;
|
2018-12-03 08:57:15 -06:00
|
|
|
|
2018-09-06 05:29:32 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
2019-06-16 06:15:32 -05:00
|
|
|
* @codeCoverageIgnore
|
2018-09-06 05:29:32 -05:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 00:59:02 -06:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-07 11:20:15 -05:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
2018-09-06 05:29:32 -05:00
|
|
|
}
|
2019-06-16 06:15:32 -05:00
|
|
|
$this->reconciled = false;
|
2019-06-04 13:42:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $reconciled
|
2019-06-16 06:15:32 -05:00
|
|
|
* @codeCoverageIgnore
|
2019-06-04 13:42:11 -05:00
|
|
|
*/
|
|
|
|
public function setReconciled(bool $reconciled): void
|
|
|
|
{
|
|
|
|
$this->reconciled = $reconciled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account $account
|
2019-06-16 06:15:32 -05:00
|
|
|
* @codeCoverageIgnore
|
2019-06-04 13:42:11 -05:00
|
|
|
*/
|
|
|
|
public function setAccount(Account $account): void
|
|
|
|
{
|
|
|
|
$this->account = $account;
|
2018-09-06 05:29:32 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:43:25 -06:00
|
|
|
/**
|
2019-03-07 22:47:51 -06:00
|
|
|
* @param TransactionCurrency $currency
|
2019-06-16 06:15:32 -05:00
|
|
|
* @codeCoverageIgnore
|
2019-06-04 13:42:11 -05:00
|
|
|
*/
|
|
|
|
public function setCurrency(TransactionCurrency $currency): void
|
|
|
|
{
|
|
|
|
$this->currency = $currency;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionCurrency $foreignCurrency |null
|
2019-06-16 06:15:32 -05:00
|
|
|
* @codeCoverageIgnore
|
2019-06-04 13:42:11 -05:00
|
|
|
*/
|
|
|
|
public function setForeignCurrency(?TransactionCurrency $foreignCurrency): void
|
|
|
|
{
|
|
|
|
$this->foreignCurrency = $foreignCurrency;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create transaction with negative amount (for source accounts).
|
2018-02-16 09:43:25 -06:00
|
|
|
*
|
2019-06-04 13:42:11 -05:00
|
|
|
* @param string $amount
|
|
|
|
* @param string|null $foreignAmount
|
2019-03-07 22:47:51 -06:00
|
|
|
* @return Transaction|null
|
2018-02-16 09:43:25 -06:00
|
|
|
*/
|
2019-06-04 13:42:11 -05:00
|
|
|
public function createNegative(string $amount, ?string $foreignAmount): ?Transaction
|
2018-02-16 09:43:25 -06:00
|
|
|
{
|
2019-06-04 13:42:11 -05:00
|
|
|
if (null !== $foreignAmount) {
|
|
|
|
$foreignAmount = app('steam')->negative($foreignAmount);
|
2019-03-07 22:47:51 -06:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:42:11 -05:00
|
|
|
return $this->create(app('steam')->negative($amount), $foreignAmount);
|
2018-02-16 09:43:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-04 13:42:11 -05:00
|
|
|
* Create transaction with positive amount (for destination accounts).
|
2018-02-16 09:43:25 -06:00
|
|
|
*
|
2019-06-04 13:42:11 -05:00
|
|
|
* @param string $amount
|
|
|
|
* @param string|null $foreignAmount
|
|
|
|
* @return Transaction|null
|
2018-02-16 09:43:25 -06:00
|
|
|
*/
|
2019-06-04 13:42:11 -05:00
|
|
|
public function createPositive(string $amount, ?string $foreignAmount): ?Transaction
|
2018-02-16 09:43:25 -06:00
|
|
|
{
|
2019-06-04 13:42:11 -05:00
|
|
|
if (null !== $foreignAmount) {
|
|
|
|
$foreignAmount = app('steam')->positive($foreignAmount);
|
2019-03-17 11:05:16 -05:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:42:11 -05:00
|
|
|
return $this->create(app('steam')->positive($amount), $foreignAmount);
|
2019-03-07 22:47:51 -06:00
|
|
|
}
|
|
|
|
|
2018-02-18 09:35:26 -06:00
|
|
|
|
2019-03-18 10:52:49 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
2019-06-16 06:15:32 -05:00
|
|
|
* @codeCoverageIgnore
|
2019-03-18 10:52:49 -05:00
|
|
|
*/
|
|
|
|
public function setJournal(TransactionJournal $journal): void
|
|
|
|
{
|
|
|
|
$this->journal = $journal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
2019-06-16 06:15:32 -05:00
|
|
|
* @codeCoverageIgnore
|
2019-03-18 10:52:49 -05:00
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2019-03-31 06:36:49 -05:00
|
|
|
|
|
|
|
/**
|
2019-06-04 13:42:11 -05:00
|
|
|
* @param string $amount
|
|
|
|
* @param string|null $foreignAmount
|
|
|
|
* @return Transaction|null
|
2019-03-31 06:36:49 -05:00
|
|
|
*/
|
2019-06-04 13:42:11 -05:00
|
|
|
private function create(string $amount, ?string $foreignAmount): ?Transaction
|
2019-03-31 06:36:49 -05:00
|
|
|
{
|
2019-06-04 13:42:11 -05:00
|
|
|
$result = null;
|
|
|
|
$data = [
|
|
|
|
'reconciled' => $this->reconciled,
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
'transaction_journal_id' => $this->journal->id,
|
|
|
|
'description' => null,
|
|
|
|
'transaction_currency_id' => $this->currency->id,
|
|
|
|
'amount' => $amount,
|
|
|
|
'foreign_amount' => null,
|
|
|
|
'foreign_currency_id' => null,
|
|
|
|
'identifier' => 0,
|
|
|
|
];
|
|
|
|
try {
|
|
|
|
$result = Transaction::create($data);
|
2019-06-16 06:15:32 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-06-04 13:42:11 -05:00
|
|
|
} catch (QueryException $e) {
|
|
|
|
Log::error(sprintf('Could not create transaction: %s', $e->getMessage()), $data);
|
2019-03-31 06:36:49 -05:00
|
|
|
}
|
2019-06-16 06:15:32 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2019-06-04 13:42:11 -05:00
|
|
|
if (null !== $result) {
|
2019-06-16 06:15:32 -05:00
|
|
|
Log::debug(sprintf('Created transaction #%d (%s %s), part of journal #%d', $result->id, $this->currency->code, $amount, $this->journal->id));
|
|
|
|
|
|
|
|
// do foreign currency thing: add foreign currency info to $one and $two if necessary.
|
|
|
|
if (null !== $this->foreignCurrency && null !== $foreignAmount && $this->foreignCurrency->id !== $this->currency->id) {
|
2019-06-04 13:42:11 -05:00
|
|
|
$result->foreign_currency_id = $this->foreignCurrency->id;
|
2019-06-16 06:15:32 -05:00
|
|
|
$result->foreign_amount = $foreignAmount;
|
2019-06-04 13:42:11 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
$result->save();
|
2019-03-31 06:36:49 -05:00
|
|
|
}
|
2019-06-04 13:42:11 -05:00
|
|
|
|
|
|
|
return $result;
|
2019-03-31 06:36:49 -05:00
|
|
|
}
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|