firefly-iii/app/models/Limit.php

116 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2014-07-30 15:31:35 -05:00
use Carbon\Carbon;
use Illuminate\Database\QueryException;
2014-12-06 05:12:55 -06:00
use Watson\Validating\ValidatingTrait;
2014-12-06 05:12:55 -06:00
class Limit extends Eloquent
{
2014-12-06 05:12:55 -06:00
use ValidatingTrait;
public static $rules
2014-11-18 02:47:50 -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-11-18 02:47:50 -06:00
* TODO see if this method is still used.
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'];
}
}