| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * SetDestinationAccount.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:57:05 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02: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 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +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 | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +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/>. | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-13 07:49:58 +02:00
										 |  |  | namespace FireflyIII\TransactionRules\Actions; | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  | use FireflyIII\Enums\TransactionTypeEnum; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Models\RuleAction; | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class SetDestinationAccount. | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class SetDestinationAccount implements ActionInterface | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  |     private AccountRepositoryInterface $repository; | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * TriggerInterface constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-05-04 13:55:42 +02:00
										 |  |  |     public function __construct(private readonly RuleAction $action) {} | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-23 07:42:14 +02:00
										 |  |  |     public function actOnArray(array $journal): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-05-24 06:24:17 +02:00
										 |  |  |         $accountName      = $this->action->getValue($journal); | 
					
						
							| 
									
										
										
										
											2024-03-06 21:38:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 17:18:31 +01:00
										 |  |  |         /** @var User $user */ | 
					
						
							| 
									
										
										
										
											2025-05-24 06:24:17 +02:00
										 |  |  |         $user             = User::find($journal['user_id']); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         /** @var null|TransactionJournal $object */ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +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; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-24 06:24:17 +02:00
										 |  |  |         $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:
 | 
					
						
							| 
									
										
										
										
											2025-05-24 06:24:17 +02:00
										 |  |  |         $newAccount       = $this->findAssetAccount($type, $accountName); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if ((TransactionTypeEnum::DEPOSIT->value === $type || TransactionTypeEnum::TRANSFER->value === $type) && !$newAccount instanceof Account) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error( | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |                 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, | 
					
						
							| 
									
										
										
										
											2024-03-06 20:54:50 -05:00
										 |  |  |                     $accountName | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2024-03-06 20:54:50 -05:00
										 |  |  |             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 */ | 
					
						
							| 
									
										
										
										
											2025-05-24 06:24:17 +02:00
										 |  |  |         $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
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if ($newAccount instanceof Account && $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.
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:05:19 +01:00
										 |  |  |         if (TransactionTypeEnum::WITHDRAWAL->value === $type) { | 
					
						
							| 
									
										
										
										
											2024-03-06 20:54:50 -05:00
										 |  |  |             $newAccount = $this->findWithdrawalDestinationAccount($accountName); | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if (!$newAccount instanceof Account) { | 
					
						
							| 
									
										
										
										
											2025-05-22 06:06:30 +02:00
										 |  |  |             app('log')->error( | 
					
						
							|  |  |  |                 sprintf( | 
					
						
							|  |  |  |                     'No destination account found for name "%s".', | 
					
						
							|  |  |  |                     $accountName | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_destination', ['name' => $accountName]))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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:
 | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |         DB::table('transactions') | 
					
						
							| 
									
										
										
										
											2025-05-24 06:24:17 +02:00
										 |  |  |             ->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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 20:54:50 -05: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 20:54:50 -05:00
										 |  |  |         return $this->repository->findByName($accountName, $allowed); | 
					
						
							| 
									
										
										
										
											2020-08-23 07:42:14 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 20:54:50 -05:00
										 |  |  |     private function findWithdrawalDestinationAccount(string $accountName): Account | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-26 06:18:30 +02:00
										 |  |  |         $allowed = config('firefly.expected_source_types.destination.Withdrawal'); | 
					
						
							| 
									
										
										
										
											2024-03-06 20:54:50 -05:00
										 |  |  |         $account = $this->repository->findByName($accountName, $allowed); | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |         if (!$account instanceof Account) { | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |             $data    = [ | 
					
						
							| 
									
										
										
										
											2024-03-06 20:54:50 -05:00
										 |  |  |                 '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, | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |             ]; | 
					
						
							|  |  |  |             $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)); | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $account; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-29 09:22:51 +02:00
										 |  |  | } |