/** * 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(); } } }); }
/** * @param $value * * @return mixed */ public static function routeBinder($value) { if (auth()->check()) { $object = LimitRepetition::where('limit_repetitions.id', $value)->leftJoin('budget_limits', 'budget_limits.id', '=', 'limit_repetitions.budget_limit_id')->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')->where('budgets.user_id', auth()->user()->id)->first(['limit_repetitions.*']); if ($object) { return $object; } } throw new NotFoundHttpException(); }
/** * @param BudgetLimitUpdated $event */ public function update(BudgetLimitUpdated $event) { $budgetLimit = $event->budgetLimit; $end = $event->end; $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()); } } if ($set->count() == 1) { $repetition = $set->first(); $repetition->amount = $budgetLimit->amount; $repetition->save(); } }
} } throw new NotFoundHttpException(); }); Route::bind('budget', function ($value) { if (Auth::check()) { $object = Budget::where('id', $value)->where('user_id', Auth::user()->id)->first(); if ($object) { return $object; } } throw new NotFoundHttpException(); }); Route::bind('limitrepetition', function ($value) { if (Auth::check()) { $object = LimitRepetition::where('limit_repetitions.id', $value)->leftjoin('budget_limits', 'budget_limits.id', '=', 'limit_repetitions.budget_limit_id')->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')->where('budgets.user_id', Auth::user()->id)->first(['limit_repetitions.*']); if ($object) { return $object; } } throw new NotFoundHttpException(); }); Route::bind('piggyBank', function ($value) { if (Auth::check()) { $object = PiggyBank::where('piggy_banks.id', $value)->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', Auth::user()->id)->first(['piggy_banks.*']); if ($object) { return $object; } } throw new NotFoundHttpException(); });
/** * @param Carbon $start * @param Carbon $end * * @return Collection */ public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end) { /** @var Collection $repetitions */ return LimitRepetition::leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'))->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'))->where('budgets.user_id', Auth::user()->id)->get(['limit_repetitions.*', 'budget_limits.budget_id']); }
/** * @param Carbon $start * @param Carbon $end * * @return Collection */ public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end) : Collection { $query = LimitRepetition::leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'))->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'))->where('budgets.user_id', $this->user->id); $set = $query->get(['limit_repetitions.*', 'budget_limits.budget_id']); return $set; }
/** * @deprecated * * @param Budget $budget * @param Carbon $date * * @return float|null */ public function getLimitAmountOnDate(Budget $budget, Carbon $date) { $repetition = LimitRepetition::leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')->where('limit_repetitions.startdate', $date->format('Y-m-d 00:00:00'))->where('budget_limits.budget_id', $budget->id)->first(['limit_repetitions.*']); if ($repetition) { return $repetition->amount; } return null; }