| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * AccountObserver.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\Handlers\Observer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Models\Account; | 
					
						
							| 
									
										
										
										
											2025-02-09 07:02:12 +01:00
										 |  |  | use FireflyIII\Models\Attachment; | 
					
						
							| 
									
										
										
										
											2025-02-16 19:30:40 +01:00
										 |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionGroup; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							| 
									
										
										
										
											2025-03-08 15:10:24 +01:00
										 |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2025-05-04 17:41:26 +02:00
										 |  |  | use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2024-12-28 07:35:20 +01:00
										 |  |  | use FireflyIII\Support\Facades\Amount; | 
					
						
							| 
									
										
										
										
											2024-12-20 05:20:37 +01:00
										 |  |  | use FireflyIII\Support\Http\Api\ExchangeRateConverter; | 
					
						
							| 
									
										
										
										
											2025-02-16 19:30:40 +01:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2024-12-20 05:20:37 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class AccountObserver | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AccountObserver | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     public function created(Account $account): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-27 12:43:57 +01:00
										 |  |  |         //        Log::debug('Observe "created" of an account.');
 | 
					
						
							| 
									
										
										
										
											2025-07-31 20:55:30 +02:00
										 |  |  |         $this->updatePrimaryCurrencyAmount($account); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-31 20:55:30 +02:00
										 |  |  |     private function updatePrimaryCurrencyAmount(Account $account): void | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-07-31 20:24:19 +02:00
										 |  |  |         if (!Amount::convertToPrimary($account->user)) { | 
					
						
							| 
									
										
										
										
											2024-12-28 07:35:20 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-07-31 20:17:46 +02:00
										 |  |  |         $userCurrency = app('amount')->getPrimaryCurrencyByUserGroup($account->user->userGroup); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $repository   = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |         $currency     = $repository->getAccountCurrency($account); | 
					
						
							|  |  |  |         if (null !== $currency && $currency->id !== $userCurrency->id && '' !== (string) $account->virtual_balance && 0 !== bccomp($account->virtual_balance, '0')) { | 
					
						
							| 
									
										
										
										
											2025-02-17 04:11:50 +01:00
										 |  |  |             $converter                       = new ExchangeRateConverter(); | 
					
						
							| 
									
										
										
										
											2025-02-01 21:14:28 +01:00
										 |  |  |             $converter->setUserGroup($account->user->userGroup); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $converter->setIgnoreSettings(true); | 
					
						
							|  |  |  |             $account->native_virtual_balance = $converter->convert($currency, $userCurrency, today(), $account->virtual_balance); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-04 08:02:05 +01:00
										 |  |  |         if ('' === (string) $account->virtual_balance || (0 === bccomp($account->virtual_balance, '0'))) { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |             $account->virtual_balance        = null; | 
					
						
							|  |  |  |             $account->native_virtual_balance = null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $account->saveQuietly(); | 
					
						
							| 
									
										
										
										
											2025-07-31 20:55:30 +02:00
										 |  |  |         // Log::debug('Account primary currency virtual balance is updated.');
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Also delete related objects. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function deleting(Account $account): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-16 19:30:40 +01:00
										 |  |  |         app('log')->debug('Observe "deleting" of an account.'); | 
					
						
							| 
									
										
										
										
											2024-12-14 21:55:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-05 06:36:51 +01:00
										 |  |  |         $repository = app(AttachmentRepositoryInterface::class); | 
					
						
							|  |  |  |         $repository->setUser($account->user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 19:30:40 +01:00
										 |  |  |         DB::table('account_piggy_bank')->where('account_id', $account->id)->delete(); | 
					
						
							| 
									
										
										
										
											2025-02-09 07:10:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-09 07:02:12 +01:00
										 |  |  |         /** @var Attachment $attachment */ | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  |         foreach ($account->attachments()->get() as $attachment) { | 
					
						
							| 
									
										
										
										
											2025-02-05 06:36:51 +01:00
										 |  |  |             $repository->destroy($attachment); | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-02-16 19:30:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $journalIds = Transaction::where('account_id', $account->id)->get(['transactions.transaction_journal_id'])->pluck('transaction_journal_id')->toArray(); | 
					
						
							| 
									
										
										
										
											2025-03-08 19:55:21 +01:00
										 |  |  |         $groupIds   = TransactionJournal::whereIn('id', $journalIds)->get(['transaction_journals.transaction_group_id'])->pluck('transaction_group_id')->toArray(); // @phpstan-ignore-line
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-16 19:30:40 +01:00
										 |  |  |         if (count($journalIds) > 0) { | 
					
						
							|  |  |  |             Transaction::whereIn('transaction_journal_id', $journalIds)->delete(); | 
					
						
							|  |  |  |             TransactionJournal::whereIn('id', $journalIds)->delete(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (count($groupIds) > 0) { | 
					
						
							|  |  |  |             TransactionGroup::whereIn('id', $groupIds)->delete(); | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-02-16 19:30:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Log::debug(sprintf('Delete %d journal(s)', count($journalIds))); | 
					
						
							|  |  |  |         Log::debug(sprintf('Delete %d group(s)', count($groupIds))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  |         $account->notes()->delete(); | 
					
						
							|  |  |  |         $account->locations()->delete(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-12-20 05:20:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function updated(Account $account): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-27 12:43:57 +01:00
										 |  |  |         //        Log::debug('Observe "updated" of an account.');
 | 
					
						
							| 
									
										
										
										
											2025-07-31 20:55:30 +02:00
										 |  |  |         $this->updatePrimaryCurrencyAmount($account); | 
					
						
							| 
									
										
										
										
											2024-12-20 05:20:37 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  | } |