SalesmanorderController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\AffiliateController;
  4. use App\Http\Controllers\OTPVerificationController;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\ClubPointController;
  7. use App\Models\Order;
  8. use App\Models\Cart;
  9. use App\Models\Address;
  10. use App\Models\Product;
  11. use App\Models\ProductStock;
  12. use App\Models\CommissionHistory;
  13. use App\Models\Color;
  14. use App\Models\OrderDetail;
  15. use App\Models\CouponUsage;
  16. use App\Models\Coupon;
  17. use App\OtpConfiguration;
  18. use App\Models\User;
  19. use App\Models\BusinessSetting;
  20. use App\Models\CombinedOrder;
  21. use App\Models\SmsTemplate;
  22. use Auth;
  23. use Session;
  24. use DB;
  25. use Mail;
  26. use App\Mail\InvoiceEmailManager;
  27. use App\Utility\NotificationUtility;
  28. use CoreComponentRepository;
  29. use App\Utility\SmsUtility;
  30. class SalesmanorderController extends Controller
  31. {
  32. /**
  33. * Display a listing of the resource to seller.
  34. *
  35. * @return \Illuminate\Http\Response
  36. */
  37. public function index( Request $request ) {
  38. $payment_status = NULL;
  39. $delivery_status = NULL;
  40. $sort_search = NULL;
  41. $pid = Auth::user()->id;
  42. $userIds = User::where('user_type', 'seller')->where(function ($user) use ($pid) {
  43. $user->where('pid', $pid);
  44. })->pluck('id')->toArray();
  45. $orders =Order::orderBy('id', 'desc')
  46. ->whereIn('seller_id', $userIds)
  47. ->select('orders.id')
  48. ->distinct();
  49. if ( $request->payment_status != NULL )
  50. {
  51. $orders = $orders->where('payment_status', $request->payment_status);
  52. $payment_status = $request->payment_status;
  53. }
  54. if ( $request->delivery_status != NULL )
  55. {
  56. $orders = $orders->where('delivery_status', $request->delivery_status);
  57. $delivery_status = $request->delivery_status;
  58. }
  59. if ( $request->has('search') )
  60. {
  61. $sort_search = $request->search;
  62. $orders = $orders->where(function ($query)use ($sort_search){
  63. $query->where('code', 'like', '%' . $sort_search . '%')->orWhereHas("shop",function ($query)use ($sort_search){
  64. $query->whereHas("user",function ($query)use ($sort_search){
  65. $query->where('email', 'like', '%' . $sort_search . '%');
  66. });
  67. });
  68. });
  69. }
  70. $orders = $orders->paginate(15);
  71. return view('salesman.orders.index', compact('orders', 'payment_status', 'delivery_status', 'sort_search'));
  72. }
  73. // All Orders
  74. public function all_orders( Request $request ) {
  75. CoreComponentRepository::instantiateShopRepository();
  76. $date = $request->date;
  77. $sort_search = NULL;
  78. $delivery_status = NULL;
  79. $orders = Order::orderBy('id', 'desc');
  80. if ( $request->has('search') )
  81. {
  82. $sort_search = $request->search;
  83. $orders = $orders->where('code', 'like', '%' . $sort_search . '%');
  84. }
  85. if ( $request->delivery_status != NULL )
  86. {
  87. $orders = $orders->where('delivery_status', $request->delivery_status);
  88. $delivery_status = $request->delivery_status;
  89. }
  90. if ( $date != NULL )
  91. {
  92. $orders = $orders->where('created_at', '>=', date('Y-m-d', strtotime(explode(" to ", $date)[0])))->where('created_at', '<=', date('Y-m-d', strtotime(explode(" to ", $date)[1])));
  93. }
  94. $orders = $orders->paginate(15);
  95. return view('backend.sales.all_orders.index', compact('orders', 'sort_search', 'delivery_status', 'date'));
  96. }
  97. public function all_orders_show( $id ) {
  98. $order = Order::findOrFail(decrypt($id));
  99. $express = '';
  100. if ( $order->express_info )
  101. {
  102. $express = json_decode($order->express_info);
  103. }
  104. error_reporting(0);
  105. $order_shipping_address = json_decode($order->shipping_address);
  106. $delivery_boys = [];
  107. if ( isset($order_shipping_address->city) )
  108. {
  109. $delivery_boys = User::where('city', $order_shipping_address->city)
  110. ->where('user_type', 'delivery_boy')
  111. ->get();
  112. }
  113. return view('backend.sales.all_orders.show', compact('order', 'delivery_boys', 'express'));
  114. }
  115. public function show( $id ) {
  116. $order = Order::findOrFail(decrypt($id));
  117. $express = '';
  118. if ( $order->express_info )
  119. {
  120. $express = json_decode($order->express_info);
  121. }
  122. error_reporting(0);
  123. $order_shipping_address = json_decode($order->shipping_address);
  124. $delivery_boys = [];
  125. if ( isset($order_shipping_address->city) )
  126. {
  127. $delivery_boys = User::where('city', $order_shipping_address->city)
  128. ->where('user_type', 'delivery_boy')
  129. ->get();
  130. }
  131. return view('salesman.orders.show', compact('order', 'delivery_boys', 'express'));
  132. }
  133. // Seller Orders
  134. public function seller_orders( Request $request ) {
  135. CoreComponentRepository::instantiateShopRepository();
  136. $date = $request->date;
  137. $seller_id = $request->seller_id;
  138. $payment_status = NULL;
  139. $delivery_status = NULL;
  140. $sort_search = NULL;
  141. $admin_user_id = User::where('user_type', 'admin')->first()->id;
  142. $orders = Order::orderBy('code', 'desc')
  143. ->where('orders.seller_id', '!=', $admin_user_id);
  144. if ( $request->payment_type != NULL )
  145. {
  146. $orders = $orders->where('payment_status', $request->payment_type);
  147. $payment_status = $request->payment_type;
  148. }
  149. if ( $request->delivery_status != NULL )
  150. {
  151. $orders = $orders->where('delivery_status', $request->delivery_status);
  152. $delivery_status = $request->delivery_status;
  153. }
  154. if ( $request->has('search') )
  155. {
  156. $sort_search = $request->search;
  157. $orders = $orders->where('code', 'like', '%' . $sort_search . '%');
  158. }
  159. if ( $date != NULL )
  160. {
  161. $orders = $orders->whereDate('created_at', '>=', date('Y-m-d', strtotime(explode(" to ", $date)[0])))->whereDate('created_at', '<=', date('Y-m-d', strtotime(explode(" to ", $date)[1])));
  162. }
  163. if ( $seller_id )
  164. {
  165. $orders = $orders->where('seller_id', $seller_id);
  166. }
  167. $orders = $orders->paginate(15);
  168. return view('backend.sales.seller_orders.index', compact('orders', 'payment_status', 'delivery_status', 'sort_search', 'admin_user_id', 'seller_id', 'date'));
  169. }
  170. public function seller_orders_show( $id ) {
  171. $order = Order::findOrFail(decrypt($id));
  172. $order->viewed = 1;
  173. $order->save();
  174. return view('backend.sales.seller_orders.show', compact('order'));
  175. }
  176. /**
  177. * Display a single sale to admin.
  178. *
  179. * @return \Illuminate\Http\Response
  180. */
  181. /**
  182. * Show the form for creating a new resource.
  183. *
  184. * @return \Illuminate\Http\Response
  185. */
  186. public function create() {
  187. //
  188. }
  189. /**
  190. * Store a newly created resource in storage.
  191. *
  192. * @param \Illuminate\Http\Request $request
  193. *
  194. * @return \Illuminate\Http\Response
  195. */
  196. public function store( Request $request ) {
  197. $carts = Cart::where('user_id', Auth::user()->id)
  198. ->get();
  199. if ( $carts->isEmpty() )
  200. {
  201. flash(translate('Your cart is empty'))->warning();
  202. return redirect()->route('home');
  203. }
  204. $address = Address::where('id', $carts[0]['address_id'])->first();
  205. $shippingAddress = [];
  206. if ( $address != NULL )
  207. {
  208. $shippingAddress['name'] = Auth::user()->name;
  209. $shippingAddress['email'] = Auth::user()->email;
  210. $shippingAddress['address'] = $address->address;
  211. $shippingAddress['country'] = $address->country->name;
  212. $shippingAddress['state'] = $address->state->name;
  213. $shippingAddress['city'] = $address->city->name;
  214. $shippingAddress['postal_code'] = $address->postal_code;
  215. $shippingAddress['phone'] = $address->phone;
  216. if ( $address->latitude || $address->longitude )
  217. {
  218. $shippingAddress['lat_lang'] = $address->latitude . ',' . $address->longitude;
  219. }
  220. }
  221. $combined_order = new CombinedOrder;
  222. $combined_order->user_id = Auth::user()->id;
  223. $combined_order->shipping_address = json_encode($shippingAddress);
  224. $combined_order->save();
  225. $seller_products = [];
  226. foreach ( $carts as $cartItem )
  227. {
  228. $product_ids = [];
  229. $product = Product::find($cartItem['product_id']);
  230. if ( isset($seller_products[$product->user_id]) )
  231. {
  232. $product_ids = $seller_products[$product->user_id];
  233. }
  234. array_push($product_ids, $cartItem);
  235. $seller_products[$product->user_id] = $product_ids;
  236. }
  237. foreach ( $seller_products as $seller_product )
  238. {
  239. $order = new Order;
  240. $order->combined_order_id = $combined_order->id;
  241. $order->user_id = Auth::user()->id;
  242. $order->shipping_address = $combined_order->shipping_address;
  243. $order->additional_info = $request->additional_info;
  244. $order->shipping_type = $carts[0]['shipping_type'];
  245. if ( $carts[0]['shipping_type'] == 'pickup_point' )
  246. {
  247. $order->pickup_point_id = $cartItem['pickup_point'];
  248. }
  249. $order->payment_type = $request->payment_option;
  250. $order->delivery_viewed = '0';
  251. $order->payment_status_viewed = '0';
  252. $order->code = date('Ymd-His') . rand(10, 99);
  253. $order->date = strtotime('now');
  254. $order->save();
  255. $subtotal = 0;
  256. $tax = 0;
  257. $shipping = 0;
  258. $coupon_discount = 0;
  259. // 产品仓库的产品货款
  260. $productStorehouseTotal = 0;
  261. //Order Details Storing
  262. foreach ( $seller_product as $cartItem )
  263. {
  264. $product = Product::find($cartItem['product_id']);
  265. // 计算产品仓库的产品货款
  266. $originalProduct = NULL;
  267. if ( $product->original_id )
  268. {
  269. $originalProduct = Product::query()->find($product->original_id);
  270. if ( $originalProduct )
  271. {
  272. $productStorehouseTotal += cart_product_price($cartItem, $originalProduct, false, false) * $cartItem['quantity'];
  273. }
  274. }
  275. $subtotal += cart_product_price($cartItem, $product, false, false) * $cartItem['quantity'];
  276. $tax += cart_product_tax($cartItem, $product, false) * $cartItem['quantity'];
  277. $coupon_discount += $cartItem['discount'];
  278. $product_variation = $cartItem['variation'];
  279. $product_stock = $product->stocks->where('variant', $product_variation)->first();
  280. if ( $product->digital != 1 && $cartItem['quantity'] > $product_stock->qty )
  281. {
  282. flash(translate('The requested quantity is not available for ') . $product->getTranslation('name'))->warning();
  283. $order->delete();
  284. return redirect()->route('cart')->send();
  285. }
  286. else if ( $product->digital != 1 )
  287. {
  288. $product_stock->qty -= $cartItem['quantity'];
  289. $product_stock->save();
  290. }
  291. $order_detail = new OrderDetail;
  292. $order_detail->order_id = $order->id;
  293. $order_detail->seller_id = $product->user_id;
  294. $order_detail->product_id = $product->id;
  295. $order_detail->is_storehouse_product = $product->original_id ? 1 : 0; // 是否产品仓库产品
  296. $order_detail->original_product_id = $product->original_id ?: NULL; // 原产品仓库产品ID
  297. $order_detail->original_product_price = $originalProduct ? $originalProduct->unit_price : NULL; // 原产品仓库产品价格(进货价)
  298. $order_detail->variation = $product_variation;
  299. $order_detail->price = cart_product_price($cartItem, $product, false, false) * $cartItem['quantity'];
  300. $order_detail->tax = cart_product_tax($cartItem, $product, false) * $cartItem['quantity'];
  301. $order_detail->shipping_type = $cartItem['shipping_type'];
  302. $order_detail->product_referral_code = $cartItem['product_referral_code'];
  303. $order_detail->shipping_cost = $cartItem['shipping_cost'];
  304. $shipping += $order_detail->shipping_cost;
  305. //End of storing shipping cost
  306. $order_detail->quantity = $cartItem['quantity'];
  307. $order_detail->save();
  308. $product->num_of_sale += $cartItem['quantity'];
  309. $product->save();
  310. $order->seller_id = $product->user_id;
  311. if ( $product->added_by == 'seller' && $product->user->seller != NULL )
  312. {
  313. $seller = $product->user->seller;
  314. $seller->num_of_sale += $cartItem['quantity'];
  315. $seller->save();
  316. }
  317. if ( addon_is_activated('affiliate_system') )
  318. {
  319. if ( $order_detail->product_referral_code )
  320. {
  321. $referred_by_user = User::where('referral_code', $order_detail->product_referral_code)->first();
  322. $affiliateController = new AffiliateController;
  323. $affiliateController->processAffiliateStats($referred_by_user->id, 0, $order_detail->quantity, 0, 0);
  324. }
  325. }
  326. }
  327. $order->grand_total = $subtotal + $tax + $shipping;
  328. $order->product_storehouse_total = $productStorehouseTotal;
  329. if ( $seller_product[0]->coupon_code != NULL )
  330. {
  331. // if (Session::has('club_point')) {
  332. // $order->club_point = Session::get('club_point');
  333. // }
  334. $order->coupon_discount = $coupon_discount;
  335. $order->grand_total -= $coupon_discount;
  336. $coupon_usage = new CouponUsage;
  337. $coupon_usage->user_id = Auth::user()->id;
  338. $coupon_usage->coupon_id = Coupon::where('code', $seller_product[0]->coupon_code)->first()->id;
  339. $coupon_usage->save();
  340. }
  341. $combined_order->grand_total += $order->grand_total;
  342. $order->save();
  343. }
  344. $combined_order->save();
  345. $request->session()->put('combined_order_id', $combined_order->id);
  346. }
  347. /**
  348. * Display the specified resource.
  349. *
  350. * @param int $id
  351. *
  352. * @return \Illuminate\Http\Response
  353. */
  354. /**
  355. * Show the form for editing the specified resource.
  356. *
  357. * @param int $id
  358. *
  359. * @return \Illuminate\Http\Response
  360. */
  361. public function edit( $id ) {
  362. //
  363. }
  364. /**
  365. * Update the specified resource in storage.
  366. *
  367. * @param \Illuminate\Http\Request $request
  368. * @param int $id
  369. *
  370. * @return \Illuminate\Http\Response
  371. */
  372. public function update( Request $request, $id ) {
  373. //
  374. }
  375. /**
  376. * Remove the specified resource from storage.
  377. *
  378. * @param int $id
  379. *
  380. * @return \Illuminate\Http\Response
  381. */
  382. public function destroy( $id ) {
  383. $order = Order::findOrFail($id);
  384. if ( $order != NULL )
  385. {
  386. foreach ( $order->orderDetails as $key => $orderDetail )
  387. {
  388. try
  389. {
  390. $product_stock = ProductStock::where('product_id', $orderDetail->product_id)->where('variant', $orderDetail->variation)->first();
  391. if ( $product_stock != NULL )
  392. {
  393. $product_stock->qty += $orderDetail->quantity;
  394. $product_stock->save();
  395. }
  396. }
  397. catch ( \Exception $e )
  398. {
  399. }
  400. $orderDetail->delete();
  401. }
  402. $order->delete();
  403. flash(translate('Order has been deleted successfully'))->success();
  404. }
  405. else
  406. {
  407. flash(translate('Something went wrong'))->error();
  408. }
  409. return back();
  410. }
  411. public function order_details( Request $request ) {
  412. $order = Order::findOrFail($request->order_id);
  413. $order->save();
  414. return view('seller.order_details_seller', compact('order'));
  415. }
  416. }