| 
									
										
										
										
											2014-07-20 18:24:27 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Firefly\Storage\Limit; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 13:02:01 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class EloquentLimitRepository | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package Firefly\Storage\Limit | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-07-20 18:24:27 +02:00
										 |  |  | class EloquentLimitRepository implements LimitRepositoryInterface | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-07-20 20:46:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 13:02:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-10 15:01:46 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param \Limit $limit | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-08-09 10:01:37 +02:00
										 |  |  |     public function destroy(\Limit $limit) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $limit->delete(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-19 13:24:11 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param \Limit $limit | 
					
						
							|  |  |  |      * @param $data | 
					
						
							|  |  |  |      * @return mixed|void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function update(\Limit $limit, $data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $limit->startdate = new Carbon($data['startdate']); | 
					
						
							|  |  |  |         $limit->repeat_freq = $data['period']; | 
					
						
							|  |  |  |         $limit->repeats = isset($data['repeats']) && $data['repeats'] == '1' ? 1 : 0; | 
					
						
							|  |  |  |         $limit->amount = floatval($data['amount']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $limit->save(); | 
					
						
							|  |  |  |         return $limit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 13:02:01 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $limitId | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-07-20 20:46:10 +02:00
										 |  |  |     public function find($limitId) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-07-24 06:47:28 +02:00
										 |  |  |         return \Limit::with('limitrepetitions')->where('limits.id', $limitId)->leftJoin( | 
					
						
							|  |  |  |             'components', 'components.id', '=', 'limits.component_id' | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2014-07-23 08:16:04 +02:00
										 |  |  |             ->where('components.user_id', \Auth::user()->id)->first(['limits.*']); | 
					
						
							| 
									
										
										
										
											2014-07-20 20:46:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-09 10:01:37 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param \Budget $budget | 
					
						
							| 
									
										
										
										
											2014-08-19 13:24:11 +02:00
										 |  |  |      * @param Carbon $start | 
					
						
							|  |  |  |      * @param Carbon $end | 
					
						
							| 
									
										
										
										
											2014-08-09 10:01:37 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getTJByBudgetAndDateRange(\Budget $budget, Carbon $start, Carbon $end) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $result = $budget->transactionjournals()->with('transactions')->after($start)->before($end)->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-25 13:02:01 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param $data | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return \Limit | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-07-20 18:24:27 +02:00
										 |  |  |     public function store($data) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $budget = \Budget::find($data['budget_id']); | 
					
						
							|  |  |  |         if (is_null($budget)) { | 
					
						
							|  |  |  |             \Session::flash('error', 'No such budget.'); | 
					
						
							| 
									
										
										
										
											2014-07-29 19:37:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-20 18:24:27 +02:00
										 |  |  |             return new \Limit; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // set the date to the correct start period:
 | 
					
						
							| 
									
										
										
										
											2014-07-25 13:02:01 +02:00
										 |  |  |         $date = new Carbon($data['startdate']); | 
					
						
							| 
									
										
										
										
											2014-07-20 18:24:27 +02:00
										 |  |  |         switch ($data['period']) { | 
					
						
							|  |  |  |             case 'daily': | 
					
						
							|  |  |  |                 $date->startOfDay(); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'weekly': | 
					
						
							|  |  |  |                 $date->startOfWeek(); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'monthly': | 
					
						
							|  |  |  |                 $date->startOfMonth(); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'quarterly': | 
					
						
							|  |  |  |                 $date->firstOfQuarter(); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'half-year': | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (intval($date->format('m')) >= 7) { | 
					
						
							|  |  |  |                     $date->startOfYear(); | 
					
						
							|  |  |  |                     $date->addMonths(6); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $date->startOfYear(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 'yearly': | 
					
						
							|  |  |  |                 $date->startOfYear(); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // find existing:
 | 
					
						
							|  |  |  |         $count = \Limit:: | 
					
						
							| 
									
										
										
										
											2014-08-19 13:24:11 +02:00
										 |  |  |         leftJoin('components', 'components.id', '=', 'limits.component_id')->where( | 
					
						
							|  |  |  |             'components.user_id', \Auth::user()->id | 
					
						
							|  |  |  |         )->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where( | 
					
						
							|  |  |  |             'repeat_freq', $data['period'] | 
					
						
							|  |  |  |         )->count(); | 
					
						
							| 
									
										
										
										
											2014-07-20 18:24:27 +02:00
										 |  |  |         if ($count > 0) { | 
					
						
							|  |  |  |             \Session::flash('error', 'There already is an entry for these parameters.'); | 
					
						
							| 
									
										
										
										
											2014-07-29 19:37:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-20 18:24:27 +02:00
										 |  |  |             return new \Limit; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // create new limit:
 | 
					
						
							|  |  |  |         $limit = new \Limit; | 
					
						
							|  |  |  |         $limit->budget()->associate($budget); | 
					
						
							|  |  |  |         $limit->startdate = $date; | 
					
						
							|  |  |  |         $limit->amount = floatval($data['amount']); | 
					
						
							|  |  |  |         $limit->repeats = isset($data['repeats']) ? intval($data['repeats']) : 0; | 
					
						
							|  |  |  |         $limit->repeat_freq = $data['period']; | 
					
						
							|  |  |  |         if (!$limit->save()) { | 
					
						
							| 
									
										
										
										
											2014-07-30 22:31:35 +02:00
										 |  |  |             \Session::flash('error', 'Could not save: ' . $limit->errors()->first()); | 
					
						
							| 
									
										
										
										
											2014-07-20 18:24:27 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-07-29 19:37:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-20 18:24:27 +02:00
										 |  |  |         return $limit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  |