firefly-iii/app/models/Piggybank.php

128 lines
4.5 KiB
PHP
Raw Normal View History

2014-07-31 15:01:52 -05:00
<?php
use Carbon\Carbon;
2014-12-23 14:55:08 -06:00
use Illuminate\Database\Eloquent\Model as Eloquent;
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
/**
* Class PiggyBank
2014-12-13 14:59:02 -06:00
*/
class PiggyBank extends Eloquent
2014-07-31 15:01:52 -05: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'];
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
'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?
'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');
}
/**
* Grabs the PiggyBankRepetition that's currently relevant / active
*
* @returns \PiggyBankRepetition
*/
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) {
$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) {
$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-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-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-11-15 00:46:01 -06:00
return $result;
}
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function piggyBankRepetitions()
{
return $this->hasMany('PiggyBankRepetition');
}
2014-11-12 15:37:44 -06:00
/**
* @return array
*/
public function getDates()
{
return ['created_at', 'updated_at', 'targetdate', 'startdate'];
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function piggyBankEvents()
{
return $this->hasMany('PiggyBankEvent');
}
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
}