2015-02-05 21:52:16 -06:00
|
|
|
<?php namespace FireflyIII\Models;
|
|
|
|
|
2015-02-24 14:10:25 -06:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
2015-02-05 21:52:16 -06:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2015-02-07 16:19:28 -06:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-02-05 21:52:16 -06:00
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* Class PiggyBank
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Models
|
|
|
|
*/
|
2015-02-05 22:04:06 -06:00
|
|
|
class PiggyBank extends Model
|
|
|
|
{
|
2015-02-07 16:19:28 -06:00
|
|
|
use SoftDeletes;
|
2015-02-05 21:52:16 -06:00
|
|
|
|
2015-02-27 04:02:08 -06:00
|
|
|
protected $fillable = ['repeats', 'name', 'account_id','rep_every', 'rep_times', 'reminder_skip', 'targetamount', 'startdate', 'targetdate', 'reminder',];
|
2015-02-25 12:32:33 -06:00
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-02-05 22:14:27 -06:00
|
|
|
public function account()
|
|
|
|
{
|
2015-02-05 22:35:00 -06:00
|
|
|
return $this->belongsTo('FireflyIII\Models\Account');
|
2015-02-05 22:14:27 -06:00
|
|
|
}
|
2015-02-05 21:52:16 -06:00
|
|
|
|
2015-02-24 14:10:25 -06:00
|
|
|
/**
|
|
|
|
* Grabs the PiggyBankRepetition that's currently relevant / active
|
|
|
|
*
|
|
|
|
* @returns PiggyBankRepetition
|
|
|
|
*/
|
|
|
|
public function currentRelevantRep()
|
|
|
|
{
|
|
|
|
if ($this->currentRep) {
|
|
|
|
return $this->currentRep;
|
|
|
|
}
|
|
|
|
if ($this->repeats == 0) {
|
|
|
|
$rep = $this->piggyBankRepetitions()->first(['piggy_bank_repetitions.*']);
|
|
|
|
$this->currentRep = $rep;
|
|
|
|
|
|
|
|
return $rep;
|
|
|
|
} else {
|
2015-02-25 12:32:33 -06:00
|
|
|
$query = $this->piggyBankRepetitions()->where(
|
2015-02-24 14:10:25 -06:00
|
|
|
function (EloquentBuilder $q) {
|
|
|
|
|
|
|
|
$q->where(
|
|
|
|
function (EloquentBuilder $q) {
|
|
|
|
|
|
|
|
$q->where(
|
|
|
|
function (EloquentBuilder $q) {
|
|
|
|
$today = new Carbon;
|
|
|
|
$q->whereNull('startdate');
|
|
|
|
$q->orWhere('startdate', '<=', $today->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
)->where(
|
|
|
|
function (EloquentBuilder $q) {
|
|
|
|
$today = new Carbon;
|
|
|
|
$q->whereNull('targetdate');
|
|
|
|
$q->orWhere('targetdate', '>=', $today->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)->orWhere(
|
|
|
|
function (EloquentBuilder $q) {
|
|
|
|
$today = new Carbon;
|
|
|
|
$q->where('startdate', '>=', $today->format('Y-m-d 00:00:00'));
|
|
|
|
$q->where('targetdate', '>=', $today->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
)->orderBy('startdate', 'ASC');
|
2015-02-25 12:32:33 -06:00
|
|
|
$result = $query->first(['piggy_bank_repetitions.*']);
|
2015-02-24 14:10:25 -06:00
|
|
|
$this->currentRep = $result;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2015-02-25 12:32:33 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function piggyBankRepetitions()
|
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\PiggyBankRepetition');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDates()
|
|
|
|
{
|
|
|
|
return ['created_at', 'updated_at', 'deleted_at', 'startdate', 'targetdate'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function piggyBankEvents()
|
|
|
|
{
|
|
|
|
return $this->hasMany('FireflyIII\Models\PiggyBankEvent');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
|
|
|
|
*/
|
|
|
|
public function reminders()
|
|
|
|
{
|
|
|
|
return $this->morphMany('FireflyIII\Models\Reminder', 'remindersable');
|
|
|
|
}
|
2015-02-05 21:52:16 -06:00
|
|
|
}
|