| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * PiggyBankEventFactory.php | 
					
						
							| 
									
										
										
										
											2020-02-16 14:00:57 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01: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. | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01: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. | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01: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/>. | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Factory; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Models\PiggyBank; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							|  |  |  | use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |  * Create piggy bank events. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |  * Class PiggyBankEventFactory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class PiggyBankEventFactory | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-11 07:17:59 +01:00
										 |  |  |     public function create(TransactionJournal $journal, ?PiggyBank $piggyBank): void | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Now in PiggyBankEventCreate for a %s', $journal->transactionType->type)); | 
					
						
							| 
									
										
										
										
											2018-04-02 14:42:07 +02:00
										 |  |  |         if (null === $piggyBank) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug('Piggy bank is null'); | 
					
						
							| 
									
										
										
										
											2020-03-17 14:57:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-11 07:17:59 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 19:44:46 +01:00
										 |  |  |         /** @var PiggyBankRepositoryInterface $piggyRepos */ | 
					
						
							|  |  |  |         $piggyRepos = app(PiggyBankRepositoryInterface::class); | 
					
						
							|  |  |  |         $piggyRepos->setUser($journal->user); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-21 23:48:54 +02:00
										 |  |  |         $repetition = $piggyRepos->getRepetition($piggyBank); | 
					
						
							|  |  |  |         if (null === $repetition) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('No piggy bank repetition on %s!', $journal->date->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-11 07:17:59 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Found repetition'); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $amount     = $piggyRepos->getExactAmount($piggyBank, $repetition, $journal); | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |         if (0 === bccomp($amount, '0')) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug('Amount is zero, will not create event.'); | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-11 07:17:59 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-11 07:29:06 +01:00
										 |  |  |         // amount can be negative here
 | 
					
						
							|  |  |  |         $piggyRepos->addAmountToRepetition($repetition, $amount, $journal); | 
					
						
							| 
									
										
										
										
											2018-02-18 10:31:15 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-05 19:35:58 +01:00
										 |  |  | } |