VoucherController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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, GeneralSettings $general_settings)
  114. {
  115. return view('admin.vouchers.users', [
  116. 'voucher' => $voucher,
  117. 'locale_datatables' => $locale_settings->datatables,
  118. 'credits_display_name' => $general_settings->credits_display_name
  119. ]);
  120. }
  121. /**
  122. * @param Request $request
  123. * @return JsonResponse
  124. *
  125. * @throws ValidationException
  126. */
  127. public function redeem(Request $request, GeneralSettings $general_settings)
  128. {
  129. //general validations
  130. $request->validate([
  131. 'code' => 'required|exists:vouchers,code',
  132. ]);
  133. //get voucher by code
  134. $voucher = Voucher::where('code', '=', $request->input('code'))->firstOrFail();
  135. //extra validations
  136. if ($voucher->getStatus() == 'USES_LIMIT_REACHED') {
  137. throw ValidationException::withMessages([
  138. 'code' => __('This voucher has reached the maximum amount of uses'),
  139. ]);
  140. }
  141. if ($voucher->getStatus() == 'EXPIRED') {
  142. throw ValidationException::withMessages([
  143. 'code' => __('This voucher has expired'),
  144. ]);
  145. }
  146. if (! $request->user()->vouchers()->where('id', '=', $voucher->id)->get()->isEmpty()) {
  147. throw ValidationException::withMessages([
  148. 'code' => __('You already redeemed this voucher code'),
  149. ]);
  150. }
  151. if ($request->user()->credits + $voucher->credits >= 99999999) {
  152. throw ValidationException::withMessages([
  153. 'code' => "You can't redeem this voucher because you would exceed the limit of " . $general_settings->credits_display_name,
  154. ]);
  155. }
  156. //redeem voucher
  157. $voucher->redeem($request->user());
  158. event(new UserUpdateCreditsEvent($request->user()));
  159. return response()->json([
  160. 'success' => "{$voucher->credits} ". $general_settings->credits_display_name .' '.__('have been added to your balance!'),
  161. ]);
  162. }
  163. public function usersDataTable(Voucher $voucher)
  164. {
  165. $users = $voucher->users();
  166. return datatables($users)
  167. ->editColumn('name', function (User $user) {
  168. return '<a class="text-info" target="_blank" href="'.route('admin.users.show', $user->id).'">'.$user->name.'</a>';
  169. })
  170. ->addColumn('credits', function (User $user) {
  171. return '<i class="fas fa-coins mr-2"></i> '.$user->credits();
  172. })
  173. ->addColumn('last_seen', function (User $user) {
  174. return $user->last_seen ? $user->last_seen->diffForHumans() : '';
  175. })
  176. ->rawColumns(['name', 'credits', 'last_seen'])
  177. ->make();
  178. }
  179. public function dataTable()
  180. {
  181. $query = Voucher::query();
  182. return datatables($query)
  183. ->addColumn('actions', function (Voucher $voucher) {
  184. return '
  185. <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>
  186. <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>
  187. <form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.vouchers.destroy', $voucher->id).'">
  188. '.csrf_field().'
  189. '.method_field('DELETE').'
  190. <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>
  191. </form>
  192. ';
  193. })
  194. ->addColumn('status', function (Voucher $voucher) {
  195. $color = 'success';
  196. if ($voucher->getStatus() != __('VALID')) {
  197. $color = 'danger';
  198. }
  199. return '<span class="badge badge-'.$color.'">'.$voucher->getStatus().'</span>';
  200. })
  201. ->editColumn('uses', function (Voucher $voucher) {
  202. return "{$voucher->used} / {$voucher->uses}";
  203. })
  204. ->editColumn('credits', function (Voucher $voucher) {
  205. return number_format($voucher->credits, 2, '.', '');
  206. })
  207. ->editColumn('expires_at', function (Voucher $voucher) {
  208. if (! $voucher->expires_at) {
  209. return '';
  210. }
  211. return $voucher->expires_at ? $voucher->expires_at->diffForHumans() : '';
  212. })
  213. ->editColumn('code', function (Voucher $voucher) {
  214. return "<code>{$voucher->code}</code>";
  215. })
  216. ->rawColumns(['actions', 'code', 'status'])
  217. ->make();
  218. }
  219. }