Update SMS message format for rejected hubs, add JO rejection entries for no inventory or no riders #800

This commit is contained in:
Ramon Gutierrez 2024-05-17 21:40:16 +08:00
parent 21c97df677
commit 4be9134090

View file

@ -4,11 +4,13 @@ namespace App\Service;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Proxies\__CG__\App\Entity\JORejection;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point; use CrEOF\Spatial\PHP\Types\Geometry\Point;
use App\Entity\Hub; use App\Entity\Hub;
use App\Entity\JobOrder;
use App\Service\HubDistributor; use App\Service\HubDistributor;
use App\Service\InventoryManager; use App\Service\InventoryManager;
@ -17,6 +19,9 @@ use App\Service\RisingTideGateway;
use App\Ramcar\HubCriteria; use App\Ramcar\HubCriteria;
use App\Ramcar\ServiceType; use App\Ramcar\ServiceType;
use App\Ramcar\JOEventType;
use App\Ramcar\JORejectionReason;
use DateTime;
class HubSelector class HubSelector
{ {
@ -57,6 +62,9 @@ class HubSelector
$results = []; $results = [];
// get the job order object
$jo = $this->em->getRepository(JobOrder::class)->find($jo_id);
// error_log('payment methods ' . $payment_method); // error_log('payment methods ' . $payment_method);
// error_log('distance limit ' . $limit_distance); // error_log('distance limit ' . $limit_distance);
// error_log('emergency flag ' . $flag_emergency); // error_log('emergency flag ' . $flag_emergency);
@ -91,11 +99,11 @@ class HubSelector
// error_log('round robin hubs ' . json_encode($filtered_hubs)); // error_log('round robin hubs ' . json_encode($filtered_hubs));
// available riders filter // available riders filter
$hubs_riders = $this->filterHubsByRiderAvailability($filtered_hubs, $flag_riders_check, $jo_type, $jo_id, $customer_id); $hubs_riders = $this->filterHubsByRiderAvailability($filtered_hubs, $flag_riders_check, $jo_type, $jo, $customer_id);
$filtered_hubs = $hubs_riders; $filtered_hubs = $hubs_riders;
// inventory filter // inventory filter
$hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $flag_inventory_check, $jo_type, $items, $jo_id, $customer_id); $hubs_inventory = $this->filterHubsByInventory($filtered_hubs, $flag_inventory_check, $jo_type, $items, $jo, $customer_id);
$filtered_hubs = $hubs_inventory; $filtered_hubs = $hubs_inventory;
// round robin filter // round robin filter
@ -267,8 +275,10 @@ class HubSelector
return $results; return $results;
} }
protected function filterHubsByInventory($hubs, $flag_inventory_check, $jo_type, $items, $jo_id, $customer_id) protected function filterHubsByInventory($hubs, $flag_inventory_check, $jo_type, $items, $jo, $customer_id)
{ {
$jo_id = $jo->getID();
// check if this is enabled // check if this is enabled
if (!$flag_inventory_check) { if (!$flag_inventory_check) {
error_log("INVENTORY CHECK " . $jo_id . ": DISABLED"); error_log("INVENTORY CHECK " . $jo_id . ": DISABLED");
@ -340,11 +350,26 @@ class HubSelector
// check if we are filtering this hub // check if we are filtering this hub
if (isset($hubs_to_filter[$branch_code])) { if (isset($hubs_to_filter[$branch_code])) {
// send SMS to hub // create rejection report entry
$this->sendSMSMessage($hub_obj, $items); $robj = $this->createRejectionEntry(
$jo,
$hub_obj,
"SKU(s): " . implode(", ", $skus)
);
// build SMS message
$this->sendSMSMessage(
$hub_obj,
$jo,
$robj,
JORejectionReason::getName(JORejectionReason::NO_STOCK_SALES),
"Requested SKU(s) - " . implode(", ", $skus)
);
// log this filter // log this filter
$this->hub_filter_logger->logFilteredHub($hub_obj, 'no_inventory', $jo_id, $customer_id); $this->hub_filter_logger->logFilteredHub($hub_obj, 'no_inventory', $jo_id, $customer_id);
error_log("FILTERED HUB " . $hub_obj->getID() . " (no_inventory)");
} else if (!empty($branch_code) && isset($qtys[$branch_code])) { } else if (!empty($branch_code) && isset($qtys[$branch_code])) {
// include inventory in hub data // include inventory in hub data
$hub_data['inventory'] = $qtys[$branch_code]; $hub_data['inventory'] = $qtys[$branch_code];
@ -428,7 +453,7 @@ class HubSelector
*/ */
} }
protected function filterHubsByRiderAvailability($hubs, $flag_riders_check, $jo_type, $jo_id, $customer_id) protected function filterHubsByRiderAvailability($hubs, $flag_riders_check, $jo_type, $jo, $customer_id)
{ {
// check if this is enabled // check if this is enabled
if (!$flag_riders_check) { if (!$flag_riders_check) {
@ -440,6 +465,7 @@ class HubSelector
return $hubs; return $hubs;
} }
$jo_id = $jo->getID();
$results = []; $results = [];
foreach ($hubs as $hub_data) { foreach ($hubs as $hub_data) {
@ -447,11 +473,15 @@ class HubSelector
// check we have available riders // check we have available riders
if ($hub_obj->getAvailableRiders() === 0) { if ($hub_obj->getAvailableRiders() === 0) {
// send SMS to hub // create rejection report entry
$this->rt->sendSMS( $robj = $this->createRejectionEntry($jo, $hub_obj);
$hub_obj->getNotifNumber(),
$this->trans->trans('message.battery_brand_allcaps'), // build SMS message
$this->trans->trans('message.no_riders_message') $this->sendSMSMessage(
$hub_obj,
$jo,
$robj,
JORejectionReason::getName(JORejectionReason::NO_RIDER_AVAILABLE),
); );
// log this filter // log this filter
@ -569,26 +599,37 @@ class HubSelector
return round(($miles * 1.609344), 1); return round(($miles * 1.609344), 1);
} }
protected function sendSMSMessage($hub, $items) protected function createRejectionEntry($jo, $hub, $remarks = ""): JORejection
{ {
// compose message $robj = new JORejection();
// get the skus for the message $robj->setDateCreate(new DateTime())
$sku_text = ''; ->setHub($hub)
foreach ($items as $key => $value) ->setJobOrder($jo)
{ ->setReason(JORejectionReason::NO_STOCK_SALES)
$sku_text .= ' ' . $key; ->setRemarks(implode(" ", ["Automatically filtered by hub selector.", $remarks]));
}
$message = str_replace('item_display', trim($sku_text), $this->trans->trans('no_inventory_message'));
// get hub notification number $this->em->persist($robj);
$mobile_number = $hub->getNotifNumber(); $this->em->flush();
if (!empty($mobile_number)) return $robj;
{
// send SMS message
// error_log('sending sms to - ' . $mobile_number);
$this->rt->sendSMS($mobile_number, $this->trans->trans('message.battery_brand_allcaps'), $message);
} }
protected function sendSMSMessage($hub, $jo, $rejection, $reason = "", $remarks = ""): void
{
$message = 'Job Order #: ' . $jo->getID() . "\n" .
'Order Date and Time: ' . $jo->getDateCreate()->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());
// send SMS message to hub
$this->rt->sendSMS(
$hub->getNotifNumber(),
$this->trans->trans('message.battery_brand_allcaps'),
$message
);
} }
} }