Generate JO ID before hub selection on user app API, add additional params to HubCriteria for JORejection and SMS #800

This commit is contained in:
Ramon Gutierrez 2024-05-20 03:46:08 +08:00
parent aae4aaa390
commit 6dfaeee799
4 changed files with 133 additions and 49 deletions

View file

@ -488,6 +488,8 @@ class JobOrderController extends ApiController
JobOrderManager $jo_manager,
PriceTierManager $pt_manager
) {
//error_log("CREATING JOB ORDER WITH PARAMS " . print_r($req->request->all(), true));
// validate params
$validity = $this->validateRequest($req, [
'service_type',
@ -575,6 +577,8 @@ class JobOrderController extends ApiController
$flag_advance_order = true;
// $flag_advance_order = $advance_order ? true : false;
//error_log("RUNNING QUERY NEXT");
$jo = new JobOrder();
$jo->setSource(TransactionOrigin::MOBILE_APP)
->setStatus(JOStatus::PENDING)
@ -643,6 +647,8 @@ class JobOrderController extends ApiController
$icrit->addPromo($promo);
}
//error_log("CONTINUING QUERY BUILDING");
// check customer vehicle
$cv = $this->em->getRepository(CustomerVehicle::class)->find($req->request->get('cv_id'));
if ($cv == null) {
@ -708,6 +714,13 @@ class JobOrderController extends ApiController
$invoice = $ic->generateInvoice($icrit);
$jo->setInvoice($invoice);
//error_log("GENERATED INVOICE");
// save here first so we have a JO ID which is required for the hub selector
$this->em->persist($invoice);
$this->em->persist($jo);
$this->em->flush();
// assign hub and rider
// check if hub is null
if ($hub == null) {
@ -715,6 +728,13 @@ class JobOrderController extends ApiController
$hub_criteria = new HubCriteria();
$hub_criteria->setPoint($jo->getCoordinates());
// set job order info
$hub_criteria->setJobOrderId($jo->getID())
->setJoType($jo->getServiceType())
->setCustomerClass($cust->getCustomerClassification())
->setOrderDate($jo->getDateCreate())
->setServiceType($jo->getServiceType());
// get distance limit for mobile from env
// get value of hub_filter_enable from env
$limit_distance = $_ENV['CUST_DISTANCE_LIMIT'];
@ -845,8 +865,10 @@ class JobOrderController extends ApiController
$hub_dist->incrementJoCountForHub($hub);
}
//error_log("DONE SELECTING HUB");
// save additional hub related changes
$this->em->persist($jo);
$this->em->persist($invoice);
// add event log for JO
$event = new JOEvent();
@ -957,6 +979,8 @@ class JobOrderController extends ApiController
}
}
//error_log("DONE CREATING JOB ORDER " . $jo->getID());
// response
return new ApiResponse(true, '', [
'jo_id' => $jo->getID(),

View file

@ -21,6 +21,9 @@ class HubCriteria
protected $flag_round_robin; // flag if we use round robin or not
protected $jo_id; // JO id. This is null if called from mobile API
protected $customer_id; // customer id
protected $customer_class; // customer class
protected $order_date; // date JO was created
protected $service_type; // service type of JO
public function __construct()
{
@ -33,10 +36,13 @@ class HubCriteria
$this->flag_riders_check = false;
$this->items = [];
$this->payment_method = '';
$flag_emergency = false;
$flag_round_robin = false;
$jo_id = null;
$customer_id = null;
$this->flag_emergency = false;
$this->flag_round_robin = false;
$this->jo_id = null;
$this->customer_id = null;
$this->customer_class = null;
$this->order_date = new DateTime();
$this->service_type = null;
}
public function setPoint(Point $point)
@ -184,5 +190,37 @@ class HubCriteria
return $this->customer_id;
}
public function setCustomerClass($customer_class)
{
$this->customer_class = $customer_class;
return $this;
}
public function getCustomerClass()
{
return $this->customer_class;
}
public function setOrderDate($order_date)
{
$this->order_date = $order_date;
return $this;
}
public function getOrderDate()
{
return $this->order_date;
}
public function setServiceType($service_type)
{
$this->service_type = $service_type;
return $this;
}
public function getServiceType()
{
return $this->service_type;
}
}

View file

@ -60,13 +60,14 @@ class HubSelector
$flag_round_robin = $criteria->isRoundRobin();
$jo_id = $criteria->getJobOrderId();
$customer_id = $criteria->getCustomerId();
$customer_class = $criteria->getCustomerClass();
// needed for JORejection records and SMS notifs
$order_date = $criteria->getOrderDate();
$service_type = $criteria->getServiceType();
$results = [];
// get the job order object and customer class
$jo = $this->em->getRepository(JobOrder::class)->find($jo_id);
$customer_class = $jo->getCustomer()->getCustomerClassification();
// error_log('payment methods ' . $payment_method);
// error_log('distance limit ' . $limit_distance);
// error_log('emergency flag ' . $flag_emergency);
@ -104,13 +105,13 @@ class HubSelector
}
// only enable rider and inventory checks if not VIP
if ($customer_class !== CustomerClassification::VIP) {
if (!empty($customer_class) && $customer_class !== CustomerClassification::VIP) {
// available riders filter
$hubs_riders = $this->filterHubsByRiderAvailability($filtered_hubs, $flag_riders_check, $jo_type, $jo, $customer_id);
$hubs_riders = $this->filterHubsByRiderAvailability($filtered_hubs, $flag_riders_check, $jo_id, $customer_id, $order_date, $service_type);
$filtered_hubs = $hubs_riders;
// inventory filter
$hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $flag_inventory_check, $jo_type, $items, $jo, $customer_id);
$hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $flag_inventory_check, $jo_type, $items, $jo_id, $customer_id, $order_date, $service_type);
$filtered_hubs = $hubs_inventory;
}
@ -284,10 +285,8 @@ class HubSelector
return $results;
}
protected function filterHubsByInventory($hubs, $flag_inventory_check, $jo_type, $items, $jo, $customer_id)
protected function filterHubsByInventory($hubs, $flag_inventory_check, $jo_type, $items, $jo_id, $customer_id, $order_date, $service_type)
{
$jo_id = $jo->getID();
// check if this is enabled
if (!$flag_inventory_check) {
error_log("INVENTORY CHECK " . $jo_id . ": DISABLED");
@ -367,21 +366,26 @@ class HubSelector
// check if we are filtering this hub
if (isset($hubs_to_filter[$branch_code])) {
// create rejection report entry
$robj = $this->createRejectionEntry(
$jo,
$hub_obj,
"SKU(s): " . $battery_string
);
// if we have a JO, create rejection record and notify
if (!empty($jo_id)) {
// create rejection report entry
$robj = $this->createRejectionEntry(
$jo_id,
$hub_obj,
"SKU(s): " . $battery_string
);
// build SMS message
$this->sendSMSMessage(
$hub_obj,
$jo,
$robj,
JORejectionReason::getName(JORejectionReason::NO_STOCK_SALES),
"Requested SKU(s) - " . $battery_string
);
// build SMS message
$this->sendSMSMessage(
$hub_obj,
$jo_id,
$order_date,
$service_type,
$robj,
JORejectionReason::getName(JORejectionReason::NO_STOCK_SALES),
"Requested SKU(s) - " . $battery_string
);
}
// log this filter
$this->hub_filter_logger->logFilteredHub($hub_obj, 'no_inventory', $jo_id, $customer_id);
@ -470,7 +474,7 @@ class HubSelector
*/
}
protected function filterHubsByRiderAvailability($hubs, $flag_riders_check, $jo_type, $jo, $customer_id)
protected function filterHubsByRiderAvailability($hubs, $flag_riders_check, $jo_id, $customer_id, $order_date, $service_type)
{
// check if this is enabled
if (!$flag_riders_check) {
@ -482,7 +486,6 @@ class HubSelector
return $hubs;
}
$jo_id = $jo->getID();
$results = [];
foreach ($hubs as $hub_data) {
@ -490,17 +493,22 @@ class HubSelector
// check we have available riders
if ($hub_obj->getAvailableRiders() === 0) {
// create rejection report entry
$robj = $this->createRejectionEntry($jo, $hub_obj);
// build SMS message
$this->sendSMSMessage(
$hub_obj,
$jo,
$robj,
JORejectionReason::getName(JORejectionReason::NO_RIDER_AVAILABLE),
);
// if we have a JO, create rejection record and notify
if (!empty($jo_id)) {
// create rejection report entry
$robj = $this->createRejectionEntry($jo_id, $hub_obj);
// build SMS message
$this->sendSMSMessage(
$hub_obj,
$jo_id,
$order_date,
$service_type,
$robj,
JORejectionReason::getName(JORejectionReason::NO_RIDER_AVAILABLE),
);
}
// log this filter
$this->hub_filter_logger->logFilteredHub($hub_obj, 'no_available_rider', $jo_id, $customer_id);
@ -616,8 +624,10 @@ class HubSelector
return round(($miles * 1.609344), 1);
}
protected function createRejectionEntry($jo, $hub, $remarks = ""): JORejection
protected function createRejectionEntry($jo_id, $hub, $remarks = ""): JORejection
{
$jo = $this->em->getRepository(JobOrder::class)->find($jo_id);
$robj = new JORejection();
$robj->setDateCreate(new DateTime())
->setHub($hub)
@ -631,15 +641,15 @@ class HubSelector
return $robj;
}
protected function sendSMSMessage($hub, $jo, $rejection, $reason = "", $remarks = ""): void
protected function sendSMSMessage($hub, $jo_id, $order_date, $service_type, $rejection, $reason = "", $remarks = ""): void
{
$message = 'Job Order #: ' . $jo->getID() . "\n" .
'Order Date and Time: ' . $jo->getDateCreate()->format('d M Y g:i A') . "\n" .
$message = 'Job Order #: ' . $jo_id . "\n" .
'Order Date and Time: ' . $order_date->format('d M Y g:i A') . "\n" .
'Date and Time Rejected: ' . $rejection->getDateCreate()->format('d M Y g:i A') . "\n" .
'Enrollee Name: ' . implode(" - ", [$hub->getName(), $hub->getBranch()]) . "\n" .
'Reason of Rejection: ' . $reason . "\n" .
'Remarks: ' . $remarks . "\n" .
'Type of Service: ' . ServiceType::getName($jo->getServiceType());
'Type of Service: ' . ServiceType::getName($service_type);
error_log("SENDING SMS MESsAGE:\r\n" . $message);

View file

@ -2552,7 +2552,13 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
->setLimitResults(50);
// NOTE: set JO type regardless, for now
$hub_criteria->setJoType($obj->getServiceType());
$hub_criteria->setJoType($obj->getServiceType())
->setOrderDate($obj->getDateCreate())
->setServiceType($obj->getServiceType());
// set customer class
$cust = $obj->getCustomer();
$hub_criteria->setCustomerClass($cust->getCustomerClass());
// check if hub filter is enabled. If not, use default values
// for the rest of the HubCriteria fields
@ -2900,7 +2906,13 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
->setLimitResults(50);
// NOTE: set JO type regardless, for now
$hub_criteria->setJoType($obj->getServiceType());
$hub_criteria->setJoType($obj->getServiceType())
->setOrderDate($obj->getDateCreate())
->setServiceType($obj->getServiceType());
// set customer class
$cust = $obj->getCustomer();
$hub_criteria->setCustomerClass($cust->getCustomerClass());
if ($this->hub_geofence->isCovered($long, $lat))
{
@ -2926,7 +2938,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
// get JO and customer id for logging purposes
$jo_id = $obj->getID();
$customer_id = $obj->getCustomer()->getID();
$customer_id = $cust->getID();
$hub_criteria->setJobOrderId($jo_id)
->setCustomerId($customer_id);