/**
  * Register any other events for your application.
  *
  * @param  \Illuminate\Contracts\Events\Dispatcher $events
  *
  * @return void
  */
 public function boot(DispatcherContract $events)
 {
     parent::boot($events);
     $this->registerDeleteEvents();
     $this->registerCreateEvents();
     BudgetLimit::saved(function (BudgetLimit $budgetLimit) {
         $end = Navigation::addPeriod(clone $budgetLimit->startdate, $budgetLimit->repeat_freq, 0);
         $end->subDay();
         $set = $budgetLimit->limitrepetitions()->where('startdate', $budgetLimit->startdate->format('Y-m-d 00:00:00'))->where('enddate', $end->format('Y-m-d 00:00:00'))->get();
         if ($set->count() == 0) {
             $repetition = new LimitRepetition();
             $repetition->startdate = $budgetLimit->startdate;
             $repetition->enddate = $end;
             $repetition->amount = $budgetLimit->amount;
             $repetition->budgetLimit()->associate($budgetLimit);
             try {
                 $repetition->save();
             } catch (QueryException $e) {
                 Log::error('Trying to save new LimitRepetition failed: ' . $e->getMessage());
                 // @codeCoverageIgnore
             }
         } else {
             if ($set->count() == 1) {
                 $repetition = $set->first();
                 $repetition->amount = $budgetLimit->amount;
                 $repetition->save();
             }
         }
     });
 }