Add auto assign to controller. #374

This commit is contained in:
Korina Cordero 2020-04-06 09:46:54 +00:00
parent d77574bc13
commit 9a661763a0
6 changed files with 1959 additions and 3 deletions

View file

@ -264,6 +264,8 @@ access_keys:
label: Walk-in
- id: jo_walkin.edit
label: Walk-in Edit
- id: jo_autoassign.test
label: Autoassign Test
- id: support
label: Customer Support Access

View file

@ -122,6 +122,10 @@ main_menu:
acl: jo_all.list
label: View All
parent: joborder
- id: jo_autoassign
acl: jo_autoassign.test
label: Autoassign Test
parent: joborder
- id: support
acl: support.menu

View file

@ -226,3 +226,12 @@ jo_walkin_edit_submit:
controller: App\Controller\JobOrderController::walkInEditSubmit
methods: [POST]
jo_autoassign:
path: /job-order/autoassign
controller: App\Controller\JobOrderController::autoAssignForm
methods: [GET]
jo_autoassign_test_submit:
path: /job-order/autoassign
controller: App\Controller\JobOrderController::autoAssignSubmit
methods: [POST]

View file

@ -5,6 +5,7 @@ namespace App\Controller;
use App\Ramcar\JOStatus;
use App\Ramcar\InvoiceCriteria;
use App\Ramcar\CMBServiceType;
use App\Ramcar\ServiceType;
use App\Entity\CustomerVehicle;
use App\Entity\Promo;
@ -96,7 +97,9 @@ class JobOrderController extends Controller
$this->denyAccessUnlessGranted('jo_open.edit', null, 'No access.');
$error_array = [];
$error_array = $jo_handler->generateJobOrder($req, $id);
$result = $jo_handler->generateJobOrder($req, $id);
$error_array = $result['error_array'];
// check if any errors were found
if (!empty($error_array)) {
@ -145,7 +148,9 @@ class JobOrderController extends Controller
// initialize error list
$error_array = [];
$id = -1;
$error_array = $jo_handler->generateJobOrder($req, $id);
$result = $jo_handler->generateJobOrder($req, $id);
$error_array = $result['error_array'];
// check if any errors were found
if (!empty($error_array)) {
@ -1053,5 +1058,160 @@ class JobOrderController extends Controller
]);
}
/**
* @Menu(selected="jo_autoassign")
*/
public function autoAssignForm(GISManagerInterface $gis, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_autoassign.test', null, 'No access.');
$params = $jo_handler->initializeIncomingForm();
$params['submit_url'] = $this->generateUrl('jo_autoassign_test_submit');
$params['return_url'] = $this->generateUrl('jo_autoassign');
$params['map_js_file'] = $gis->getJSJOFile();
$template = 'job-order/autoassign.form.html.twig';
// response
return $this->render($template, $params);
}
public function autoAssignSubmit(Request $req, JobOrderHandlerInterface $jo_handler,
EntityManagerInterface $em, MapTools $map_tools)
{
$this->denyAccessUnlessGranted('jo_autoassign.test', null, 'No access.');
// initialize error list
$error_array = [];
$id = -1;
$result = $jo_handler->generateJobOrder($req, $id);
$error_array = $result['error_array'];
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// get job order
$jo = $result['job_order'];
if (($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) ||
($jo->getServicetype() == ServiceType::BATTERY_REPLACEMENT_WARRANTY))
$this->autoAssignHubAndRider($jo, $em, $map_tools);
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
protected function autoAssignHubAndRider($jo, EntityManagerInterface $em, MapTools $map_tools)
{
// get the nearest 10 hubs
// TODO: move this snippet to a function
$hubs = $map_tools->getClosestHubs($jo->getCoordinates(), 10, date("H:i:s"));
$nearest_hubs = [];
$nearest_hubs_with_distance = [];
foreach ($hubs as $hub)
{
$nearest_hubs_with_distance[] = $hub;
$nearest_hubs[] = $hub['hub'];
}
// get battery sku
$invoice = $jo->getInvoice();
if ($invoice != null)
{
$items = $invoice->getItems();
$sku = '';
foreach ($items as $item)
{
$sku = $item->getBattery()->getSAPCode();
}
// api call to check inventory
// pass the list of nearest hubs and the sku
// go through returned list of hubs for available riders
// for now, use all hubs
$hubs_with_inventory = $em->getRepository(Hub::class)->findAll();
$nearest = [];
foreach ($hubs_with_inventory as $hub_with_inventory)
{
// check rider availability
if (count($hub_with_inventory->getAvailableRiders()) > 0)
{
// check against nearest hubs with distance
foreach ($nearest_hubs_with_distance as $nhd)
{
// get distance of hub from location, compare with $nearest. if less, replace nearest
if ($hub_with_inventory->getID() == $nhd['hub']->getID())
{
if (empty($nearest))
{
$nearest = $nhd;
}
else
{
if ($nhd['distance'] < $nearest['distance'])
$nearest = $nhd;
}
}
}
}
}
// return $nearest
error_log('Nearest Hub ' . $nearest['hub']->getID());
error_log('With distance ' . $nearest['distance']);
$jo->setHub($nearest['hub']);
$hub_riders = $nearest['hub']->getAvailableRiders();
$rider = null;
if (count($hub_riders) > 1)
{
// this will no longer be necessary when the contents
// of randomizeRider changes
$available_riders = [];
foreach ($hub_riders as $rider)
{
$available_riders[] = $rider;
}
$rider = $this->randomizeRider($available_riders);
}
else
$rider = $hub_riders[0];
$jo->setRider($rider);
$jo->setStatus(JOStatus::ASSIGNED);
$em->persist($jo);
$em->flush();
}
}
protected function randomizeRider($riders)
{
// TODO: get redis to track the sales per rider per day
// check the time they came in
// for now, randomize the rider
$selected_index = array_rand($riders);
$selected_rider = $riders[$selected_index];
return $selected_rider;
}
}

View file

@ -405,7 +405,14 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
}
}
return $error_array;
$data['error_array'] = $error_array;
if ($jo != null)
{
$data['job_order'] = $jo;
}
return $data;
}
// dispatch job order

File diff suppressed because it is too large Load diff