OrderController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Http\Controllers\Seller;
  3. use App\Models\Order;
  4. use App\Models\ProductStock;
  5. use App\Models\SmsTemplate;
  6. use App\Models\User;
  7. use App\Utility\NotificationUtility;
  8. use App\Utility\SmsUtility;
  9. use Illuminate\Http\Request;
  10. use Auth;
  11. use DB;
  12. use Illuminate\Support\Carbon;
  13. use function dd;
  14. class OrderController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource to seller.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index(Request $request)
  22. {
  23. $payment_status = null;
  24. $delivery_status = null;
  25. $sort_search = null;
  26. $orders = Order::orderBy('id', 'desc')
  27. ->where('seller_id', Auth::user()->id)
  28. ->select('orders.id')
  29. ->distinct();
  30. if ($request->payment_status != null) {
  31. $orders = $orders->where('payment_status', $request->payment_status);
  32. $payment_status = $request->payment_status;
  33. }
  34. if ($request->delivery_status != null) {
  35. $orders = $orders->where('delivery_status', $request->delivery_status);
  36. $delivery_status = $request->delivery_status;
  37. }
  38. if ($request->has('search')) {
  39. $sort_search = $request->search;
  40. $orders = $orders->where(function ($query)use ($sort_search){
  41. $query->where('code', 'like', '%' . $sort_search . '%')->orWhereHas("user",function ($query)use ($sort_search){
  42. $query->where('email', 'like', '%' . $sort_search . '%');
  43. });
  44. });
  45. }
  46. $orders = $orders->paginate(15);
  47. foreach ($orders as $key => $value) {
  48. $order = Order::find($value->id);
  49. $order->viewed = 1;
  50. $order->save();
  51. }
  52. return view('seller.orders.index', compact('orders', 'payment_status', 'delivery_status', 'sort_search'));
  53. }
  54. public function show( $id ) {
  55. $order = Order::findOrFail(decrypt($id));
  56. $order_shipping_address = json_decode($order->shipping_address);
  57. $delivery_boys = User::where('city', $order_shipping_address->city)
  58. ->where('user_type', 'delivery_boy')
  59. ->get();
  60. $tpwd = Auth::user()->tpwd;
  61. $express = '';
  62. if ( $order->express_info )
  63. {
  64. $express = json_decode($order->express_info);
  65. }
  66. error_reporting(0);
  67. $order->viewed = 1;
  68. $order->save();
  69. return view('seller.orders.show', compact('order', 'delivery_boys', 'express', 'tpwd'));
  70. }
  71. // 钱包余额支付货款
  72. public function buy_package(Request $request)
  73. {
  74. }
  75. public function paymentForStorehouseProductAmount(Request $request)
  76. {
  77. if (!$request->filled('order_id')) return response()->json(['success' => 0, 'message' => translate('Something went wrong!')]);
  78. $orderId = decrypt($request->order_id);
  79. $order = Order::findOrFail($orderId);
  80. // dd($order);
  81. if (!$order || $order->product_storehouse_total <= 0) return response()->json(['success' => 0, 'message' => translate('Something went wrong!')]);
  82. if ($order->product_storehouse_status == 1) return response()->json(['success' => 0, 'message' => translate('Payment completed')]);
  83. DB::beginTransaction();
  84. $shop = $order->shop;
  85. $user = $shop->user;
  86. if ($user->balance >= $order->product_storehouse_total) {
  87. $user->balance -= $order->product_storehouse_total;
  88. $user->save();
  89. $shop->admin_to_pay += $order->grand_total;
  90. $shop->save();
  91. // 保存订单冻结资金过期时间
  92. $freezeDays = get_setting('frozen_funds_unfrozen_days', 15);
  93. $order->freeze_expired_at = Carbon::now()->addDays($freezeDays)->timestamp;
  94. $order->product_storehouse_status = 1;
  95. $order->save();
  96. DB::commit();
  97. return response()->json(['success' => 1, 'message' => translate('Payment completed')]);
  98. }
  99. DB::rollBack();
  100. return response()->json(['success' => 0, 'message' => translate('Insufficient balance')]);
  101. }
  102. // Update Delivery Status
  103. public function update_delivery_status(Request $request)
  104. {
  105. $order = Order::findOrFail($request->order_id);
  106. $order->delivery_viewed = '0';
  107. $order->delivery_status = $request->status;
  108. $order->save();
  109. if ($request->status == 'cancelled' && $order->payment_type == 'wallet' && $order->picking_switch==1) {
  110. $user = User::where('id', $order->user_id)->first();
  111. $user->balance += $order->grand_total;
  112. $user->save();
  113. }
  114. // if ($request->status == 'delivered') {
  115. // product_storehouse_order_free_up($order);
  116. // }
  117. foreach ($order->orderDetails->where('seller_id', Auth::user()->id) as $key => $orderDetail) {
  118. $orderDetail->delivery_status = $request->status;
  119. $orderDetail->save();
  120. if ($request->status == 'cancelled') {
  121. $variant = $orderDetail->variation;
  122. if ($orderDetail->variation == null) {
  123. $variant = '';
  124. }
  125. $product_stock = ProductStock::where('product_id', $orderDetail->product_id)
  126. ->where('variant', $variant)
  127. ->first();
  128. if ($product_stock != null) {
  129. $product_stock->qty += $orderDetail->quantity;
  130. $product_stock->save();
  131. }
  132. }
  133. }
  134. if (addon_is_activated('otp_system') && SmsTemplate::where('identifier', 'delivery_status_change')->first()->status == 1) {
  135. try {
  136. SmsUtility::delivery_status_change(json_decode($order->shipping_address)->phone, $order);
  137. } catch (\Exception $e) {
  138. }
  139. }
  140. //sends Notifications to user
  141. NotificationUtility::sendNotification($order, $request->status);
  142. if (get_setting('google_firebase') == 1 && $order->user->device_token != null) {
  143. $request->device_token = $order->user->device_token;
  144. $request->title = "Order updated !";
  145. $status = str_replace("_", "", $order->delivery_status);
  146. $request->text = " Your order {$order->code} has been {$status}";
  147. $request->type = "order";
  148. $request->id = $order->id;
  149. $request->user_id = $order->user->id;
  150. NotificationUtility::sendFirebaseNotification($request);
  151. }
  152. if (addon_is_activated('delivery_boy')) {
  153. if (Auth::user()->user_type == 'delivery_boy') {
  154. $deliveryBoyController = new DeliveryBoyController;
  155. $deliveryBoyController->store_delivery_history($order);
  156. }
  157. }
  158. return 1;
  159. }
  160. // Update Payment Status
  161. public function update_payment_status(Request $request)
  162. {
  163. $order = Order::findOrFail($request->order_id);
  164. $order->payment_status_viewed = '0';
  165. $order->save();
  166. foreach ($order->orderDetails->where('seller_id', Auth::user()->id) as $key => $orderDetail) {
  167. $orderDetail->payment_status = $request->status;
  168. $orderDetail->save();
  169. }
  170. $status = 'paid';
  171. foreach ($order->orderDetails as $key => $orderDetail) {
  172. if ($orderDetail->payment_status != 'paid') {
  173. $status = 'unpaid';
  174. }
  175. }
  176. $order->payment_status = $status;
  177. $order->save();
  178. if ($order->payment_status == 'paid' && $order->commission_calculated == 0) {
  179. calculateCommissionAffilationClubPoint($order);
  180. }
  181. //sends Notifications to user
  182. NotificationUtility::sendNotification($order, $request->status);
  183. if (get_setting('google_firebase') == 1 && $order->user->device_token != null) {
  184. $request->device_token = $order->user->device_token;
  185. $request->title = "Order updated !";
  186. $status = str_replace("_", "", $order->payment_status);
  187. $request->text = " Your order {$order->code} has been {$status}";
  188. $request->type = "order";
  189. $request->id = $order->id;
  190. $request->user_id = $order->user->id;
  191. NotificationUtility::sendFirebaseNotification($request);
  192. }
  193. if (addon_is_activated('otp_system') && SmsTemplate::where('identifier', 'payment_status_change')->first()->status == 1) {
  194. try {
  195. SmsUtility::payment_status_change(json_decode($order->shipping_address)->phone, $order);
  196. } catch (\Exception $e) {
  197. }
  198. }
  199. return 1;
  200. }
  201. }