authorize('viewAny', Shop::class); $query = Shop::query(); if ($request->has('pickup-exception')) { request()->session()->forget('shops.filter.pickup-exception'); request()->session()->put( 'shops.filter.pickup-exception', $request->input('pickup-exception') ); } $pickupException = request()->session()->get('shops.filter.pickup-exception'); if (! is_null($pickupException)) { $query->whereHas( 'pickupExceptions', fn (Builder $q) => $q->where('id', $pickupException) ); } $dataTable->sortQuery(Shop::class, $query); $shops = $dataTable->paginateQuery($query); $pickupExceptions = PickupException::query()->get(); return view('bo.shops.index', compact('shops', 'pickupExceptions')); } public function create(): View { $this->authorize('create', Shop::class); return view('bo.shops.create'); } public function store(StoreShopRequest $request): RedirectResponse { $this->authorize('create', Shop::class); return DB::transaction(function () use ($request) { $shop = new Shop(); $shop->fill($request->only( $shop->getFillable() )); if ($shop->saveOrFail()) { return to_route('bo.shops.edit', $shop) ->with('success', trans('crud.messages.has_been_created', [ 'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)), ])); } return to_route('bo.shops.create') ->with('error', trans('crud.messages.cannot_be_created', [ 'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)), ])); }); } public function edit(Shop $shop): View { $this->authorize('update', $shop); return view('bo.shops.edit', compact('shop')); } public function update(UpdateShopRequest $request, Shop $shop): RedirectResponse { $this->authorize('update', $shop); return DB::transaction(function () use ($request, $shop) { $shop->fill($request->only( $shop->getFillable() )); if ($shop->saveOrFail()) { return to_route('bo.shops.edit', $shop) ->with('success', trans('crud.messages.has_been_updated', [ 'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)), ])); } return to_route('bo.shops.edit', $shop) ->with('error', trans('crud.messages.cannot_be_updated', [ 'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)), ])); }); } public function destroy(Shop $shop): RedirectResponse { $this->authorize('delete', $shop); return DB::transaction(function () use ($shop) { if ($shop->deleteOrFail()) { return to_route('bo.shops.index') ->with('warning', trans('crud.messages.has_been_deleted', ['model' => Str::ucfirst(trans_choice('models.classes.shops', 1))])); } return to_route('bo.shops.edit', $shop) ->with('error', trans('crud.messages.cannot_be_deleted', [ 'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)), ])); }); } public function pickupExceptionsEdit(Shop $shop): View { $this->authorize('pickupExceptionUpdate', $shop); $shop->load('pickupExceptions'); $domainShop = $this->shopRepository->find($shop->getId()); if ($domainShop === null) { abort(404, 'Shop not found in domain'); } $pickupExceptions = $this->pickupService->getEditablePickupExceptionsFor($domainShop); $pickupDay = $this->pickupService->getPickupDay(); $pickupPeriod = $this->pickupService->getPeriodFromPickUpDay(); return view('bo.shops.pickup-exceptions', compact( 'shop', 'pickupExceptions', 'pickupDay', 'pickupPeriod', )); } public function pickupExceptionsUpdate(UpdateShopPickupExceptionsRequest $request, Shop $shop): RedirectResponse { $this->authorize('pickupExceptionUpdate', $shop); return DB::transaction(function () use ($request, $shop) { $pickupExceptions = $request->array('pickup-exceptions'); $replacementDates = $request->array('replacement_date'); $orderDeadlines = $request->array('order_deadline_at'); $syncData = []; foreach ($pickupExceptions as $exceptionId) { $replacementDate = ! empty($replacementDates[$exceptionId]) ? $replacementDates[$exceptionId] : null; $orderDeadline = ! empty($orderDeadlines[$exceptionId]) ? $orderDeadlines[$exceptionId] : null; $syncData[$exceptionId] = [ 'replacement_date' => $replacementDate, 'order_deadline_at' => $orderDeadline, ]; } $shop->pickupExceptions()->sync($syncData); return back()->with('success', trans('crud.messages.has_been_updated', [ 'model' => Str::ucfirst(trans_choice('models.classes.shops', 1)), ])); }); } }