VoucherController.php 8.7 KB

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