| 
									
										
										
										
											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
										 |  |  | /* | 
					
						
							|  |  |  |  * PiggyBankObserver.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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-09 07:02:12 +01:00
										 |  |  | use FireflyIII\Models\Attachment; | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							| 
									
										
										
										
											2025-02-05 06:36:51 +01:00
										 |  |  | use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2024-12-20 05:31:16 +01:00
										 |  |  | use FireflyIII\Support\Http\Api\ExchangeRateConverter; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class PiggyBankObserver | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class PiggyBankObserver | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     public function created(PiggyBank $piggyBank): void | 
					
						
							| 
									
										
										
										
											2024-12-20 05:31:16 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         Log::debug('Observe "created" of a piggy bank.'); | 
					
						
							| 
									
										
										
										
											2025-07-31 20:55:30 +02:00
										 |  |  |         $this->updatePrimaryCurrencyAmount($piggyBank); | 
					
						
							| 
									
										
										
										
											2024-12-20 05:31:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-31 20:55:30 +02:00
										 |  |  |     private function updatePrimaryCurrencyAmount(PiggyBank $piggyBank): void | 
					
						
							| 
									
										
										
										
											2025-05-04 17:41:26 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $group                           = $piggyBank->accounts()->first()?->user->userGroup; | 
					
						
							|  |  |  |         if (null === $group) { | 
					
						
							|  |  |  |             Log::debug(sprintf('No account(s) yet for piggy bank #%d.', $piggyBank->id)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-07-31 20:17:46 +02:00
										 |  |  |         $userCurrency                    = app('amount')->getPrimaryCurrencyByUserGroup($group); | 
					
						
							| 
									
										
										
										
											2025-05-04 17:41:26 +02:00
										 |  |  |         $piggyBank->native_target_amount = null; | 
					
						
							|  |  |  |         if ($piggyBank->transactionCurrency->id !== $userCurrency->id) { | 
					
						
							|  |  |  |             $converter                       = new ExchangeRateConverter(); | 
					
						
							|  |  |  |             $converter->setIgnoreSettings(true); | 
					
						
							|  |  |  |             $converter->setUserGroup($group); | 
					
						
							|  |  |  |             $piggyBank->native_target_amount = $converter->convert($piggyBank->transactionCurrency, $userCurrency, today(), $piggyBank->target_amount); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $piggyBank->saveQuietly(); | 
					
						
							| 
									
										
										
										
											2025-07-31 20:55:30 +02:00
										 |  |  |         Log::debug('Piggy bank primary currency target amount is updated.'); | 
					
						
							| 
									
										
										
										
											2025-05-04 17:41:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Also delete related objects. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function deleting(PiggyBank $piggyBank): void | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         app('log')->debug('Observe "deleting" of a piggy bank.'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-05 06:36:51 +01:00
										 |  |  |         $repository = app(AttachmentRepositoryInterface::class); | 
					
						
							|  |  |  |         $repository->setUser($piggyBank->accounts()->first()->user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-09 07:02:12 +01:00
										 |  |  |         /** @var Attachment $attachment */ | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         foreach ($piggyBank->attachments()->get() as $attachment) { | 
					
						
							| 
									
										
										
										
											2025-02-05 06:36:51 +01:00
										 |  |  |             $repository->destroy($attachment); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $piggyBank->piggyBankEvents()->delete(); | 
					
						
							|  |  |  |         $piggyBank->piggyBankRepetitions()->delete(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $piggyBank->notes()->delete(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function updated(PiggyBank $piggyBank): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Log::debug('Observe "updated" of a piggy bank.'); | 
					
						
							| 
									
										
										
										
											2025-07-31 20:55:30 +02:00
										 |  |  |         $this->updatePrimaryCurrencyAmount($piggyBank); | 
					
						
							| 
									
										
										
										
											2024-12-20 05:31:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-21 11:29:09 +02:00
										 |  |  | } |