Files
firefly-iii/app/lib/Firefly/Trigger/Limits/EloquentLimitTrigger.php

178 lines
7.3 KiB
PHP
Raw Normal View History

<?php
namespace Firefly\Trigger\Limits;
/**
* Class EloquentLimitTrigger
*
* @package Firefly\Trigger\Limits
*/
class EloquentLimitTrigger
{
public function updateLimitRepetitions()
{
if (!\Auth::check()) {
return;
}
// get budgets with limits:
$budgets = \Auth::user()->budgets()
->with(['limits', 'limits.limitrepetitions'])
->whereNotNull('limits.id')
->leftJoin('limits', 'components.id', '=', 'limits.component_id')->get(['components.*']);
// get todays date.
foreach ($budgets as $budget) {
\Log::debug(
'Now checking the ' . count($budget->limits) . ' limits in ' . $budget->name . ' (#' . $budget->id
. ').'
);
// loop limits:
foreach ($budget->limits as $limit) {
\Log::debug(
'Now at limit #' . $limit->id . ', which has ' . count($limit->limitrepetitions) . ' reps already'
);
\Log::debug(
'More: Amount: ' . $limit->amount . ', repeat: ' . $limit->repeats . ', freq: '
. $limit->repeat_freq
);
// should have a repetition, at the very least
// for the period it starts (startdate and onwards).
if (count($limit->limitrepetitions) == 0) {
\Log::debug('No reps, create one.');
// create such a repetition:
$repetition = new \LimitRepetition();
$start = clone $limit->startdate;
$end = clone $start;
// go to end:
switch ($limit->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();
$repetition->startdate = $start;
$repetition->enddate = $end;
$repetition->amount = $limit->amount;
$repetition->limit()->associate($limit);
\Log::debug('Created single rep for non-repeating limit, from ' . $start . ' until ' . $end);
try {
$repetition->save();
} catch (\Illuminate\Database\QueryException $e) {
// do nothing
\Log::error($e->getMessage());
}
} else {
// there are limits already, do they
// fall into the range surrounding today?
$today = new \Carbon\Carbon;
$today->addMonths(2);
if ($limit->repeats == 1 && $today >= $limit->startdate) {
/** @var \Carbon\Carbon $flowStart */
$flowStart = clone $today;
/** @var \Carbon\Carbon $flowEnd */
$flowEnd = clone $today;
switch ($limit->repeat_freq) {
case 'daily':
$flowStart->startOfDay();
$flowEnd->endOfDay();
break;
case 'weekly':
$flowStart->startOfWeek();
$flowEnd->endOfWeek();
break;
case 'monthly':
$flowStart->startOfMonth();
$flowEnd->endOfMonth();
break;
case 'quarterly':
$flowStart->firstOfQuarter();
$flowEnd->startOfMonth()->lastOfQuarter()->endOfDay();
break;
case 'half-year':
if (intval($flowStart->format('m')) >= 7) {
$flowStart->startOfYear();
$flowStart->addMonths(6);
} else {
$flowStart->startOfYear();
}
$flowEnd->endOfYear();
if (intval($start->format('m')) <= 6) {
$flowEnd->subMonths(6);
$flowEnd->subDay();
}
break;
case 'yearly':
$flowStart->startOfYear();
$flowEnd->endOfYear();
break;
}
$inRange = false;
foreach ($limit->limitrepetitions as $rep) {
if ($rep->startdate->format('dmY') == $flowStart->format('dmY')
&& $rep->enddate->format('dmY') == $flowEnd->format('dmY')
) {
// falls in current range, do nothing?
$inRange = true;
}
}
// if there is none that fall in range, create!
if ($inRange === false) {
// create (but check first)!
$count = \LimitRepetition::where('limit_id', $limit->id)->where('startdate', $flowStart)
->where('enddate', $flowEnd)->count();
if ($count == 0) {
$repetition = new \LimitRepetition;
$repetition->startdate = $flowStart;
$repetition->enddate = $flowEnd;
$repetition->amount = $limit->amount;
$repetition->limit()->associate($limit);
try {
$repetition->save();
} catch (\Illuminate\Database\QueryException $e) {
// do nothing
\Log::error($e->getMessage());
}
}
}
}
}
}
\Log::debug('Done checking the budget!');
}
}
public function subscribe(\Illuminate\Events\Dispatcher $events)
{
$events->listen('app.before', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
}
}
\Limit::observe(new EloquentLimitTrigger);