Move the initialization of parameters for new and edit job order forms to the service. #270

This commit is contained in:
Korina Cordero 2019-09-30 06:28:13 +00:00
parent 7754ddc594
commit 34dc619392
4 changed files with 220 additions and 9 deletions

View file

@ -207,20 +207,15 @@ class JobOrderController extends Controller
/**
* @Menu(selected="jo_in")
*/
public function incomingForm()
public function incomingForm(JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_in.list', null, 'No access.');
$params['obj'] = new JobOrder();
$params['mode'] = 'create';
$params = $jo_handler->initializeIncomingForm();
$params['submit_url'] = $this->generateUrl('jo_in_submit');
$params['return_url'] = $this->generateUrl('jo_in');
$em = $this->getDoctrine()->getManager();
$this->fillDropdownParameters($params);
$this->fillFormTags($params);
// response
return $this->render('job-order/form.html.twig', $params);
}
@ -228,7 +223,7 @@ class JobOrderController extends Controller
/**
* @Menu(selected="jo_in")
*/
public function openEditForm($id)
public function openEditForm($id, JobOrderHandlerInterface $jo_handler)
{
$this->denyAccessUnlessGranted('jo_open.edit', null, 'No access.');

View file

@ -10,9 +10,11 @@ use Doctrine\ORM\EntityManagerInterface;
use App\Entity\JobOrder;
use App\Entity\Battery;
use App\Entity\BatteryManufacturer;
use App\Entity\JOEvent;
use App\Entity\CustomerVehicle;
use App\Entity\Hub;
use App\Entity\Promo;
use App\Entity\Rider;
use App\Ramcar\InvoiceCriteria;
@ -21,6 +23,10 @@ use App\Ramcar\TradeInType;
use App\Ramcar\JOEventType;
use App\Ramcar\JOStatus;
use App\Ramcar\WarrantyClass;
use App\Ramcar\DiscountApply;
use App\Ramcar\ModeOfPayment;
use App\Ramcar\TransactionOrigin;
use App\Ramcar\FacilitatedType;
use App\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderHandlerInterface;
@ -543,6 +549,105 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface
$mclient->sendRiderEvent($obj, $payload);
}
// initialize incoming job order form
public function initializeIncomingForm()
{
$params['obj'] = new JobOrder();
$params['mode'] = 'create';
$this->fillDropdownParameters($params);
$this->fillFormTags($params);
// return params
return $params;
}
// initialize open edit job order form
public function initializeOpenEditForm($id)
{
$em = $this->em;
$jo = $em->getRepository(JobOrder::class)->find($id);
$params['obj'] = $jo;
$params['mode'] = 'open_edit';
$params['cvid'] = $jo->getCustomerVehicle()->getID();
$params['vid'] = $jo->getCustomerVehicle()->getVehicle()->getID();
$this->fillDropdownParameters($params);
$this->fillFormTags($params);
return $params;
}
protected function fillDropdownParameters(&$params)
{
$em = $this->em;
// db loaded
$params['bmfgs'] = $em->getRepository(BatteryManufacturer::class)->findAll();
$params['promos'] = $em->getRepository(Promo::class)->findAll();
// list of hubs
$hubs = $em->getRepository(Hub::class)->findBy([], ['name' => 'ASC']);
$fac_hubs = [];
foreach ($hubs as $hub)
{
$fac_hubs[$hub->getID()] = $hub->getName() . ' - ' . $hub->getBranch();
}
// name values
$params['service_types'] = ServiceType::getCollection();
$params['warranty_classes'] = WarrantyClass::getCollection();
$params['modes_of_payment'] = ModeOfPayment::getCollection();
$params['statuses'] = JOStatus::getCollection();
$params['discount_apply'] = DiscountApply::getCollection();
$params['trade_in_types'] = TradeInType::getCollection();
$params['facilitated_types'] = FacilitatedType::getCollection();
$params['facilitated_hubs'] = $fac_hubs;
$params['sources'] = TransactionOrigin::getCollection();
}
protected function initFormTags(&$params)
{
// default to editing, as we have more forms editing than creating
$params['ftags'] = [
'title' => 'Job Order Form',
'vehicle_dropdown' => false,
'invoice_edit' => false,
'set_map_coordinate' => true,
'preset_vehicle' => false,
'ticket_table' => true,
'cancel_button' => true,
];
}
protected function fillFormTags(&$params)
{
$this->initFormTags($params);
switch ($params['mode'])
{
case 'create':
$params['ftags']['vehicle_dropdown'] = true;
$params['ftags']['set_map_coordinate'] = false;
$params['ftags']['invoice_edit'] = true;
$params['ftags']['ticket_table'] = false;
$params['ftags']['cancel_button'] = false;
break;
case 'create_vehicle':
$params['ftags']['set_map_coordinate'] = false;
$params['ftags']['invoice_edit'] = true;
$params['ftags']['preset_vehicle'] = true;
$params['ftags']['ticket_table'] = false;
$params['ftags']['cancel_button'] = false;
break;
case 'open_edit':
$params['ftags']['invoice_edit'] = true;
$params['ftags']['preset_vehicle'] = true;
break;
}
}
protected function updateVehicleBattery(JobOrder $jo)
{
// check if new battery

View file

@ -10,9 +10,11 @@ use Doctrine\ORM\EntityManagerInterface;
use App\Entity\JobOrder;
use App\Entity\Battery;
use App\Entity\BatteryManufacturer;
use App\Entity\JOEvent;
use App\Entity\CustomerVehicle;
use App\Entity\Hub;
use App\Entity\Promo;
use App\Entity\Rider;
use App\Ramcar\InvoiceCriteria;
@ -21,6 +23,10 @@ use App\Ramcar\TradeInType;
use App\Ramcar\JOEventType;
use App\Ramcar\JOStatus;
use App\Ramcar\WarrantyClass;
use App\Ramcar\DiscountApply;
use App\Ramcar\ModeOfPayment;
use App\Ramcar\TransactionOrigin;
use App\Ramcar\FacilitatedType;
use App\Service\InvoiceGeneratorInterface;
use App\Service\JobOrderHandlerInterface;
@ -543,6 +549,105 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
$mclient->sendRiderEvent($obj, $payload);
}
// initialize incoming job order form
public function initializeIncomingForm()
{
$params['obj'] = new JobOrder();
$params['mode'] = 'create';
$this->fillDropdownParameters($params);
$this->fillFormTags($params);
// return params
return $params;
}
// initialize open edit job order form
public function initializeOpenEditForm($id)
{
$em = $this->em;
$jo = $em->getRepository(JobOrder::class)->find($id);
$params['obj'] = $jo;
$params['mode'] = 'open_edit';
$params['cvid'] = $jo->getCustomerVehicle()->getID();
$params['vid'] = $jo->getCustomerVehicle()->getVehicle()->getID();
$this->fillDropdownParameters($params);
$this->fillFormTags($params);
return $params;
}
protected function fillDropdownParameters(&$params)
{
$em = $this->em;
// db loaded
$params['bmfgs'] = $em->getRepository(BatteryManufacturer::class)->findAll();
$params['promos'] = $em->getRepository(Promo::class)->findAll();
// list of hubs
$hubs = $em->getRepository(Hub::class)->findBy([], ['name' => 'ASC']);
$fac_hubs = [];
foreach ($hubs as $hub)
{
$fac_hubs[$hub->getID()] = $hub->getName() . ' - ' . $hub->getBranch();
}
// name values
$params['service_types'] = ServiceType::getCollection();
$params['warranty_classes'] = WarrantyClass::getCollection();
$params['modes_of_payment'] = ModeOfPayment::getCollection();
$params['statuses'] = JOStatus::getCollection();
$params['discount_apply'] = DiscountApply::getCollection();
$params['trade_in_types'] = TradeInType::getCollection();
$params['facilitated_types'] = FacilitatedType::getCollection();
$params['facilitated_hubs'] = $fac_hubs;
$params['sources'] = TransactionOrigin::getCollection();
}
protected function initFormTags(&$params)
{
// default to editing, as we have more forms editing than creating
$params['ftags'] = [
'title' => 'Job Order Form',
'vehicle_dropdown' => false,
'invoice_edit' => false,
'set_map_coordinate' => true,
'preset_vehicle' => false,
'ticket_table' => true,
'cancel_button' => true,
];
}
protected function fillFormTags(&$params)
{
$this->initFormTags($params);
switch ($params['mode'])
{
case 'create':
$params['ftags']['vehicle_dropdown'] = true;
$params['ftags']['set_map_coordinate'] = false;
$params['ftags']['invoice_edit'] = true;
$params['ftags']['ticket_table'] = false;
$params['ftags']['cancel_button'] = false;
break;
case 'create_vehicle':
$params['ftags']['set_map_coordinate'] = false;
$params['ftags']['invoice_edit'] = true;
$params['ftags']['preset_vehicle'] = true;
$params['ftags']['ticket_table'] = false;
$params['ftags']['cancel_button'] = false;
break;
case 'open_edit':
$params['ftags']['invoice_edit'] = true;
$params['ftags']['preset_vehicle'] = true;
break;
}
}
protected function updateVehicleBattery(JobOrder $jo)
{
// check if new battery

View file

@ -23,4 +23,10 @@ interface JobOrderHandlerInterface
// cancel job order
public function cancelJobOrder(Request $req, int $id, MQTTClient $mclient);
// initialize incoming job order form
public function initializeIncomingForm();
// initialize open edit job order form
public function initializeOpenEditForm(int $id);
}