firefly-iii/app/Models/LimitRepetition.php

96 lines
3.1 KiB
PHP
Raw Normal View History

2015-02-27 09:48:33 -06:00
<?php namespace FireflyIII\Models;
2016-01-09 09:09:26 -06:00
use Auth;
2016-05-05 23:15:46 -05:00
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
2015-02-27 09:48:33 -06:00
use Illuminate\Database\Eloquent\Model;
2016-01-09 09:09:26 -06:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2015-02-27 09:48:33 -06:00
/**
2016-01-01 14:49:27 -06:00
* FireflyIII\Models\LimitRepetition
2015-02-27 09:48:33 -06:00
*
2016-01-01 14:49:27 -06:00
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
2016-01-01 14:49:27 -06:00
* @property integer $budget_limit_id
* @property \Carbon\Carbon $startdate
* @property \Carbon\Carbon $enddate
2016-01-01 14:49:27 -06:00
* @property float $amount
* @property-read BudgetLimit $budgetLimit
* @property int $budget_id
2016-05-15 10:46:53 -05:00
* @property string $spent
2016-03-12 00:36:23 -06:00
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\LimitRepetition whereId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\LimitRepetition whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\LimitRepetition whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\LimitRepetition whereBudgetLimitId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\LimitRepetition whereStartdate($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\LimitRepetition whereEnddate($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\LimitRepetition whereAmount($value)
* @mixin \Eloquent
2015-02-27 09:48:33 -06:00
*/
class LimitRepetition extends Model
{
2016-01-15 15:32:21 -06:00
protected $dates = ['created_at', 'updated_at', 'startdate', 'enddate'];
protected $hidden = ['amount_encrypted'];
2016-01-09 09:09:26 -06:00
/**
* @param $value
*
* @return mixed
*/
2016-01-09 09:09:26 -06:00
public static function routeBinder($value)
{
if (Auth::check()) {
$object = LimitRepetition::where('limit_repetitions.id', $value)
->leftjoin('budget_limits', 'budget_limits.id', '=', 'limit_repetitions.budget_limit_id')
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
->where('budgets.user_id', Auth::user()->id)
->first(['limit_repetitions.*']);
if ($object) {
return $object;
}
}
throw new NotFoundHttpException;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function budgetLimit()
{
return $this->belongsTo('FireflyIII\Models\BudgetLimit');
}
2016-05-05 23:15:46 -05:00
/**
*
* @param Builder $query
* @param Carbon $date
*
*/
public function scopeAfter(Builder $query, Carbon $date)
{
$query->where('limit_repetitions.startdate', '>=', $date->format('Y-m-d 00:00:00'));
}
/**
*
* @param Builder $query
* @param Carbon $date
*
*/
public function scopeBefore(Builder $query, Carbon $date)
{
$query->where('limit_repetitions.enddate', '<=', $date->format('Y-m-d 00:00:00'));
}
/**
* @param $value
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = strval(round($value, 2));
}
2015-02-27 09:48:33 -06:00
}