<commit_before><?php

/**
 * TransactionJournalFactory.php
 * Copyright (c) 2018 <EMAIL>
 *
 * 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/>.
 */

declare(strict_types=1);

namespace FireflyIII\Factory;

use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Services\Internal\Support\JournalServiceTrait;
use FireflyIII\Services\Internal\Support\TransactionTypeTrait;
use FireflyIII\User;
use Log;

/**
 * Class TransactionJournalFactory
 */
class TransactionJournalFactory
{
    use JournalServiceTrait, TransactionTypeTrait;
    /** @var User */
    private $user;

    /**
     * @param array $data
     *
     * @return TransactionJournal
     * @throws FireflyException
     */
    public function create(array $data): TransactionJournal
    {
        Log::debug('Start of TransactionJournalFactory::create()');
        // store basic journal first.
        $type            = $this->findTransactionType($data['type']);
        $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
        Log::debug(sprintf('Going to store a %s', $type->type));
        $journal = TransactionJournal::create(
            [
                'user_id'                 => $data['user'],
                'transaction_type_id'     => $type->id,
                'bill_id'                 => null,
                'transaction_currency_id' => $defaultCurrency->id,
                'description'             => $data['description'],
                'date'                    => $data['date']->format('Y-m-d'),
                'order'                   => 0,
                'tag_count'               => 0,
                'completed'               => 0,
            ]
        );

        if (isset($data['transactions'][0]['amount']) && '' === $data['transactions'][0]['amount']) {
            Log::error('Empty amount in data', $data);
        }

        // store basic transactions:
        /** @var TransactionFactory $factory */
        $factory = app(TransactionFactory::class);
        $factory->setUser($this->user);

        Log::debug(sprintf('Found %d transactions in array.', \count($data['transactions'])));
        /** @var array $trData */
        foreach ($data['transactions'] as $index => $trData) {
            Log::debug(sprintf('Now storing transaction %d of %d', $index + 1, \count($data['transactions'])));
            $factory->createPair($journal, $trData);
        }
        $journal->completed = true;
        $journal->save();

        // link bill:
        $this->connectBill($journal, $data);

        // link piggy bank (if transfer)
        $this->connectPiggyBank($journal, $data);

        // link tags:
        $this->connectTags($journal, $data);

        // store note:
        $this->storeNote($journal, (string)$data['notes']);

        // store date meta fields (if present):
        $fields = ['sepa-cc', 'sepa-ct-op', 'sepa-ct-id', 'sepa-db', 'sepa-country', 'sepa-ep', 'sepa-ci', 'interest_date', 'book_date', 'process_date',
                   'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'bunq_payment_id', 'importHash', 'importHashV2', 'external_id'];

        foreach ($fields as $field) {
            $this->storeMeta($journal, $data, $field);
        }
        Log::debug('End of TransactionJournalFactory::create()');

        return $journal;
    }<commit_msg>Fix #1483<commit_after><?php

/**
 * TransactionJournalFactory.php
 * Copyright (c) 2018 <EMAIL>
 *
 * 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/>.
 */

declare(strict_types=1);

namespace FireflyIII\Factory;

use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Services\Internal\Support\JournalServiceTrait;
use FireflyIII\Services\Internal\Support\TransactionTypeTrait;
use FireflyIII\User;
use Log;

/**
 * Class TransactionJournalFactory
 */
class TransactionJournalFactory
{
    use JournalServiceTrait, TransactionTypeTrait;
    /** @var User */
    private $user;

    /**
     * @param array $data
     *
     * @return TransactionJournal
     * @throws FireflyException
     */
    public function create(array $data): TransactionJournal
    {
        Log::debug('Start of TransactionJournalFactory::create()');
        // store basic journal first.
        $type            = $this->findTransactionType($data['type']);
        $defaultCurrency = app('amount')->getDefaultCurrencyByUser($this->user);
        Log::debug(sprintf('Going to store a %s', $type->type));
        $journal = TransactionJournal::create(
            [
                'user_id'                 => $data['user'],
                'transaction_type_id'     => $type->id,
                'bill_id'                 => null,
                'transaction_currency_id' => $defaultCurrency->id,
                'description'             => $data['description'],
                'date'                    => $data['date']->format('Y-m-d'),
                'order'                   => 0,
                'tag_count'               => 0,
                'completed'               => 0,
            ]
        );

        if (isset($data['transactions'][0]['amount']) && '' === $data['transactions'][0]['amount']) {
            Log::error('Empty amount in data', $data);
        }

        // store basic transactions:
        /** @var TransactionFactory $factory */
        $factory = app(TransactionFactory::class);
        $factory->setUser($this->user);

        Log::debug(sprintf('Found %d transactions in array.', \count($data['transactions'])));
        /** @var array $trData */
        foreach ($data['transactions'] as $index => $trData) {
            Log::debug(sprintf('Now storing transaction %d of %d', $index + 1, \count($data['transactions'])));
            $factory->createPair($journal, $trData);
        }
        $journal->completed = true;
        $journal->save();

        // link bill:
        $this->connectBill($journal, $data);

        // link piggy bank (if transfer)
        $this->connectPiggyBank($journal, $data);

        // link tags:
        $this->connectTags($journal, $data);

        // store note:
        $this->storeNote($journal, (string)$data['notes']);

        // store date meta fields (if present):
        $fields = ['sepa-cc', 'sepa-ct-op', 'sepa-ct-id', 'sepa-db', 'sepa-country', 'sepa-ep', 'sepa-ci', 'interest_date', 'book_date', 'process_date',
                   'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'bunq_payment_id', 'importHash', 'importHashV2', 'external_id'];

        foreach ($fields as $field) {
            $this->storeMeta($journal, $data, $field);
        }
        Log::debug('End of TransactionJournalFactory::create()');

        // invalidate cache.
        app('preferences')->mark();

        return $journal;
    }
{'fraction_non_alphanumeric': 0.14080882352941176, 'fraction_numerical': 0.003676470588235294, 'mean_word_length': 2.4683382915427114, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2353816', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2194, 'n_words': 584}