VoucherController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Voucher;
  5. use Illuminate\Contracts\Foundation\Application;
  6. use Illuminate\Contracts\View\Factory;
  7. use Illuminate\Contracts\View\View;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Http\Response;
  12. use Illuminate\Validation\ValidationException;
  13. class VoucherController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return Application|Factory|View
  19. */
  20. public function index()
  21. {
  22. return view('admin.vouchers.index');
  23. }
  24. /**
  25. * Show the form for creating a new resource.
  26. *
  27. * @return Application|Factory|View
  28. */
  29. public function create()
  30. {
  31. return view('admin.vouchers.create');
  32. }
  33. /**
  34. * Store a newly created resource in storage.
  35. *
  36. * @param Request $request
  37. * @return RedirectResponse
  38. */
  39. public function store(Request $request)
  40. {
  41. $request->validate([
  42. 'memo' => 'nullable|string|max:191',
  43. 'code' => 'required|string|alpha_dash|max:36|min:4|unique:vouchers',
  44. 'uses' => 'required|numeric|max:2147483647|min:1',
  45. 'credits' => 'required|numeric|between:0,99999999',
  46. 'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years',
  47. ]);
  48. Voucher::create($request->except('_token'));
  49. return redirect()->route('admin.vouchers.index')->with('success', 'voucher has been created!');
  50. }
  51. /**
  52. * Display the specified resource.
  53. *
  54. * @param Voucher $voucher
  55. * @return Response
  56. */
  57. public function show(Voucher $voucher)
  58. {
  59. //
  60. }
  61. /**
  62. * Show the form for editing the specified resource.
  63. *
  64. * @param Voucher $voucher
  65. * @return Application|Factory|View
  66. */
  67. public function edit(Voucher $voucher)
  68. {
  69. return view('admin.vouchers.edit', [
  70. 'voucher' => $voucher
  71. ]);
  72. }
  73. /**
  74. * Update the specified resource in storage.
  75. *
  76. * @param Request $request
  77. * @param Voucher $voucher
  78. * @return RedirectResponse
  79. */
  80. public function update(Request $request, Voucher $voucher)
  81. {
  82. $request->validate([
  83. 'memo' => 'nullable|string|max:191',
  84. 'code' => "required|string|alpha_dash|max:36|min:4|unique:vouchers,code,{$voucher->id}",
  85. 'uses' => 'required|numeric|max:2147483647|min:1',
  86. 'credits' => 'required|numeric|between:0,99999999',
  87. 'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years',
  88. ]);
  89. $voucher->update($request->except('_token'));
  90. return redirect()->route('admin.vouchers.index')->with('success', 'voucher has been updated!');
  91. }
  92. /**
  93. * Remove the specified resource from storage.
  94. *
  95. * @param Voucher $voucher
  96. * @return RedirectResponse
  97. */
  98. public function destroy(Voucher $voucher)
  99. {
  100. $voucher->delete();
  101. return redirect()->back()->with('success', 'voucher has been removed!');
  102. }
  103. /**
  104. * @param Request $request
  105. * @return JsonResponse
  106. * @throws ValidationException
  107. */
  108. public function redeem(Request $request)
  109. {
  110. #general validations
  111. $request->validate([
  112. 'code' => 'required|exists:vouchers,code'
  113. ]);
  114. #get voucher by code
  115. $voucher = Voucher::where('code', '=', $request->input('code'))->firstOrFail();
  116. #extra validations
  117. if ($voucher->getStatus() == 'USES_LIMIT_REACHED') throw ValidationException::withMessages([
  118. 'code' => 'This voucher has reached the maximum amount of uses'
  119. ]);
  120. if ($voucher->getStatus() == 'EXPIRED') throw ValidationException::withMessages([
  121. 'code' => 'This voucher has expired'
  122. ]);
  123. if (!$request->user()->vouchers()->where('id', '=', $voucher->id)->get()->isEmpty()) throw ValidationException::withMessages([
  124. 'code' => 'You already redeemed this voucher code'
  125. ]);
  126. if ($request->user()->credits + $voucher->credits >= 99999999) throw ValidationException::withMessages([
  127. 'code' => "You can't redeem this voucher because you would exceed the ".CREDITS_DISPLAY_NAME." limit"
  128. ]);
  129. #redeem voucher
  130. $voucher->redeem($request->user());
  131. return response()->json([
  132. 'success' => "{$voucher->credits} ".CREDITS_DISPLAY_NAME." have been added to your balance!"
  133. ]);
  134. }
  135. public function dataTable()
  136. {
  137. $query = Voucher::query();
  138. return datatables($query)
  139. ->addColumn('actions', function (Voucher $voucher) {
  140. return '
  141. <a data-content="Edit" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.vouchers.edit', $voucher->id) . '" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
  142. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.vouchers.destroy', $voucher->id) . '">
  143. ' . csrf_field() . '
  144. ' . method_field("DELETE") . '
  145. <button data-content="Delete" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
  146. </form>
  147. ';
  148. })
  149. ->addColumn('status', function (Voucher $voucher) {
  150. $color = 'success';
  151. if ($voucher->getStatus() != 'VALID') $color = 'danger';
  152. return '<span class="badge badge-' . $color . '">' . $voucher->getStatus() . '</span>';
  153. })
  154. ->editColumn('uses', function (Voucher $voucher) {
  155. return "{$voucher->used} / {$voucher->uses}";
  156. })
  157. ->editColumn('credits', function (Voucher $voucher) {
  158. return number_format($voucher->credits, 2, '.', '');
  159. })
  160. ->editColumn('expires_at', function (Voucher $voucher) {
  161. if (!$voucher->expires_at) return "";
  162. return $voucher->expires_at ? $voucher->expires_at->diffForHumans() : '';
  163. })
  164. ->editColumn('code', function (Voucher $voucher) {
  165. return "<code>{$voucher->code}</code>";
  166. })
  167. ->rawColumns(['actions', 'code', 'status'])
  168. ->make();
  169. }
  170. }