VoucherController.php 8.2 KB

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