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
|
|
|
/**
|
|
|
|
* TransactionJournalFactory.php
|
2020-02-16 07:00:57 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-02-16 09:43:25 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-16 09:43:25 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2018-02-16 09:43:25 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-02-16 09:43:25 -06:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-02-16 09:43:25 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-02-16 09:43:25 -06:00
|
|
|
*/
|
|
|
|
|
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 Carbon\Carbon;
|
|
|
|
use Exception;
|
2019-10-26 00:49:12 -05:00
|
|
|
use FireflyIII\Exceptions\DuplicateTransactionException;
|
2019-03-31 06:36:49 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2019-06-04 13:42:11 -05:00
|
|
|
use FireflyIII\Models\Account;
|
2020-03-11 23:06:42 -05:00
|
|
|
use FireflyIII\Models\Transaction;
|
2019-06-04 13:42:11 -05:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2018-02-16 09:43:25 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2019-10-26 00:49:12 -05:00
|
|
|
use FireflyIII\Models\TransactionJournalMeta;
|
2019-03-17 11:05:16 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2019-06-04 13:42:11 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2019-03-17 11:05:16 -05:00
|
|
|
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
2019-03-07 22:47:51 -06:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2019-03-17 11:05:16 -05:00
|
|
|
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
2019-03-07 22:47:51 -06:00
|
|
|
use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
|
2020-03-11 23:06:42 -05:00
|
|
|
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
|
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-06-04 13:42:11 -05:00
|
|
|
use FireflyIII\Validation\AccountValidator;
|
2019-03-05 10:26:49 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2018-02-23 08:12:47 -06:00
|
|
|
use Log;
|
2018-02-16 09:43:25 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TransactionJournalFactory
|
|
|
|
*/
|
|
|
|
class TransactionJournalFactory
|
|
|
|
{
|
2019-04-06 01:10:50 -05:00
|
|
|
use JournalServiceTrait;
|
|
|
|
|
2019-06-04 13:42:11 -05:00
|
|
|
/** @var AccountRepositoryInterface */
|
|
|
|
private $accountRepository;
|
2019-08-30 00:28:48 -05:00
|
|
|
/** @var AccountValidator */
|
|
|
|
private $accountValidator;
|
2019-03-17 11:05:16 -05:00
|
|
|
/** @var BillRepositoryInterface */
|
|
|
|
private $billRepository;
|
2019-03-07 22:47:51 -06:00
|
|
|
/** @var CurrencyRepositoryInterface */
|
|
|
|
private $currencyRepository;
|
2019-03-17 11:05:16 -05:00
|
|
|
/** @var array */
|
|
|
|
private $fields;
|
2019-03-18 10:52:49 -05:00
|
|
|
/** @var PiggyBankEventFactory */
|
|
|
|
private $piggyEventFactory;
|
2019-03-17 11:05:16 -05:00
|
|
|
/** @var PiggyBankRepositoryInterface */
|
|
|
|
private $piggyRepository;
|
2019-03-07 22:47:51 -06:00
|
|
|
/** @var TransactionFactory */
|
|
|
|
private $transactionFactory;
|
|
|
|
/** @var TransactionTypeRepositoryInterface */
|
|
|
|
private $typeRepository;
|
2018-09-22 23:53:15 -05:00
|
|
|
/** @var User The user */
|
|
|
|
private $user;
|
2019-10-26 00:49:12 -05:00
|
|
|
/** @var bool */
|
|
|
|
private $errorOnHash;
|
2018-09-22 23:53:15 -05:00
|
|
|
|
2018-09-06 05:29:32 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
2019-03-17 11:05:16 -05:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2019-06-16 06:15:32 -05:00
|
|
|
* @codeCoverageIgnore
|
2018-09-06 05:29:32 -05:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2019-10-26 00:49:12 -05:00
|
|
|
$this->errorOnHash = false;
|
|
|
|
$this->fields = [
|
2019-03-31 06:36:49 -05:00
|
|
|
// sepa
|
|
|
|
'sepa_cc', 'sepa_ct_op', 'sepa_ct_id',
|
|
|
|
'sepa_db', 'sepa_country', 'sepa_ep',
|
|
|
|
'sepa_ci', 'sepa_batch_id',
|
|
|
|
|
|
|
|
// dates
|
|
|
|
'interest_date', 'book_date', 'process_date',
|
|
|
|
'due_date', 'payment_date', 'invoice_date',
|
|
|
|
|
|
|
|
// others
|
2019-04-06 01:10:50 -05:00
|
|
|
'recurrence_id', 'internal_reference', 'bunq_payment_id',
|
2019-03-31 06:36:49 -05:00
|
|
|
'import_hash', 'import_hash_v2', 'external_id', 'original_source'];
|
2019-03-17 11:05:16 -05:00
|
|
|
|
|
|
|
|
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-03-31 06:36:49 -05:00
|
|
|
|
2019-03-07 22:47:51 -06:00
|
|
|
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
|
|
|
|
$this->typeRepository = app(TransactionTypeRepositoryInterface::class);
|
|
|
|
$this->transactionFactory = app(TransactionFactory::class);
|
2019-03-17 11:05:16 -05:00
|
|
|
$this->billRepository = app(BillRepositoryInterface::class);
|
|
|
|
$this->budgetRepository = app(BudgetRepositoryInterface::class);
|
|
|
|
$this->categoryRepository = app(CategoryRepositoryInterface::class);
|
|
|
|
$this->piggyRepository = app(PiggyBankRepositoryInterface::class);
|
2019-03-18 10:52:49 -05:00
|
|
|
$this->piggyEventFactory = app(PiggyBankEventFactory::class);
|
|
|
|
$this->tagFactory = app(TagFactory::class);
|
2019-06-04 13:42:11 -05:00
|
|
|
$this->accountValidator = app(AccountValidator::class);
|
|
|
|
$this->accountRepository = app(AccountRepositoryInterface::class);
|
2018-09-06 05:29:32 -05:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:43:25 -06:00
|
|
|
/**
|
2019-03-31 06:36:49 -05:00
|
|
|
* Store a new (set of) transaction journals.
|
2018-07-06 12:06:08 -05:00
|
|
|
*
|
2018-02-16 09:43:25 -06:00
|
|
|
* @param array $data
|
|
|
|
*
|
2019-03-31 06:36:49 -05:00
|
|
|
* @return Collection
|
2019-10-26 00:49:12 -05:00
|
|
|
* @throws DuplicateTransactionException
|
2020-03-11 23:06:42 -05:00
|
|
|
* @throws FireflyException
|
2018-02-16 09:43:25 -06:00
|
|
|
*/
|
2019-03-31 06:36:49 -05:00
|
|
|
public function create(array $data): Collection
|
2018-02-16 09:43:25 -06:00
|
|
|
{
|
2019-03-31 06:36:49 -05:00
|
|
|
// convert to special object.
|
2020-02-22 04:05:16 -06:00
|
|
|
$dataObject = new NullArrayObject($data);
|
2019-03-31 06:36:49 -05:00
|
|
|
|
2018-02-23 08:12:47 -06:00
|
|
|
Log::debug('Start of TransactionJournalFactory::create()');
|
2019-03-07 22:47:51 -06:00
|
|
|
$collection = new Collection;
|
2020-02-22 04:05:16 -06:00
|
|
|
$transactions = $dataObject['transactions'] ?? [];
|
2019-03-31 06:36:49 -05:00
|
|
|
if (0 === count($transactions)) {
|
2019-03-18 10:52:49 -05:00
|
|
|
Log::error('There are no transactions in the array, the TransactionJournalFactory cannot continue.');
|
2019-03-17 11:05:16 -05:00
|
|
|
|
|
|
|
return new Collection;
|
|
|
|
}
|
2020-03-11 23:06:42 -05:00
|
|
|
try {
|
|
|
|
/** @var array $row */
|
|
|
|
foreach ($transactions as $index => $row) {
|
|
|
|
Log::debug(sprintf('Now creating journal %d/%d', $index + 1, count($transactions)));
|
2020-02-22 04:05:16 -06:00
|
|
|
$journal = $this->createJournal(new NullArrayObject($row));
|
2020-03-11 23:06:42 -05:00
|
|
|
if (null !== $journal) {
|
|
|
|
$collection->push($journal);
|
|
|
|
}
|
|
|
|
if (null === $journal) {
|
|
|
|
Log::error('The createJournal() method returned NULL. This may indicate an error.');
|
|
|
|
}
|
2019-08-16 10:54:38 -05:00
|
|
|
}
|
2020-03-11 23:06:42 -05:00
|
|
|
} catch (DuplicateTransactionException $e) {
|
|
|
|
Log::warning('TransactionJournalFactory::create() caught a duplicate journal in createJournal()');
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
Log::error($e->getTraceAsString());
|
|
|
|
$this->forceDeleteOnError($collection);
|
|
|
|
throw new DuplicateTransactionException($e->getMessage());
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
Log::warning('TransactionJournalFactory::create() caught an exception.');
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
Log::error($e->getTraceAsString());
|
|
|
|
$this->forceDeleteOnError($collection);
|
|
|
|
throw new FireflyException($e->getMessage());
|
2019-03-17 11:05:16 -05:00
|
|
|
}
|
2019-03-23 02:10:59 -05:00
|
|
|
|
2019-03-31 06:36:49 -05:00
|
|
|
return $collection;
|
2018-02-16 09:43:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the user.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user): void
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
2019-03-07 22:47:51 -06:00
|
|
|
$this->currencyRepository->setUser($this->user);
|
2019-04-06 01:10:50 -05:00
|
|
|
$this->tagFactory->setUser($user);
|
2019-03-07 22:47:51 -06:00
|
|
|
$this->transactionFactory->setUser($this->user);
|
2019-03-17 11:05:16 -05:00
|
|
|
$this->billRepository->setUser($this->user);
|
|
|
|
$this->budgetRepository->setUser($this->user);
|
|
|
|
$this->categoryRepository->setUser($this->user);
|
|
|
|
$this->piggyRepository->setUser($this->user);
|
2019-06-04 13:42:11 -05:00
|
|
|
$this->accountRepository->setUser($this->user);
|
2019-03-17 11:05:16 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 06:15:32 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
2019-08-30 00:28:48 -05:00
|
|
|
* @param NullArrayObject $data
|
|
|
|
* @param string $field
|
2019-06-16 06:15:32 -05:00
|
|
|
*/
|
|
|
|
protected function storeMeta(TransactionJournal $journal, NullArrayObject $data, string $field): void
|
|
|
|
{
|
|
|
|
$set = [
|
|
|
|
'journal' => $journal,
|
|
|
|
'name' => $field,
|
|
|
|
'data' => (string)($data[$field] ?? ''),
|
|
|
|
];
|
|
|
|
|
|
|
|
Log::debug(sprintf('Going to store meta-field "%s", with value "%s".', $set['name'], $set['data']));
|
|
|
|
|
|
|
|
/** @var TransactionJournalMetaFactory $factory */
|
|
|
|
$factory = app(TransactionJournalMetaFactory::class);
|
|
|
|
$factory->updateOrCreate($set);
|
|
|
|
}
|
|
|
|
|
2019-03-31 06:36:49 -05:00
|
|
|
/**
|
|
|
|
* @param NullArrayObject $row
|
|
|
|
*
|
|
|
|
* @return TransactionJournal|null
|
2020-03-11 23:06:42 -05:00
|
|
|
* @throws FireflyException
|
2019-10-26 00:49:12 -05:00
|
|
|
* @throws DuplicateTransactionException
|
2019-03-31 06:36:49 -05:00
|
|
|
*/
|
|
|
|
private function createJournal(NullArrayObject $row): ?TransactionJournal
|
|
|
|
{
|
|
|
|
$row['import_hash_v2'] = $this->hashArray($row);
|
|
|
|
|
2019-10-26 00:49:12 -05:00
|
|
|
$this->errorIfDuplicate($row['import_hash_v2']);
|
|
|
|
|
2019-06-04 13:42:11 -05:00
|
|
|
/** Some basic fields */
|
2019-03-31 06:36:49 -05:00
|
|
|
$type = $this->typeRepository->findTransactionType(null, $row['type']);
|
|
|
|
$carbon = $row['date'] ?? new Carbon;
|
2019-04-06 01:10:50 -05:00
|
|
|
$order = $row['order'] ?? 0;
|
2019-03-31 06:36:49 -05:00
|
|
|
$currency = $this->currencyRepository->findCurrency((int)$row['currency_id'], $row['currency_code']);
|
2019-04-06 01:10:50 -05:00
|
|
|
$foreignCurrency = $this->currencyRepository->findCurrencyNull($row['foreign_currency_id'], $row['foreign_currency_code']);
|
2019-03-31 06:36:49 -05:00
|
|
|
$bill = $this->billRepository->findBill((int)$row['bill_id'], $row['bill_name']);
|
|
|
|
$billId = TransactionType::WITHDRAWAL === $type->type && null !== $bill ? $bill->id : null;
|
|
|
|
$description = app('steam')->cleanString((string)$row['description']);
|
|
|
|
|
|
|
|
/** Manipulate basic fields */
|
|
|
|
$carbon->setTimezone(config('app.timezone'));
|
|
|
|
|
2019-06-04 13:42:11 -05:00
|
|
|
/** Get source + destination account */
|
2019-10-13 04:50:04 -05:00
|
|
|
Log::debug(sprintf('Currency is #%d (%s)', $currency->id, $currency->code));
|
2019-07-27 06:54:06 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
// validate source and destination using a new Validator.
|
|
|
|
$this->validateAccounts($row);
|
|
|
|
} catch (FireflyException $e) {
|
2019-08-16 10:54:38 -05:00
|
|
|
Log::error('Could not validate source or destination.');
|
2019-07-27 06:54:06 -05:00
|
|
|
Log::error($e->getMessage());
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-11 23:06:42 -05:00
|
|
|
/** create or get source and destination accounts */
|
|
|
|
$sourceInfo = [
|
|
|
|
'id' => (int)$row['source_id'],
|
|
|
|
'name' => $row['source_name'],
|
|
|
|
'iban' => $row['source_iban'],
|
|
|
|
'number' => $row['source_number'],
|
|
|
|
'bic' => $row['source_bic'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$destInfo = [
|
|
|
|
'id' => (int)$row['destination_id'],
|
|
|
|
'name' => $row['destination_name'],
|
|
|
|
'iban' => $row['destination_iban'],
|
|
|
|
'number' => $row['destination_number'],
|
|
|
|
'bic' => $row['destination_bic'],
|
|
|
|
];
|
|
|
|
Log::debug('Source info:', $sourceInfo);
|
|
|
|
Log::debug('Destination info:', $destInfo);
|
|
|
|
|
|
|
|
$sourceAccount = $this->getAccount($type->type, 'source', $sourceInfo);
|
|
|
|
$destinationAccount = $this->getAccount($type->type, 'destination', $destInfo);
|
2019-06-04 13:42:11 -05:00
|
|
|
|
2019-08-12 10:10:58 -05:00
|
|
|
// TODO AFTER 4.8,0 better handling below:
|
2019-06-16 06:15:32 -05:00
|
|
|
|
2019-06-04 13:42:11 -05:00
|
|
|
/** double check currencies. */
|
2019-06-07 23:19:21 -05:00
|
|
|
$sourceCurrency = $currency;
|
|
|
|
$destCurrency = $currency;
|
|
|
|
$sourceForeignCurrency = $foreignCurrency;
|
|
|
|
$destForeignCurrency = $foreignCurrency;
|
2019-06-04 13:42:11 -05:00
|
|
|
|
2019-08-11 10:32:00 -05:00
|
|
|
if (TransactionType::WITHDRAWAL === $type->type) {
|
2019-06-04 13:42:11 -05:00
|
|
|
// make sure currency is correct.
|
|
|
|
$currency = $this->getCurrency($currency, $sourceAccount);
|
|
|
|
// make sure foreign currency != currency.
|
|
|
|
if (null !== $foreignCurrency && $foreignCurrency->id === $currency->id) {
|
|
|
|
$foreignCurrency = null;
|
|
|
|
}
|
|
|
|
$sourceCurrency = $currency;
|
|
|
|
$destCurrency = $currency;
|
|
|
|
$sourceForeignCurrency = $foreignCurrency;
|
|
|
|
$destForeignCurrency = $foreignCurrency;
|
|
|
|
}
|
2019-08-11 10:32:00 -05:00
|
|
|
if (TransactionType::DEPOSIT === $type->type) {
|
2019-06-04 13:42:11 -05:00
|
|
|
// make sure currency is correct.
|
|
|
|
$currency = $this->getCurrency($currency, $destinationAccount);
|
|
|
|
// make sure foreign currency != currency.
|
|
|
|
if (null !== $foreignCurrency && $foreignCurrency->id === $currency->id) {
|
|
|
|
$foreignCurrency = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sourceCurrency = $currency;
|
|
|
|
$destCurrency = $currency;
|
|
|
|
$sourceForeignCurrency = $foreignCurrency;
|
|
|
|
$destForeignCurrency = $foreignCurrency;
|
|
|
|
}
|
|
|
|
|
2019-08-09 13:33:57 -05:00
|
|
|
if (TransactionType::TRANSFER === $type->type) {
|
2019-06-04 13:42:11 -05:00
|
|
|
// get currencies
|
|
|
|
$currency = $this->getCurrency($currency, $sourceAccount);
|
|
|
|
$foreignCurrency = $this->getCurrency($foreignCurrency, $destinationAccount);
|
|
|
|
|
|
|
|
$sourceCurrency = $currency;
|
2019-08-23 06:53:05 -05:00
|
|
|
$destCurrency = $currency;
|
2019-06-04 13:42:11 -05:00
|
|
|
$sourceForeignCurrency = $foreignCurrency;
|
2019-08-23 06:53:05 -05:00
|
|
|
$destForeignCurrency = $foreignCurrency;
|
2019-06-04 13:42:11 -05:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:28:48 -05:00
|
|
|
$description = '' === $description ? '(empty description)' : $description;
|
|
|
|
$description = substr($description, 0, 255);
|
|
|
|
|
|
|
|
|
2019-03-31 06:36:49 -05:00
|
|
|
/** Create a basic journal. */
|
|
|
|
$journal = TransactionJournal::create(
|
|
|
|
[
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'transaction_type_id' => $type->id,
|
|
|
|
'bill_id' => $billId,
|
|
|
|
'transaction_currency_id' => $currency->id,
|
2019-09-21 00:33:13 -05:00
|
|
|
'description' => substr($description,0,1000),
|
2019-03-31 06:36:49 -05:00
|
|
|
'date' => $carbon->format('Y-m-d H:i:s'),
|
2019-04-06 01:10:50 -05:00
|
|
|
'order' => $order,
|
2019-03-31 06:36:49 -05:00
|
|
|
'tag_count' => 0,
|
|
|
|
'completed' => 0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
Log::debug(sprintf('Created new journal #%d: "%s"', $journal->id, $journal->description));
|
|
|
|
|
|
|
|
/** Create two transactions. */
|
2019-06-04 13:42:11 -05:00
|
|
|
/** @var TransactionFactory $transactionFactory */
|
|
|
|
$transactionFactory = app(TransactionFactory::class);
|
|
|
|
$transactionFactory->setUser($this->user);
|
|
|
|
$transactionFactory->setJournal($journal);
|
|
|
|
$transactionFactory->setAccount($sourceAccount);
|
|
|
|
$transactionFactory->setCurrency($sourceCurrency);
|
|
|
|
$transactionFactory->setForeignCurrency($sourceForeignCurrency);
|
|
|
|
$transactionFactory->setReconciled($row['reconciled'] ?? false);
|
2020-03-11 23:06:42 -05:00
|
|
|
try {
|
|
|
|
$negative = $transactionFactory->createNegative((string)$row['amount'], (string)$row['foreign_amount']);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
Log::error('Exception creating negative transaction.');
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
Log::error($e->getTraceAsString());
|
|
|
|
$this->forceDeleteOnError(new Collection([$journal]));
|
|
|
|
throw new FireflyException($e->getMessage());
|
|
|
|
}
|
2019-06-04 13:42:11 -05:00
|
|
|
|
|
|
|
// and the destination one:
|
|
|
|
/** @var TransactionFactory $transactionFactory */
|
|
|
|
$transactionFactory = app(TransactionFactory::class);
|
|
|
|
$transactionFactory->setUser($this->user);
|
|
|
|
$transactionFactory->setJournal($journal);
|
|
|
|
$transactionFactory->setAccount($destinationAccount);
|
|
|
|
$transactionFactory->setCurrency($destCurrency);
|
|
|
|
$transactionFactory->setForeignCurrency($destForeignCurrency);
|
|
|
|
$transactionFactory->setReconciled($row['reconciled'] ?? false);
|
2020-03-11 23:06:42 -05:00
|
|
|
try {
|
|
|
|
$transactionFactory->createPositive((string)$row['amount'], (string)$row['foreign_amount']);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
Log::error('Exception creating positive transaction.');
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
Log::error($e->getTraceAsString());
|
|
|
|
Log::warning('Delete negative transaction.');
|
|
|
|
$this->forceTrDelete($negative);
|
|
|
|
$this->forceDeleteOnError(new Collection([$journal]));
|
|
|
|
throw new FireflyException($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2019-03-31 06:36:49 -05:00
|
|
|
|
|
|
|
// verify that journal has two transactions. Otherwise, delete and cancel.
|
2019-06-16 06:15:32 -05:00
|
|
|
// TODO this can't be faked so it can't be tested.
|
2019-08-30 00:28:48 -05:00
|
|
|
// $count = $journal->transactions()->count();
|
|
|
|
// if (2 !== $count) {
|
|
|
|
// // @codeCoverageIgnoreStart
|
|
|
|
// Log::error(sprintf('The journal unexpectedly has %d transaction(s). This is not OK. Cancel operation.', $count));
|
|
|
|
// try {
|
|
|
|
// $journal->delete();
|
|
|
|
// } catch (Exception $e) {
|
|
|
|
// Log::debug(sprintf('Dont care: %s.', $e->getMessage()));
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return null;
|
|
|
|
// // @codeCoverageIgnoreEnd
|
|
|
|
// }
|
2019-03-31 06:36:49 -05:00
|
|
|
$journal->completed = true;
|
|
|
|
$journal->save();
|
|
|
|
|
|
|
|
/** Link all other data to the journal. */
|
|
|
|
|
|
|
|
/** Link budget */
|
|
|
|
$this->storeBudget($journal, $row);
|
|
|
|
|
|
|
|
/** Link category */
|
|
|
|
$this->storeCategory($journal, $row);
|
|
|
|
|
|
|
|
/** Set notes */
|
2019-04-06 01:10:50 -05:00
|
|
|
$this->storeNotes($journal, $row['notes']);
|
2019-03-31 06:36:49 -05:00
|
|
|
|
|
|
|
/** Set piggy bank */
|
|
|
|
$this->storePiggyEvent($journal, $row);
|
|
|
|
|
|
|
|
/** Set tags */
|
|
|
|
$this->storeTags($journal, $row['tags']);
|
|
|
|
|
|
|
|
/** set all meta fields */
|
|
|
|
$this->storeMetaFields($journal, $row);
|
|
|
|
|
|
|
|
return $journal;
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:49:12 -05:00
|
|
|
/**
|
|
|
|
* If this transaction already exists, throw an error.
|
|
|
|
*
|
|
|
|
* @param string $hash
|
|
|
|
*
|
|
|
|
* @throws DuplicateTransactionException
|
|
|
|
*/
|
|
|
|
private function errorIfDuplicate(string $hash): void
|
|
|
|
{
|
2020-02-22 04:05:16 -06:00
|
|
|
Log::debug(sprintf('In errorIfDuplicate(%s)', $hash));
|
2019-10-26 00:49:12 -05:00
|
|
|
if (false === $this->errorOnHash) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$result = null;
|
|
|
|
if ($this->errorOnHash) {
|
2020-02-22 04:05:16 -06:00
|
|
|
Log::debug('Will verify duplicate!');
|
2019-10-26 00:49:12 -05:00
|
|
|
/** @var TransactionJournalMeta $result */
|
|
|
|
$result = TransactionJournalMeta::where('data', json_encode($hash, JSON_THROW_ON_ERROR))
|
|
|
|
->with(['transactionJournal', 'transactionJournal.transactionGroup'])
|
|
|
|
->first();
|
|
|
|
}
|
|
|
|
if (null !== $result) {
|
2020-02-22 04:05:16 -06:00
|
|
|
Log::warning('Found a duplicate!');
|
2019-10-26 00:49:12 -05:00
|
|
|
throw new DuplicateTransactionException(sprintf('Duplicate of transaction #%d.', $result->transactionJournal->transaction_group_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 23:06:42 -05:00
|
|
|
/**
|
|
|
|
* Force the deletion of an entire set of transaction journals and their meta object in case of
|
|
|
|
* an error creating a group.
|
|
|
|
*
|
|
|
|
* @param Collection $collection
|
|
|
|
*/
|
|
|
|
private function forceDeleteOnError(Collection $collection): void
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('forceDeleteOnError on collection size %d item(s)', $collection->count()));
|
|
|
|
$service = app(JournalDestroyService::class);
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
foreach ($collection as $journal) {
|
|
|
|
Log::debug(sprintf('forceDeleteOnError on journal #%d', $journal->id));
|
|
|
|
$service->destroy($journal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Transaction $transaction
|
|
|
|
*/
|
|
|
|
private function forceTrDelete(Transaction $transaction): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$transaction->delete();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
Log::error($e->getTraceAsString());
|
|
|
|
Log::error('Could not delete negative transaction.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 00:28:48 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionCurrency|null $currency
|
|
|
|
* @param Account $account
|
|
|
|
*
|
|
|
|
* @return TransactionCurrency
|
|
|
|
*/
|
|
|
|
private function getCurrency(?TransactionCurrency $currency, Account $account): TransactionCurrency
|
|
|
|
{
|
|
|
|
$preference = $this->accountRepository->getAccountCurrency($account);
|
|
|
|
if (null === $preference && null === $currency) {
|
|
|
|
// return user's default:
|
|
|
|
return app('amount')->getDefaultCurrencyByUser($this->user);
|
|
|
|
}
|
2019-10-13 04:50:04 -05:00
|
|
|
$result = $preference ?? $currency;
|
|
|
|
Log::debug(sprintf('Currency is now #%d (%s) because of account #%d (%s)', $result->id, $result->code, $account->id, $account->name));
|
2019-08-30 00:28:48 -05:00
|
|
|
|
2019-10-13 04:50:04 -05:00
|
|
|
return $result;
|
2019-08-30 00:28:48 -05:00
|
|
|
}
|
|
|
|
|
2019-03-31 06:36:49 -05:00
|
|
|
/**
|
|
|
|
* @param NullArrayObject $row
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function hashArray(NullArrayObject $row): string
|
|
|
|
{
|
2019-08-09 11:35:34 -05:00
|
|
|
$dataRow = $row->getArrayCopy();
|
|
|
|
|
|
|
|
unset($dataRow['import_hash_v2'], $dataRow['original_source']);
|
2019-08-30 00:28:48 -05:00
|
|
|
$json = json_encode($dataRow);
|
2019-03-31 06:36:49 -05:00
|
|
|
if (false === $json) {
|
2019-06-16 06:15:32 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-03-31 06:36:49 -05:00
|
|
|
$json = json_encode((string)microtime());
|
2019-08-09 11:35:34 -05:00
|
|
|
Log::error(sprintf('Could not hash the original row! %s', json_last_error_msg()), $dataRow);
|
2019-06-16 06:15:32 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2019-03-31 06:36:49 -05:00
|
|
|
}
|
|
|
|
$hash = hash('sha256', $json);
|
|
|
|
Log::debug(sprintf('The hash is: %s', $hash));
|
|
|
|
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
2019-03-17 11:05:16 -05:00
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
2019-08-30 00:28:48 -05:00
|
|
|
* @param NullArrayObject $transaction
|
2019-03-17 11:05:16 -05:00
|
|
|
*/
|
|
|
|
private function storeMetaFields(TransactionJournal $journal, NullArrayObject $transaction): void
|
|
|
|
{
|
|
|
|
foreach ($this->fields as $field) {
|
|
|
|
$this->storeMeta($journal, $transaction, $field);
|
|
|
|
}
|
2018-02-16 09:43:25 -06:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:28:48 -05:00
|
|
|
/**
|
|
|
|
* Link a piggy bank to this journal.
|
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
* @param NullArrayObject $data
|
|
|
|
*/
|
|
|
|
private function storePiggyEvent(TransactionJournal $journal, NullArrayObject $data): void
|
|
|
|
{
|
|
|
|
Log::debug('Will now store piggy event.');
|
|
|
|
if (!$journal->isTransfer()) {
|
|
|
|
Log::debug('Journal is not a transfer, do nothing.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$piggyBank = $this->piggyRepository->findPiggyBank((int)$data['piggy_bank_id'], $data['piggy_bank_name']);
|
|
|
|
|
|
|
|
if (null !== $piggyBank) {
|
|
|
|
$this->piggyEventFactory->create($journal, $piggyBank);
|
|
|
|
Log::debug('Create piggy event.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log::debug('Create no piggy event');
|
|
|
|
}
|
2019-03-05 10:26:49 -06:00
|
|
|
|
2019-06-04 13:42:11 -05:00
|
|
|
/**
|
|
|
|
* @param NullArrayObject $data
|
2019-08-30 00:28:48 -05:00
|
|
|
*
|
2019-06-04 13:42:11 -05:00
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
private function validateAccounts(NullArrayObject $data): void
|
|
|
|
{
|
|
|
|
$transactionType = $data['type'] ?? 'invalid';
|
|
|
|
$this->accountValidator->setUser($this->user);
|
|
|
|
$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) {
|
2019-06-16 06:15:32 -05:00
|
|
|
throw new FireflyException(sprintf('Source: %s', $this->accountValidator->sourceError)); // @codeCoverageIgnore
|
2019-06-04 13:42:11 -05:00
|
|
|
}
|
|
|
|
Log::debug('Source seems valid.');
|
|
|
|
// 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) {
|
2019-06-16 06:15:32 -05:00
|
|
|
throw new FireflyException(sprintf('Destination: %s', $this->accountValidator->destError)); // @codeCoverageIgnore
|
2019-06-04 13:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-26 00:49:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $errorOnHash
|
|
|
|
*/
|
|
|
|
public function setErrorOnHash(bool $errorOnHash): void
|
|
|
|
{
|
|
|
|
$this->errorOnHash = $errorOnHash;
|
2020-02-22 04:05:16 -06:00
|
|
|
if (true === $errorOnHash) {
|
|
|
|
Log::info('Will trigger duplication alert for this journal.');
|
|
|
|
}
|
2019-10-26 00:49:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|