CouponController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\CouponRequest;
  4. use Illuminate\Http\Request;
  5. use App\Models\Coupon;
  6. use App\Models\User;
  7. class CouponController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index()
  15. {
  16. $coupons = Coupon::where('user_id', User::where('user_type', 'admin')->first()->id)->orderBy('id','desc')->get();
  17. return view('backend.marketing.coupons.index', compact('coupons'));
  18. }
  19. /**
  20. * Show the form for creating a new resource.
  21. *
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function create()
  25. {
  26. return view('backend.marketing.coupons.create');
  27. }
  28. /**
  29. * Store a newly created resource in storage.
  30. *
  31. * @param \Illuminate\Http\Request $request
  32. * @return \Illuminate\Http\Response
  33. */
  34. public function store(CouponRequest $request)
  35. {
  36. $user_id = User::where('user_type', 'admin')->first()->id;
  37. Coupon::create($request->validated() + [
  38. 'user_id' => $user_id,
  39. ]);
  40. flash(translate('Coupon has been saved successfully'))->success();
  41. return redirect()->route('coupon.index');
  42. }
  43. /**
  44. * Display the specified resource.
  45. *
  46. * @param int $id
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function show($id)
  50. {
  51. //
  52. }
  53. /**
  54. * Show the form for editing the specified resource.
  55. *
  56. * @param int $id
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function edit($id)
  60. {
  61. $coupon = Coupon::findOrFail(decrypt($id));
  62. return view('backend.marketing.coupons.edit', compact('coupon'));
  63. }
  64. /**
  65. * Update the specified resource in storage.
  66. *
  67. * @param \Illuminate\Http\Request $request
  68. * @param int $id
  69. * @return \Illuminate\Http\Response
  70. */
  71. public function update(CouponRequest $request, Coupon $coupon)
  72. {
  73. $coupon->update($request->validated());
  74. flash(translate('Coupon has been updated successfully'))->success();
  75. return redirect()->route('coupon.index');
  76. }
  77. /**
  78. * Remove the specified resource from storage.
  79. *
  80. * @param int $id
  81. * @return \Illuminate\Http\Response
  82. */
  83. public function destroy($id)
  84. {
  85. Coupon::destroy($id);
  86. flash(translate('Coupon has been deleted successfully'))->success();
  87. return redirect()->route('coupon.index');
  88. }
  89. public function get_coupon_form(Request $request)
  90. {
  91. if($request->coupon_type == "product_base") {
  92. $admin_id = \App\Models\User::where('user_type', 'admin')->first()->id;
  93. $products = filter_products(\App\Models\Product::query()->from("products as p")->where('user_id', $admin_id))->get();
  94. return view('partials.coupons.product_base_coupon', compact('products'));
  95. }
  96. elseif($request->coupon_type == "cart_base"){
  97. return view('partials.coupons.cart_base_coupon');
  98. }
  99. }
  100. public function get_coupon_form_edit(Request $request)
  101. {
  102. if($request->coupon_type == "product_base") {
  103. $coupon = Coupon::findOrFail($request->id);
  104. $admin_id = \App\Models\User::where('user_type', 'admin')->first()->id;
  105. $products = filter_products(\App\Models\Product::query()->from("products as p")->where('user_id', $admin_id))->get();
  106. return view('partials.coupons.product_base_coupon_edit',compact('coupon', 'products'));
  107. }
  108. elseif($request->coupon_type == "cart_base"){
  109. $coupon = Coupon::findOrFail($request->id);
  110. return view('partials.coupons.cart_base_coupon_edit',compact('coupon'));
  111. }
  112. }
  113. }