firefly-iii/app/TransactionRules/Actions/SetDestinationAccount.php

171 lines
5.8 KiB
PHP
Raw Normal View History

<?php
/**
* SetDestinationAccount.php
2020-02-16 06:57:05 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -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/>.
*/
declare(strict_types=1);
2017-09-13 00:49:58 -05:00
namespace FireflyIII\TransactionRules\Actions;
2020-08-23 09:12:16 -05:00
use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
2021-04-05 15:12:11 -05:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2020-08-23 09:12:16 -05:00
use FireflyIII\User;
use Log;
/**
2017-11-15 05:25:49 -06:00
* Class SetDestinationAccount.
*/
class SetDestinationAccount implements ActionInterface
{
2020-08-23 09:12:16 -05:00
private RuleAction $action;
private AccountRepositoryInterface $repository;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
2020-08-23 00:42:14 -05:00
/**
* @inheritDoc
*/
public function actOnArray(array $journal): bool
{
2021-04-25 23:18:30 -05:00
$user = User::find($journal['user_id']);
$type = $journal['transaction_type_type'];
2021-04-06 06:30:09 -05:00
/** @var TransactionJournal|null $object */
2021-04-05 15:12:11 -05:00
$object = $user->transactionJournals()->find((int)$journal['transaction_journal_id']);
2020-08-23 09:12:16 -05:00
$this->repository = app(AccountRepositoryInterface::class);
2021-04-05 15:12:11 -05:00
if (null === $object) {
Log::error('Could not find journal.');
return false;
}
2020-08-23 09:12:16 -05:00
$this->repository->setUser($user);
2021-04-05 15:12:11 -05:00
// if this is a transfer or a deposit, the new destination account must be an asset account or a default account, and it MUST exist:
$newAccount = $this->findAssetAccount($type);
if ((TransactionType::DEPOSIT === $type || TransactionType::TRANSFER === $type) && null === $newAccount) {
Log::error(
sprintf(
2021-04-05 15:12:11 -05:00
'Cant change destination account of journal #%d because no asset account with name "%s" exists.', $object->id, $this->action->action_value
)
);
2020-08-23 09:12:16 -05:00
return false;
}
2021-04-05 15:12:11 -05:00
// new destination account must be different from the current source account:
2021-04-06 06:30:09 -05:00
/** @var Transaction|null $source */
2021-04-05 15:12:11 -05:00
$source = $object->transactions()->where('amount', '<', 0)->first();
if (null === $source) {
Log::error('Could not find source transaction.');
return false;
2020-08-23 09:12:16 -05:00
}
2021-04-05 15:12:11 -05:00
// account must not be deleted (in the mean time):
if (null === $source->account) {
Log::error('Could not find source transaction account.');
return false;
2020-08-23 09:12:16 -05:00
}
2021-04-05 15:12:11 -05:00
if (null !== $newAccount && (int)$newAccount->id === (int)$source->account_id) {
Log::error(
sprintf(
'New destination account ID #%d and current source account ID #%d are the same. Do nothing.', $newAccount->id,
$source->account_id
)
);
return false;
}
// if this is a withdrawal, the new destination account must be a expense account and may be created:
2021-04-25 23:18:30 -05:00
// or it is a liability, in which case it must be returned.
2021-04-05 15:12:11 -05:00
if (TransactionType::WITHDRAWAL === $type) {
2021-04-25 23:18:30 -05:00
$newAccount = $this->findWithdrawalDestinationAccount();
2021-04-05 15:12:11 -05:00
}
Log::debug(sprintf('New destination account is #%d ("%s").', $newAccount->id, $newAccount->name));
// update destination transaction with new destination account:
DB::table('transactions')
->where('transaction_journal_id', '=', $object->id)
->where('amount', '>', 0)
->update(['account_id' => $newAccount->id]);
Log::debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_id));
return true;
2020-08-23 09:12:16 -05:00
}
/**
2021-04-05 15:12:11 -05:00
* @param string $type
*
2020-08-23 09:12:16 -05:00
* @return Account|null
*/
2021-04-05 15:12:11 -05:00
private function findAssetAccount(string $type): ?Account
2020-08-23 09:12:16 -05:00
{
2021-04-05 15:12:11 -05:00
// switch on type:
$allowed = config(sprintf('firefly.expected_source_types.destination.%s', $type));
$allowed = is_array($allowed) ? $allowed : [];
Log::debug(sprintf('Check config for expected_source_types.destination.%s, result is', $type), $allowed);
return $this->repository->findByName($this->action->action_value, $allowed);
2020-08-23 00:42:14 -05:00
}
/**
2021-04-06 06:30:09 -05:00
* @return Account
*/
2021-04-25 23:18:30 -05:00
private function findWithdrawalDestinationAccount(): Account
{
2021-04-25 23:18:30 -05:00
$allowed = config('firefly.expected_source_types.destination.Withdrawal');
$account = $this->repository->findByName($this->action->action_value, $allowed);
if (null === $account) {
$data = [
2021-04-05 15:12:11 -05:00
'name' => $this->action->action_value,
'account_type_name' => 'expense',
'account_type_id' => null,
'virtual_balance' => 0,
'active' => true,
'iban' => null,
];
$account = $this->repository->store($data);
}
Log::debug(sprintf('Found or created expense account #%d ("%s")', $account->id, $account->name));
return $account;
}
2021-04-05 15:12:11 -05:00
}