2018-09-15 06:43:57 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ConvertToTransfer.php
|
2020-02-16 06:57:05 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-09-15 06:43:57 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-09-15 06:43:57 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -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.
|
2018-09-15 06:43:57 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-09-15 06:43:57 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-09-15 06:43:57 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -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/>.
|
2018-09-15 06:43:57 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\TransactionRules\Actions;
|
2021-04-05 15:12:57 -05:00
|
|
|
|
2021-03-23 00:42:26 -05:00
|
|
|
use DB;
|
2022-10-01 23:23:31 -05:00
|
|
|
use FireflyIII\Events\TriggeredAuditLog;
|
2018-09-15 06:43:57 -05:00
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
use FireflyIII\Models\RuleAction;
|
2022-06-25 07:36:53 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2018-09-15 06:43:57 -05:00
|
|
|
use FireflyIII\Models\TransactionType;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2020-08-23 00:42:14 -05:00
|
|
|
use FireflyIII\User;
|
2018-09-15 06:43:57 -05:00
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class ConvertToTransfer
|
|
|
|
*/
|
|
|
|
class ConvertToTransfer implements ActionInterface
|
|
|
|
{
|
2020-08-23 00:42:14 -05:00
|
|
|
private RuleAction $action;
|
2018-09-15 06:43:57 -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
|
|
|
|
{
|
2022-06-25 07:36:53 -05:00
|
|
|
$groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
2022-10-01 23:23:31 -05:00
|
|
|
if ($groupCount > 1) {
|
2022-06-25 07:36:53 -05:00
|
|
|
Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to transfer.', $journal['transaction_group_id']));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-05 15:12:57 -05:00
|
|
|
$type = $journal['transaction_type_type'];
|
|
|
|
$user = User::find($journal['user_id']);
|
2020-08-23 00:42:14 -05:00
|
|
|
if (TransactionType::TRANSFER === $type) {
|
2021-03-23 00:42:26 -05:00
|
|
|
Log::error(
|
|
|
|
sprintf('Journal #%d is already a transfer so cannot be converted (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id)
|
|
|
|
);
|
2020-08-23 00:42:14 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the asset account in the action value.
|
|
|
|
/** @var AccountRepositoryInterface $repository */
|
|
|
|
$repository = app(AccountRepositoryInterface::class);
|
|
|
|
$repository->setUser($user);
|
|
|
|
$asset = $repository->findByName($this->action->action_value, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]);
|
|
|
|
if (null === $asset) {
|
2021-03-23 00:42:26 -05:00
|
|
|
Log::error(
|
|
|
|
sprintf(
|
|
|
|
'Journal #%d cannot be converted because no asset with name "%s" exists (rule #%d).', $journal['transaction_journal_id'],
|
|
|
|
$this->action->action_value, $this->action->rule_id
|
|
|
|
)
|
|
|
|
);
|
2020-08-23 00:42:14 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (TransactionType::WITHDRAWAL === $type) {
|
|
|
|
Log::debug('Going to transform a withdrawal to a transfer.');
|
2022-10-01 23:23:31 -05:00
|
|
|
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
2022-10-02 07:37:50 -05:00
|
|
|
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::WITHDRAWAL, TransactionType::TRANSFER));
|
2020-08-23 00:42:14 -05:00
|
|
|
|
|
|
|
return $this->convertWithdrawalArray($journal, $asset);
|
|
|
|
}
|
|
|
|
if (TransactionType::DEPOSIT === $type) {
|
|
|
|
Log::debug('Going to transform a deposit to a transfer.');
|
|
|
|
|
2022-10-01 23:23:31 -05:00
|
|
|
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
2022-10-02 07:37:50 -05:00
|
|
|
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::TRANSFER));
|
2022-10-01 23:23:31 -05:00
|
|
|
|
2020-08-23 00:42:14 -05:00
|
|
|
return $this->convertDepositArray($journal, $asset);
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:26:12 -05:00
|
|
|
return false;
|
2020-08-23 00:42:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-23 00:42:26 -05:00
|
|
|
* A withdrawal is from Asset to Expense.
|
|
|
|
* We replace the Expense with another asset.
|
|
|
|
*
|
2020-08-23 00:42:14 -05:00
|
|
|
* @param array $journal
|
|
|
|
* @param Account $asset
|
2021-03-23 00:42:26 -05:00
|
|
|
*
|
2020-08-23 00:42:14 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-23 00:42:26 -05:00
|
|
|
private function convertWithdrawalArray(array $journal, Account $asset): bool
|
2020-08-23 00:42:14 -05:00
|
|
|
{
|
2021-03-23 00:42:26 -05:00
|
|
|
if ($journal['source_account_id'] === $asset->id) {
|
|
|
|
Log::error(
|
|
|
|
vsprintf(
|
|
|
|
'Journal #%d has already has "%s" as a source asset. ConvertToTransfer failed. (rule #%d).',
|
|
|
|
[$journal['transaction_journal_id'], $asset->name, $this->action->rule_id]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2020-08-23 00:42:14 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-23 00:42:26 -05:00
|
|
|
// update destination transaction:
|
2020-08-23 00:42:14 -05:00
|
|
|
DB::table('transactions')
|
|
|
|
->where('transaction_journal_id', '=', $journal['transaction_journal_id'])
|
2021-03-23 00:42:26 -05:00
|
|
|
->where('amount', '>', 0)
|
2020-08-23 00:42:14 -05:00
|
|
|
->update(['account_id' => $asset->id]);
|
|
|
|
|
|
|
|
// change transaction type of journal:
|
2021-03-23 00:42:26 -05:00
|
|
|
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
2020-08-23 00:42:14 -05:00
|
|
|
|
|
|
|
DB::table('transaction_journals')
|
|
|
|
->where('id', '=', $journal['transaction_journal_id'])
|
2022-08-01 22:38:24 -05:00
|
|
|
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
2020-08-23 00:42:14 -05:00
|
|
|
|
2021-03-23 00:42:26 -05:00
|
|
|
Log::debug('Converted withdrawal to transfer.');
|
2020-08-23 00:42:14 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-23 00:42:26 -05:00
|
|
|
* A deposit is from Revenue to Asset.
|
|
|
|
* We replace the Revenue with another asset.
|
|
|
|
*
|
2020-08-23 00:42:14 -05:00
|
|
|
* @param array $journal
|
|
|
|
* @param Account $asset
|
2021-03-23 00:42:26 -05:00
|
|
|
*
|
2020-08-23 00:42:14 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-23 00:42:26 -05:00
|
|
|
private function convertDepositArray(array $journal, Account $asset): bool
|
2020-08-23 00:42:14 -05:00
|
|
|
{
|
2021-03-23 00:42:26 -05:00
|
|
|
if ($journal['destination_account_id'] === $asset->id) {
|
|
|
|
Log::error(
|
|
|
|
vsprintf(
|
|
|
|
'Journal #%d has already has "%s" as a destination asset. ConvertToTransfer failed. (rule #%d).',
|
|
|
|
[$journal['transaction_journal_id'], $asset->name, $this->action->rule_id]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2020-08-23 00:42:14 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-23 00:42:26 -05:00
|
|
|
// update source transaction:
|
2020-08-23 00:42:14 -05:00
|
|
|
DB::table('transactions')
|
|
|
|
->where('transaction_journal_id', '=', $journal['transaction_journal_id'])
|
2021-03-23 00:42:26 -05:00
|
|
|
->where('amount', '<', 0)
|
2020-08-23 00:42:14 -05:00
|
|
|
->update(['account_id' => $asset->id]);
|
|
|
|
|
|
|
|
// change transaction type of journal:
|
2021-03-23 00:42:26 -05:00
|
|
|
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
2020-08-23 00:42:14 -05:00
|
|
|
|
|
|
|
DB::table('transaction_journals')
|
|
|
|
->where('id', '=', $journal['transaction_journal_id'])
|
2022-08-01 22:38:24 -05:00
|
|
|
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
2020-08-23 00:42:14 -05:00
|
|
|
|
2021-03-23 00:42:26 -05:00
|
|
|
Log::debug('Converted deposit to transfer.');
|
2020-08-23 00:42:14 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-31 00:48:23 -06:00
|
|
|
}
|