2014-08-13 00:02:13 -05:00
|
|
|
<?php
|
2014-11-22 16:32:10 -06:00
|
|
|
use Carbon\Carbon;
|
2014-12-06 05:12:55 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Watson\Validating\ValidatingTrait;
|
2014-12-20 08:00:53 -06:00
|
|
|
use \Illuminate\Database\Eloquent\Model as Eloquent;
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
|
|
|
* Class PiggybankRepetition
|
|
|
|
*/
|
2014-12-06 05:12:55 -06:00
|
|
|
class PiggybankRepetition extends Eloquent
|
2014-08-13 00:02:13 -05:00
|
|
|
{
|
2014-12-06 05:12:55 -06:00
|
|
|
use ValidatingTrait;
|
2014-08-13 00:02:13 -05:00
|
|
|
public static $rules
|
2014-11-18 02:47:50 -06:00
|
|
|
= [
|
|
|
|
'piggybank_id' => 'required|exists:piggybanks,id',
|
|
|
|
'targetdate' => 'date',
|
|
|
|
'startdate' => 'date',
|
|
|
|
'currentamount' => 'required|numeric'];
|
2014-08-13 00:02:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDates()
|
|
|
|
{
|
|
|
|
return ['created_at', 'updated_at', 'targetdate', 'startdate'];
|
|
|
|
}
|
|
|
|
|
2014-11-17 13:45:55 -06:00
|
|
|
/**
|
|
|
|
* TODO remove this method in favour of something in the FireflyIII libraries.
|
|
|
|
*
|
|
|
|
* @return float|int
|
|
|
|
*/
|
2014-08-21 14:00:02 -05:00
|
|
|
public function pct()
|
|
|
|
{
|
|
|
|
$total = $this->piggybank->targetamount;
|
|
|
|
$saved = $this->currentamount;
|
|
|
|
if ($total == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$pct = round(($saved / $total) * 100, 1);
|
|
|
|
|
|
|
|
return $pct;
|
|
|
|
}
|
|
|
|
|
2014-08-13 00:02:13 -05:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function piggybank()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('Piggybank');
|
|
|
|
}
|
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
|
|
|
* @param Builder $query
|
|
|
|
* @param Carbon $date
|
|
|
|
*/
|
2014-12-06 05:12:55 -06:00
|
|
|
public function scopeStarts(Builder $query, Carbon $date)
|
|
|
|
{
|
|
|
|
$query->where('startdate', $date->format('Y-m-d'));
|
2014-11-22 16:32:10 -06:00
|
|
|
}
|
2014-12-06 05:12:55 -06:00
|
|
|
|
2014-12-13 14:59:02 -06:00
|
|
|
/**
|
|
|
|
* @param Builder $query
|
|
|
|
* @param Carbon $date
|
|
|
|
*/
|
2014-12-06 05:12:55 -06:00
|
|
|
public function scopeTargets(Builder $query, Carbon $date)
|
|
|
|
{
|
|
|
|
$query->where('targetdate', $date->format('Y-m-d'));
|
2014-11-22 16:32:10 -06:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:07:42 -05:00
|
|
|
|
2014-08-13 00:02:13 -05:00
|
|
|
}
|