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-03-27 18:17:15 +01:00
|
|
|
use App;
|
2015-03-28 07:00:53 +01:00
|
|
|
use Log;
|
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-21 21:33:52 +01:00
|
|
|
protected $fillable
|
|
|
|
= ['repeats', 'name', 'account_id', 'rep_every', 'rep_times', 'reminder_skip', 'targetamount', 'startdate', 'targetdate', 'reminder', 'remind_me',
|
|
|
|
'rep_length'];
|
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-02-24 21:10:25 +01:00
|
|
|
/**
|
|
|
|
* Grabs the PiggyBankRepetition that's currently relevant / active
|
|
|
|
*
|
|
|
|
* @returns PiggyBankRepetition
|
|
|
|
*/
|
|
|
|
public function currentRelevantRep()
|
|
|
|
{
|
2015-03-21 21:33:52 +01:00
|
|
|
if (!is_null($this->currentRep)) {
|
2015-02-24 21:10:25 +01:00
|
|
|
return $this->currentRep;
|
|
|
|
}
|
2015-03-27 18:17:15 +01:00
|
|
|
// repeating piggy banks are no longer supported.
|
2015-03-21 21:33:52 +01:00
|
|
|
if (intval($this->repeats) === 0) {
|
2015-02-24 21:10:25 +01:00
|
|
|
$rep = $this->piggyBankRepetitions()->first(['piggy_bank_repetitions.*']);
|
|
|
|
$this->currentRep = $rep;
|
|
|
|
|
|
|
|
return $rep;
|
|
|
|
} else {
|
2015-03-28 07:00:53 +01:00
|
|
|
Log::error('Tried to work with a piggy bank with a repeats=1 value! (id is '.$this->id.')');
|
2015-02-24 21:10:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
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'];
|
|
|
|
}
|
|
|
|
|
2015-03-21 21:33:52 +01:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getRemindMeAttribute($value)
|
|
|
|
{
|
|
|
|
return intval($value) == 1;
|
|
|
|
}
|
|
|
|
|
2015-02-25 19:32:33 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|