DeliveryBoyController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace App\Http\Controllers\Api\V2;
  3. use App\Http\Controllers\OTPVerificationController;
  4. use App\Http\Resources\V2\PurchaseHistoryMiniCollection;
  5. use App\Http\Resources\V2\DeliveryBoyPurchaseHistoryMiniCollection;
  6. use Illuminate\Http\Request;
  7. use App\Http\Resources\V2\DeliveryBoyCollection;
  8. use App\Http\Resources\V2\DeliveryHistoryCollection;
  9. use App\Http\Resources\V2\PurchaseHistoryCollection;
  10. use App\Http\Resources\V2\PurchaseHistoryItemsCollection;
  11. use Auth;
  12. use App\Models\DeliveryBoy;
  13. use App\Models\DeliveryHistory;
  14. use App\Models\Order;
  15. use App\Models\OrderDetail;
  16. use App\Models\User;
  17. use App\Models\SmsTemplate;
  18. use App\Utility\SmsUtility;
  19. class DeliveryBoyController extends Controller
  20. {
  21. /**
  22. * Show the list of assigned delivery by the admin.
  23. *
  24. * @param int $id
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function dashboard_summary($id)
  28. {
  29. $order_query = Order::query();
  30. $order_query->where('assign_delivery_boy', $id);
  31. $delivery_boy = DeliveryBoy::where('user_id', $id)->first();
  32. //dummy
  33. /* return response()->json([
  34. 'completed_delivery' => 123,
  35. 'pending_delivery' => 0,
  36. 'total_collection' => format_price(154126.00),
  37. 'total_earning' => format_price(365.00),
  38. 'cancelled' => 5,
  39. 'on_the_way' => 123,
  40. 'picked' => 24,
  41. 'assigned' => 55,
  42. ]);*/
  43. return response()->json([
  44. 'completed_delivery' => Order::where('assign_delivery_boy', $id)->where('delivery_status', 'delivered')->count(),
  45. 'pending_delivery' => Order::where('assign_delivery_boy', $id)->where('delivery_status', '!=', 'delivered')->where('delivery_status', '!=', 'cancelled')->where('cancel_request', '0')->count(),
  46. 'total_collection' => format_price($delivery_boy->total_collection),
  47. 'total_earning' => format_price($delivery_boy->total_earning),
  48. 'cancelled' => Order::where('assign_delivery_boy', $id)->where('delivery_status', 'cancelled')->count(),
  49. 'on_the_way' => Order::where('assign_delivery_boy', $id)->where('delivery_status', 'on_the_way')->where('cancel_request', '0')->count(),
  50. 'picked' => Order::where('assign_delivery_boy', $id)->where('delivery_status', 'picked_up')->where('cancel_request', '0')->count(),
  51. 'assigned' => Order::where('assign_delivery_boy', $id)->where('delivery_status', 'pending')->where('cancel_request', '0')->count(),
  52. ]);
  53. }
  54. public function assigned_delivery($id)
  55. {
  56. // $order_query = Order::query();
  57. // $order_query->where('delivery_status', 'pending');
  58. // $order_query->where('cancel_request', '0');
  59. $order_query = Order::query();
  60. $order_query->where('assign_delivery_boy', $id);
  61. $order_query->where(function ($order_query) {
  62. $order_query->where('delivery_status', 'pending')
  63. ->where('cancel_request', '0');
  64. })->orWhere(function ($order_query) {
  65. $order_query->where('delivery_status', 'confirmed')
  66. ->where('cancel_request', '0');
  67. });
  68. return new DeliveryBoyPurchaseHistoryMiniCollection($order_query->latest('delivery_history_date')->paginate(10));
  69. // return new DeliveryBoyPurchaseHistoryMiniCollection($order_query->where('assign_delivery_boy', $id)->latest('delivery_history_date')->paginate(10));
  70. }
  71. /**
  72. * Show the list of pickup delivery by the delivery boy.
  73. *
  74. * @param int $id
  75. * @return \Illuminate\Http\Response
  76. */
  77. public function picked_up_delivery($id)
  78. {
  79. $order_query = Order::query();
  80. $order_query->where('delivery_status', 'picked_up');
  81. $order_query->where('cancel_request', '0');
  82. return new DeliveryBoyPurchaseHistoryMiniCollection($order_query->where('assign_delivery_boy', $id)->latest('delivery_history_date')->paginate(10));
  83. }
  84. /**
  85. * Show the list of pickup delivery by the delivery boy.
  86. *
  87. * @param int $id
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function on_the_way_delivery($id)
  91. {
  92. $order_query = Order::query();
  93. $order_query->where('delivery_status', 'on_the_way');
  94. $order_query->where('cancel_request', '0');
  95. return new DeliveryBoyPurchaseHistoryMiniCollection($order_query->where('assign_delivery_boy', $id)->latest('delivery_history_date')->paginate(10));
  96. }
  97. /**
  98. * Show the list of completed delivery by the delivery boy.
  99. *
  100. * @param int $id
  101. * @return \Illuminate\Http\Response
  102. */
  103. public function completed_delivery($id)
  104. {
  105. $order_query = Order::query();
  106. $order_query->where('delivery_status', 'delivered');
  107. //dd(request()->date_range);
  108. if (request()->has('date_range') && request()->date_range != null && request()->date_range != "") {
  109. $max_date = date('Y-m-d H:i:s');
  110. $min_date = date('Y-m-d 00:00:00');
  111. if (request()->date_range == "today") {
  112. $min_date = date('Y-m-d 00:00:00');
  113. } else if (request()->date_range == "this_week") {
  114. //dd("hello");
  115. $min_date = date('Y-m-d 00:00:00', strtotime("-7 days"));
  116. } else if (request()->date_range == "this_month") {
  117. $min_date = date('Y-m-d 00:00:00', strtotime("-30 days"));
  118. }
  119. $order_query->where('delivery_history_date','>=',$min_date)->where('delivery_history_date','<=',$max_date);
  120. }
  121. if (request()->has('payment_type') && request()->payment_type != null && request()->payment_type != "") {
  122. if (request()->payment_type == "cod") {
  123. $order_query->where('payment_type','=','cash_on_delivery');
  124. } else if (request()->payment_type == "non-cod") {
  125. $order_query->where('payment_type','!=','cash_on_delivery');
  126. }
  127. }
  128. return new DeliveryBoyPurchaseHistoryMiniCollection($order_query->where('assign_delivery_boy', $id)->latest('delivery_history_date')->paginate(10));
  129. }
  130. /**
  131. * Show the list of pending delivery by the delivery boy.
  132. *
  133. * @param int $id
  134. * @return \Illuminate\Http\Response
  135. */
  136. public function pending_delivery($id)
  137. {
  138. $order_query = Order::query();
  139. $order_query->where('delivery_status', '!=', 'delivered');
  140. $order_query->where('delivery_status', '!=', 'cancelled');
  141. $order_query->where('cancel_request', '0');
  142. return new DeliveryBoyPurchaseHistoryMiniCollection($order_query->where('assign_delivery_boy', $id)->latest('delivery_history_date')->paginate(10));
  143. }
  144. /**
  145. * Show the list of cancelled delivery by the delivery boy.
  146. *
  147. * @param int $id
  148. * @return \Illuminate\Http\Response
  149. */
  150. public function cancelled_delivery($id)
  151. {
  152. $order_query = Order::query();
  153. $order_query->where('delivery_status', 'cancelled');
  154. if (request()->has('date_range') && request()->date_range != null && request()->date_range != "") {
  155. $max_date = date('Y-m-d H:i:s');
  156. $min_date = date('Y-m-d 00:00:00');
  157. if (request()->date_range == "today") {
  158. $min_date = date('Y-m-d 00:00:00');
  159. } else if (request()->date_range == "this_week") {
  160. //dd("hello");
  161. $min_date = date('Y-m-d 00:00:00', strtotime("-7 days"));
  162. } else if (request()->date_range == "this_month") {
  163. $min_date = date('Y-m-d 00:00:00', strtotime("-30 days"));
  164. }
  165. $order_query->where('delivery_history_date','>=',$min_date)->where('delivery_history_date','<=',$max_date);
  166. }
  167. if (request()->has('payment_type') && request()->payment_type != null && request()->payment_type != "") {
  168. if (request()->payment_type == "cod") {
  169. $order_query->where('payment_type','=','cash_on_delivery');
  170. } else if (request()->payment_type == "non-cod") {
  171. $order_query->where('payment_type','!=','cash_on_delivery');
  172. }
  173. }
  174. return new PurchaseHistoryMiniCollection($order_query->where('assign_delivery_boy', $id)->latest()->paginate(10));
  175. }
  176. /**
  177. * Show the list of today's collection by the delivery boy.
  178. *
  179. * @param int $id
  180. * @return \Illuminate\Http\Response
  181. */
  182. public function collection($id)
  183. {
  184. $collection_query = DeliveryHistory::query();
  185. $collection_query->where('delivery_status', 'delivered');
  186. $collection_query->where('payment_type', 'cash_on_delivery');
  187. return new DeliveryHistoryCollection($collection_query->where('delivery_boy_id', $id)->latest()->paginate(10));
  188. }
  189. public function earning($id)
  190. {
  191. $collection_query = DeliveryHistory::query();
  192. $collection_query->where('delivery_status', 'delivered');
  193. return new DeliveryHistoryCollection($collection_query->where('delivery_boy_id', $id)->latest()->paginate(10));
  194. }
  195. public function collection_summary($id)
  196. {
  197. $collection_query = DeliveryHistory::query();
  198. $collection_query->where('delivery_status', 'delivered');
  199. $collection_query->where('payment_type', 'cash_on_delivery');
  200. $today_date = date('Y-m-d');
  201. $yesterday_date = date('Y-m-d', strtotime("-1 day"));
  202. $today_date_formatted = date('d M, Y');
  203. $yesterday_date_formatted = date('d M,Y', strtotime("-1 day"));
  204. $today_collection = DeliveryHistory::where('delivery_status', 'delivered')
  205. ->where('payment_type', 'cash_on_delivery')
  206. ->where('delivery_boy_id', $id)
  207. ->where('created_at','like',"%$today_date%")
  208. ->sum('collection');
  209. $yesterday_collection = DeliveryHistory::where('delivery_status', 'delivered')
  210. ->where('payment_type', 'cash_on_delivery')
  211. ->where('delivery_boy_id', $id)
  212. ->where('created_at','like',"%$yesterday_date%")
  213. ->sum('collection');
  214. return response()->json([
  215. 'today_date' => $today_date_formatted,
  216. 'today_collection' => format_price($today_collection) ,
  217. 'yesterday_date' => $yesterday_date_formatted,
  218. 'yesterday_collection' => format_price($yesterday_collection) ,
  219. ]);
  220. }
  221. public function earning_summary($id)
  222. {
  223. $collection_query = DeliveryHistory::query();
  224. $collection_query->where('delivery_status', 'delivered');
  225. // $collection_query->where('payment_type', 'cash_on_delivery');
  226. $today_date = date('Y-m-d');
  227. $yesterday_date = date('Y-m-d', strtotime("-1 day"));
  228. $today_date_formatted = date('d M, Y');
  229. $yesterday_date_formatted = date('d M,Y', strtotime("-1 day"));
  230. $today_collection = DeliveryHistory::where('delivery_status', 'delivered')
  231. ->where('delivery_boy_id', $id)
  232. ->where('created_at','like',"%$today_date%")
  233. ->sum('earning');
  234. $yesterday_collection = DeliveryHistory::where('delivery_status', 'delivered')
  235. ->where('delivery_boy_id', $id)
  236. ->where('created_at','like',"%$yesterday_date%")
  237. ->sum('earning');
  238. return response()->json([
  239. 'today_date' => $today_date_formatted,
  240. 'today_earning' => format_price($today_collection) ,
  241. 'yesterday_date' => $yesterday_date_formatted,
  242. 'yesterday_earning' => format_price($yesterday_collection) ,
  243. ]);
  244. }
  245. /**
  246. * For only delivery boy while changing delivery status.
  247. * Call from order controller
  248. *
  249. * @param int $id
  250. * @return \Illuminate\Http\Response
  251. */
  252. public function change_delivery_status(Request $request) {
  253. $order = Order::find($request->order_id);
  254. $order->delivery_viewed = '0';
  255. $order->delivery_status = $request->status;
  256. $order->save();
  257. $delivery_history = new DeliveryHistory;
  258. $delivery_history->order_id = $order->id;
  259. $delivery_history->delivery_boy_id = $request->delivery_boy_id;
  260. $delivery_history->delivery_status = $order->delivery_status;
  261. $delivery_history->payment_type = $order->payment_type;
  262. if($order->delivery_status == 'delivered') {
  263. foreach ($order->orderDetails as $key => $orderDetail) {
  264. if (addon_is_activated('affiliate_system')) {
  265. if ($orderDetail->product_referral_code) {
  266. $no_of_delivered = 0;
  267. $no_of_canceled = 0;
  268. if($request->status == 'delivered') {
  269. $no_of_delivered = $orderDetail->quantity;
  270. }
  271. if($request->status == 'cancelled') {
  272. $no_of_canceled = $orderDetail->quantity;
  273. }
  274. $referred_by_user = User::where('referral_code', $orderDetail->product_referral_code)->first();
  275. $affiliateController = new AffiliateController;
  276. $affiliateController->processAffiliateStats($referred_by_user->id, 0, 0, $no_of_delivered, $no_of_canceled);
  277. }
  278. }
  279. }
  280. $delivery_boy = DeliveryBoy::where('user_id', $request->delivery_boy_id)->first();
  281. if (get_setting('delivery_boy_payment_type') == 'commission') {
  282. $delivery_history->earning = get_setting('delivery_boy_commission');
  283. $delivery_boy->total_earning += get_setting('delivery_boy_commission');
  284. }
  285. if ($order->payment_type == 'cash_on_delivery') {
  286. $delivery_history->collection = $order->grand_total;
  287. $delivery_boy->total_collection += $order->grand_total;
  288. $order->payment_status = 'paid';
  289. if ($order->commission_calculated == 0) {
  290. calculateCommissionAffilationClubPoint($order);
  291. $order->commission_calculated = 1;
  292. }
  293. }
  294. $delivery_boy->save();
  295. }
  296. $order->delivery_history_date = date("Y-m-d H:i:s");
  297. $order->save();
  298. $delivery_history->save();
  299. if (addon_is_activated('otp_system') && SmsTemplate::where('identifier','delivery_status_change')->first()->status == 1){
  300. try {
  301. SmsUtility::delivery_status_change($order->user->phone, $order);
  302. } catch (\Exception $e) {
  303. }
  304. }
  305. return response()->json([
  306. 'result' => true,
  307. 'message' => translate('Delivery status changed to ').ucwords(str_replace('_',' ',$request->status))
  308. ]);
  309. }
  310. public function cancel_request($id)
  311. {
  312. $order = Order::find($id);
  313. $order->cancel_request = 1;
  314. $order->cancel_request_at = date('Y-m-d H:i:s');
  315. $order->save();
  316. return response()->json([
  317. 'result' => true,
  318. 'message' => translate('Requested for cancellation')
  319. ]);
  320. }
  321. public function details($id)
  322. {
  323. $order_detail = Order::where('id', $id)->where('assign_delivery_boy', auth()->user()->id)->get();
  324. // $order_query = auth()->user()->orders->where('id', $id);
  325. // return new PurchaseHistoryCollection($order_query->get());
  326. return new PurchaseHistoryCollection($order_detail);
  327. }
  328. public function items($id)
  329. {
  330. $order_id = Order::select('id')->where('id', $id)->where('assign_delivery_boy', auth()->user()->id)->first();
  331. $order_query = OrderDetail::where('order_id', $order_id->id);
  332. return new PurchaseHistoryItemsCollection($order_query->get());
  333. }
  334. }