VoucherController.php 6.6 KB

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