Various updates [skip ci]

This commit is contained in:
James Cole
2014-07-30 22:31:35 +02:00
parent 00a767cfc9
commit 78d575fbb1
13 changed files with 411 additions and 263 deletions

View File

@@ -88,7 +88,7 @@ class EloquentLimitRepository implements LimitRepositoryInterface
$limit->repeats = isset($data['repeats']) ? intval($data['repeats']) : 0;
$limit->repeat_freq = $data['period'];
if (!$limit->save()) {
Session::flash('error', 'Could not save: ' . $limit->errors()->first());
\Session::flash('error', 'Could not save: ' . $limit->errors()->first());
}
return $limit;

View File

@@ -3,7 +3,6 @@
namespace Firefly\Trigger\Limits;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Events\Dispatcher;
/**
@@ -14,6 +13,15 @@ use Illuminate\Events\Dispatcher;
class EloquentLimitTrigger
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
}
public function updateLimitRepetitions()
{
if (!\Auth::check()) {
@@ -22,149 +30,246 @@ class EloquentLimitTrigger
// get budgets with limits:
$budgets = \Auth::user()->budgets()
->with(['limits', 'limits.limitrepetitions'])
->whereNotNull('limits.id')
->leftJoin('limits', 'components.id', '=', 'limits.component_id')->get(['components.*']);
->with(
['limits', 'limits.limitrepetitions']
)
->where('components.class', 'Budget')
->get(['components.*']);
$start = \Session::get('start');
$end = new Carbon;
// get todays date.
// double check the non-repeating budgetlimits first.
foreach ($budgets as $budget) {
// loop limits:
\Log::debug('Budgetstart: ' . $budget->name);
foreach ($budget->limits as $limit) {
// should have a repetition, at the very least
// for the period it starts (startdate and onwards).
if (count($limit->limitrepetitions) == 0) {
// 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);
try {
$repetition->save();
} catch (QueryException $e) {
// do nothing
\Log::error($e->getMessage());
}
} else {
// there are limits already, do they
// fall into the range surrounding today?
$today = new 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;
if ($limit->repeats == 0) {
$limit->createRepetition($limit->startdate);
}
if($limit->repeats == 1) {
$start = $limit->startdate;
$end = new Carbon;
// repeat for period:
$current = clone $start;
\Log::debug('Create repeating limit for #'.$limit->id.' starting on ' . $current);
while($current <= $end) {
\Log::debug('Current is now: ' . $current);
$limit->createRepetition(clone $current);
// switch period, add time:
switch ($limit->repeat_freq) {
case 'daily':
$flowStart->startOfDay();
$flowEnd->endOfDay();
$current->addDay();
break;
case 'weekly':
$flowStart->startOfWeek();
$flowEnd->endOfWeek();
$current->addWeek();
break;
case 'monthly':
$flowStart->startOfMonth();
$flowEnd->endOfMonth();
$current->addMonth();
break;
case 'quarterly':
$flowStart->firstOfQuarter();
$flowEnd->startOfMonth()->lastOfQuarter()->endOfDay();
$current->addMonths(3);
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();
}
$current->addMonths(6);
break;
case 'yearly':
$flowStart->startOfYear();
$flowEnd->endOfYear();
$current->addYear();
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 (QueryException $e) {
// do nothing
\Log::error($e->getMessage());
}
}
}
}
}
// \Log::debug(
// 'Now at budget ' . $budget->name . ', limit #' . $limit->id . ' (' . $limit->repeats . ', '
// . $limit->repeat_freq . ', ' . $limit->startdate . ').'
// );
// $count = count($limit->limitrepetitions);
// if ($count == 0) {
// // 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);
//
// try {
// $repetition->save();
// \Log::debug('Created new repetition with id #' . $repetition->id);
// } catch (QueryException $e) {
// // do nothing
// \Log::error('Trying to save new Limitrepetition failed!');
// \Log::error($e->getMessage());
// }
//
// }
}
}
}
}
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen('app.before', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
}
//
//
// exit;
//
//
// // get todays date.
//
// foreach ($budgets as $budget) {
// // loop limits:
// foreach ($budget->limits as $limit) {
// // should have a repetition, at the very least
// // for the period it starts (startdate and onwards).
// \Log::debug('Limit #' . $limit->id . ' has ' . count($limit->limitrepetitions) . ' limitreps!');
// if (count($limit->limitrepetitions) == 0) {
//
// // 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);
//
// try {
// $repetition->save();
// } catch (QueryException $e) {
// // do nothing
// \Log::error('Trying to save new Limitrepetition!');
// \Log::error($e->getMessage());
// }
// } else {
// // there are limits already, do they
// // fall into the range surrounding today?
// $today = new 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 (QueryException $e) {
// // do nothing
// \Log::error($e->getMessage());
// }
// }
// }
// }
// }
// }
// }
}