ClubPointController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\BusinessSetting;
  5. use App\Models\ClubPointDetail;
  6. use App\Models\ClubPoint;
  7. use App\Models\Product;
  8. use App\Models\Wallet;
  9. use App\Models\Order;
  10. use Artisan;
  11. use Auth;
  12. class ClubPointController extends Controller
  13. {
  14. public function configure_index()
  15. {
  16. return view('club_points.config');
  17. }
  18. public function index()
  19. {
  20. $club_points = ClubPoint::latest()->paginate(15);
  21. return view('club_points.index', compact('club_points'));
  22. }
  23. public function invitation_code()
  24. {
  25. $invit_user_url='https://'.$_SERVER['SERVER_NAME'].'/users/registration?leader_id='.Auth::user()->id;
  26. $invit_store_url='https://'.$_SERVER['SERVER_NAME'].'/shops/create?leader_id='.Auth::user()->id;
  27. return view('club_points.invitation_code', compact('invit_user_url','invit_store_url'));
  28. }
  29. public function registered_shop()
  30. {
  31. $invit_user_url='https://'.$_SERVER['SERVER_NAME'].'/users/registration?leader_id='.Auth::user()->id;
  32. $invit_store_url='https://'.$_SERVER['SERVER_NAME'].'/shops/create?leader_id='.Auth::user()->id;
  33. return view('club_points.registered_shop', compact('invit_user_url','invit_store_url'));
  34. }
  35. public function userpoint_index()
  36. {
  37. $club_points = ClubPoint::where('user_id', Auth::user()->id)->latest()->paginate(15);
  38. return view('club_points.frontend.index', compact('club_points'));
  39. }
  40. public function set_point()
  41. {
  42. $products = Product::latest()->paginate(15);
  43. return view('club_points.set_point', compact('products'));
  44. }
  45. public function set_products_point(Request $request)
  46. {
  47. $products = Product::whereBetween('unit_price', [$request->min_price, $request->max_price])->get();
  48. foreach ($products as $product) {
  49. $product->earn_point = $request->point;
  50. $product->save();
  51. }
  52. flash(translate('Point has been inserted successfully for ').count($products).translate(' products'))->success();
  53. return redirect()->route('set_product_points');
  54. }
  55. public function set_all_products_point(Request $request)
  56. {
  57. $products = Product::all();
  58. foreach ($products as $product) {;
  59. $product->earn_point = $product->unit_price * $request->point;
  60. $product->save();
  61. }
  62. flash(translate('Point has been inserted successfully for ').count($products).translate(' products'))->success();
  63. return redirect()->route('set_product_points');
  64. }
  65. public function set_point_edit($id)
  66. {
  67. $product = Product::findOrFail(decrypt($id));
  68. return view('club_points.product_point_edit', compact('product'));
  69. }
  70. public function update_product_point(Request $request, $id)
  71. {
  72. $product = Product::findOrFail($id);
  73. $product->earn_point = $request->point;
  74. $product->save();
  75. flash(translate('Point has been updated successfully'))->success();
  76. return redirect()->route('set_product_points');
  77. }
  78. public function convert_rate_store(Request $request)
  79. {
  80. $club_point_convert_rate = BusinessSetting::where('type', $request->type)->first();
  81. if ($club_point_convert_rate != null) {
  82. $club_point_convert_rate->value = $request->value;
  83. }
  84. else {
  85. $club_point_convert_rate = new BusinessSetting;
  86. $club_point_convert_rate->type = $request->type;
  87. $club_point_convert_rate->value = $request->value;
  88. }
  89. $club_point_convert_rate->save();
  90. Artisan::call('cache:clear');
  91. flash(translate('Point convert rate has been updated successfully'))->success();
  92. return redirect()->route('club_points.configs');
  93. }
  94. public function processClubPoints(Order $order)
  95. {
  96. $club_point = new ClubPoint;
  97. $club_point->user_id = $order->user_id;
  98. $club_point->points = 0;
  99. foreach ($order->orderDetails as $key => $orderDetail) {
  100. $total_pts = ($orderDetail->product->earn_point) * $orderDetail->quantity;
  101. $club_point->points += $total_pts;
  102. }
  103. $club_point->order_id = $order->id;
  104. $club_point->convert_status = 0;
  105. $club_point->save();
  106. foreach ($order->orderDetails as $key => $orderDetail) {
  107. $club_point_detail = new ClubPointDetail;
  108. $club_point_detail->club_point_id = $club_point->id;
  109. $club_point_detail->product_id = $orderDetail->product_id;
  110. $club_point_detail->point = ($orderDetail->product->earn_point) * $orderDetail->quantity;
  111. $club_point_detail->save();
  112. }
  113. }
  114. public function club_point_detail($id)
  115. {
  116. $club_point_details = ClubPointDetail::where('club_point_id', decrypt($id))->paginate(12);
  117. return view('club_points.club_point_details', compact('club_point_details'));
  118. }
  119. public function convert_point_into_wallet(Request $request)
  120. {
  121. $club_point = ClubPoint::findOrFail($request->el);
  122. if($club_point->convert_status == 0) {
  123. $wallet = new Wallet;
  124. $wallet->user_id = Auth::user()->id;
  125. $wallet->amount = floatval($club_point->points / get_setting('club_point_convert_rate'));
  126. $wallet->payment_method = 'Club Point Convert';
  127. $wallet->payment_details = 'Club Point Convert';
  128. $wallet->save();
  129. $user = Auth::user();
  130. $user->balance = $user->balance + floatval($club_point->points / get_setting('club_point_convert_rate'));
  131. $user->save();
  132. $club_point->convert_status = 1;
  133. }
  134. if ($club_point->save()) {
  135. return 1;
  136. }
  137. else {
  138. return 0;
  139. }
  140. }
  141. }