OrderController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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 App\Models\Shop;
  23. use App\Models\SellerPackage;
  24. use Auth;
  25. use Illuminate\Support\Carbon;
  26. use Session;
  27. use DB;
  28. use Mail;
  29. use App\Mail\InvoiceEmailManager;
  30. use App\Utility\NotificationUtility;
  31. use CoreComponentRepository;
  32. use App\Utility\SmsUtility;
  33. use function get_setting;
  34. use function response;
  35. use function translate;
  36. class OrderController extends Controller
  37. {
  38. public function update_delivery_info(Request $request)
  39. {
  40. # echo "<PRE>";
  41. #print_r( $_POST['data'] );
  42. parse_str( $_POST['data'] , $arr );
  43. $json = json_encode( $arr );
  44. $order = Order::findOrFail( $arr['order_id'] );
  45. $order->express_info = $json;
  46. $order->save();
  47. echo 'Save Success';
  48. }
  49. /**
  50. * Display a listing of the resource to seller.
  51. *
  52. * @return \Illuminate\Http\Response
  53. */
  54. public function index(Request $request)
  55. {
  56. $payment_status = null;
  57. $delivery_status = null;
  58. $sort_search = null;
  59. $orders = DB::table('orders')
  60. ->orderBy('id', 'desc')
  61. //->join('order_details', 'orders.id', '=', 'order_details.order_id')
  62. ->where('seller_id', Auth::user()->id)
  63. ->select('orders.id')
  64. ->distinct();
  65. if ($request->payment_status != null) {
  66. $orders = $orders->where('payment_status', $request->payment_status);
  67. $payment_status = $request->payment_status;
  68. }
  69. if ($request->delivery_status != null) {
  70. $orders = $orders->where('delivery_status', $request->delivery_status);
  71. $delivery_status = $request->delivery_status;
  72. }
  73. if ($request->has('search')) {
  74. $sort_search = $request->search;
  75. $orders = $orders->where('code', 'like', '%' . $sort_search . '%');
  76. }
  77. $orders = $orders->paginate(15);
  78. foreach ($orders as $key => $value) {
  79. $order = \App\Models\Order::find($value->id);
  80. $order->viewed = 1;
  81. $order->save();
  82. }
  83. return view('seller.orders.index', compact('orders', 'payment_status', 'delivery_status', 'sort_search'));
  84. }
  85. // All Orders
  86. public function all_orders(Request $request)
  87. {
  88. CoreComponentRepository::instantiateShopRepository();
  89. $date = $request->date;
  90. $sort_search = null;
  91. $delivery_status = null;
  92. $orders = Order::orderBy('id', 'desc');
  93. if ($request->has('search')) {
  94. $sort_search = $request->search;
  95. $orders = $orders->where('code', 'like', '%' . $sort_search . '%');
  96. }
  97. if ($request->delivery_status != null) {
  98. $orders = $orders->where('delivery_status', $request->delivery_status);
  99. $delivery_status = $request->delivery_status;
  100. }
  101. if ($date != null) {
  102. $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])));
  103. }
  104. $orders = $orders->paginate(15);
  105. return view('backend.sales.all_orders.index', compact('orders', 'sort_search', 'delivery_status', 'date'));
  106. }
  107. // Storehouse Orders
  108. public function storehouse_orders(Request $request)
  109. {
  110. CoreComponentRepository::instantiateShopRepository();
  111. $date = $request->date;
  112. $sort_search = null;
  113. $delivery_status = null;
  114. $orders = Order::orderBy('id', 'desc');
  115. $orders = $orders->where('product_storehouse_total', '>', 0);
  116. if ($request->has('search')) {
  117. $sort_search = $request->search;
  118. $orders = $orders->where('code', 'like', '%' . $sort_search . '%');
  119. }
  120. if ($request->delivery_status != null) {
  121. $orders = $orders->where('delivery_status', $request->delivery_status);
  122. $delivery_status = $request->delivery_status;
  123. }
  124. if ($date != null) {
  125. $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])));
  126. }
  127. $orders = $orders->paginate(15);
  128. return view('backend.sales.storehouse_orders.index', compact('orders', 'sort_search', 'delivery_status', 'date'));
  129. }
  130. public function storehouse_orders_show($id)
  131. {
  132. $order = Order::findOrFail(decrypt($id));
  133. $order_shipping_address = json_decode($order->shipping_address);
  134. $delivery_boys = array();
  135. if (isset($order_shipping_address->city)) {
  136. $delivery_boys = User::where('city', $order_shipping_address->city)
  137. ->where('user_type', 'delivery_boy')
  138. ->get();
  139. }
  140. return view('backend.sales.storehouse_orders.show', compact('order', 'delivery_boys'));
  141. }
  142. public function all_orders_show($id)
  143. {
  144. $order = Order::findOrFail(decrypt($id));
  145. $express = '';
  146. if( $order->express_info )
  147. {
  148. $express = json_decode($order->express_info );
  149. }
  150. error_reporting(0);
  151. $order_shipping_address = json_decode($order->shipping_address);
  152. $delivery_boys = array();
  153. if (isset($order_shipping_address->city)) {
  154. $delivery_boys = User::where('city', $order_shipping_address->city)
  155. ->where('user_type', 'delivery_boy')
  156. ->get();
  157. }
  158. return view('backend.sales.all_orders.show', compact('order', 'delivery_boys','express'));
  159. }
  160. // Inhouse Orders
  161. public function admin_orders(Request $request)
  162. {
  163. CoreComponentRepository::instantiateShopRepository();
  164. $date = $request->date;
  165. $payment_status = null;
  166. $delivery_status = null;
  167. $sort_search = null;
  168. $admin_user_id = User::where('user_type', 'admin')->first()->id;
  169. $orders = Order::orderBy('id', 'desc')
  170. ->where('seller_id', $admin_user_id);
  171. if ($request->payment_type != null) {
  172. $orders = $orders->where('payment_status', $request->payment_type);
  173. $payment_status = $request->payment_type;
  174. }
  175. if ($request->delivery_status != null) {
  176. $orders = $orders->where('delivery_status', $request->delivery_status);
  177. $delivery_status = $request->delivery_status;
  178. }
  179. if ($request->has('search')) {
  180. $sort_search = $request->search;
  181. $orders = $orders->where('code', 'like', '%' . $sort_search . '%');
  182. }
  183. if ($date != null) {
  184. $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])));
  185. }
  186. $orders = $orders->paginate(15);
  187. return view('backend.sales.inhouse_orders.index', compact('orders', 'payment_status', 'delivery_status', 'sort_search', 'admin_user_id', 'date'));
  188. }
  189. public function show($id)
  190. {
  191. $order = Order::findOrFail(decrypt($id));
  192. $order_shipping_address = json_decode($order->shipping_address);
  193. $delivery_boys = User::where('city', $order_shipping_address->city)
  194. ->where('user_type', 'delivery_boy')
  195. ->get();
  196. $order->viewed = 1;
  197. $order->save();
  198. return view('backend.sales.inhouse_orders.show', compact('order', 'delivery_boys'));
  199. }
  200. // Seller Orders
  201. public function seller_orders(Request $request)
  202. {
  203. CoreComponentRepository::instantiateShopRepository();
  204. $date = $request->date;
  205. $seller_id = $request->seller_id;
  206. $payment_status = null;
  207. $delivery_status = null;
  208. $sort_search = null;
  209. $admin_user_id = User::where('user_type', 'admin')->first()->id;
  210. $orders = Order::orderBy('code', 'desc')
  211. ->where('orders.seller_id', '!=', $admin_user_id);
  212. if ($request->payment_type != null) {
  213. $orders = $orders->where('payment_status', $request->payment_type);
  214. $payment_status = $request->payment_type;
  215. }
  216. if ($request->delivery_status != null) {
  217. $orders = $orders->where('delivery_status', $request->delivery_status);
  218. $delivery_status = $request->delivery_status;
  219. }
  220. if ($request->has('search')) {
  221. $sort_search = $request->search;
  222. $orders = $orders->where(function ($query)use ($sort_search){
  223. $query->where('code', 'like', '%' . $sort_search . '%')->orWhereHas("user",function ($query)use ($sort_search){
  224. $query->where('email', 'like', '%' . $sort_search . '%');
  225. });
  226. });
  227. }
  228. if ($date != null) {
  229. $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])));
  230. }
  231. if ($seller_id) {
  232. $orders = $orders->where('seller_id', $seller_id);
  233. }
  234. $orders = $orders->paginate(15);
  235. return view('backend.sales.seller_orders.index', compact('orders', 'payment_status', 'delivery_status', 'sort_search', 'admin_user_id', 'seller_id', 'date'));
  236. }
  237. public function seller_orders_show($id)
  238. {
  239. $order = Order::findOrFail(decrypt($id));
  240. $order->viewed = 1;
  241. $order->save();
  242. return view('backend.sales.seller_orders.show', compact('order'));
  243. }
  244. // Pickup point orders
  245. public function pickup_point_order_index(Request $request)
  246. {
  247. $date = $request->date;
  248. $sort_search = null;
  249. $orders = Order::query();
  250. if (Auth::user()->user_type == 'staff' && Auth::user()->staff->pick_up_point != null) {
  251. $orders->where('shipping_type', 'pickup_point')
  252. ->where('pickup_point_id', Auth::user()->staff->pick_up_point->id)
  253. ->orderBy('code', 'desc');
  254. if ($request->has('search')) {
  255. $sort_search = $request->search;
  256. $orders = $orders->where('code', 'like', '%' . $sort_search . '%');
  257. }
  258. if ($date != null) {
  259. $orders = $orders->whereDate('orders.created_at', '>=', date('Y-m-d', strtotime(explode(" to ", $date)[0])))->whereDate('orders.created_at', '<=', date('Y-m-d', strtotime(explode(" to ", $date)[1])));
  260. }
  261. $orders = $orders->paginate(15);
  262. return view('backend.sales.pickup_point_orders.index', compact('orders', 'sort_search', 'date'));
  263. } else {
  264. $orders->where('shipping_type', 'pickup_point')->orderBy('code', 'desc');
  265. if ($request->has('search')) {
  266. $sort_search = $request->search;
  267. $orders = $orders->where('code', 'like', '%' . $sort_search . '%');
  268. }
  269. if ($date != null) {
  270. $orders = $orders->whereDate('orders.created_at', '>=', date('Y-m-d', strtotime(explode(" to ", $date)[0])))->whereDate('orders.created_at', '<=', date('Y-m-d', strtotime(explode(" to ", $date)[1])));
  271. }
  272. $orders = $orders->paginate(15);
  273. return view('backend.sales.pickup_point_orders.index', compact('orders', 'sort_search', 'date'));
  274. }
  275. }
  276. public function pickup_point_order_sales_show($id)
  277. {
  278. if (Auth::user()->user_type == 'staff') {
  279. $order = Order::findOrFail(decrypt($id));
  280. $order_shipping_address = json_decode($order->shipping_address);
  281. $delivery_boys = User::where('city', $order_shipping_address->city)
  282. ->where('user_type', 'delivery_boy')
  283. ->get();
  284. return view('backend.sales.pickup_point_orders.show', compact('order', 'delivery_boys'));
  285. } else {
  286. $order = Order::findOrFail(decrypt($id));
  287. $order_shipping_address = json_decode($order->shipping_address);
  288. $delivery_boys = User::where('city', $order_shipping_address->city)
  289. ->where('user_type', 'delivery_boy')
  290. ->get();
  291. return view('backend.sales.pickup_point_orders.show', compact('order', 'delivery_boys'));
  292. }
  293. }
  294. /**
  295. * Display a single sale to admin.
  296. *
  297. * @return \Illuminate\Http\Response
  298. */
  299. /**
  300. * Show the form for creating a new resource.
  301. *
  302. * @return \Illuminate\Http\Response
  303. */
  304. public function create()
  305. {
  306. //
  307. }
  308. /**
  309. * Store a newly created resource in storage.
  310. *
  311. * @param \Illuminate\Http\Request $request
  312. * @return \Illuminate\Http\Response
  313. */
  314. public function store(Request $request)
  315. {
  316. $carts = Cart::where('user_id', Auth::user()->id)
  317. ->get();
  318. if ($carts->isEmpty()) {
  319. flash(translate('Your cart is empty'))->warning();
  320. return redirect()->route('home');
  321. }
  322. $address = Address::where('id', $carts[0]['address_id'])->first();
  323. $shippingAddress = [];
  324. if ($address != null) {
  325. $shippingAddress['name'] = Auth::user()->name;
  326. $shippingAddress['email'] = Auth::user()->email;
  327. $shippingAddress['address'] = $address->address;
  328. $shippingAddress['country'] = $address->country->name;
  329. $shippingAddress['state'] = $address->state->name;
  330. $shippingAddress['city'] = $address->city->name;
  331. $shippingAddress['postal_code'] = $address->postal_code;
  332. $shippingAddress['phone'] = $address->phone;
  333. if ($address->latitude || $address->longitude) {
  334. $shippingAddress['lat_lang'] = $address->latitude . ',' . $address->longitude;
  335. }
  336. }
  337. $combined_order = new CombinedOrder;
  338. $combined_order->user_id = Auth::user()->id;
  339. $combined_order->shipping_address = json_encode($shippingAddress);
  340. $combined_order->save();
  341. $seller_products = array();
  342. foreach ($carts as $cartItem) {
  343. $product_ids = array();
  344. $product = Product::find($cartItem['product_id']);
  345. if (isset($seller_products[$product->user_id])) {
  346. $product_ids = $seller_products[$product->user_id];
  347. }
  348. array_push($product_ids, $cartItem);
  349. $seller_products[$product->user_id] = $product_ids;
  350. }
  351. foreach ($seller_products as $key => $seller_product) {
  352. $order = new Order;
  353. $order->combined_order_id = $combined_order->id;
  354. $order->user_id = Auth::user()->id;
  355. $order->shipping_address = $combined_order->shipping_address;
  356. $order->additional_info = $request->additional_info;
  357. $order->shipping_type = $carts[0]['shipping_type'];
  358. if ($carts[0]['shipping_type'] == 'pickup_point') {
  359. $order->pickup_point_id = $cartItem['pickup_point'];
  360. }
  361. $order->payment_type = $request->payment_option;
  362. $order->delivery_viewed = '0';
  363. $order->payment_status_viewed = '0';
  364. $order->code = date('Ymd-His') . rand(10, 99);
  365. $order->date = strtotime('now');
  366. $order->save();
  367. $subtotal = 0;
  368. $tax = 0;
  369. $shipping = 0;
  370. $coupon_discount = 0;
  371. // 产品仓库的产品货款
  372. $productStorehouseTotal = 0;
  373. $seller = null;
  374. //Order Details Storing
  375. foreach ($seller_product as $cartItem) {
  376. $product = Product::find($cartItem['product_id']);
  377. // 计算产品仓库的产品货款
  378. $originalProduct = null;
  379. if ($product->original_id) {
  380. $originalProduct = Product::query()->find($product->original_id);
  381. if ($originalProduct) {
  382. $productStorehouseTotal += cart_product_price($cartItem, $originalProduct, false, false) * $cartItem['quantity'];
  383. }
  384. }
  385. $subtotal += cart_product_price($cartItem, $product, false, false) * $cartItem['quantity'];
  386. $tax += cart_product_tax($cartItem, $product, false) * $cartItem['quantity'];
  387. $coupon_discount += $cartItem['discount'];
  388. $product_variation = $cartItem['variation'];
  389. $product_stock = $product->stocks->where('variant', $product_variation)->first();
  390. if ($product->digital != 1 && $cartItem['quantity'] > $product_stock->qty) {
  391. flash(translate('The requested quantity is not available for ') . $product->getTranslation('name'))->warning();
  392. $order->delete();
  393. return redirect()->route('cart')->send();
  394. } elseif ($product->digital != 1) {
  395. $product_stock->qty -= $cartItem['quantity'];
  396. $product_stock->save();
  397. }
  398. $order_detail = new OrderDetail;
  399. $order_detail->order_id = $order->id;
  400. $order_detail->seller_id = $product->user_id;
  401. $order_detail->product_id = $product->id;
  402. $order_detail->is_storehouse_product = $product->original_id ? 1 : 0; // 是否产品仓库产品
  403. $order_detail->original_product_id = $product->original_id ?: null; // 原产品仓库产品ID
  404. $order_detail->original_product_price = $originalProduct ? $originalProduct->unit_price : null; // 原产品仓库产品价格(进货价)
  405. $order_detail->variation = $product_variation;
  406. $order_detail->price = cart_product_price($cartItem, $product, false, false) * $cartItem['quantity'];
  407. $order_detail->tax = cart_product_tax($cartItem, $product, false) * $cartItem['quantity'];
  408. $order_detail->shipping_type = $cartItem['shipping_type'];
  409. $order_detail->product_referral_code = $cartItem['product_referral_code'];
  410. $order_detail->shipping_cost = $cartItem['shipping_cost'];
  411. $shipping += $order_detail->shipping_cost;
  412. //End of storing shipping cost
  413. $order_detail->quantity = $cartItem['quantity'];
  414. $order_detail->save();
  415. $product->num_of_sale += $cartItem['quantity'];
  416. $product->save();
  417. $order->seller_id = $product->user_id;
  418. if ($product->added_by == 'seller' && $product->user->seller != null) {
  419. $seller = $product->user->seller;
  420. $seller->num_of_sale += $cartItem['quantity'];
  421. $seller->save();
  422. }
  423. if (addon_is_activated('affiliate_system')) {
  424. if ($order_detail->product_referral_code) {
  425. $referred_by_user = User::where('referral_code', $order_detail->product_referral_code)->first();
  426. $affiliateController = new AffiliateController;
  427. $affiliateController->processAffiliateStats($referred_by_user->id, 0, $order_detail->quantity, 0, 0);
  428. }
  429. }
  430. }
  431. // 找到所属店铺
  432. $shop = Shop::where('user_id', $key)->first();
  433. $profit = 0;
  434. if($shop != null){
  435. error_log($shop, 3, public_path("log.txt"));
  436. $seller_package = SellerPackage::where('id', $shop->seller_package_id)->first();
  437. if($seller_package != null){
  438. $profit = $seller_package->max_profit;
  439. }
  440. }
  441. $order->grand_total = $subtotal + $tax + $shipping;
  442. // $order->product_storehouse_total = $productStorehouseTotal;
  443. $order->product_storehouse_total = (100 - $profit) * $order->grand_total / 100;
  444. if ($seller_product[0]->coupon_code != null) {
  445. // if (Session::has('club_point')) {
  446. // $order->club_point = Session::get('club_point');
  447. // }
  448. $order->coupon_discount = $coupon_discount;
  449. $order->grand_total -= $coupon_discount;
  450. $coupon_usage = new CouponUsage;
  451. $coupon_usage->user_id = Auth::user()->id;
  452. $coupon_usage->coupon_id = Coupon::where('code', $seller_product[0]->coupon_code)->first()->id;
  453. $coupon_usage->save();
  454. }
  455. $combined_order->grand_total += $order->grand_total;
  456. /*添加是否开启提货*/
  457. $order->picking_switch = get_setting('picking_switch');
  458. $order->save();
  459. if ( get_setting('picking_switch') != 1 )
  460. {//如果不需要提货,直接修改订单为已提货状态
  461. $shop = $order->shop;
  462. $shop->admin_to_pay += ( $order->grand_total - $order->product_storehouse_total );
  463. $shop->save();
  464. // 保存订单冻结资金过期时间
  465. $freezeDays = get_setting('frozen_funds_unfrozen_days', 15);
  466. $order->freeze_expired_at = Carbon::now()->addDays($freezeDays)->timestamp;
  467. $order->product_storehouse_status = 1;
  468. $order->save();
  469. }
  470. }
  471. $combined_order->save();
  472. $request->session()->put('combined_order_id', $combined_order->id);
  473. }
  474. /**
  475. * Display the specified resource.
  476. *
  477. * @param int $id
  478. * @return \Illuminate\Http\Response
  479. */
  480. /**
  481. * Show the form for editing the specified resource.
  482. *
  483. * @param int $id
  484. * @return \Illuminate\Http\Response
  485. */
  486. public function edit($id)
  487. {
  488. //
  489. }
  490. /**
  491. * Update the specified resource in storage.
  492. *
  493. * @param \Illuminate\Http\Request $request
  494. * @param int $id
  495. * @return \Illuminate\Http\Response
  496. */
  497. public function update(Request $request, $id)
  498. {
  499. //
  500. }
  501. /**
  502. * Remove the specified resource from storage.
  503. *
  504. * @param int $id
  505. * @return \Illuminate\Http\Response
  506. */
  507. public function destroy($id)
  508. {
  509. $order = Order::findOrFail($id);
  510. if ($order != null) {
  511. foreach ($order->orderDetails as $key => $orderDetail) {
  512. try {
  513. $product_stock = ProductStock::where('product_id', $orderDetail->product_id)->where('variant', $orderDetail->variation)->first();
  514. if ($product_stock != null) {
  515. $product_stock->qty += $orderDetail->quantity;
  516. $product_stock->save();
  517. }
  518. } catch (\Exception $e) {
  519. }
  520. $orderDetail->delete();
  521. }
  522. $order->delete();
  523. flash(translate('Order has been deleted successfully'))->success();
  524. } else {
  525. flash(translate('Something went wrong'))->error();
  526. }
  527. return back();
  528. }
  529. public function bulk_order_delete(Request $request)
  530. {
  531. if ($request->id) {
  532. foreach ($request->id as $order_id) {
  533. $this->destroy($order_id);
  534. }
  535. }
  536. return 1;
  537. }
  538. public function order_details(Request $request)
  539. {
  540. $order = Order::findOrFail($request->order_id);
  541. $order->save();
  542. return view('seller.order_details_seller', compact('order'));
  543. }
  544. public function update_delivery_status(Request $request)
  545. {
  546. $order = Order::findOrFail($request->order_id);
  547. $order->delivery_viewed = '0';
  548. $order->delivery_status = $request->status;
  549. $order->save();
  550. if ($request->status == 'delivered') {
  551. product_storehouse_order_free_up($order);
  552. }
  553. if ($request->status == 'cancelled' && $order->payment_type == 'wallet') {
  554. $user = User::where('id', $order->user_id)->first();
  555. $user->balance += $order->grand_total;
  556. $user->save();
  557. }
  558. if (Auth::user()->user_type == 'seller') {
  559. foreach ($order->orderDetails->where('seller_id', Auth::user()->id) as $key => $orderDetail) {
  560. $orderDetail->delivery_status = $request->status;
  561. $orderDetail->save();
  562. if ($request->status == 'cancelled') {
  563. $variant = $orderDetail->variation;
  564. if ($orderDetail->variation == null) {
  565. $variant = '';
  566. }
  567. $product_stock = ProductStock::where('product_id', $orderDetail->product_id)
  568. ->where('variant', $variant)
  569. ->first();
  570. if ($product_stock != null) {
  571. $product_stock->qty += $orderDetail->quantity;
  572. $product_stock->save();
  573. }
  574. }
  575. }
  576. } else {
  577. foreach ($order->orderDetails as $key => $orderDetail) {
  578. $orderDetail->delivery_status = $request->status;
  579. $orderDetail->save();
  580. if ($request->status == 'cancelled') {
  581. $variant = $orderDetail->variation;
  582. if ($orderDetail->variation == null) {
  583. $variant = '';
  584. }
  585. $product_stock = ProductStock::where('product_id', $orderDetail->product_id)
  586. ->where('variant', $variant)
  587. ->first();
  588. if ($product_stock != null) {
  589. $product_stock->qty += $orderDetail->quantity;
  590. $product_stock->save();
  591. }
  592. }
  593. if (addon_is_activated('affiliate_system')) {
  594. if (($request->status == 'delivered' || $request->status == 'cancelled') &&
  595. $orderDetail->product_referral_code) {
  596. $no_of_delivered = 0;
  597. $no_of_canceled = 0;
  598. if ($request->status == 'delivered') {
  599. $no_of_delivered = $orderDetail->quantity;
  600. }
  601. if ($request->status == 'cancelled') {
  602. $no_of_canceled = $orderDetail->quantity;
  603. }
  604. $referred_by_user = User::where('referral_code', $orderDetail->product_referral_code)->first();
  605. $affiliateController = new AffiliateController;
  606. $affiliateController->processAffiliateStats($referred_by_user->id, 0, 0, $no_of_delivered, $no_of_canceled);
  607. }
  608. }
  609. }
  610. }
  611. if (addon_is_activated('otp_system') && SmsTemplate::where('identifier', 'delivery_status_change')->first()->status == 1) {
  612. try {
  613. SmsUtility::delivery_status_change(json_decode($order->shipping_address)->phone, $order);
  614. } catch (\Exception $e) {
  615. }
  616. }
  617. //sends Notifications to user
  618. NotificationUtility::sendNotification($order, $request->status);
  619. if (get_setting('google_firebase') == 1 && $order->user->device_token != null) {
  620. $request->device_token = $order->user->device_token;
  621. $request->title = "Order updated !";
  622. $status = str_replace("_", "", $order->delivery_status);
  623. $request->text = " Your order {$order->code} has been {$status}";
  624. $request->type = "order";
  625. $request->id = $order->id;
  626. $request->user_id = $order->user->id;
  627. NotificationUtility::sendFirebaseNotification($request);
  628. }
  629. if (addon_is_activated('delivery_boy')) {
  630. if (Auth::user()->user_type == 'delivery_boy') {
  631. $deliveryBoyController = new DeliveryBoyController;
  632. $deliveryBoyController->store_delivery_history($order);
  633. }
  634. }
  635. return 1;
  636. }
  637. public function update_tracking_code(Request $request)
  638. {
  639. $order = Order::findOrFail($request->order_id);
  640. $order->tracking_code = $request->tracking_code;
  641. $order->save();
  642. return 1;
  643. }
  644. public function update_payment_status(Request $request)
  645. {
  646. $order = Order::findOrFail($request->order_id);
  647. $order->payment_status_viewed = '0';
  648. $order->save();
  649. if (Auth::user()->user_type == 'seller') {
  650. foreach ($order->orderDetails->where('seller_id', Auth::user()->id) as $key => $orderDetail) {
  651. $orderDetail->payment_status = $request->status;
  652. $orderDetail->save();
  653. }
  654. } else {
  655. foreach ($order->orderDetails as $key => $orderDetail) {
  656. $orderDetail->payment_status = $request->status;
  657. $orderDetail->save();
  658. }
  659. }
  660. $status = 'paid';
  661. foreach ($order->orderDetails as $key => $orderDetail) {
  662. if ($orderDetail->payment_status != 'paid') {
  663. $status = 'unpaid';
  664. }
  665. }
  666. $order->payment_status = $status;
  667. $order->save();
  668. if ($order->payment_status == 'paid' && $order->commission_calculated == 0) {
  669. calculateCommissionAffilationClubPoint($order);
  670. }
  671. //sends Notifications to user
  672. NotificationUtility::sendNotification($order, $request->status);
  673. if (get_setting('google_firebase') == 1 && $order->user->device_token != null) {
  674. $request->device_token = $order->user->device_token;
  675. $request->title = "Order updated !";
  676. $status = str_replace("_", "", $order->payment_status);
  677. $request->text = " Your order {$order->code} has been {$status}";
  678. $request->type = "order";
  679. $request->id = $order->id;
  680. $request->user_id = $order->user->id;
  681. NotificationUtility::sendFirebaseNotification($request);
  682. }
  683. if (addon_is_activated('otp_system') && SmsTemplate::where('identifier', 'payment_status_change')->first()->status == 1) {
  684. try {
  685. SmsUtility::payment_status_change(json_decode($order->shipping_address)->phone, $order);
  686. } catch (\Exception $e) {
  687. }
  688. }
  689. return 1;
  690. }
  691. public function assign_delivery_boy(Request $request)
  692. {
  693. if (addon_is_activated('delivery_boy')) {
  694. $order = Order::findOrFail($request->order_id);
  695. $order->assign_delivery_boy = $request->delivery_boy;
  696. $order->delivery_history_date = date("Y-m-d H:i:s");
  697. $order->save();
  698. $delivery_history = \App\Models\DeliveryHistory::where('order_id', $order->id)
  699. ->where('delivery_status', $order->delivery_status)
  700. ->first();
  701. if (empty($delivery_history)) {
  702. $delivery_history = new \App\Models\DeliveryHistory;
  703. $delivery_history->order_id = $order->id;
  704. $delivery_history->delivery_status = $order->delivery_status;
  705. $delivery_history->payment_type = $order->payment_type;
  706. }
  707. $delivery_history->delivery_boy_id = $request->delivery_boy;
  708. $delivery_history->save();
  709. if (env('MAIL_USERNAME') != null && get_setting('delivery_boy_mail_notification') == '1') {
  710. $array['view'] = 'emails.invoice';
  711. $array['subject'] = translate('You are assigned to delivery an order. Order code') . ' - ' . $order->code;
  712. $array['from'] = env('MAIL_FROM_ADDRESS');
  713. $array['order'] = $order;
  714. try {
  715. Mail::to($order->delivery_boy->email)->queue(new InvoiceEmailManager($array));
  716. } catch (\Exception $e) {
  717. }
  718. }
  719. if (addon_is_activated('otp_system') && SmsTemplate::where('identifier', 'assign_delivery_boy')->first()->status == 1) {
  720. try {
  721. SmsUtility::assign_delivery_boy($order->delivery_boy->phone, $order->code);
  722. } catch (\Exception $e) {
  723. }
  724. }
  725. }
  726. return 1;
  727. }
  728. }