firefly-iii/app/Models/PiggyBank.php
2015-05-23 17:11:16 +02:00

145 lines
3.2 KiB
PHP

<?php namespace FireflyIII\Models;
use Crypt;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class PiggyBank
*
* @codeCoverageIgnore
*
* @package FireflyIII\Models
*/
class PiggyBank extends Model
{
use SoftDeletes;
protected $fillable
= ['name', 'account_id', 'order', 'reminder_skip', 'targetamount', 'startdate', 'targetdate', 'reminder', 'remind_me'];
protected $hidden = ['targetamount_encrypted', 'encrypted'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function account()
{
return $this->belongsTo('FireflyIII\Models\Account');
}
/**
* Grabs the PiggyBankRepetition that's currently relevant / active
*
* @returns PiggyBankRepetition
*/
public function currentRelevantRep()
{
if (!is_null($this->currentRep)) {
return $this->currentRep;
}
// repeating piggy banks are no longer supported.
$rep = $this->piggyBankRepetitions()->first(['piggy_bank_repetitions.*']);
$this->currentRep = $rep;
return $rep;
}
/**
* @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'];
}
/**
*
* @param $value
*
* @return string
*/
public function getNameAttribute($value)
{
if (intval($this->encrypted) == 1) {
return Crypt::decrypt($value);
}
return $value;
}
/**
*
* @param $value
*
* @return int
*/
public function getRemindMeAttribute($value)
{
return intval($value) == 1;
}
/**
* @param $value
*
* @return float|int
*/
public function getTargetamountAttribute($value)
{
if (is_null($this->targetamount_encrypted)) {
return $value;
}
$value = intval(Crypt::decrypt($this->targetamount_encrypted));
$value = $value / 100;
return $value;
}
/**
* @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');
}
/**
*
* @param $value
*/
public function setNameAttribute($value)
{
$this->attributes['name'] = Crypt::encrypt($value);
$this->attributes['encrypted'] = true;
}
/**
* @param $value
*/
public function setTargetamountAttribute($value)
{
// save in cents:
$value = intval($value * 100);
$this->attributes['targetamount_encrypted'] = Crypt::encrypt($value);
$this->attributes['targetamount'] = ($value / 100);
}
}