discountRepository->filter($request->merge(['page' => $request->query('page') ?? 1])->all()); return view("admin.coupon.index", compact('title', 'coupons')); } public function create() { $title = "Create new Coupon"; return view("admin.coupon.add", compact('title')); } public function store(CouponRequest $request) { $request->validated(); $isExits = $this->discountRepository->findBy('code', $request->code); if ($isExits) { return back()->withInput()->withErrors('Coupon code already exists'); } if (Carbon::createFromFormat('d/m/Y', $request->start)->timestamp > Carbon::createFromFormat('d/m/Y', $request->end)->timestamp) { return back()->withInput()->withErrors('Coupon code expiration date is invalid'); } if (Carbon::createFromFormat('d/m/Y', $request->end)->timestamp < Carbon::now()->timestamp) { return back()->withInput()->withErrors('Coupon code expiration date is invalid'); } DB::beginTransaction(); try { $data = $request->all(); $data['start'] = Carbon::createFromFormat('d/m/Y', $request->start)->timestamp; $data['end'] = Carbon::createFromFormat('d/m/Y', $request->end)->timestamp; $this->discountRepository->create($data); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withInput()->withErrors($error->getMessage()); } return redirect()->route('coupon.index')->with('success', 'Added successfully'); } public function edit(int $id = 0) { $coupon = $this->discountRepository->find($id); if (! $coupon) { return back()->withErrors("Invalid request"); } $title = "Edit coupon #" . $coupon->code; return view("admin.coupon.edit", compact('title', 'coupon')); } public function update(int $id = 0, CouponRequest $request) { $coupon = $this->discountRepository->find($id); if (! $coupon) { return back()->withErrors("Invalid request"); } $request->validated(); if ($request->code !== $coupon->code) { $isExits = $this->discountRepository->findBy('code', $request->code); if ($isExits) { return back()->withInput()->withErrors('Coupon code already exists'); } } if (Carbon::createFromFormat('d/m/Y', $request->start)->timestamp > Carbon::createFromFormat('d/m/Y', $request->end)->timestamp) { return back()->withInput()->withErrors('Coupon code expiration date is invalid'); } if (Carbon::createFromFormat('d/m/Y', $request->end)->timestamp < Carbon::now()->timestamp) { return back()->withInput()->withErrors('Coupon code expiration date is invalid'); } DB::beginTransaction(); try { $data = $request->all(); $data['start'] = Carbon::createFromFormat('d/m/Y', $request->start)->timestamp; $data['end'] = Carbon::createFromFormat('d/m/Y', $request->end)->timestamp; $this->discountRepository->update($id,$data); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withInput()->withErrors($error->getMessage()); } return redirect()->route('coupon.index')->with('success', 'Updated successfully'); } public function destroy(int $id = 0) { $coupon = $this->discountRepository->find($id); if (! $coupon) { return back()->withErrors("Invalid request"); } DB::beginTransaction(); try { $this->discountRepository->delete($id); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withInput()->withErrors($error->getMessage()); } return back()->with('success', 'Deleted successfully'); } }