Skip to content

Instantly share code, notes, and snippets.

@ferdiunal
Created February 15, 2019 19:22
Show Gist options
  • Save ferdiunal/18ead2c100707f370999bfce4acb6600 to your computer and use it in GitHub Desktop.
Save ferdiunal/18ead2c100707f370999bfce4acb6600 to your computer and use it in GitHub Desktop.
try {
$operationLogCount = OperationLog::where('action', self::YEMEK_SEPETI_DISPATCH_TYPE)
->where('type', self::YEMEK_SEPETI_DISPATCH_TYPE)
->where('status', 1)
->where('order_id', $order->id)
->count();
if (count($radiusList) > $operationLogCount) {
$radius = $radiusList[$operationLogCount];
$order->radius = $radius;
// Automatic assign
$latitude = $order->senderData->latitude;
$longitude = $order->senderData->longitude;
$locationBasedCouriers = User::select(
'id',
'name',
'latitude',
'longitude',
'device_token',
'endpoint',
'device_type',
'is_developer',
'is_online',
'is_mp',
'user_type',
'courier_type',
'district_id',
DB::raw('(3959 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance')
)
->where('users.company_id', '?')
->where('users.user_type', '?')
->where('users.courier_type', '?')
->where('users.dispatch_type', '?')
->where('users.device_token', '<>', '?')
->where('users.is_blocked', '?')
->where('users.is_online', '?')
->having('distance', '<', '?')
->orderBy('distance')
->setBindings([$latitude, $longitude, $latitude], 'select')
->setBindings([$ulakCompany->id, '2', '4', Config::get('enums.dispatch_type')['AUTOMATIC'], '', 0, 1], 'where')
->setBindings([$radius], 'having')
->get();
if (!$locationBasedCouriers->isEmpty()) {
/** @var User $courier */
foreach ($locationBasedCouriers as $courier) {
if (strpos(strval($order->district_code), strval($courier->district_id)) !== false) {
/** @var bool $isNotificationSendable */
$notificationCanBeSent = true;
// Write conditions for sending notifications
if ((int)$order->payment_method_id === 4) {
// Do not send notification to the courier without pos device when credit card payment is required
if (PosDevice::where('user_id', '=', $courier->id)
->where('app_id', '=', $order->app_id)->count() === 0) {
$notificationCanBeSent = false;
}
}
if ($notificationCanBeSent) {
$orderRejectedCount = OrderCheckpoint::where('courier_id', $courier->id)
->where('order_id', $order->id)
->whereIn('tag', ['reject_order', 'accept'])
->count();
if ($orderRejectedCount === 0) {
$orderCount = Order::where('status', 2)->where('courier_id', $courier->id)->count();
$maxPackageCount = $courier->max_package_count != null ? $courier->max_package_count : User::DEFAULT_MAX_PACKAGE_COUNT_IN_TRANSIT_ON_COURIER;
if ($orderCount < $maxPackageCount) {
$notificationData = [
'user' => $courier,
'order' => $order
];
$this->sendNotification($notificationData);
$order->dispatch_type = Config::get('enums.dispatch_type')['AUTOMATIC'];
if ($order->app_id == 3) { // Yemek Sepeti
$job = (new YemeksepetiCourierAssign($courier->id, $order->id));
$this->dispatch($job);
}
}
}
}
}
}
}
} else {
// no courier found by automatic assign
$order->dispatch_type = -1;
}
} catch (\Exception $exception) {
\Log::alert(['title' => 'Unable to assign automatically. Order id: '. $order->id, 'message' => $exception->getMessage()]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment