From 174f0142c981b89376bb5f894aa3af98c649f908 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 13 Nov 2019 02:14:46 +0000 Subject: [PATCH] Move getCustomerVehicleInfo to the customer handler service. #270 --- src/Controller/CustomerController.php | 80 +++---------------- .../CustomerHandler/CMBCustomerHandler.php | 60 ++++++++++++++ .../CustomerHandler/ResqCustomerHandler.php | 62 ++++++++++++++ src/Service/CustomerHandlerInterface.php | 3 + 4 files changed, 136 insertions(+), 69 deletions(-) diff --git a/src/Controller/CustomerController.php b/src/Controller/CustomerController.php index 38c4e5c4..5c15bfaa 100644 --- a/src/Controller/CustomerController.php +++ b/src/Controller/CustomerController.php @@ -6,23 +6,12 @@ use App\Ramcar\CrudException; use App\Service\CustomerHandlerInterface; -use App\Entity\Customer; -use App\Entity\CustomerVehicle; -use App\Entity\MobileNumber; -use App\Entity\Vehicle; -use App\Entity\VehicleManufacturer; -use App\Entity\Battery; -use App\Entity\BatteryManufacturer; - -use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Catalyst\MenuBundle\Annotation\Menu; -use DateTime; - class CustomerController extends Controller { @@ -209,73 +198,26 @@ class CustomerController extends Controller ]); } - public function getCustomerVehicleInfo(Request $req) + public function getCustomerVehicleInfo(Request $req, CustomerHandlerInterface $cust_handler) { $this->denyAccessUnlessGranted('jo_in.list', null, 'No access.'); - // get id - $id = $req->query->get('id'); + $result = $cust_handler->getCustomerVehicleInfo($req); - // get row data - $em = $this->getDoctrine()->getManager(); - $obj = $em->getRepository(CustomerVehicle::class)->find($id); - - // make sure this row exists - if (empty($obj)) { + if ($result == null) + { return $this->json([ 'success' => false, 'error' => 'The item does not exist' ]); } - - $customer = $obj->getCustomer(); - $vehicle = $obj->getVehicle(); - $battery = $obj->getCurrentBattery(); - - // build response - $row = [ - 'customer' => [ - 'id' => $customer->getID(), - 'first_name' => $customer->getFirstName(), - 'last_name' => $customer->getLastName(), - 'customer_notes' => $customer->getCustomerNotes(), - 'phone_mobile' => $customer->getPhoneMobile(), - 'phone_landline' => $customer->getPhoneLandline(), - 'phone_office' => $customer->getPhoneOffice(), - 'phone_fax' => $customer->getPhoneFax(), - ], - 'vehicle' => [ - 'id' => $vehicle->getID(), - 'mfg_name' => $vehicle->getManufacturer()->getName(), - 'make' => $vehicle->getMake(), - 'model_year_from' => $vehicle->getModelYearFrom(), - 'model_year_to' => $vehicle->getModelYearTo(), - 'model_year' => $obj->getModelYear(), - 'color' => $obj->getColor(), - 'plate_number' => $obj->getPlateNumber(), - //'fuel_type' => $obj->getFuelType(), - //'status_condition' => $obj->getStatusCondition(), - ] - ]; - - if (!empty($battery)) { - $row['battery'] = [ - 'id' => $battery->getID(), - 'mfg_name' => $battery->getManufacturer()->getName(), - 'model_name' => $battery->getModel()->getName(), - 'size_name' => $battery->getSize()->getName(), - 'prod_code' => $battery->getProductCode(), - 'warranty_code' => $obj->getWarrantyCode(), - 'warranty_expiration' => $obj->getWarrantyExpiration() ? $obj->getWarrantyExpiration()->format("d M Y") : "", - 'has_motolite_battery' => $obj->hasMotoliteBattery(), - 'is_active' => $obj->isActive() - ]; + else + { + // response + return $this->json([ + 'success' => true, + 'data' => $result + ]); } - - // response - return $this->json([ - 'success' => true, - 'data' => $row - ]); } } diff --git a/src/Service/CustomerHandler/CMBCustomerHandler.php b/src/Service/CustomerHandler/CMBCustomerHandler.php index 8a26feba..a0f3ca2e 100644 --- a/src/Service/CustomerHandler/CMBCustomerHandler.php +++ b/src/Service/CustomerHandler/CMBCustomerHandler.php @@ -479,6 +479,66 @@ class CMBCustomerHandler implements CustomerHandlerInterface return $results; } + // get customer vehicle info + public function getCustomerVehicleInfo(Request $req) + { + // get id + $id = $req->query->get('id'); + + // get row data + $em = $this->em; + $obj = $em->getRepository(CustomerVehicle::class)->find($id); + + // make sure this row exists + if (empty($obj)) { + return null; + } + + $customer = $obj->getCustomer(); + $vehicle = $obj->getVehicle(); + $battery = $obj->getCurrentBattery(); + + // build response + $row = [ + 'customer' => [ + 'id' => $customer->getID(), + 'first_name' => $customer->getFirstName(), + 'last_name' => $customer->getLastName(), + 'customer_notes' => $customer->getCustomerNotes(), + 'phone_mobile' => $customer->getPhoneMobile(), + 'phone_landline' => $customer->getPhoneLandline(), + 'phone_office' => $customer->getPhoneOffice(), + 'phone_fax' => $customer->getPhoneFax(), + ], + 'vehicle' => [ + 'id' => $vehicle->getID(), + 'mfg_name' => $vehicle->getManufacturer()->getName(), + 'make' => $vehicle->getMake(), + 'model_year_from' => $vehicle->getModelYearFrom(), + 'model_year_to' => $vehicle->getModelYearTo(), + 'model_year' => $obj->getModelYear(), + 'color' => $obj->getColor(), + 'plate_number' => $obj->getPlateNumber(), + ] + ]; + + if (!empty($battery)) { + $row['battery'] = [ + 'id' => $battery->getID(), + 'mfg_name' => $battery->getManufacturer()->getName(), + 'model_name' => $battery->getModel()->getName(), + 'size_name' => $battery->getSize()->getName(), + 'prod_code' => $battery->getProductCode(), + 'warranty_code' => $obj->getWarrantyCode(), + 'warranty_expiration' => $obj->getWarrantyExpiration() ? $obj->getWarrantyExpiration()->format("d M Y") : "", + 'has_motolite_battery' => $obj->hasMotoliteBattery(), + 'is_active' => $obj->isActive() + ]; + } + + return $row; + } + protected function getTwigTemplate($id) { if (isset($this->template_hash[$id])) diff --git a/src/Service/CustomerHandler/ResqCustomerHandler.php b/src/Service/CustomerHandler/ResqCustomerHandler.php index a302c83e..8ea688cf 100644 --- a/src/Service/CustomerHandler/ResqCustomerHandler.php +++ b/src/Service/CustomerHandler/ResqCustomerHandler.php @@ -477,6 +477,68 @@ class ResqCustomerHandler implements CustomerHandlerInterface return $results; } + // get customer vehicle info + public function getCustomerVehicleInfo(Request $req) + { + // get id + $id = $req->query->get('id'); + + // get row data + $em = $this->em; + $obj = $em->getRepository(CustomerVehicle::class)->find($id); + + // make sure this row exists + if (empty($obj)) { + return null; + } + + $customer = $obj->getCustomer(); + $vehicle = $obj->getVehicle(); + $battery = $obj->getCurrentBattery(); + + // build response + $row = [ + 'customer' => [ + 'id' => $customer->getID(), + 'first_name' => $customer->getFirstName(), + 'last_name' => $customer->getLastName(), + 'customer_notes' => $customer->getCustomerNotes(), + 'phone_mobile' => $customer->getPhoneMobile(), + 'phone_landline' => $customer->getPhoneLandline(), + 'phone_office' => $customer->getPhoneOffice(), + 'phone_fax' => $customer->getPhoneFax(), + ], + 'vehicle' => [ + 'id' => $vehicle->getID(), + 'mfg_name' => $vehicle->getManufacturer()->getName(), + 'make' => $vehicle->getMake(), + 'model_year_from' => $vehicle->getModelYearFrom(), + 'model_year_to' => $vehicle->getModelYearTo(), + 'model_year' => $obj->getModelYear(), + 'color' => $obj->getColor(), + 'plate_number' => $obj->getPlateNumber(), + 'fuel_type' => $obj->getFuelType(), + 'status_condition' => $obj->getStatusCondition(), + ] + ]; + + if (!empty($battery)) { + $row['battery'] = [ + 'id' => $battery->getID(), + 'mfg_name' => $battery->getManufacturer()->getName(), + 'model_name' => $battery->getModel()->getName(), + 'size_name' => $battery->getSize()->getName(), + 'prod_code' => $battery->getProductCode(), + 'warranty_code' => $obj->getWarrantyCode(), + 'warranty_expiration' => $obj->getWarrantyExpiration() ? $obj->getWarrantyExpiration()->format("d M Y") : "", + 'has_motolite_battery' => $obj->hasMotoliteBattery(), + 'is_active' => $obj->isActive() + ]; + } + + return $row; + } + protected function getTwigTemplate($id) { if (isset($this->template_hash[$id])) diff --git a/src/Service/CustomerHandlerInterface.php b/src/Service/CustomerHandlerInterface.php index cc857bfd..41256cb8 100644 --- a/src/Service/CustomerHandlerInterface.php +++ b/src/Service/CustomerHandlerInterface.php @@ -29,4 +29,7 @@ interface CustomerHandlerInterface // get customer vehicles public function getCustomerVehicles(Request $req); + + // get customer vehicle info + public function getCustomerVehicleInfo(Request $req); }