Files
firefly-iii/app/Services/Internal/Support/TransactionServiceTrait.php

276 lines
8.5 KiB
PHP
Raw Normal View History

2018-02-23 15:12:47 +01:00
<?php
/**
* TransactionServiceTrait.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;
use FireflyIII\Factory\AccountFactory;
use FireflyIII\Factory\BudgetFactory;
use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-03-01 20:54:50 +01:00
use Log;
2018-02-23 15:12:47 +01:00
/**
* Trait TransactionServiceTrait
*
*/
trait TransactionServiceTrait
{
/**
* @param TransactionJournal $journal
* @param string $direction
*
* @return string|null
*/
public function accountType(TransactionJournal $journal, string $direction): ?string
{
$types = [];
$type = $journal->transactionType->type;
switch ($type) {
default:
2018-03-01 20:54:50 +01:00
// @codeCoverageIgnoreStart
Log::error(sprintf('Cannot handle type "%s" in accountType()', $type));
return null;
// @codeCoverageIgnoreEnd
2018-02-23 15:12:47 +01:00
case TransactionType::WITHDRAWAL:
$types['source'] = AccountType::ASSET;
$types['destination'] = AccountType::EXPENSE;
break;
case TransactionType::DEPOSIT:
$types['source'] = AccountType::REVENUE;
$types['destination'] = AccountType::ASSET;
break;
case TransactionType::TRANSFER:
$types['source'] = AccountType::ASSET;
$types['destination'] = AccountType::ASSET;
break;
case TransactionType::RECONCILIATION:
// always NULL, since this is handled by the reconciliation.
$types['source'] = null;
$types['destination'] = null;
// return here:
return $types[$direction];
}
if (!isset($types[$direction])) {
2018-03-01 20:54:50 +01:00
// @codeCoverageIgnoreStart
Log::error(sprintf('No type set for direction "%s" and type "%s"', $type, $direction));
return null;
// @codeCoverageIgnoreEnd
2018-02-23 15:12:47 +01:00
}
return $types[$direction];
}
/**
2018-03-01 20:54:50 +01:00
* @param string|null $expectedType
2018-02-23 15:12:47 +01:00
* @param int|null $accountId
* @param string|null $accountName
*
2018-05-29 07:25:04 +02:00
* @return Account|null
2018-02-23 15:12:47 +01:00
*/
2018-05-29 07:25:04 +02:00
public function findAccount(?string $expectedType, ?int $accountId, ?string $accountName): ?Account
2018-02-23 15:12:47 +01:00
{
2018-04-02 14:50:17 +02:00
$accountId = (int)$accountId;
$accountName = (string)$accountName;
2018-02-23 15:12:47 +01:00
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($this->user);
Log::debug(sprintf('Going to find account #%d ("%s")', $accountId, $accountName));
2018-04-02 14:50:17 +02:00
if (null === $expectedType) {
2018-02-23 15:12:47 +01:00
return $repository->findNull($accountId);
}
switch ($expectedType) {
case AccountType::ASSET:
if ($accountId > 0) {
// must be able to find it based on ID. Validator should catch invalid ID's.
return $repository->findNull($accountId);
}
// alternatively, return by name. Validator should catch invalid names.
return $repository->findByName($accountName, [AccountType::ASSET]);
break;
case AccountType::EXPENSE:
if ($accountId > 0) {
// must be able to find it based on ID. Validator should catch invalid ID's.
return $repository->findNull($accountId);
}
2018-04-28 06:23:13 +02:00
if (\strlen($accountName) > 0) {
2018-02-23 15:12:47 +01:00
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
return $factory->findOrCreate($accountName, AccountType::EXPENSE);
}
// return cash account:
return $repository->getCashAccount();
break;
case AccountType::REVENUE:
if ($accountId > 0) {
// must be able to find it based on ID. Validator should catch invalid ID's.
return $repository->findNull($accountId);
}
2018-04-28 06:23:13 +02:00
if (\strlen($accountName) > 0) {
2018-02-23 15:12:47 +01:00
// alternatively, return by name.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
return $factory->findOrCreate($accountName, AccountType::REVENUE);
}
// return cash account:
return $repository->getCashAccount();
default:
2018-03-01 20:54:50 +01:00
// @codeCoverageIgnoreStart
Log::error(sprintf('Cannot find account of type "%s".', $expectedType));
return null;
// @codeCoverageIgnoreEnd
2018-02-23 15:12:47 +01:00
}
}
/**
* @param int|null $budgetId
* @param null|string $budgetName
*
* @return Budget|null
*/
protected function findBudget(?int $budgetId, ?string $budgetName): ?Budget
{
/** @var BudgetFactory $factory */
$factory = app(BudgetFactory::class);
$factory->setUser($this->user);
return $factory->find($budgetId, $budgetName);
}
/**
* @param int|null $categoryId
* @param null|string $categoryName
*
* @return Category|null
*/
protected function findCategory(?int $categoryId, ?string $categoryName): ?Category
{
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user);
return $factory->findOrCreate($categoryId, $categoryName);
}
/**
* @param int|null $currencyId
* @param null|string $currencyCode
*
* @return TransactionCurrency|null
*/
protected function findCurrency(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
$factory = app(TransactionCurrencyFactory::class);
return $factory->find($currencyId, $currencyCode);
}
/**
* @param Transaction $transaction
* @param Budget|null $budget
*/
protected function setBudget(Transaction $transaction, ?Budget $budget): void
{
2018-04-02 14:50:17 +02:00
if (null === $budget) {
2018-03-04 08:22:32 +01:00
$transaction->budgets()->sync([]);
2018-03-10 22:38:20 +01:00
2018-02-23 15:12:47 +01:00
return;
}
$transaction->budgets()->sync([$budget->id]);
}
/**
* @param Transaction $transaction
* @param Category|null $category
*/
protected function setCategory(Transaction $transaction, ?Category $category): void
{
2018-04-02 14:50:17 +02:00
if (null === $category) {
2018-03-04 08:22:32 +01:00
$transaction->categories()->sync([]);
2018-03-10 22:38:20 +01:00
2018-02-23 15:12:47 +01:00
return;
}
$transaction->categories()->sync([$category->id]);
}
/**
* @param Transaction $transaction
2018-03-01 20:54:50 +01:00
* @param string|null $amount
2018-02-23 15:12:47 +01:00
*/
protected function setForeignAmount(Transaction $transaction, ?string $amount): void
{
$amount = '' === (string)$amount ? null : $amount;
2018-02-23 15:12:47 +01:00
$transaction->foreign_amount = $amount;
$transaction->save();
}
/**
* @param Transaction $transaction
* @param TransactionCurrency|null $currency
*/
protected function setForeignCurrency(Transaction $transaction, ?TransactionCurrency $currency): void
{
2018-04-02 14:50:17 +02:00
if (null === $currency) {
2018-03-09 04:47:43 +01:00
$transaction->foreign_currency_id = null;
$transaction->save();
2018-03-10 22:38:20 +01:00
2018-02-23 15:12:47 +01:00
return;
}
2018-03-24 14:05:29 +01:00
2018-02-23 15:12:47 +01:00
$transaction->foreign_currency_id = $currency->id;
$transaction->save();
}
2018-03-05 19:35:58 +01:00
}