Guest User

Untitled

a guest
Feb 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. try {
  2. $operationLogCount = OperationLog::where('action', self::YEMEK_SEPETI_DISPATCH_TYPE)
  3. ->where('type', self::YEMEK_SEPETI_DISPATCH_TYPE)
  4. ->where('status', 1)
  5. ->where('order_id', $order->id)
  6. ->count();
  7.  
  8. if (count($radiusList) > $operationLogCount) {
  9. $radius = $radiusList[$operationLogCount];
  10. $order->radius = $radius;
  11.  
  12. // Automatic assign
  13. $latitude = $order->senderData->latitude;
  14. $longitude = $order->senderData->longitude;
  15.  
  16. $locationBasedCouriers = User::select(
  17. 'id',
  18. 'name',
  19. 'latitude',
  20. 'longitude',
  21. 'device_token',
  22. 'endpoint',
  23. 'device_type',
  24. 'is_developer',
  25. 'is_online',
  26. 'is_mp',
  27. 'user_type',
  28. 'courier_type',
  29. 'district_id',
  30. DB::raw('(3959 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance')
  31. )
  32. ->where('users.company_id', '?')
  33. ->where('users.user_type', '?')
  34. ->where('users.courier_type', '?')
  35. ->where('users.dispatch_type', '?')
  36. ->where('users.device_token', '<>', '?')
  37. ->where('users.is_blocked', '?')
  38. ->where('users.is_online', '?')
  39. ->having('distance', '<', '?')
  40. ->orderBy('distance')
  41. ->setBindings([$latitude, $longitude, $latitude], 'select')
  42. ->setBindings([$ulakCompany->id, '2', '4', Config::get('enums.dispatch_type')['AUTOMATIC'], '', 0, 1], 'where')
  43. ->setBindings([$radius], 'having')
  44. ->get();
  45.  
  46. if (!$locationBasedCouriers->isEmpty()) {
  47.  
  48. /** @var User $courier */
  49. foreach ($locationBasedCouriers as $courier) {
  50. if (strpos(strval($order->district_code), strval($courier->district_id)) !== false) {
  51. /** @var bool $isNotificationSendable */
  52. $notificationCanBeSent = true;
  53.  
  54. // Write conditions for sending notifications
  55. if ((int)$order->payment_method_id === 4) {
  56. // Do not send notification to the courier without pos device when credit card payment is required
  57. if (PosDevice::where('user_id', '=', $courier->id)
  58. ->where('app_id', '=', $order->app_id)->count() === 0) {
  59. $notificationCanBeSent = false;
  60. }
  61. }
  62.  
  63. if ($notificationCanBeSent) {
  64. $orderRejectedCount = OrderCheckpoint::where('courier_id', $courier->id)
  65. ->where('order_id', $order->id)
  66. ->whereIn('tag', ['reject_order', 'accept'])
  67. ->count();
  68.  
  69. if ($orderRejectedCount === 0) {
  70. $orderCount = Order::where('status', 2)->where('courier_id', $courier->id)->count();
  71. $maxPackageCount = $courier->max_package_count != null ? $courier->max_package_count : User::DEFAULT_MAX_PACKAGE_COUNT_IN_TRANSIT_ON_COURIER;
  72. if ($orderCount < $maxPackageCount) {
  73. $notificationData = [
  74. 'user' => $courier,
  75. 'order' => $order
  76. ];
  77. $this->sendNotification($notificationData);
  78. $order->dispatch_type = Config::get('enums.dispatch_type')['AUTOMATIC'];
  79.  
  80. if ($order->app_id == 3) { // Yemek Sepeti
  81. $job = (new YemeksepetiCourierAssign($courier->id, $order->id));
  82. $this->dispatch($job);
  83. }
  84. }
  85. }
  86. }
  87.  
  88. }
  89. }
  90. }
  91. } else {
  92. // no courier found by automatic assign
  93. $order->dispatch_type = -1;
  94. }
  95. } catch (\Exception $exception) {
  96. \Log::alert(['title' => 'Unable to assign automatically. Order id: '. $order->id, 'message' => $exception->getMessage()]);
  97. }
Add Comment
Please, Sign In to add comment