- Changed view extends from "admin.main" to "administrator.layout.master" across multiple files. - Updated route names in admin.php and administrator.php to reflect new structure for user, order, invoice, and other management functionalities. - Adjusted view includes to point to the new directory structure for blocks and components. - Ensured all permission checks remain intact while refactoring the views.
140 lines
4.7 KiB
PHP
140 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\CouponRequest;
|
|
use App\Models\Discount;
|
|
use App\Repositories\Discount\DiscountRepositoryInterface;
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
use Illuminate\Contracts\View\Factory;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class CouponController extends Controller
|
|
{
|
|
public function __construct(
|
|
public DiscountRepositoryInterface $discountRepository
|
|
) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$title = "List Coupon";
|
|
|
|
$coupons = $this->discountRepository->filter($request->merge(['page' => $request->query('page') ?? 1])->all());
|
|
|
|
return view("administrator.pages.coupon.index", compact('title', 'coupons'));
|
|
}
|
|
public function create()
|
|
{
|
|
$title = "Create new Coupon";
|
|
|
|
return view("administrator.pages.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("administrator.pages.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');
|
|
}
|
|
}
|