| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  |  * BudgetEventHandler.php | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |  * Copyright (C) 2016 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * This software may be modified and distributed under the terms of the | 
					
						
							|  |  |  |  * Creative Commons Attribution-ShareAlike 4.0 International License. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See the LICENSE file for details. | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:27:31 +02:00
										 |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  | namespace FireflyIII\Handlers\Events; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 19:12:16 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  | use FireflyIII\Events\StoredBudgetLimit; | 
					
						
							|  |  |  | use FireflyIII\Events\UpdatedBudgetLimit; | 
					
						
							| 
									
										
										
										
											2016-11-12 19:12:16 +01:00
										 |  |  | use FireflyIII\Models\BudgetLimit; | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  | use FireflyIII\Models\LimitRepetition; | 
					
						
							|  |  |  | use Illuminate\Database\QueryException; | 
					
						
							|  |  |  | use Log; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  |  * Handles budget related events. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Class BudgetEventHandler | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Handlers\Events | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  | class BudgetEventHandler | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  |      * This method creates a new budget limit repetition when a new budget limit has been created. | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-12-06 16:55:13 +01:00
										 |  |  |      * @param StoredBudgetLimit $budgetLimitEvent | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-14 18:59:12 +01:00
										 |  |  |     public function storeRepetition(StoredBudgetLimit $budgetLimitEvent): bool | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-12-06 16:55:13 +01:00
										 |  |  |         return $this->processRepetitionChange($budgetLimitEvent->budgetLimit, $budgetLimitEvent->end); | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  |      * Updates, if present the budget limit repetition part of a budget limit. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-12-06 16:55:13 +01:00
										 |  |  |      * @param UpdatedBudgetLimit $budgetLimitEvent | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-06 16:55:13 +01:00
										 |  |  |     public function updateRepetition(UpdatedBudgetLimit $budgetLimitEvent): bool | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-12-06 16:55:13 +01:00
										 |  |  |         return $this->processRepetitionChange($budgetLimitEvent->budgetLimit, $budgetLimitEvent->end); | 
					
						
							| 
									
										
										
										
											2016-11-12 19:12:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param BudgetLimit $budgetLimit | 
					
						
							|  |  |  |      * @param Carbon      $date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-14 18:59:12 +01:00
										 |  |  |     private function processRepetitionChange(BudgetLimit $budgetLimit, Carbon $date): bool | 
					
						
							| 
									
										
										
										
											2016-11-12 19:12:16 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $set = $budgetLimit->limitrepetitions() | 
					
						
							|  |  |  |                            ->where('startdate', $budgetLimit->startdate->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                            ->where('enddate', $date->format('Y-m-d 00:00:00')) | 
					
						
							|  |  |  |                            ->get(); | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |         if ($set->count() == 0) { | 
					
						
							|  |  |  |             $repetition            = new LimitRepetition; | 
					
						
							|  |  |  |             $repetition->startdate = $budgetLimit->startdate; | 
					
						
							| 
									
										
										
										
											2016-11-12 19:12:16 +01:00
										 |  |  |             $repetition->enddate   = $date; | 
					
						
							| 
									
										
										
										
											2016-04-28 10:59:36 +02:00
										 |  |  |             $repetition->amount    = $budgetLimit->amount; | 
					
						
							|  |  |  |             $repetition->budgetLimit()->associate($budgetLimit); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 $repetition->save(); | 
					
						
							|  |  |  |             } catch (QueryException $e) { | 
					
						
							|  |  |  |                 Log::error('Trying to save new LimitRepetition failed: ' . $e->getMessage()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($set->count() == 1) { | 
					
						
							|  |  |  |             $repetition         = $set->first(); | 
					
						
							|  |  |  |             $repetition->amount = $budgetLimit->amount; | 
					
						
							|  |  |  |             $repetition->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 09:31:27 +02:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-23 12:42:44 +02:00
										 |  |  | } |