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

159 lines
5.7 KiB
PHP
Raw Normal View History

2016-10-29 02:03:14 -05:00
<?php
/**
* SetSourceAccount.php
2020-02-16 06:57:05 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2016-10-29 02:03:14 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2016-10-29 02:03:14 -05:00
*
* 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/>.
2016-10-29 02:03:14 -05:00
*/
declare(strict_types=1);
2016-10-29 02:03:14 -05:00
2017-09-13 00:49:58 -05:00
namespace FireflyIII\TransactionRules\Actions;
2016-10-29 02:03:14 -05:00
2020-08-23 09:12:16 -05:00
use DB;
2016-10-29 02:03:14 -05:00
use FireflyIII\Models\Account;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
2016-10-29 02:03:14 -05:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2020-08-23 09:12:16 -05:00
use FireflyIII\User;
2016-10-29 02:03:14 -05:00
use Log;
/**
2017-11-15 05:25:49 -06:00
* Class SetSourceAccount.
2016-10-29 02:03:14 -05:00
*/
class SetSourceAccount implements ActionInterface
{
2020-08-23 09:12:16 -05:00
private RuleAction $action;
private AccountRepositoryInterface $repository;
2016-10-29 02:03:14 -05:00
/**
* 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-05 14:53:28 -05:00
/** @var TransactionJournal $object */
/** @var AccountRepositoryInterface repository */
2021-04-05 14:53:28 -05:00
/** @var Transaction $destination */
$user = User::find($journal['user_id']);
$type = $journal['transaction_type_type'];
$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 14:53:28 -05:00
2020-08-23 09:12:16 -05:00
$this->repository->setUser($user);
// if this is a transfer or a withdrawal, the new source account must be an asset account or a default account, and it MUST exist:
$newAccount = $this->findAssetAccount($type);
if ((TransactionType::WITHDRAWAL === $type || TransactionType::TRANSFER === $type) && null === $newAccount) {
Log::error(
2021-04-05 14:53:28 -05:00
sprintf('Cant change src account of journal #%d because no asset account with name "%s" exists.', $object->id, $this->action->action_value)
);
return false;
}
2021-04-05 14:53:28 -05:00
// new source account must be different from the current destination:
$destinationId = (int)$journal['destination_account_id'];
2021-04-05 14:53:28 -05:00
$destination = $object->transactions()->where('amount', '>', 0)->first();
if (null !== $destination) {
$destinationId = $destination->account ? (int)$destination->account->id : $destinationId;
}
if (TransactionType::TRANSFER === $type && null !== $newAccount && (int)$newAccount->id === $destinationId) {
Log::error(
sprintf(
'New source account ID #%d and current destination account ID #%d are the same. Do nothing.', $newAccount->id,
$destinationId
)
);
2020-08-23 09:12:16 -05:00
return false;
}
2021-04-05 14:53:28 -05:00
2020-08-23 09:12:16 -05:00
// if this is a deposit, the new source account must be a revenue account and may be created:
if (TransactionType::DEPOSIT === $type) {
$newAccount = $this->findRevenueAccount();
}
if (null === $newAccount) {
Log::error('New account is NULL');
2020-08-23 09:12:16 -05:00
return false;
}
Log::debug(sprintf('New source account is #%d ("%s").', $newAccount->id, $newAccount->name));
// update source transaction with new source account:
// get source transaction:
DB::table('transactions')
2021-04-05 14:53:28 -05:00
->where('transaction_journal_id', '=', $object->id)
2020-08-23 09:12:16 -05:00
->where('amount', '<', 0)
->update(['account_id' => $newAccount->id]);
2021-04-05 14:53:28 -05:00
Log::debug(sprintf('Updated journal #%d (group #%d) and gave it new source account ID.', $object->id, $object->transaction_group_id));
2020-08-23 09:12:16 -05:00
return true;
2020-08-23 00:42:14 -05:00
}
/**
* @param string $type
*
* @return Account|null
*/
private function findAssetAccount(string $type): ?Account
{
// switch on type:
$allowed = config(sprintf('firefly.expected_source_types.source.%s', $type));
$allowed = is_array($allowed) ? $allowed : [];
Log::debug(sprintf('Check config for expected_source_types.source.%s, result is', $type), $allowed);
return $this->repository->findByName($this->action->action_value, $allowed);
}
/**
* @return Account|null
*/
private function findRevenueAccount(): ?Account
{
$allowed = config('firefly.expected_source_types.source.Deposit');
$account = $this->repository->findByName($this->action->action_value, $allowed);
if (null === $account) {
// create new revenue account with this name:
$data = [
2021-04-05 14:53:28 -05:00
'name' => $this->action->action_value,
'account_type_name' => 'revenue',
'account_type_id' => null,
'virtual_balance' => 0,
'active' => true,
'iban' => null,
];
$account = $this->repository->store($data);
}
Log::debug(sprintf('Found or created revenue account #%d ("%s")', $account->id, $account->name));
return $account;
}
2016-10-29 02:03:14 -05:00
}