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

151 lines
4.9 KiB
PHP
Raw Normal View History

2016-10-29 02:03:14 -05:00
<?php
/**
* SetSourceAccount.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-10-29 02:03:14 -05:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
2016-10-29 02:03:14 -05:00
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://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
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Log;
/**
2017-11-15 05:25:49 -06:00
* Class SetSourceAccount.
2016-10-29 02:03:14 -05:00
*/
class SetSourceAccount implements ActionInterface
{
2017-11-25 08:20:53 -06:00
/** @var RuleAction The rule action */
2016-10-29 02:03:14 -05:00
private $action;
2017-11-25 08:20:53 -06:00
/** @var TransactionJournal The journal */
2016-10-29 02:03:14 -05:00
private $journal;
2017-12-22 11:32:43 -06:00
/** @var Account The new source account */
2016-10-29 02:03:14 -05:00
private $newSourceAccount;
2017-11-25 08:20:53 -06:00
/** @var AccountRepositoryInterface Account repository */
2016-10-29 02:03:14 -05:00
private $repository;
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
$this->action = $action;
}
/**
2017-11-25 08:20:53 -06:00
* Set source account to X
*
2016-10-29 02:03:14 -05:00
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$this->journal = $journal;
2017-02-05 09:16:15 -06:00
$this->repository = app(AccountRepositoryInterface::class);
$this->repository->setUser($journal->user);
2017-02-16 23:42:36 -06:00
$count = $journal->transactions()->count();
2016-10-29 02:03:14 -05:00
if ($count > 2) {
Log::error(sprintf('Cannot change source account of journal #%d because it is a split journal.', $journal->id));
2017-10-04 08:27:20 -05:00
return false;
2016-10-29 02:03:14 -05:00
}
// journal type:
$type = $journal->transactionType->type;
// 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:
2017-11-15 05:25:49 -06:00
if ((TransactionType::WITHDRAWAL === $type || TransactionType::TRANSFER === $type) && !$this->findAssetAccount()) {
2016-10-29 02:03:14 -05:00
Log::error(
sprintf(
'Cannot change source account of journal #%d because no asset account with name "%s" exists.',
2017-11-15 03:52:29 -06:00
$journal->id,
$this->action->action_value
2016-10-29 02:03:14 -05:00
)
);
2017-10-04 08:27:20 -05:00
return false;
2016-10-29 02:03:14 -05:00
}
// if this is a deposit, the new source account must be a revenue account and may be created:
2017-11-15 05:25:49 -06:00
if (TransactionType::DEPOSIT === $type) {
2016-10-29 02:03:14 -05:00
$this->findRevenueAccount();
}
Log::debug(sprintf('New source account is #%d ("%s").', $this->newSourceAccount->id, $this->newSourceAccount->name));
// update source transaction with new source account:
// get source transaction:
$transaction = $journal->transactions()->where('amount', '<', 0)->first();
$transaction->account_id = $this->newSourceAccount->id;
$transaction->save();
$journal->touch();
2016-10-29 02:03:14 -05:00
Log::debug(sprintf('Updated transaction #%d and gave it new account ID.', $transaction->id));
return true;
}
/**
* @return bool
*/
private function findAssetAccount(): bool
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::DEFAULT, AccountType::ASSET]);
2017-11-15 05:25:49 -06:00
if (null === $account->id) {
2016-10-29 02:03:14 -05:00
Log::debug(sprintf('There is NO asset account called "%s".', $this->action->action_value));
return false;
}
Log::debug(sprintf('There exists an asset account called "%s". ID is #%d', $this->action->action_value, $account->id));
$this->newSourceAccount = $account;
return true;
}
/**
*
*/
private function findRevenueAccount()
{
$account = $this->repository->findByName($this->action->action_value, [AccountType::REVENUE]);
2017-11-15 05:25:49 -06:00
if (null === $account->id) {
2016-10-29 02:03:14 -05:00
// create new revenue account with this name:
$data = [
'name' => $this->action->action_value,
'accountType' => 'revenue',
'virtualBalance' => 0,
'active' => true,
'iban' => null,
];
$account = $this->repository->store($data);
}
Log::debug(sprintf('Found or created revenue account #%d ("%s")', $account->id, $account->name));
$this->newSourceAccount = $account;
}
}