| 
									
										
										
										
											2014-07-31 22:01:52 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2014-08-09 12:23:42 +02:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2014-07-31 22:01:52 +02:00
										 |  |  | use LaravelBook\Ardent\Ardent as Ardent; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-02 07:34:38 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Piggybank | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:44 +01:00
										 |  |  |  * @property integer                                                              $id | 
					
						
							|  |  |  |  * @property \Carbon\Carbon                                                       $created_at | 
					
						
							|  |  |  |  * @property \Carbon\Carbon                                                       $updated_at | 
					
						
							|  |  |  |  * @property integer                                                              $account_id | 
					
						
							|  |  |  |  * @property string                                                               $name | 
					
						
							|  |  |  |  * @property float                                                                $targetamount | 
					
						
							|  |  |  |  * @property \Carbon\Carbon                                                       $startdate | 
					
						
							|  |  |  |  * @property \Carbon\Carbon                                                       $targetdate | 
					
						
							|  |  |  |  * @property boolean                                                              $repeats | 
					
						
							|  |  |  |  * @property string                                                               $rep_length | 
					
						
							|  |  |  |  * @property integer                                                              $rep_every | 
					
						
							|  |  |  |  * @property integer                                                              $rep_times | 
					
						
							|  |  |  |  * @property string                                                               $reminder | 
					
						
							|  |  |  |  * @property integer                                                              $reminder_skip | 
					
						
							|  |  |  |  * @property boolean                                                              $remind_me | 
					
						
							|  |  |  |  * @property integer                                                              $order | 
					
						
							|  |  |  |  * @property-read \Account                                                        $account | 
					
						
							| 
									
										
										
										
											2014-08-16 07:07:42 +02:00
										 |  |  |  * @property-read \Illuminate\Database\Eloquent\Collection|\PiggybankRepetition[] $piggybankrepetitions | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:44 +01:00
										 |  |  |  * @property-read \Illuminate\Database\Eloquent\Collection|\PiggybankEvent[]      $piggybankevents | 
					
						
							|  |  |  |  * @property-read \Illuminate\Database\Eloquent\Collection|\Transaction[]         $transactions | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereId($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereCreatedAt($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereUpdatedAt($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereAccountId($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereName($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetamount($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereStartdate($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetdate($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepeats($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepLength($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepEvery($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepTimes($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminder($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminderSkip($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereRemindMe($value) | 
					
						
							|  |  |  |  * @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value) | 
					
						
							| 
									
										
										
										
											2014-08-02 07:34:38 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-07-31 22:01:52 +02:00
										 |  |  | class Piggybank extends Ardent | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public static $rules | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:44 +01:00
										 |  |  |         = ['account_id'    => 'required|exists:accounts,id', // link to Account
 | 
					
						
							|  |  |  |            'name'          => 'required|between:1,255', // name
 | 
					
						
							|  |  |  |            'targetamount'  => 'required|min:0', // amount you want to save
 | 
					
						
							|  |  |  |            'startdate'     => 'date', // when you started
 | 
					
						
							|  |  |  |            'targetdate'    => 'date', // when its due
 | 
					
						
							|  |  |  |            'repeats'       => 'required|boolean', // does it repeat?
 | 
					
						
							|  |  |  |            'rep_length'    => 'in:day,week,month,year', // how long is the period?
 | 
					
						
							|  |  |  |            'rep_every'     => 'required|min:1|max:100', // how often does it repeat? every 3 years.
 | 
					
						
							|  |  |  |            'rep_times'     => 'min:1|max:100', // how many times do you want to save this amount? eg. 3 times
 | 
					
						
							|  |  |  |            'reminder'      => 'in:day,week,month,year', // want a reminder to put money in this?
 | 
					
						
							|  |  |  |            'reminder_skip' => 'required|min:0|max:100', // every week? every 2 months?
 | 
					
						
							|  |  |  |            'remind_me'     => 'required|boolean', 'order' => 'required:min:1', // not yet used.
 | 
					
						
							| 
									
										
										
										
											2014-07-31 22:01:52 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:44 +01:00
										 |  |  |     public        $fillable | 
					
						
							|  |  |  |         = ['account_id', 'name', 'targetamount', 'startdate', 'targetdate', 'repeats', 'rep_length', 'rep_every', 'rep_times', 'reminder', 'reminder_skip', | 
					
						
							|  |  |  |            'remind_me', 'order']; | 
					
						
							| 
									
										
										
										
											2014-07-31 22:01:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-10 18:22:42 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-07-31 22:01:52 +02:00
										 |  |  |     public function account() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->belongsTo('Account'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-21 21:00:02 +02:00
										 |  |  |     public function createRepetition(Carbon $start = null, Carbon $target = null) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-08-21 15:14:46 +02:00
										 |  |  |         $rep = new \PiggybankRepetition; | 
					
						
							|  |  |  |         $rep->piggybank()->associate($this); | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:44 +01:00
										 |  |  |         $rep->startdate     = $start; | 
					
						
							|  |  |  |         $rep->targetdate    = $target; | 
					
						
							| 
									
										
										
										
											2014-08-21 15:14:46 +02:00
										 |  |  |         $rep->currentamount = 0; | 
					
						
							|  |  |  |         $rep->save(); | 
					
						
							| 
									
										
										
										
											2014-08-21 21:00:02 +02:00
										 |  |  |         \Event::fire('piggybanks.repetition', [$rep]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-21 15:14:46 +02:00
										 |  |  |         return $rep; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-16 07:07:42 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Grabs the PiggyBankRepetition that's currently relevant / active | 
					
						
							| 
									
										
										
										
											2014-08-17 08:45:22 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @returns \PiggybankRepetition | 
					
						
							| 
									
										
										
										
											2014-08-16 07:07:42 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-08-17 21:17:31 +02:00
										 |  |  |     public function currentRelevantRep() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-11-15 07:46:01 +01:00
										 |  |  |         if($this->currentRep) { | 
					
						
							|  |  |  |             return $this->currentRep; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($this->repeats == 0) { | 
					
						
							|  |  |  |             $rep = $this->piggybankrepetitions()->first(); | 
					
						
							|  |  |  |             $this->currentRep = $rep; | 
					
						
							|  |  |  |             return $rep; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             $query  = $this->piggybankrepetitions()->where( | 
					
						
							| 
									
										
										
										
											2014-08-17 21:17:31 +02:00
										 |  |  |                 function ($q) { | 
					
						
							| 
									
										
										
										
											2014-08-19 06:57:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     $q->where( | 
					
						
							|  |  |  |                         function ($q) { | 
					
						
							|  |  |  |                             $today = new Carbon; | 
					
						
							|  |  |  |                             $q->whereNull('startdate'); | 
					
						
							|  |  |  |                             $q->orWhere('startdate', '<=', $today->format('Y-m-d')); | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:44 +01:00
										 |  |  |                     )->where( | 
					
						
							| 
									
										
										
										
											2014-11-15 07:46:01 +01:00
										 |  |  |                         function ($q) { | 
					
						
							|  |  |  |                             $today = new Carbon; | 
					
						
							|  |  |  |                             $q->whereNull('targetdate'); | 
					
						
							|  |  |  |                             $q->orWhere('targetdate', '>=', $today->format('Y-m-d')); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     ); | 
					
						
							| 
									
										
										
										
											2014-08-17 21:17:31 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2014-08-19 06:57:11 +02:00
										 |  |  |             )->orWhere( | 
					
						
							| 
									
										
										
										
											2014-08-17 21:17:31 +02:00
										 |  |  |                 function ($q) { | 
					
						
							| 
									
										
										
										
											2014-08-16 07:07:42 +02:00
										 |  |  |                     $today = new Carbon; | 
					
						
							| 
									
										
										
										
											2014-08-19 06:57:11 +02:00
										 |  |  |                     $q->where('startdate', '>=', $today->format('Y-m-d')); | 
					
						
							|  |  |  |                     $q->where('targetdate', '>=', $today->format('Y-m-d')); | 
					
						
							| 
									
										
										
										
											2014-08-17 21:17:31 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2014-08-19 06:57:11 +02:00
										 |  |  |             )->orderBy('startdate', 'ASC'); | 
					
						
							| 
									
										
										
										
											2014-11-15 07:46:01 +01:00
										 |  |  |             $result = $query->first(); | 
					
						
							|  |  |  |             $this->currentRep = $result; | 
					
						
							| 
									
										
										
										
											2014-08-17 21:17:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 07:46:01 +01:00
										 |  |  |             return $result; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-08-16 07:07:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @return \Illuminate\Database\Eloquent\Relations\HasMany | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-08-14 07:40:15 +02:00
										 |  |  |     public function piggybankrepetitions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany('PiggybankRepetition'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:44 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getDates() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return ['created_at', 'updated_at', 'targetdate', 'startdate']; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-18 12:28:33 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return \Illuminate\Database\Eloquent\Relations\HasMany | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function piggybankevents() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany('PiggybankEvent'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-19 06:57:11 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Same but for specific date. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Carbon $date | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @returns \PiggybankRepetition | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function repetitionForDate(Carbon $date) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-11-12 22:37:44 +01:00
										 |  |  |         $query  = $this->piggybankrepetitions()->where( | 
					
						
							| 
									
										
										
										
											2014-11-15 07:46:01 +01:00
										 |  |  |             function ($q) use ($date) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $q->where( | 
					
						
							|  |  |  |                     function ($q) use ($date) { | 
					
						
							|  |  |  |                         $q->whereNull('startdate'); | 
					
						
							|  |  |  |                         $q->orWhere('startdate', '<=', $date->format('Y-m-d')); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 )->where( | 
					
						
							|  |  |  |                     function ($q) use ($date) { | 
					
						
							|  |  |  |                         $q->whereNull('targetdate'); | 
					
						
							|  |  |  |                         $q->orWhere('targetdate', '>=', $date->format('Y-m-d')); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         )->orWhere( | 
					
						
							|  |  |  |             function ($q) use ($date) { | 
					
						
							|  |  |  |                 $q->where('startdate', '>=', $date->format('Y-m-d')); | 
					
						
							|  |  |  |                 $q->where('targetdate', '>=', $date->format('Y-m-d')); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         )->orderBy('startdate', 'ASC'); | 
					
						
							| 
									
										
										
										
											2014-08-19 06:57:11 +02:00
										 |  |  |         $result = $query->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function transactions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany('Transaction'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-31 22:01:52 +02:00
										 |  |  | }  |