VoucherController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\Support\Facades\Validator;
  13. use Illuminate\Validation\ValidationData;
  14. use Illuminate\Validation\ValidationException;
  15. class VoucherController extends Controller
  16. {
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return Application|Factory|View
  21. */
  22. public function index()
  23. {
  24. return view('admin.vouchers.index');
  25. }
  26. /**
  27. * Show the form for creating a new resource.
  28. *
  29. * @return Application|Factory|View
  30. */
  31. public function create()
  32. {
  33. return view('admin.vouchers.create');
  34. }
  35. /**
  36. * Store a newly created resource in storage.
  37. *
  38. * @param Request $request
  39. * @return RedirectResponse
  40. */
  41. public function store(Request $request)
  42. {
  43. $request->validate([
  44. 'memo' => 'sometimes|string|max:191',
  45. 'code' => 'required|string|alpha_dash|max:36',
  46. 'uses' => 'required|numeric|max:2147483647',
  47. 'credits' => 'required|numeric|between:0,99999999',
  48. 'expires_at' => 'nullable|date|after:1 hour',
  49. ]);
  50. Voucher::create($request->except('_token'));
  51. return redirect()->route('admin.vouchers.index')->with('success', 'voucher has been created!');
  52. }
  53. /**
  54. * Display the specified resource.
  55. *
  56. * @param Voucher $voucher
  57. * @return Response
  58. */
  59. public function show(Voucher $voucher)
  60. {
  61. //
  62. }
  63. /**
  64. * Show the form for editing the specified resource.
  65. *
  66. * @param Voucher $voucher
  67. * @return Application|Factory|View
  68. */
  69. public function edit(Voucher $voucher)
  70. {
  71. return view('admin.vouchers.edit' , [
  72. 'voucher' => $voucher
  73. ]);
  74. }
  75. /**
  76. * Update the specified resource in storage.
  77. *
  78. * @param Request $request
  79. * @param Voucher $voucher
  80. * @return RedirectResponse
  81. */
  82. public function update(Request $request, Voucher $voucher)
  83. {
  84. $request->validate([
  85. 'memo' => 'sometimes|string|max:191',
  86. 'code' => 'required|string|alpha_dash|max:36',
  87. 'uses' => 'required|numeric|max:2147483647',
  88. 'credits' => 'required|numeric|between:0,99999999',
  89. 'expires_at' => 'nullable|date|after:1 hour',
  90. ]);
  91. $voucher->update($request->except('_token'));
  92. return redirect()->route('admin.vouchers.index')->with('success', 'voucher has been updated!');
  93. }
  94. /**
  95. * Remove the specified resource from storage.
  96. *
  97. * @param Voucher $voucher
  98. * @return RedirectResponse
  99. */
  100. public function destroy(Voucher $voucher)
  101. {
  102. $voucher->delete();
  103. return redirect()->back()->with('success', 'voucher has been removed!');
  104. }
  105. /**
  106. * @param Request $request
  107. * @return JsonResponse
  108. * @throws ValidationException
  109. */
  110. public function redeem(Request $request)
  111. {
  112. #general validations
  113. $request->validate([
  114. 'code' => 'required|exists:vouchers,code'
  115. ]);
  116. #get voucher by code
  117. $voucher = Voucher::where('code' , '=' , $request->input('code'))->firstOrFail();
  118. #extra validations
  119. if ($voucher->getStatus() == 'USES_LIMIT_REACHED') throw ValidationException::withMessages([
  120. 'code' => 'This voucher has reached the maximum amount of uses'
  121. ]);
  122. if ($voucher->getStatus() == 'EXPIRED') throw ValidationException::withMessages([
  123. 'code' => 'This voucher has expired'
  124. ]);
  125. if (!$request->user()->vouchers()->where('id' , '=' , $voucher->id)->get()->isEmpty()) throw ValidationException::withMessages([
  126. 'code' => 'You already redeemed this voucher code'
  127. ]);
  128. if ($request->user()->credits + $voucher->credits >= 99999999) throw ValidationException::withMessages([
  129. 'code' => "You can't redeem a voucher with this many credits"
  130. ]);
  131. #redeem voucher
  132. $voucher->redeem($request->user());
  133. return response()->json([
  134. 'success' => "{$voucher->credits} credits 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. $userCount = $voucher->users()->count();
  158. return "{$userCount} / {$voucher->uses}";
  159. })
  160. ->editColumn('credits', function (Voucher $voucher) {
  161. return number_format($voucher->credits, 2, '.', '');
  162. })
  163. ->editColumn('expires_at', function (Voucher $voucher) {
  164. if (!$voucher->expires_at) return "";
  165. return $voucher->expires_at ? $voucher->expires_at->diffForHumans() : '';
  166. })
  167. ->editColumn('code', function (Voucher $voucher) {
  168. return "<code>{$voucher->code}</code>";
  169. })
  170. ->rawColumns(['actions', 'code', 'status'])
  171. ->make();
  172. }
  173. }