firefly-iii/app/Models/LimitRepetition.php

104 lines
2.5 KiB
PHP
Raw Normal View History

2016-05-20 01:57:45 -05:00
<?php
/**
* LimitRepetition.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2016-05-20 01:57:45 -05:00
declare(strict_types = 1);
namespace FireflyIII\Models;
2015-02-27 09:48:33 -06:00
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-11-18 13:06:08 -06:00
/**
* Class LimitRepetition
*
2016-12-30 01:41:48 -06:00
* @deprecated
2016-11-18 13:06:08 -06:00
* @package FireflyIII\Models
*/
2015-02-27 09:48:33 -06:00
class LimitRepetition extends Model
{
2016-12-24 10:36:51 -06:00
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts
= [
'created_at' => 'date',
'updated_at' => 'date',
'startdate' => 'date',
'enddate' => 'date',
];
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)
{
2016-09-16 05:07:45 -05:00
if (auth()->check()) {
2016-12-15 14:35:33 -06:00
$object = self::where('limit_repetitions.id', $value)
2016-12-24 10:36:51 -06:00
->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.*']);
2016-01-09 09:09:26 -06:00
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
}