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;
|
2018-02-16 09:43:25 -06:00
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2018-03-11 08:13:23 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2018-02-23 08:12:47 -06:00
|
|
|
use FireflyIII\Services\Internal\Support\TransactionServiceTrait;
|
2018-02-16 09:43:25 -06:00
|
|
|
use FireflyIII\User;
|
|
|
|
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
|
|
|
|
{
|
2018-02-23 08:12:47 -06:00
|
|
|
use TransactionServiceTrait;
|
|
|
|
|
2018-02-16 09:43:25 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Transaction
|
2018-05-06 13:42:07 -05:00
|
|
|
* @throws FireflyException
|
2018-02-16 09:43:25 -06:00
|
|
|
*/
|
2018-05-06 13:42:07 -05:00
|
|
|
public function create(array $data): ?Transaction
|
2018-02-16 09:43:25 -06:00
|
|
|
{
|
2018-05-10 02:10:16 -05:00
|
|
|
Log::debug('Start of TransactionFactory::create()');
|
|
|
|
$currencyId = $data['currency_id'] ?? null;
|
|
|
|
$currencyId = isset($data['currency']) ? $data['currency']->id : $currencyId;
|
2018-05-06 13:42:07 -05:00
|
|
|
if ('' === $data['amount']) {
|
2018-06-03 12:14:03 -05:00
|
|
|
Log::error('Empty string in data.', $data);
|
2018-06-01 15:04:52 -05:00
|
|
|
throw new FireflyException('Amount is an empty string, which Firefly III cannot handle. Apologies.'); // @codeCoverageIgnore
|
2018-05-06 13:42:07 -05:00
|
|
|
}
|
2018-05-10 02:10:16 -05:00
|
|
|
if (null === $currencyId) {
|
2018-06-01 15:04:52 -05:00
|
|
|
throw new FireflyException('Cannot store transaction without currency information.'); // @codeCoverageIgnore
|
2018-05-10 02:10:16 -05:00
|
|
|
}
|
|
|
|
$data['foreign_amount'] = '' === (string)$data['foreign_amount'] ? null : $data['foreign_amount'];
|
2018-05-23 00:38:03 -05:00
|
|
|
Log::debug(sprintf('Create transaction for account #%d ("%s") with amount %s', $data['account']->id, $data['account']->name, $data['amount']));
|
2018-02-21 13:34:24 -06:00
|
|
|
|
2018-02-19 12:44:46 -06:00
|
|
|
return Transaction::create(
|
|
|
|
[
|
|
|
|
'reconciled' => $data['reconciled'],
|
|
|
|
'account_id' => $data['account']->id,
|
|
|
|
'transaction_journal_id' => $data['transaction_journal']->id,
|
|
|
|
'description' => $data['description'],
|
2018-02-21 13:34:24 -06:00
|
|
|
'transaction_currency_id' => $currencyId,
|
2018-02-19 12:44:46 -06:00
|
|
|
'amount' => $data['amount'],
|
|
|
|
'foreign_amount' => $data['foreign_amount'],
|
|
|
|
'foreign_currency_id' => null,
|
|
|
|
'identifier' => $data['identifier'],
|
|
|
|
]
|
|
|
|
);
|
2018-02-16 09:43:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a pair of transactions based on the data given in the array.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Collection
|
2018-05-06 13:42:07 -05:00
|
|
|
* @throws FireflyException
|
2018-02-16 09:43:25 -06:00
|
|
|
*/
|
|
|
|
public function createPair(TransactionJournal $journal, array $data): Collection
|
|
|
|
{
|
2018-05-10 02:10:16 -05:00
|
|
|
Log::debug('Start of TransactionFactory::createPair()');
|
2018-02-16 09:43:25 -06:00
|
|
|
// all this data is the same for both transactions:
|
2018-02-18 09:35:26 -06:00
|
|
|
$currency = $this->findCurrency($data['currency_id'], $data['currency_code']);
|
|
|
|
$description = $journal->description === $data['description'] ? null : $data['description'];
|
2018-02-16 09:43:25 -06:00
|
|
|
|
|
|
|
// type of source account depends on journal type:
|
2018-06-06 14:23:00 -05:00
|
|
|
$sourceType = $this->accountType($journal, 'source');
|
2018-05-23 00:38:03 -05:00
|
|
|
Log::debug(sprintf('Expect source account to be of type %s', $sourceType));
|
2018-02-18 03:31:15 -06:00
|
|
|
$sourceAccount = $this->findAccount($sourceType, $data['source_id'], $data['source_name']);
|
2018-02-16 09:43:25 -06:00
|
|
|
|
|
|
|
// same for destination account:
|
2018-06-06 14:23:00 -05:00
|
|
|
$destinationType = $this->accountType($journal, 'destination');
|
2018-05-23 00:38:03 -05:00
|
|
|
Log::debug(sprintf('Expect source destination to be of type %s', $destinationType));
|
2018-02-18 03:31:15 -06:00
|
|
|
$destinationAccount = $this->findAccount($destinationType, $data['destination_id'], $data['destination_name']);
|
2018-02-16 09:43:25 -06:00
|
|
|
// first make a "negative" (source) transaction based on the data in the array.
|
2018-02-18 09:35:26 -06:00
|
|
|
$source = $this->create(
|
|
|
|
[
|
|
|
|
'description' => $description,
|
2018-04-02 07:42:07 -05:00
|
|
|
'amount' => app('steam')->negative((string)$data['amount']),
|
2018-02-18 09:35:26 -06:00
|
|
|
'foreign_amount' => null,
|
|
|
|
'currency' => $currency,
|
|
|
|
'account' => $sourceAccount,
|
|
|
|
'transaction_journal' => $journal,
|
|
|
|
'reconciled' => $data['reconciled'],
|
|
|
|
'identifier' => $data['identifier'],
|
|
|
|
]
|
|
|
|
);
|
2018-02-16 09:43:25 -06:00
|
|
|
// then make a "positive" transaction based on the data in the array.
|
2018-02-18 09:35:26 -06:00
|
|
|
$dest = $this->create(
|
|
|
|
[
|
|
|
|
'description' => $description,
|
2018-04-02 07:42:07 -05:00
|
|
|
'amount' => app('steam')->positive((string)$data['amount']),
|
2018-02-18 09:35:26 -06:00
|
|
|
'foreign_amount' => null,
|
|
|
|
'currency' => $currency,
|
|
|
|
'account' => $destinationAccount,
|
|
|
|
'transaction_journal' => $journal,
|
|
|
|
'reconciled' => $data['reconciled'],
|
|
|
|
'identifier' => $data['identifier'],
|
|
|
|
]
|
|
|
|
);
|
2018-02-19 12:44:46 -06:00
|
|
|
|
2018-02-18 09:35:26 -06:00
|
|
|
// set foreign currency
|
|
|
|
$foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']);
|
|
|
|
$this->setForeignCurrency($source, $foreign);
|
|
|
|
$this->setForeignCurrency($dest, $foreign);
|
|
|
|
|
|
|
|
// set foreign amount:
|
2018-04-02 07:42:07 -05:00
|
|
|
if (null !== $data['foreign_amount']) {
|
|
|
|
$this->setForeignAmount($source, app('steam')->negative((string)$data['foreign_amount']));
|
|
|
|
$this->setForeignAmount($dest, app('steam')->positive((string)$data['foreign_amount']));
|
2018-02-18 09:35:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// set budget:
|
2018-05-31 14:30:25 -05:00
|
|
|
if ($journal->transactionType->type !== TransactionType::WITHDRAWAL) {
|
2018-03-11 08:13:23 -05:00
|
|
|
$data['budget_id'] = null;
|
|
|
|
$data['budget_name'] = null;
|
|
|
|
}
|
|
|
|
|
2018-02-18 09:35:26 -06:00
|
|
|
$budget = $this->findBudget($data['budget_id'], $data['budget_name']);
|
|
|
|
$this->setBudget($source, $budget);
|
|
|
|
$this->setBudget($dest, $budget);
|
|
|
|
|
|
|
|
// set category
|
|
|
|
$category = $this->findCategory($data['category_id'], $data['category_name']);
|
|
|
|
$this->setCategory($source, $category);
|
|
|
|
$this->setCategory($dest, $category);
|
2018-02-16 09:57:46 -06:00
|
|
|
|
|
|
|
return new Collection([$source, $dest]);
|
2018-02-16 09:43:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2018-02-18 09:35:26 -06:00
|
|
|
|
|
|
|
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|