VoucherController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Response
  68. */
  69. public function edit(Voucher $voucher)
  70. {
  71. //
  72. }
  73. /**
  74. * Update the specified resource in storage.
  75. *
  76. * @param Request $request
  77. * @param Voucher $voucher
  78. * @return Response
  79. */
  80. public function update(Request $request, Voucher $voucher)
  81. {
  82. //
  83. }
  84. /**
  85. * Remove the specified resource from storage.
  86. *
  87. * @param Voucher $voucher
  88. * @return RedirectResponse
  89. */
  90. public function destroy(Voucher $voucher)
  91. {
  92. $voucher->delete();
  93. return redirect()->back()->with('success', 'voucher has been removed!');
  94. }
  95. /**
  96. * @param Request $request
  97. * @return JsonResponse
  98. * @throws ValidationException
  99. */
  100. public function redeem(Request $request)
  101. {
  102. #general validations
  103. $request->validate([
  104. 'code' => 'required|exists:vouchers,code'
  105. ]);
  106. #get voucher by code
  107. $voucher = Voucher::where('code' , '=' , $request->input('code'))->firstOrFail();
  108. #extra validations
  109. if ($voucher->getStatus() == 'USES_LIMIT_REACHED') throw ValidationException::withMessages([
  110. 'code' => 'This voucher has reached the maximum amount of uses'
  111. ]);
  112. if ($voucher->getStatus() == 'EXPIRED') throw ValidationException::withMessages([
  113. 'code' => 'This voucher has expired'
  114. ]);
  115. if (!$request->user()->vouchers()->where('id' , '=' , $voucher->id)->get()->isEmpty()) throw ValidationException::withMessages([
  116. 'code' => 'You already redeemed this voucher code'
  117. ]);
  118. if ($request->user()->credits + $voucher->credits >= 99999999) throw ValidationException::withMessages([
  119. 'code' => "You can't redeem a voucher with this many credits"
  120. ]);
  121. #redeem voucher
  122. $voucher->redeem($request->user());
  123. return response()->json([
  124. 'success' => "{$voucher->credits} credits have been added to your balance!"
  125. ]);
  126. }
  127. public function dataTable()
  128. {
  129. $query = Voucher::query();
  130. return datatables($query)
  131. ->addColumn('actions', function (Voucher $voucher) {
  132. return '
  133. <a data-content="Show" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.vouchers.show', $voucher->id) . '" class="btn btn-sm text-white btn-warning mr-1"><i class="fas fa-eye"></i></a>
  134. <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>
  135. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.vouchers.destroy', $voucher->id) . '">
  136. ' . csrf_field() . '
  137. ' . method_field("DELETE") . '
  138. <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>
  139. </form>
  140. ';
  141. })
  142. ->addColumn('status', function (Voucher $voucher) {
  143. $color = 'success';
  144. if ($voucher->getStatus() != 'VALID') $color = 'danger';
  145. return '<span class="badge badge-' . $color . '">' . $voucher->getStatus() . '</span>';
  146. })
  147. ->editColumn('uses', function (Voucher $voucher) {
  148. $userCount = $voucher->users()->count();
  149. return "{$userCount} / {$voucher->uses}";
  150. })
  151. ->editColumn('credits', function (Voucher $voucher) {
  152. return number_format($voucher->credits, 2, '.', '');
  153. })
  154. ->editColumn('expires_at', function (Voucher $voucher) {
  155. if (!$voucher->expires_at) return "";
  156. return $voucher->expires_at ? $voucher->expires_at->diffForHumans() : '';
  157. })
  158. ->editColumn('code', function (Voucher $voucher) {
  159. return "<code>{$voucher->code}</code>";
  160. })
  161. ->rawColumns(['actions', 'code', 'status'])
  162. ->make();
  163. }
  164. }