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