firefly-iii/app/Services/Internal/Support/JournalServiceTrait.php

291 lines
9.3 KiB
PHP
Raw Normal View History

2018-02-23 08:12:47 -06:00
<?php
/**
* JournalServiceTrait.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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Internal\Support;
2018-09-06 00:38:51 -05:00
use Exception;
use FireflyIII\Exceptions\FireflyException;
2018-02-23 08:12:47 -06:00
use FireflyIII\Factory\TagFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2018-02-23 08:12:47 -06:00
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Support\NullArrayObject;
2018-06-02 11:19:35 -05:00
use Log;
2018-02-23 08:12:47 -06:00
/**
* Trait JournalServiceTrait
*
*/
trait JournalServiceTrait
{
/** @var AccountRepositoryInterface */
private $accountRepository;
/** @var BudgetRepositoryInterface */
private $budgetRepository;
/** @var CategoryRepositoryInterface */
private $categoryRepository;
/** @var TagFactory */
private $tagFactory;
2018-02-23 08:12:47 -06:00
/**
* @param string|null $amount
2018-07-27 23:27:30 -05:00
*
* @return string
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
*/
protected function getForeignAmount(?string $amount): ?string
{
if (null === $amount) {
Log::debug('No foreign amount info in array. Return NULL');
return null;
}
if ('' === $amount) {
Log::debug('Foreign amount is empty string, return NULL.');
return null;
}
if (0 === bccomp('0', $amount)) {
Log::debug('Foreign amount is 0.0, return NULL.');
return null;
}
Log::debug(sprintf('Foreign amount is %s', $amount));
return $amount;
}
2018-02-23 08:12:47 -06:00
2019-03-05 10:26:49 -06:00
/**
* @param string $transactionType
* @param string $direction
* @param int|null $accountId
* @param string|null $accountName
2019-03-05 10:26:49 -06:00
*
* @return Account
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
2019-03-05 10:26:49 -06:00
*/
protected function getAccount(string $transactionType, string $direction, ?int $accountId, ?string $accountName): Account
2019-03-05 10:26:49 -06:00
{
// some debug logging:
Log::debug(sprintf('Now in getAccount(%s, %d, %s)', $direction, $accountId, $accountName));
// final result:
$result = null;
2019-03-05 10:26:49 -06:00
// expected type of source account, in order of preference
/** @var array $array */
$array = config('firefly.expected_source_types');
$expectedTypes = $array[$direction];
unset($array);
// and now try to find it, based on the type of transaction.
$message = 'Based on the fact that the transaction is a %s, the %s account should be in: %s';
Log::debug(sprintf($message, $transactionType, $direction, implode(', ', $expectedTypes[$transactionType])));
// first attempt, find by ID.
if (null !== $accountId) {
$search = $this->accountRepository->findNull($accountId);
if (null !== $search && in_array($search->accountType->type, $expectedTypes[$transactionType], true)) {
Log::debug(
sprintf('Found "account_id" object for %s: #%d, "%s" of type %s', $direction, $search->id, $search->name, $search->accountType->type)
);
$result = $search;
}
}
// second attempt, find by name.
if (null === $result && null !== $accountName) {
Log::debug('Found nothing by account ID.');
// find by preferred type.
$source = $this->accountRepository->findByName($accountName, [$expectedTypes[$transactionType][0]]);
// or any expected type.
$source = $source ?? $this->accountRepository->findByName($accountName, $expectedTypes[$transactionType]);
if (null !== $source) {
Log::debug(sprintf('Found "account_name" object for %s: #%d, %s', $direction, $source->id, $source->name));
$result = $source;
}
}
// return cash account.
if (null === $result && null === $accountName
&& in_array(AccountType::CASH, $expectedTypes[$transactionType], true)) {
$result = $this->accountRepository->getCashAccount();
}
// return new account.
if (null === $result) {
$accountName = $accountName ?? '(no name)';
// 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".', $accountId, $accountName));
}
$result = $this->accountRepository->store(
[
'account_type_id' => null,
'account_type' => $preferredType,
'name' => $accountName,
'active' => true,
'iban' => null,
]
);
}
return $result;
2019-03-05 10:26:49 -06:00
}
/**
* @param string $amount
2019-03-05 10:26:49 -06:00
*
* @return string
* @throws FireflyException
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
2019-03-05 10:26:49 -06:00
*/
protected function getAmount(string $amount): string
2019-03-05 10:26:49 -06:00
{
if ('' === $amount) {
throw new FireflyException(sprintf('The amount cannot be an empty string: "%s"', $amount));
}
if (0 === bccomp('0', $amount)) {
throw new FireflyException(sprintf('The amount seems to be zero: "%s"', $amount));
}
2019-03-05 10:26:49 -06:00
return $amount;
2019-03-05 10:26:49 -06:00
}
/**
* @param TransactionJournal $journal
* @param NullArrayObject $data
2019-07-31 09:53:09 -05:00
*
* @codeCoverageIgnore
2019-03-05 10:26:49 -06:00
*/
protected function storeBudget(TransactionJournal $journal, NullArrayObject $data): void
2019-03-05 10:26:49 -06:00
{
if (TransactionType::WITHDRAWAL !== $journal->transactionType->type) {
2019-03-05 10:26:49 -06:00
$journal->budgets()->sync([]);
return;
}
$budget = $this->budgetRepository->findBudget($data['budget_id'], $data['budget_name']);
if (null !== $budget) {
Log::debug(sprintf('Link budget #%d to journal #%d', $budget->id, $journal->id));
$journal->budgets()->sync([$budget->id]);
2019-03-05 10:26:49 -06:00
return;
}
// if the budget is NULL, sync empty.
$journal->budgets()->sync([]);
2019-03-05 10:26:49 -06:00
}
2018-02-23 08:12:47 -06:00
/**
* @param TransactionJournal $journal
* @param NullArrayObject $data
2019-07-31 09:53:09 -05:00
*
* @codeCoverageIgnore
2018-02-23 08:12:47 -06:00
*/
protected function storeCategory(TransactionJournal $journal, NullArrayObject $data): void
2018-02-23 08:12:47 -06:00
{
$category = $this->categoryRepository->findCategory($data['category_id'], $data['category_name']);
if (null !== $category) {
Log::debug(sprintf('Link category #%d to journal #%d', $category->id, $journal->id));
$journal->categories()->sync([$category->id]);
2018-06-02 11:19:35 -05:00
return;
}
// if the category is NULL, sync empty.
$journal->categories()->sync([]);
2018-02-23 08:12:47 -06:00
}
/**
* @param TransactionJournal $journal
* @param string $notes
2019-07-31 09:53:09 -05:00
*
* @codeCoverageIgnore
2018-02-23 08:12:47 -06:00
*/
protected function storeNotes(TransactionJournal $journal, ?string $notes): void
2018-02-23 08:12:47 -06:00
{
2018-04-02 07:50:17 -05:00
$notes = (string)$notes;
$note = $journal->notes()->first();
if ('' !== $notes) {
2018-04-02 07:50:17 -05:00
if (null === $note) {
2018-02-23 08:12:47 -06:00
$note = new Note;
$note->noteable()->associate($journal);
}
$note->text = $notes;
$note->save();
Log::debug(sprintf('Stored notes for journal #%d', $journal->id));
2018-02-23 08:12:47 -06:00
return;
}
if ('' === $notes && null !== $note) {
// try to delete existing notes.
2018-09-06 00:38:51 -05:00
try {
$note->delete();
// @codeCoverageIgnoreStart
2018-09-06 00:38:51 -05:00
} catch (Exception $e) {
Log::debug(sprintf('Could not delete journal notes: %s', $e->getMessage()));
2018-09-06 00:38:51 -05:00
}
// @codeCoverageIgnoreEnd
2018-02-23 08:12:47 -06:00
}
}
2018-02-23 08:12:47 -06:00
/**
* Link tags to journal.
*
* @param TransactionJournal $journal
* @param array $tags
2019-08-17 03:47:29 -05:00
*
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
*/
protected function storeTags(TransactionJournal $journal, ?array $tags): void
{
2019-08-02 23:27:56 -05:00
$this->tagFactory->setUser($journal->user);
$set = [];
if (!is_array($tags)) {
return;
}
foreach ($tags as $string) {
$string = (string)$string;
if ('' !== $string) {
$tag = $this->tagFactory->findOrCreate($string);
if (null !== $tag) {
$set[] = $tag->id;
}
}
}
$journal->tags()->sync($set);
2018-02-23 08:12:47 -06:00
}
2018-03-05 12:35:58 -06:00
}