| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * SetSourceAccount.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:57:05 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +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:03:14 +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:03:14 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-13 07:49:58 +02:00
										 |  |  | namespace FireflyIII\TransactionRules\Actions; | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  | use DB; | 
					
						
							| 
									
										
										
										
											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:03:14 +02:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Models\RuleAction; | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  | use FireflyIII\Models\TransactionType; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:04:42 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class SetSourceAccount. | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class SetSourceAccount implements ActionInterface | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  |     private RuleAction                 $action; | 
					
						
							|  |  |  |     private AccountRepositoryInterface $repository; | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * TriggerInterface constructor. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param RuleAction $action | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(RuleAction $action) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->action = $action; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-23 07:42:14 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @inheritDoc | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function actOnArray(array $journal): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |         $user = User::find($journal['user_id']); | 
					
						
							|  |  |  |         $type = $journal['transaction_type_type']; | 
					
						
							| 
									
										
										
										
											2021-04-06 13:30:09 +02:00
										 |  |  |         /** @var TransactionJournal|null $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) { | 
					
						
							|  |  |  |             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'))); | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-10 17:57:43 +01:00
										 |  |  |         $type = $object->transactionType->type; | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02: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) { | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |             Log::error( | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  |                 sprintf('Cant change source account of journal #%d because no asset account with name "%s" exists.', $object->id, $this->action->action_value) | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |             ); | 
					
						
							| 
									
										
										
										
											2023-08-13 15:01:12 +02:00
										 |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_asset', ['name' => $this->action->action_value]))); | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-05 21:53:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  |         // new source account must be different from the current destination account:
 | 
					
						
							| 
									
										
										
										
											2021-04-06 13:30:09 +02:00
										 |  |  |         /** @var Transaction|null $destination */ | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  |         $destination = $object->transactions()->where('amount', '>', 0)->first(); | 
					
						
							|  |  |  |         if (null === $destination) { | 
					
						
							|  |  |  |             Log::error('Could not find destination transaction.'); | 
					
						
							| 
									
										
										
										
											2023-08-13 15:01:12 +02:00
										 |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction'))); | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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 === $destination->account) { | 
					
						
							|  |  |  |             Log::error('Could not find destination transaction account.'); | 
					
						
							| 
									
										
										
										
											2023-08-13 15:01:12 +02:00
										 |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction_account'))); | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |         if (null !== $newAccount && (int)$newAccount->id === (int)$destination->account_id) { | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |             Log::error( | 
					
						
							|  |  |  |                 sprintf( | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |                     'New source account ID #%d and current destination account ID #%d are the same. Do nothing.', | 
					
						
							|  |  |  |                     $newAccount->id, | 
					
						
							| 
									
										
										
										
											2021-04-05 22:12:11 +02:00
										 |  |  |                     $destination->account_id | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2023-08-13 15:01:12 +02:00
										 |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_source', ['name' => $newAccount->name]))); | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-05 21:53:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  |         // if this is a deposit, the new source account must be a revenue account and may be created:
 | 
					
						
							| 
									
										
										
										
											2023-08-13 15:01:12 +02:00
										 |  |  |         // or it's a liability
 | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  |         if (TransactionType::DEPOSIT === $type) { | 
					
						
							| 
									
										
										
										
											2021-04-26 06:18:30 +02:00
										 |  |  |             $newAccount = $this->findDepositSourceAccount(); | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Log::debug(sprintf('New source account is #%d ("%s").', $newAccount->id, $newAccount->name)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // update source transaction with new source account:
 | 
					
						
							|  |  |  |         DB::table('transactions') | 
					
						
							| 
									
										
										
										
											2021-04-05 21:53:28 +02:00
										 |  |  |           ->where('transaction_journal_id', '=', $object->id) | 
					
						
							| 
									
										
										
										
											2020-08-23 16:12:16 +02:00
										 |  |  |           ->where('amount', '<', 0) | 
					
						
							|  |  |  |           ->update(['account_id' => $newAccount->id]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-02 06:23:31 +02:00
										 |  |  |         event(new TriggeredAuditLog($this->action->rule, $object, 'set_source', null, $newAccount->name)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-05 21:53:28 +02: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 16:12:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2020-08-23 07:42:14 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param string $type | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-04-06 13:30:09 +02:00
										 |  |  |      * @return Account | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-04-26 06:18:30 +02:00
										 |  |  |     private function findDepositSourceAccount(): Account | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $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 21:53:28 +02:00
										 |  |  |                 'name'              => $this->action->action_value, | 
					
						
							|  |  |  |                 'account_type_name' => 'revenue', | 
					
						
							|  |  |  |                 'account_type_id'   => null, | 
					
						
							|  |  |  |                 'virtual_balance'   => 0, | 
					
						
							|  |  |  |                 'active'            => true, | 
					
						
							|  |  |  |                 'iban'              => null, | 
					
						
							| 
									
										
										
										
											2021-03-23 06:42:26 +01:00
										 |  |  |             ]; | 
					
						
							|  |  |  |             $account = $this->repository->store($data); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug(sprintf('Found or created revenue account #%d ("%s")', $account->id, $account->name)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $account; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-29 09:03:14 +02:00
										 |  |  | } |