firefly-iii/app/models/BudgetLimit.php

117 lines
3.2 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-20 08:00:53 -06:00
use \Illuminate\Database\Eloquent\Model as Eloquent;
2014-12-13 14:59:02 -06:00
/**
* Class Limit
*/
class BudgetLimit extends Eloquent
{
2014-12-06 05:12:55 -06:00
use ValidatingTrait;
public static $rules
2014-11-18 02:47:50 -06:00
= [
2014-12-17 14:32:27 -06:00
'budget_id' => 'required|exists:budgets,id',
2014-11-18 02:47:50 -06:00
'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()
{
2014-12-20 00:33:59 -06:00
return $this->belongsTo('Budget','budget_id');
2014-07-30 15:31:35 -05:00
}
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-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-12-13 14:59:02 -06:00
$repetition->budgetLimit()->associate($this);
2014-07-30 15:31:35 -05:00
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'];
}
}