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;
|
|
|
|
|
|
|
|
|
2018-05-06 13:42:07 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
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;
|
2019-03-07 22:47:51 -06:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2019-04-06 01:10:50 -05:00
|
|
|
use FireflyIII\Services\Internal\Support\JournalServiceTrait;
|
2019-03-17 11:05:16 -05:00
|
|
|
use FireflyIII\Support\NullArrayObject;
|
2018-02-16 09:43:25 -06:00
|
|
|
use FireflyIII\User;
|
2019-03-31 06:36:49 -05:00
|
|
|
use FireflyIII\Validation\AccountValidator;
|
2019-03-18 10:52:49 -05:00
|
|
|
use Illuminate\Database\QueryException;
|
2018-02-16 09:43:25 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2018-05-10 02:10:16 -05:00
|
|
|
use Log;
|
2018-02-16 09:43:25 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TransactionFactory
|
|
|
|
*/
|
|
|
|
class TransactionFactory
|
|
|
|
{
|
2019-04-06 01:10:50 -05:00
|
|
|
use JournalServiceTrait;
|
|
|
|
|
2019-03-31 06:36:49 -05:00
|
|
|
/** @var AccountValidator */
|
|
|
|
private $accountValidator;
|
2019-03-07 22:47:51 -06:00
|
|
|
/** @var TransactionJournal */
|
|
|
|
private $journal;
|
2018-12-03 08:57:15 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
2018-09-06 05:29:32 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 00:59:02 -06:00
|
|
|
if ('testing' === config('app.env')) {
|
2018-09-06 05:29:32 -05:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
|
|
|
|
}
|
2019-03-07 22:47:51 -06:00
|
|
|
$this->accountRepository = app(AccountRepositoryInterface::class);
|
2019-03-31 06:36:49 -05:00
|
|
|
$this->accountValidator = app(AccountValidator::class);
|
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 Account $account
|
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
* @param string $amount
|
2018-02-16 09:43:25 -06:00
|
|
|
*
|
2019-03-07 22:47:51 -06:00
|
|
|
* @return Transaction|null
|
2018-02-16 09:43:25 -06:00
|
|
|
*/
|
2019-03-07 22:47:51 -06:00
|
|
|
public function create(Account $account, TransactionCurrency $currency, string $amount): ?Transaction
|
2018-02-16 09:43:25 -06:00
|
|
|
{
|
2019-03-18 10:52:49 -05:00
|
|
|
$result = null;
|
|
|
|
$data = [
|
|
|
|
'reconciled' => false,
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'transaction_journal_id' => $this->journal->id,
|
|
|
|
'description' => null,
|
|
|
|
'transaction_currency_id' => $currency->id,
|
|
|
|
'amount' => $amount,
|
|
|
|
'foreign_amount' => null,
|
|
|
|
'foreign_currency_id' => null,
|
|
|
|
'identifier' => 0,
|
|
|
|
];
|
|
|
|
try {
|
|
|
|
$result = Transaction::create($data);
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
Log::error(sprintf('Could not create transaction: %s', $e->getMessage()), $data);
|
|
|
|
}
|
2019-03-07 22:47:51 -06:00
|
|
|
if (null !== $result) {
|
|
|
|
Log::debug(
|
|
|
|
sprintf(
|
|
|
|
'Created transaction #%d (%s %s), part of journal #%d', $result->id,
|
|
|
|
$currency->code, $amount, $this->journal->id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2018-02-16 09:43:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-17 11:05:16 -05:00
|
|
|
* @param NullArrayObject $data
|
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
* @param TransactionCurrency|null $foreignCurrency
|
2018-02-16 09:43:25 -06:00
|
|
|
*
|
|
|
|
* @return Collection
|
2018-05-06 13:42:07 -05:00
|
|
|
* @throws FireflyException
|
2018-02-16 09:43:25 -06:00
|
|
|
*/
|
2019-03-17 11:05:16 -05:00
|
|
|
public function createPair(NullArrayObject $data, TransactionCurrency $currency, ?TransactionCurrency $foreignCurrency): Collection
|
2018-02-16 09:43:25 -06:00
|
|
|
{
|
2019-04-06 01:10:50 -05:00
|
|
|
Log::debug('Going to create a pair of transactions.');
|
|
|
|
Log::debug(sprintf('Source info: ID #%d, name "%s"', $data['source_id'], $data['source_name']));
|
|
|
|
Log::debug(sprintf('Destination info: ID #%d, name "%s"', $data['destination_id'], $data['destination_name']));
|
2019-03-31 06:36:49 -05:00
|
|
|
// validate source and destination using a new Validator.
|
|
|
|
$this->validateAccounts($data);
|
2018-07-06 12:06:08 -05:00
|
|
|
|
2019-03-31 06:36:49 -05:00
|
|
|
// create or get source and destination accounts:
|
2019-04-06 01:10:50 -05:00
|
|
|
$type = $this->journal->transactionType->type;
|
|
|
|
$sourceAccount = $this->getAccount($type, 'source', (int)$data['source_id'], $data['source_name']);
|
|
|
|
$destinationAccount = $this->getAccount($type, 'destination', (int)$data['destination_id'], $data['destination_name']);
|
2019-03-18 10:52:49 -05:00
|
|
|
|
2019-04-16 09:20:46 -05:00
|
|
|
// at this point we know the
|
|
|
|
|
2019-03-31 06:36:49 -05:00
|
|
|
$amount = $this->getAmount($data['amount']);
|
|
|
|
$foreignAmount = $this->getForeignAmount($data['foreign_amount']);
|
|
|
|
$one = $this->create($sourceAccount, $currency, app('steam')->negative($amount));
|
|
|
|
$two = $this->create($destinationAccount, $currency, app('steam')->positive($amount));
|
2018-08-25 14:33:22 -05:00
|
|
|
|
2019-03-17 11:05:16 -05:00
|
|
|
$one->reconciled = $data['reconciled'] ?? false;
|
|
|
|
$two->reconciled = $data['reconciled'] ?? false;
|
|
|
|
|
|
|
|
// add foreign currency info to $one and $two if necessary.
|
2019-05-23 22:47:26 -05:00
|
|
|
if (null !== $foreignCurrency && null !== $foreignAmount
|
|
|
|
&& $foreignCurrency->id !== $currency->id
|
|
|
|
) {
|
2019-03-17 11:05:16 -05:00
|
|
|
$one->foreign_currency_id = $foreignCurrency->id;
|
|
|
|
$two->foreign_currency_id = $foreignCurrency->id;
|
2019-03-31 06:36:49 -05:00
|
|
|
$one->foreign_amount = app('steam')->negative($foreignAmount);
|
|
|
|
$two->foreign_amount = app('steam')->positive($foreignAmount);
|
2019-03-17 11:05:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$one->save();
|
|
|
|
$two->save();
|
|
|
|
|
2019-03-07 22:47:51 -06:00
|
|
|
return new Collection([$one, $two]);
|
2018-02-19 12:44:46 -06:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function setJournal(TransactionJournal $journal): void
|
|
|
|
{
|
|
|
|
$this->journal = $journal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
$this->accountRepository->setUser($user);
|
|
|
|
}
|
2019-03-31 06:36:49 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param NullArrayObject $data
|
|
|
|
*
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
private function validateAccounts(NullArrayObject $data): void
|
|
|
|
{
|
|
|
|
$transactionType = $data['type'] ?? 'invalid';
|
2019-04-06 01:10:50 -05:00
|
|
|
$this->accountValidator->setUser($this->journal->user);
|
2019-03-31 06:36:49 -05:00
|
|
|
$this->accountValidator->setTransactionType($transactionType);
|
|
|
|
|
|
|
|
// validate source account.
|
|
|
|
$sourceId = isset($data['source_id']) ? (int)$data['source_id'] : null;
|
|
|
|
$sourceName = $data['source_name'] ?? null;
|
|
|
|
$validSource = $this->accountValidator->validateSource($sourceId, $sourceName);
|
|
|
|
|
|
|
|
// do something with result:
|
|
|
|
if (false === $validSource) {
|
|
|
|
throw new FireflyException($this->accountValidator->sourceError);
|
|
|
|
}
|
2019-04-06 01:10:50 -05:00
|
|
|
Log::debug('Source seems valid.');
|
2019-03-31 06:36:49 -05:00
|
|
|
// validate destination account
|
|
|
|
$destinationId = isset($data['destination_id']) ? (int)$data['destination_id'] : null;
|
|
|
|
$destinationName = $data['destination_name'] ?? null;
|
|
|
|
$validDestination = $this->accountValidator->validateDestination($destinationId, $destinationName);
|
|
|
|
// do something with result:
|
|
|
|
if (false === $validDestination) {
|
|
|
|
throw new FireflyException($this->accountValidator->destError);
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|