From 3a33a16cc57f1c97af392956ffc97e304f863150 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 12 Nov 2019 06:41:16 +0000 Subject: [PATCH] Move the loading of customer list page to the customer service. #270 --- src/Controller/CustomerController.php | 129 +----------------- .../CustomerHandler/CMBCustomerHandler.php | 10 +- .../CustomerHandler/ResqCustomerHandler.php | 9 ++ src/Service/CustomerHandlerInterface.php | 3 + 4 files changed, 27 insertions(+), 124 deletions(-) diff --git a/src/Controller/CustomerController.php b/src/Controller/CustomerController.php index d485a611..379dd146 100644 --- a/src/Controller/CustomerController.php +++ b/src/Controller/CustomerController.php @@ -34,11 +34,15 @@ class CustomerController extends Controller /** * @Menu(selected="customer_list") */ - public function index() + public function index(CustomerHandlerInterface $cust_handler) { $this->denyAccessUnlessGranted('customer.list', null, 'No access.'); - return $this->render('customer/list.html.twig'); + $params = $cust_handler->initializeCustomerIndexForm(); + + $template = $params['template']; + + return $this->render($template); } public function rows(Request $req) @@ -143,21 +147,6 @@ class CustomerController extends Controller ]); } - protected function fillDropdownParameters(&$params) - { - $em = $this->getDoctrine()->getManager(); - - $params['bmfgs'] = $em->getRepository(BatteryManufacturer::class)->findAll(); - $params['vmfgs'] = $em->getRepository(VehicleManufacturer::class)->findAll(); - - $params['classifications'] = CustomerClassification::getCollection(); - $params['fuel_types'] = FuelType::getCollection(); - $params['status_conditions'] = VehicleStatusCondition::getCollection(); - - $params['years'] = $this->generateYearOptions(); - $params['batteries'] = $em->getRepository(Battery::class)->findAll(); - } - /** * @Menu(selected="customer_list") */ @@ -173,26 +162,6 @@ class CustomerController extends Controller return $this->render($template, $params); } - protected function setObject($obj, $req) - { - // set and save values - $obj->setTitle($req->request->get('title')) - ->setFirstName($req->request->get('first_name')) - ->setLastName($req->request->get('last_name')) - ->setCustomerClassification($req->request->get('customer_classification')) - ->setCustomerNotes($req->request->get('customer_notes')) - ->setEmail($req->request->get('email')) - ->setIsCSAT($req->request->get('flag_csat') ? true : false) - ->setActive($req->request->get('flag_active') ? true : false); - - // phone numbers - $obj->setPhoneMobile($req->request->get('phone_mobile')) - ->setPhoneLandline($req->request->get('phone_landline')) - ->setPhoneOffice($req->request->get('phone_office')) - ->setPhoneFax($req->request->get('phone_fax')); - } - - public function addSubmit(Request $req, CustomerHandlerInterface $cust_handler) { $this->denyAccessUnlessGranted('customer.add', null, 'No access.'); @@ -240,86 +209,6 @@ class CustomerController extends Controller return $this->render($template, $params); } - protected function updateVehicles($em, Customer $cust, $vehicles) - { - $vehicle_ids = []; - - foreach ($vehicles as $vehicle) - { - // check if customer vehicle exists - if (!empty($vehicle->id)) - { - $cust_vehicle = $em->getRepository(CustomerVehicle::class)->find($vehicle->id); - if ($cust_vehicle == null) - throw new CrudException("Could not find customer vehicle."); - - } - // this is a new vehicle - else - { - $cust_vehicle = new CustomerVehicle(); - $cust_vehicle->setCustomer($cust); - $cust->addVehicle($cust_vehicle); - $em->persist($cust_vehicle); - } - - // vehicle, because they could have changed vehicle type - $vobj = $em->getRepository(Vehicle::class)->find($vehicle->vehicle); - if ($vobj == null) - throw new CrudException("Could not find vehicle."); - - // TODO: validate details - - $cust_vehicle->setName($vehicle->name) - ->setVehicle($vobj) - ->setPlateNumber($vehicle->plate_number) - ->setModelYear($vehicle->model_year) - ->setColor('') - ->setStatusCondition('') - ->setFuelType('') - ->setActive($vehicle->flag_active); - - // if specified, check if battery exists - if ($vehicle->battery) - { - // check if battery exists - $bobj = $em->getRepository(Battery::class)->find($vehicle->battery); - if ($bobj == null) - throw new CrudException("Could not find battery."); - - // check if warranty expiration was specified - $warr_ex = DateTime::createFromFormat("d M Y", $vehicle->warranty_expiration); - if (!$warr_ex) - $warr_ex = null; - - $cust_vehicle->setHasMotoliteBattery(true) - ->setCurrentBattery($bobj) - ->setWarrantyCode($vehicle->warranty_code) - ->setWarrantyExpiration($warr_ex); - } - else - { - $cust_vehicle->setHasMotoliteBattery(false); - } - - - // add to list of vehicles to keep - $vehicle_ids[$cust_vehicle->getID()] = true; - } - - // cleanup - // delete all vehicles not in list - $cvs = $cust->getVehicles(); - foreach ($cvs as $cv) - { - if (!isset($vehicle_ids[$cv->getID()])) - { - $cust->removeVehicle($cv); - $em->remove($cv); - } - } - } - public function updateSubmit(Request $req, CustomerHandlerInterface $cust_handler, $id) { $this->denyAccessUnlessGranted('customer.update', null, 'No access.'); @@ -373,12 +262,6 @@ class CustomerController extends Controller $response->send(); } - protected function generateYearOptions() - { - $start_year = 1950; - return range($start_year, date("Y") + 1); - } - public function getCustomerVehicles(Request $req) { if (!$this->isGranted('jo_in.list')) { diff --git a/src/Service/CustomerHandler/CMBCustomerHandler.php b/src/Service/CustomerHandler/CMBCustomerHandler.php index a3007e0d..a4bf5118 100644 --- a/src/Service/CustomerHandler/CMBCustomerHandler.php +++ b/src/Service/CustomerHandler/CMBCustomerHandler.php @@ -38,6 +38,14 @@ class CMBCustomerHandler implements CustomerHandlerInterface $this->loadTemplates(); } + // initialize form to display customer list + public function initializeCustomerIndexForm() + { + $params['template'] = $this->getTwigTemplate('cust_list'); + + return $params; + } + // initialize add customer form public function initializeAddCustomerForm() { @@ -192,7 +200,6 @@ class CMBCustomerHandler implements CustomerHandlerInterface // get dropdown parameters $this->fillDropdownParameters($params); - // get template to display $params['template'] = $this->getTwigTemplate('cust_update_form'); @@ -316,6 +323,7 @@ class CMBCustomerHandler implements CustomerHandlerInterface // add all twig templates for customer to hash $this->template_hash['cust_add_form'] = 'customer/cmb.form.html.twig'; $this->template_hash['cust_update_form'] = 'customer/cmb.form.html.twig'; + $this->template_hash['cust_list'] = 'customer/list.html.twig'; } protected function updateVehicles($em, Customer $cust, $vehicles) diff --git a/src/Service/CustomerHandler/ResqCustomerHandler.php b/src/Service/CustomerHandler/ResqCustomerHandler.php index 6d61b985..cb38b16b 100644 --- a/src/Service/CustomerHandler/ResqCustomerHandler.php +++ b/src/Service/CustomerHandler/ResqCustomerHandler.php @@ -40,6 +40,14 @@ class ResqCustomerHandler implements CustomerHandlerInterface $this->loadTemplates(); } + // initialize form to display customer list + public function initializeCustomerIndexForm() + { + $params['template'] = $this->getTwigTemplate('cust_list'); + + return $params; + } + // initialize add customer form public function initializeAddCustomerForm() { @@ -319,6 +327,7 @@ class ResqCustomerHandler implements CustomerHandlerInterface // add all twig templates for customer to hash $this->template_hash['cust_add_form'] = 'customer/form.html.twig'; $this->template_hash['cust_update_form'] = 'customer/form.html.twig'; + $this->template_hash['cust_list'] = 'customer/list.html.twig'; } protected function updateVehicles($em, Customer $cust, $vehicles) diff --git a/src/Service/CustomerHandlerInterface.php b/src/Service/CustomerHandlerInterface.php index 19f1d3bb..7c2448ff 100644 --- a/src/Service/CustomerHandlerInterface.php +++ b/src/Service/CustomerHandlerInterface.php @@ -6,6 +6,9 @@ use Symfony\Component\HttpFoundation\Request; interface CustomerHandlerInterface { + // initialize form to display customer list + public function initializeCustomerIndexForm(); + // initialize add customer form public function initializeAddCustomerForm();