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-06-13 12:03:18 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
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;
|
2018-03-11 08:13:23 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2019-03-07 22:47:51 -06:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
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
|
|
|
|
{
|
2019-03-07 22:47:51 -06:00
|
|
|
/** @var AccountRepositoryInterface */
|
|
|
|
private $accountRepository;
|
|
|
|
/** @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);
|
2018-09-06 05:29:32 -05:00
|
|
|
}
|
|
|
|
|
2019-03-07 22:47:51 -06:00
|
|
|
//use TransactionServiceTrait;
|
|
|
|
|
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-07 22:47:51 -06:00
|
|
|
$result = Transaction::create(
|
2018-02-19 12:44:46 -06:00
|
|
|
[
|
2019-03-07 22:47:51 -06:00
|
|
|
'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,
|
2019-03-05 10:26:49 -06:00
|
|
|
'identifier' => 0,
|
2018-02-19 12:44:46 -06:00
|
|
|
]
|
|
|
|
);
|
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-07 22:47:51 -06:00
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
* @param array $data
|
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-07 22:47:51 -06:00
|
|
|
public function createPair(TransactionCurrency $currency, array $data): Collection
|
2018-02-16 09:43:25 -06:00
|
|
|
{
|
2019-03-07 22:47:51 -06:00
|
|
|
$sourceAccount = $this->getAccount('source', $data['source'], $data['source_id'], $data['source_name']);
|
|
|
|
$destinationAccount = $this->getAccount('destination', $data['destination'], $data['destination_id'], $data['destination_name']);
|
|
|
|
$amount = $this->getAmount($data['amount']);
|
2018-07-06 12:06:08 -05:00
|
|
|
|
2019-03-07 22:47:51 -06:00
|
|
|
$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-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
|
|
|
// Log::debug('Start of TransactionFactory::createPair()' );
|
|
|
|
//
|
|
|
|
// // type of source account and destination account depends on journal type:
|
|
|
|
// $sourceType = $this->accountType($journal, 'source');
|
|
|
|
// $destinationType = $this->accountType($journal, 'destination');
|
|
|
|
//
|
|
|
|
// Log::debug(sprintf('Journal is a %s.', $journal->transactionType->type));
|
|
|
|
// Log::debug(sprintf('Expect source account to be of type "%s"', $sourceType));
|
|
|
|
// Log::debug(sprintf('Expect source destination to be of type "%s"', $destinationType));
|
|
|
|
//
|
|
|
|
// // find source and destination account:
|
|
|
|
// $sourceAccount = $this->findAccount($sourceType, $data['source'], (int)$data['source_id'], $data['source_name']);
|
|
|
|
// $destinationAccount = $this->findAccount($destinationType, $data['destination'], (int)$data['destination_id'], $data['destination_name']);
|
|
|
|
//
|
|
|
|
// if (null === $sourceAccount || null === $destinationAccount) {
|
|
|
|
// $debugData = $data;
|
|
|
|
// $debugData['source_type'] = $sourceType;
|
|
|
|
// $debugData['dest_type'] = $destinationType;
|
|
|
|
// Log::error('Info about source/dest:', $debugData);
|
|
|
|
// throw new FireflyException('Could not determine source or destination account.');
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Log::debug(sprintf('Source type is "%s", destination type is "%s"', $sourceAccount->accountType->type, $destinationAccount->accountType->type));
|
|
|
|
//
|
|
|
|
// // based on the source type, destination type and transaction type, the system can start throwing FireflyExceptions.
|
|
|
|
// $this->validateTransaction($sourceAccount->accountType->type, $destinationAccount->accountType->type, $journal->transactionType->type);
|
|
|
|
// $source = $this->create(
|
|
|
|
// [
|
|
|
|
// 'description' => null,
|
|
|
|
// 'amount' => app('steam')->negative((string)$data['amount']),
|
|
|
|
// 'foreign_amount' => $data['foreign_amount'] ? app('steam')->negative((string)$data['foreign_amount']): null,
|
|
|
|
// 'currency' => $data['currency'],
|
|
|
|
// 'foreign_currency' => $data['foreign_currency'],
|
|
|
|
// 'account' => $sourceAccount,
|
|
|
|
// 'transaction_journal' => $journal,
|
|
|
|
// 'reconciled' => $data['reconciled'],
|
|
|
|
// ]
|
|
|
|
// );
|
|
|
|
// $dest = $this->create(
|
|
|
|
// [
|
|
|
|
// 'description' => null,
|
|
|
|
// 'amount' => app('steam')->positive((string)$data['amount']),
|
|
|
|
// 'foreign_amount' => $data['foreign_amount'] ? app('steam')->positive((string)$data['foreign_amount']): null,
|
|
|
|
// 'currency' => $data['currency'],
|
|
|
|
// 'foreign_currency' => $data['foreign_currency'],
|
|
|
|
// 'account' => $destinationAccount,
|
|
|
|
// 'transaction_journal' => $journal,
|
|
|
|
// 'reconciled' => $data['reconciled'],
|
|
|
|
// ]
|
|
|
|
// );
|
|
|
|
// if (null === $source || null === $dest) {
|
|
|
|
// throw new FireflyException('Could not create transactions.'); // @codeCoverageIgnore
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return new Collection([$source, $dest]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * @param array $data
|
|
|
|
// *
|
|
|
|
// * @return Transaction
|
|
|
|
// */
|
|
|
|
// public function create(array $data): ?Transaction
|
|
|
|
// {
|
|
|
|
// $data['foreign_amount'] = '' === (string)$data['foreign_amount'] ? null : $data['foreign_amount'];
|
|
|
|
// Log::debug(sprintf('Create transaction for account #%d ("%s") with amount %s', $data['account']->id, $data['account']->name, $data['amount']));
|
|
|
|
//
|
|
|
|
// return Transaction::create(
|
|
|
|
// [
|
|
|
|
// 'reconciled' => $data['reconciled'],
|
|
|
|
// 'account_id' => $data['account']->id,
|
|
|
|
// 'transaction_journal_id' => $data['transaction_journal']->id,
|
|
|
|
// 'description' => $data['description'],
|
|
|
|
// 'transaction_currency_id' => $data['currency']->id,
|
|
|
|
// 'amount' => $data['amount'],
|
|
|
|
// 'foreign_amount' => $data['foreign_amount'],
|
|
|
|
// 'foreign_currency_id' => $data['foreign_currency'] ? $data['foreign_currency']->id : null,
|
|
|
|
// 'identifier' => 0,
|
|
|
|
// ]
|
|
|
|
// );
|
|
|
|
// }
|
2018-02-18 09:35:26 -06:00
|
|
|
|
2019-03-07 22:47:51 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*/
|
|
|
|
public function setJournal(TransactionJournal $journal): void
|
|
|
|
{
|
|
|
|
$this->journal = $journal;
|
2018-02-16 09:43:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
2018-07-06 12:06:08 -05:00
|
|
|
public function setUser(User $user): void
|
2018-02-16 09:43:25 -06:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
2019-03-07 22:47:51 -06:00
|
|
|
$this->accountRepository->setUser($user);
|
2018-02-16 09:43:25 -06:00
|
|
|
}
|
2018-02-18 09:35:26 -06:00
|
|
|
|
2018-08-25 14:33:22 -05:00
|
|
|
/**
|
2019-03-07 22:47:51 -06:00
|
|
|
* @param string $direction
|
|
|
|
* @param Account|null $source
|
|
|
|
* @param int|null $sourceId
|
|
|
|
* @param string|null $sourceName
|
2018-08-25 14:33:22 -05:00
|
|
|
*
|
2019-03-07 22:47:51 -06:00
|
|
|
* @return Account
|
2018-08-25 14:33:22 -05:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
2019-03-07 22:47:51 -06:00
|
|
|
private function getAccount(string $direction, ?Account $source, ?int $sourceId, ?string $sourceName): Account
|
2018-08-25 14:33:22 -05:00
|
|
|
{
|
2019-03-07 22:47:51 -06:00
|
|
|
// expected type of source account, in order of preference
|
|
|
|
$array = [
|
|
|
|
'source' => [
|
|
|
|
TransactionType::WITHDRAWAL => [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
|
|
|
|
TransactionType::DEPOSIT => [AccountType::REVENUE, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
|
|
|
|
TransactionType::TRANSFER => [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
|
|
|
|
TransactionType::OPENING_BALANCE => [AccountType::INITIAL_BALANCE, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT,
|
|
|
|
AccountType::MORTGAGE],
|
|
|
|
TransactionType::RECONCILIATION => [AccountType::RECONCILIATION, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT,
|
|
|
|
AccountType::MORTGAGE],
|
|
|
|
],
|
|
|
|
'destination' => [
|
|
|
|
TransactionType::WITHDRAWAL => [AccountType::EXPENSE, AccountType::ASSET],
|
|
|
|
TransactionType::DEPOSIT => [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
|
|
|
|
TransactionType::TRANSFER => [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE],
|
|
|
|
TransactionType::OPENING_BALANCE => [AccountType::INITIAL_BALANCE, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT,
|
|
|
|
AccountType::MORTGAGE],
|
|
|
|
TransactionType::RECONCILIATION => [AccountType::RECONCILIATION, AccountType::ASSET, AccountType::LOAN, AccountType::DEBT,
|
|
|
|
AccountType::MORTGAGE],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$expectedTypes = $array[$direction];
|
|
|
|
unset($array);
|
|
|
|
|
|
|
|
// and now try to find it, based on the type of transaction.
|
|
|
|
$transactionType = $this->journal->transactionType->type;
|
|
|
|
Log::debug(
|
|
|
|
sprintf(
|
|
|
|
'Based on the fact that the transaction is a %s, the %s account should be in %s', $transactionType, $direction,
|
|
|
|
implode(', ', $expectedTypes[$transactionType])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// first attempt, check the "source" object.
|
|
|
|
if (null !== $source && $source->user_id === $this->user->id && \in_array($source->accountType->type, $expectedTypes[$transactionType], true)) {
|
|
|
|
Log::debug(sprintf('Found "account" object for %s: #%d, %s', $direction, $source->id, $source->name));
|
|
|
|
|
|
|
|
return $source;
|
|
|
|
}
|
|
|
|
|
|
|
|
// second attempt, find by ID.
|
|
|
|
if (null !== $sourceId) {
|
|
|
|
$source = $this->accountRepository->findNull($sourceId);
|
|
|
|
if (null !== $source && \in_array($source->accountType->type, $expectedTypes[$transactionType], true)) {
|
|
|
|
Log::debug(sprintf('Found "account_id" object for %s: #%d, %s', $direction, $source->id, $source->name));
|
|
|
|
|
|
|
|
return $source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// third attempt, find by name.
|
|
|
|
if (null !== $sourceName) {
|
|
|
|
// find by preferred type.
|
|
|
|
$source = $this->accountRepository->findByName($sourceName, [$expectedTypes[$transactionType][0]]);
|
|
|
|
// or any type.
|
|
|
|
$source = $source ?? $this->accountRepository->findByName($sourceName, $expectedTypes[$transactionType]);
|
|
|
|
|
|
|
|
if (null !== $source) {
|
|
|
|
Log::debug(sprintf('Found "account_name" object for %s: #%d, %s', $direction, $source->id, $source->name));
|
|
|
|
|
|
|
|
return $source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// final attempt, create it.
|
|
|
|
$preferredType = $expectedTypes[$transactionType][0];
|
|
|
|
if (AccountType::ASSET === $preferredType) {
|
|
|
|
throw new FireflyException(sprintf('TransactionFactory: Cannot create asset account with ID #%d or name "%s".', $sourceId, $sourceName));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->accountRepository->store(
|
|
|
|
[
|
|
|
|
'account_type_id' => null,
|
|
|
|
'accountType' => $preferredType,
|
|
|
|
'name' => $sourceName,
|
|
|
|
'active' => true,
|
|
|
|
'iban' => null,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $amount
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
private function getAmount(string $amount): string
|
|
|
|
{
|
|
|
|
if ('' === $amount) {
|
|
|
|
throw new FireflyException(sprintf('The amount cannot be an empty string: "%s"', $amount));
|
2018-08-25 14:33:22 -05:00
|
|
|
}
|
2019-03-07 22:47:51 -06:00
|
|
|
if (0 === bccomp('0', $amount)) {
|
|
|
|
throw new FireflyException(sprintf('The amount seems to be zero: "%s"', $amount));
|
2018-08-25 14:33:22 -05:00
|
|
|
}
|
2019-03-07 22:47:51 -06:00
|
|
|
|
|
|
|
return $amount;
|
2018-08-25 14:33:22 -05:00
|
|
|
}
|
2019-03-07 22:47:51 -06:00
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * @param string $sourceType
|
|
|
|
// * @param string $destinationType
|
|
|
|
// * @param string $transactionType
|
|
|
|
// *
|
|
|
|
// * @throws FireflyException
|
|
|
|
// */
|
|
|
|
// private function validateTransaction(string $sourceType, string $destinationType, string $transactionType): void
|
|
|
|
// {
|
|
|
|
// // throw big fat error when source type === dest type and it's not a transfer or reconciliation.
|
|
|
|
// if ($sourceType === $destinationType && $transactionType !== TransactionType::TRANSFER) {
|
|
|
|
// throw new FireflyException(sprintf('Source and destination account cannot be both of the type "%s"', $destinationType));
|
|
|
|
// }
|
|
|
|
// // source must be in this list AND dest must be in this list:
|
|
|
|
// $list = [AccountType::DEFAULT, AccountType::ASSET, AccountType::CREDITCARD, AccountType::CASH, AccountType::DEBT, AccountType::MORTGAGE,
|
|
|
|
// AccountType::LOAN, AccountType::MORTGAGE];
|
|
|
|
// if (
|
|
|
|
// !\in_array($sourceType, $list, true)
|
|
|
|
// && !\in_array($destinationType, $list, true)) {
|
|
|
|
// throw new FireflyException(sprintf('At least one of the accounts must be an asset account (%s, %s).', $sourceType, $destinationType));
|
|
|
|
// }
|
|
|
|
// }
|
2018-08-25 14:33:22 -05:00
|
|
|
|
2018-02-18 09:35:26 -06:00
|
|
|
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|