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

169 lines
6.7 KiB
PHP
Raw Normal View History

<?php
2022-12-29 19:42:26 +01:00
/**
* SetDestinationAccount.php
2020-02-16 13:57:05 +01: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 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02: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 08:40:00 +02: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 07:49:58 +02:00
namespace FireflyIII\TransactionRules\Actions;
2023-08-13 15:01:12 +02:00
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
2022-10-02 06:23:31 +02:00
use FireflyIII\Events\TriggeredAuditLog;
use FireflyIII\Models\Account;
use FireflyIII\Models\RuleAction;
2021-04-05 22:12:11 +02:00
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2020-08-23 16:12:16 +02:00
use FireflyIII\User;
/**
2017-11-15 12:25:49 +01:00
* Class SetDestinationAccount.
*/
class SetDestinationAccount implements ActionInterface
{
2020-08-23 16:12:16 +02:00
private RuleAction $action;
private AccountRepositoryInterface $repository;
/**
* TriggerInterface constructor.
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
2020-08-23 07:42:14 +02:00
public function actOnArray(array $journal): bool
{
$accountName = $this->action->getValue($journal);
2023-11-28 17:18:31 +01:00
/** @var User $user */
$user = User::find($journal['user_id']);
2023-12-20 19:35:52 +01:00
/** @var null|TransactionJournal $object */
2022-12-29 19:42:26 +01:00
$object = $user->transactionJournals()->find((int)$journal['transaction_journal_id']);
2020-08-23 16:12:16 +02:00
$this->repository = app(AccountRepositoryInterface::class);
2021-04-05 22:12:11 +02:00
if (null === $object) {
2023-10-29 06:32:00 +01:00
app('log')->error('Could not find journal.');
2023-08-13 15:01:12 +02:00
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal')));
2023-12-20 19:35:52 +01:00
2021-04-05 22:12:11 +02:00
return false;
}
$type = $object->transactionType->type;
2020-08-23 16:12:16 +02:00
$this->repository->setUser($user);
2021-04-05 22:12:11 +02: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, $accountName);
2021-04-05 22:12:11 +02:00
if ((TransactionType::DEPOSIT === $type || TransactionType::TRANSFER === $type) && null === $newAccount) {
2023-10-29 06:32:00 +01:00
app('log')->error(
sprintf(
2022-10-30 14:24:37 +01:00
'Cant change destination account of journal #%d because no asset account with name "%s" exists.',
$object->id,
$accountName
)
);
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_asset', ['name' => $accountName])));
2023-12-20 19:35:52 +01:00
2020-08-23 16:12:16 +02:00
return false;
}
2021-04-05 22:12:11 +02:00
// new destination account must be different from the current source account:
2023-12-20 19:35:52 +01:00
/** @var null|Transaction $source */
$source = $object->transactions()->where('amount', '<', 0)->first();
2021-04-05 22:12:11 +02:00
if (null === $source) {
2023-10-29 06:32:00 +01:00
app('log')->error('Could not find source transaction.');
2023-08-13 15:01:12 +02:00
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction')));
2023-12-20 19:35:52 +01:00
2021-04-05 22:12:11 +02:00
return false;
2020-08-23 16:12:16 +02:00
}
2022-10-02 06:23:31 +02:00
// account must not be deleted (in the meantime):
2021-04-05 22:12:11 +02:00
if (null === $source->account) {
2023-10-29 06:32:00 +01:00
app('log')->error('Could not find source transaction account.');
2023-08-13 15:01:12 +02:00
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction_account')));
2023-12-20 19:35:52 +01:00
2021-04-05 22:12:11 +02:00
return false;
2020-08-23 16:12:16 +02:00
}
2023-11-05 19:41:37 +01:00
if (null !== $newAccount && $newAccount->id === $source->account_id) {
2023-10-29 06:32:00 +01:00
app('log')->error(
2021-04-05 22:12:11 +02:00
sprintf(
2022-10-30 14:24:37 +01:00
'New destination account ID #%d and current source account ID #%d are the same. Do nothing.',
$newAccount->id,
2021-04-05 22:12:11 +02:00
$source->account_id
)
);
2023-08-13 15:01:12 +02:00
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_destination', ['name' => $newAccount->name])));
2023-12-20 19:35:52 +01:00
2021-04-05 22:12:11 +02:00
return false;
}
// if this is a withdrawal, the new destination account must be a expense account and may be created:
2021-04-26 06:18:30 +02:00
// or it is a liability, in which case it must be returned.
2021-04-05 22:12:11 +02:00
if (TransactionType::WITHDRAWAL === $type) {
$newAccount = $this->findWithdrawalDestinationAccount($accountName);
2021-04-05 22:12:11 +02:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('New destination account is #%d ("%s").', $newAccount->id, $newAccount->name));
2021-04-05 22:12:11 +02:00
2022-10-02 06:23:31 +02:00
event(new TriggeredAuditLog($this->action->rule, $object, 'set_destination', null, $newAccount->name));
2021-04-05 22:12:11 +02:00
// update destination transaction with new destination account:
2023-12-20 19:35:52 +01:00
\DB::table('transactions')
->where('transaction_journal_id', '=', $object->id)
->where('amount', '>', 0)
->update(['account_id' => $newAccount->id])
;
2021-04-05 22:12:11 +02:00
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_id));
2021-04-05 22:12:11 +02:00
return true;
2020-08-23 16:12:16 +02:00
}
private function findAssetAccount(string $type, string $accountName): ?Account
2020-08-23 16:12:16 +02:00
{
2021-04-05 22:12:11 +02:00
// switch on type:
$allowed = config(sprintf('firefly.expected_source_types.destination.%s', $type));
$allowed = is_array($allowed) ? $allowed : [];
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Check config for expected_source_types.destination.%s, result is', $type), $allowed);
2021-04-05 22:12:11 +02:00
return $this->repository->findByName($accountName, $allowed);
2020-08-23 07:42:14 +02:00
}
private function findWithdrawalDestinationAccount(string $accountName): Account
{
2021-04-26 06:18:30 +02:00
$allowed = config('firefly.expected_source_types.destination.Withdrawal');
$account = $this->repository->findByName($accountName, $allowed);
if (null === $account) {
$data = [
'name' => $accountName,
2021-04-05 22:12:11 +02:00
'account_type_name' => 'expense',
'account_type_id' => null,
'virtual_balance' => 0,
'active' => true,
'iban' => null,
];
$account = $this->repository->store($data);
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Found or created expense account #%d ("%s")', $account->id, $account->name));
return $account;
}
}