| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  | /* | 
					
						
							|  |  |  |  * SetsourceToCashAccount.php | 
					
						
							|  |  |  |  * Copyright (c) 2023 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program 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 Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\TransactionRules\Actions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-03 09:09:15 +01:00
										 |  |  | use FireflyIII\Enums\TransactionTypeEnum; | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  | use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray; | 
					
						
							|  |  |  | use FireflyIII\Events\TriggeredAuditLog; | 
					
						
							|  |  |  | use FireflyIII\Models\RuleAction; | 
					
						
							|  |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  |  * Class SetSourceToCashAccount | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class SetSourceToCashAccount implements ActionInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * TriggerInterface constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-05-04 13:55:42 +02:00
										 |  |  |     public function __construct(private readonly RuleAction $action) {} | 
					
						
							| 
									
										
										
										
											2023-12-10 06:51:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-03 08:28:50 +01:00
										 |  |  |     public function actOnArray(array $journal): bool | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         /** @var User $user */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01: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']); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $repository  = app(AccountRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (null === $object) { | 
					
						
							|  |  |  |             app('log')->error('Could not find journal.'); | 
					
						
							|  |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_such_journal'))); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $type        = $object->transactionType->type; | 
					
						
							| 
									
										
										
										
											2025-01-03 09:09:15 +01:00
										 |  |  |         if (TransactionTypeEnum::DEPOSIT->value !== $type) { | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |             app('log')->error('Transaction must be deposit.'); | 
					
						
							|  |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.not_deposit'))); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // find cash account
 | 
					
						
							|  |  |  |         $repository->setUser($user); | 
					
						
							|  |  |  |         $cashAccount = $repository->getCashAccount(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // new source account must be different from the current destination account:
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         /** @var null|Transaction $destination */ | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |         $destination = $object->transactions()->where('amount', '>', 0)->first(); | 
					
						
							|  |  |  |         if (null === $destination) { | 
					
						
							|  |  |  |             app('log')->error('Could not find destination transaction.'); | 
					
						
							|  |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction'))); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // account must not be deleted (in the meantime):
 | 
					
						
							|  |  |  |         if (null === $destination->account) { | 
					
						
							|  |  |  |             app('log')->error('Could not find destination transaction account.'); | 
					
						
							|  |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_destination_transaction_account'))); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-03 08:28:50 +01:00
										 |  |  |         if ($cashAccount->id === $destination->account_id) { | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |             app('log')->error( | 
					
						
							|  |  |  |                 sprintf( | 
					
						
							|  |  |  |                     'New source account ID #%d and current destination account ID #%d are the same. Do nothing.', | 
					
						
							|  |  |  |                     $cashAccount->id, | 
					
						
							|  |  |  |                     $destination->account_id | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_has_source', ['name' => $cashAccount->name]))); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         event(new TriggeredAuditLog($this->action->rule, $object, 'set_source', null, $cashAccount->name)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // update destination transaction with new destination account:
 | 
					
						
							| 
									
										
										
										
											2025-02-23 12:47:04 +01:00
										 |  |  |         DB::table('transactions') | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ->where('transaction_journal_id', '=', $object->id) | 
					
						
							|  |  |  |             ->where('amount', '<', 0) | 
					
						
							|  |  |  |             ->update(['account_id' => $cashAccount->id]) | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2023-12-02 15:38:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new source account ID.', $object->id, $object->transaction_group_id)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |