firefly-iii/app/models/Limit.php

134 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2014-07-30 15:31:35 -05:00
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use LaravelBook\Ardent\Ardent as Ardent;
2014-11-17 00:33:18 -06:00
/**
* Limit
*
2014-11-17 00:33:18 -06:00
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $component_id
* @property \Carbon\Carbon $startdate
* @property float $amount
* @property boolean $repeats
* @property string $repeat_freq
* @property-read \Budget $budget
* @property-read \Component $component
* @property-read \Illuminate\Database\Eloquent\Collection|\LimitRepetition[] $limitrepetitions
* @method static \Illuminate\Database\Query\Builder|\Limit whereId($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereComponentId($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereStartdate($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereAmount($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereRepeats($value)
* @method static \Illuminate\Database\Query\Builder|\Limit whereRepeatFreq($value)
*/
class Limit extends Ardent
{
public static $rules
2014-11-12 15:37:44 -06:00
= ['component_id' => 'required|exists:components,id', 'startdate' => 'required|date', 'amount' => 'numeric|required|min:0.01',
'repeats' => 'required|boolean', 'repeat_freq' => 'required|in:daily,weekly,monthly,quarterly,half-year,yearly'
];
2014-08-10 11:22:42 -05:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2014-07-30 15:31:35 -05:00
public function budget()
{
return $this->belongsTo('Budget', 'component_id');
}
2014-08-10 11:22:42 -05:00
/**
2014-08-19 06:24:11 -05:00
* Create a new repetition for this limit, starting on
* the given date.
*
2014-08-10 11:22:42 -05:00
* @param Carbon $start
*/
2014-07-30 15:31:35 -05:00
public function createRepetition(Carbon $start)
{
2014-07-30 15:31:35 -05:00
$end = clone $start;
// go to end:
switch ($this->repeat_freq) {
case 'daily':
$end->addDay();
break;
case 'weekly':
$end->addWeek();
break;
case 'monthly':
$end->addMonth();
break;
case 'quarterly':
$end->addMonths(3);
break;
case 'half-year':
$end->addMonths(6);
break;
case 'yearly':
$end->addYear();
break;
}
$end->subDay();
2014-11-04 13:37:00 -06:00
$count = $this->limitrepetitions()->where('startdate', $start->format('Y-m-d'))->where('enddate', $end->format('Y-m-d'))->count();
2014-11-12 15:37:44 -06:00
\Log::debug('All: ' . $this->limitrepetitions()->count() . ' (#' . $this->id . ')');
\Log::debug('Found ' . $count . ' limit-reps for limit #' . $this->id . ' with start ' . $start->format('Y-m-d') . ' and end ' . $end->format('Y-m-d'));
2014-07-30 15:31:35 -05:00
if ($count == 0) {
2014-11-12 15:37:44 -06:00
$repetition = new \LimitRepetition();
2014-07-30 15:31:35 -05:00
$repetition->startdate = $start;
2014-11-12 15:37:44 -06:00
$repetition->enddate = $end;
$repetition->amount = $this->amount;
2014-07-30 15:31:35 -05:00
$repetition->limit()->associate($this);
try {
$repetition->save();
\Log::debug('Created new repetition with id #' . $repetition->id);
} catch (QueryException $e) {
// do nothing
2014-08-09 12:10:31 -05:00
2014-07-30 15:31:35 -05:00
\Log::error('Trying to save new Limitrepetition failed!');
\Log::error($e->getMessage());
}
2014-08-28 00:53:54 -05:00
if (isset($repetition->id)) {
2014-11-17 13:55:31 -06:00
\Event::fire('limits.repetition', [$repetition]); // not used, I guess?
}
2014-11-12 15:37:44 -06:00
} else {
if ($count == 1) {
// update this one:
$repetition = $this->limitrepetitions()->where('startdate', $start->format('Y-m-d'))->where('enddate', $end->format('Y-m-d'))->first();
$repetition->amount = $this->amount;
$repetition->save();
2014-11-12 15:37:44 -06:00
}
2014-07-30 15:31:35 -05:00
}
}
2014-08-10 11:22:42 -05:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2014-07-22 23:57:51 -05:00
public function limitrepetitions()
{
return $this->hasMany('LimitRepetition');
}
2014-08-09 12:10:31 -05:00
2014-08-10 11:22:42 -05:00
/**
* @return array
*/
public function getDates()
{
return ['created_at', 'updated_at', 'startdate', 'enddate'];
}
}