| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | namespace FireflyIII\Event; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Illuminate\Database\QueryException; | 
					
						
							|  |  |  | use Illuminate\Events\Dispatcher; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class Budget | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package FireflyIII\Event | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  | class Budget | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |      * @param \BudgetLimit $budgetLimit | 
					
						
							| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |     public function storeOrUpdateLimit(\BudgetLimit $budgetLimit) | 
					
						
							| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |         $end = \DateKit::addPeriod(clone $budgetLimit->startdate, $budgetLimit->repeat_freq, 0); | 
					
						
							| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  |         $end->subDay(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |         $set = $budgetLimit->limitrepetitions()->where('startdate', $budgetLimit->startdate->format('Y-m-d'))->where('enddate', $end->format('Y-m-d'))->get(); | 
					
						
							| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  |         /* | 
					
						
							|  |  |  |          * Create new LimitRepetition: | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         if ($set->count() == 0) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $repetition            = new \LimitRepetition(); | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |             $repetition->startdate = $budgetLimit->startdate; | 
					
						
							| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  |             $repetition->enddate   = $end; | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |             $repetition->amount    = $budgetLimit->amount; | 
					
						
							|  |  |  |             $repetition->budgetLimit()->associate($budgetLimit); | 
					
						
							| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |                 $repetition->save(); | 
					
						
							|  |  |  |             } catch (QueryException $e) { | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |                 \Log::error('Trying to save new LimitRepetition failed!'); | 
					
						
							| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  |                 \Log::error($e->getMessage()); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if ($set->count() == 1) { | 
					
						
							|  |  |  |                 /* | 
					
						
							|  |  |  |                  * Update existing one. | 
					
						
							|  |  |  |                  */ | 
					
						
							|  |  |  |                 $repetition         = $set->first(); | 
					
						
							| 
									
										
										
										
											2014-12-13 21:59:02 +01:00
										 |  |  |                 $repetition->amount = $budgetLimit->amount; | 
					
						
							| 
									
										
										
										
											2014-11-17 21:50:11 +01:00
										 |  |  |                 $repetition->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Dispatcher $events | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function subscribe(Dispatcher $events) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $events->listen('limits.store', 'FireflyIII\Event\Budget@storeOrUpdateLimit'); | 
					
						
							|  |  |  |         $events->listen('limits.update', 'FireflyIII\Event\Budget@storeOrUpdateLimit'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }  |