2014-07-31 15:01:52 -05:00
|
|
|
<?php
|
2014-08-09 05:23:42 -05:00
|
|
|
use Carbon\Carbon;
|
2014-12-23 14:55:08 -06:00
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent;
|
2014-12-31 10:15:59 -06:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletingTrait;
|
2014-12-06 05:12:55 -06:00
|
|
|
use Watson\Validating\ValidatingTrait;
|
2015-01-02 01:59:16 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
2014-12-23 14:55:08 -06:00
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
2014-12-24 13:55:42 -06:00
|
|
|
* Class PiggyBank
|
2014-12-13 14:59:02 -06:00
|
|
|
*/
|
2014-12-24 13:55:42 -06:00
|
|
|
class PiggyBank extends Eloquent
|
2014-07-31 15:01:52 -05:00
|
|
|
{
|
2014-12-31 10:15:59 -06:00
|
|
|
use ValidatingTrait, SoftDeletingTrait;
|
2014-12-30 10:27:31 -06:00
|
|
|
public $fillable
|
|
|
|
= ['account_id', 'name', 'targetamount', 'startdate', 'targetdate', 'repeats', 'rep_length', 'rep_every', 'rep_times', 'reminder', 'reminder_skip',
|
|
|
|
'remind_me', 'order'];
|
2014-12-30 08:17:01 -06:00
|
|
|
protected $rules
|
2014-11-12 15:37:44 -06:00
|
|
|
= ['account_id' => 'required|exists:accounts,id', // link to Account
|
|
|
|
'name' => 'required|between:1,255', // name
|
2014-12-30 08:17:01 -06:00
|
|
|
'targetamount' => 'required|min:0.01|numeric', // amount you want to save
|
2014-11-12 15:37:44 -06:00
|
|
|
'startdate' => 'date', // when you started
|
|
|
|
'targetdate' => 'date', // when its due
|
|
|
|
'repeats' => 'required|boolean', // does it repeat?
|
2014-11-22 16:32:10 -06:00
|
|
|
'rep_length' => 'in:day,week,month,quarter,year', // how long is the period?
|
2014-11-12 15:37:44 -06:00
|
|
|
'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
|
2014-11-24 03:12:34 -06:00
|
|
|
'reminder' => 'in:day,week,quarter,month,year', // want a reminder to put money in this?
|
2014-11-12 15:37:44 -06:00
|
|
|
'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 15:01:52 -05:00
|
|
|
];
|
|
|
|
|
2014-08-10 11:22:42 -05:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2014-07-31 15:01:52 -05:00
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('Account');
|
|
|
|
}
|
|
|
|
|
2014-08-16 00:07:42 -05:00
|
|
|
/**
|
|
|
|
* Grabs the PiggyBankRepetition that's currently relevant / active
|
2014-08-17 01:45:22 -05:00
|
|
|
*
|
2014-12-24 13:55:42 -06:00
|
|
|
* @returns \PiggyBankRepetition
|
2014-08-16 00:07:42 -05:00
|
|
|
*/
|
2014-08-17 14:17:31 -05:00
|
|
|
public function currentRelevantRep()
|
|
|
|
{
|
2014-11-15 04:36:27 -06:00
|
|
|
if ($this->currentRep) {
|
2014-11-15 00:46:01 -06:00
|
|
|
return $this->currentRep;
|
|
|
|
}
|
|
|
|
if ($this->repeats == 0) {
|
2014-12-24 13:55:42 -06:00
|
|
|
$rep = $this->piggyBankRepetitions()->first(['piggy_bank_repetitions.*']);
|
2014-11-15 00:46:01 -06:00
|
|
|
$this->currentRep = $rep;
|
2014-12-06 05:12:55 -06:00
|
|
|
|
2014-11-15 00:46:01 -06:00
|
|
|
return $rep;
|
|
|
|
} else {
|
2014-12-30 10:27:31 -06:00
|
|
|
$query = $this->piggyBankRepetitions()->where(
|
2015-01-02 01:59:16 -06:00
|
|
|
function (EloquentBuilder $q) {
|
2014-08-18 23:57:11 -05:00
|
|
|
|
|
|
|
$q->where(
|
2015-01-02 01:59:16 -06:00
|
|
|
function (EloquentBuilder $q) {
|
2014-11-24 03:12:34 -06:00
|
|
|
|
|
|
|
$q->where(
|
2015-01-02 01:59:16 -06:00
|
|
|
function (EloquentBuilder $q) {
|
2014-11-24 03:12:34 -06:00
|
|
|
$today = new Carbon;
|
|
|
|
$q->whereNull('startdate');
|
2014-12-30 10:27:31 -06:00
|
|
|
$q->orWhere('startdate', '<=', $today->format('Y-m-d 00:00:00'));
|
2014-11-24 03:12:34 -06:00
|
|
|
}
|
|
|
|
)->where(
|
2015-01-02 01:59:16 -06:00
|
|
|
function (EloquentBuilder $q) {
|
2014-11-24 03:12:34 -06:00
|
|
|
$today = new Carbon;
|
|
|
|
$q->whereNull('targetdate');
|
2014-12-30 10:27:31 -06:00
|
|
|
$q->orWhere('targetdate', '>=', $today->format('Y-m-d 00:00:00'));
|
2014-11-24 03:12:34 -06:00
|
|
|
}
|
|
|
|
);
|
2014-08-18 23:57:11 -05:00
|
|
|
}
|
2014-11-24 03:12:34 -06:00
|
|
|
)->orWhere(
|
2015-01-02 01:59:16 -06:00
|
|
|
function (EloquentBuilder $q) {
|
2014-11-15 00:46:01 -06:00
|
|
|
$today = new Carbon;
|
2014-12-30 10:27:31 -06:00
|
|
|
$q->where('startdate', '>=', $today->format('Y-m-d 00:00:00'));
|
|
|
|
$q->where('targetdate', '>=', $today->format('Y-m-d 00:00:00'));
|
2014-11-15 00:46:01 -06:00
|
|
|
}
|
|
|
|
);
|
2014-11-24 03:12:34 -06:00
|
|
|
|
2014-08-17 14:17:31 -05:00
|
|
|
}
|
2014-12-30 10:27:31 -06:00
|
|
|
)->orderBy('startdate', 'ASC');
|
|
|
|
$result = $query->first(['piggy_bank_repetitions.*']);
|
2014-11-15 00:46:01 -06:00
|
|
|
$this->currentRep = $result;
|
2014-11-24 03:12:34 -06:00
|
|
|
\Log::debug('Found relevant rep in currentRelevantRep(): ' . $result->id);
|
2014-08-17 14:17:31 -05:00
|
|
|
|
2014-11-15 00:46:01 -06:00
|
|
|
return $result;
|
|
|
|
}
|
2014-08-16 00:07:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2014-12-24 13:55:42 -06:00
|
|
|
public function piggyBankRepetitions()
|
2014-08-14 00:40:15 -05:00
|
|
|
{
|
2014-12-24 13:55:42 -06:00
|
|
|
return $this->hasMany('PiggyBankRepetition');
|
2014-08-14 00:40:15 -05:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:37:44 -06:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDates()
|
|
|
|
{
|
|
|
|
return ['created_at', 'updated_at', 'targetdate', 'startdate'];
|
|
|
|
}
|
|
|
|
|
2014-08-18 05:28:33 -05:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2014-12-24 13:55:42 -06:00
|
|
|
public function piggyBankEvents()
|
2014-08-18 05:28:33 -05:00
|
|
|
{
|
2014-12-24 13:55:42 -06:00
|
|
|
return $this->hasMany('PiggyBankEvent');
|
2014-08-18 05:28:33 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
|
|
*/
|
2014-11-24 03:12:34 -06:00
|
|
|
public function reminders()
|
|
|
|
{
|
|
|
|
return $this->morphMany('Reminder', 'remindersable');
|
|
|
|
}
|
2015-01-01 23:16:49 -06:00
|
|
|
}
|