2015-02-06 04:52:16 +01:00
|
|
|
<?php namespace FireflyIII\Models;
|
|
|
|
|
2015-02-24 21:10:25 +01:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
2015-02-06 04:52:16 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2015-02-07 23:19:28 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2015-02-06 04:52:16 +01:00
|
|
|
|
2015-02-11 07:35:10 +01:00
|
|
|
/**
|
|
|
|
* Class PiggyBank
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Models
|
|
|
|
*/
|
2015-02-06 05:04:06 +01:00
|
|
|
class PiggyBank extends Model
|
|
|
|
{
|
2015-02-07 23:19:28 +01:00
|
|
|
use SoftDeletes;
|
2015-02-06 04:52:16 +01:00
|
|
|
|
2015-03-06 15:12:07 +01:00
|
|
|
protected $fillable = ['repeats', 'name', 'account_id','rep_every', 'rep_times', 'reminder_skip', 'targetamount', 'startdate', 'targetdate', 'reminder','remind_me'];
|
2015-02-25 19:32:33 +01:00
|
|
|
|
2015-02-11 07:35:10 +01:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-02-06 05:14:27 +01:00
|
|
|
public function account()
|
|
|
|
{
|
2015-02-06 05:35:00 +01:00
|
|
|
return $this->belongsTo('FireflyIII\Models\Account');
|
2015-02-06 05:14:27 +01:00
|
|
|
}
|
2015-02-06 04:52:16 +01:00
|
|
|
|
2015-03-06 15:12:07 +01:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getRemindMeAttribute($value) {
|
|
|
|
return intval($value) == 1;
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:10:25 +01: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 19:32:33 +01:00
|
|
|
$query = $this->piggyBankRepetitions()->where(
|
2015-02-24 21:10:25 +01: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 19:32:33 +01:00
|
|
|
$result = $query->first(['piggy_bank_repetitions.*']);
|
2015-02-24 21:10:25 +01:00
|
|
|
$this->currentRep = $result;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2015-02-25 19:32:33 +01: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-06 04:52:16 +01:00
|
|
|
}
|