diff --git a/src/Controller/CustomerController.php b/src/Controller/CustomerController.php index 29ae9cdc..3d32002a 100644 --- a/src/Controller/CustomerController.php +++ b/src/Controller/CustomerController.php @@ -228,27 +228,16 @@ class CustomerController extends Controller /** * @Menu(selected="customer_list") */ - public function updateForm($id) + public function updateForm($id, CustomerHandlerInterface $cust_handler) { $this->denyAccessUnlessGranted('customer.update', null, 'No access.'); - $params['mode'] = 'update'; + $params = $cust_handler->initializeUpdateCustomerForm($id); - // get row data - $em = $this->getDoctrine()->getManager(); - $row = $em->getRepository(Customer::class)->find($id); - - // make sure this row exists - if (empty($row)) - throw $this->createNotFoundException('The item does not exist'); - - // get dropdown parameters - $this->fillDropdownParameters($params); - - $params['obj'] = $row; + $template = $params['template']; // response - return $this->render('customer/form.html.twig', $params); + return $this->render($template, $params); } protected function updateVehicles($em, Customer $cust, $vehicles) diff --git a/src/Service/CustomerHandler/CMBCustomerHandler.php b/src/Service/CustomerHandler/CMBCustomerHandler.php index 28118b25..a3007e0d 100644 --- a/src/Service/CustomerHandler/CMBCustomerHandler.php +++ b/src/Service/CustomerHandler/CMBCustomerHandler.php @@ -7,7 +7,10 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Validator\Validator\ValidatorInterface; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + use App\Ramcar\CustomerClassification; +use App\Ramcar\CrudException; use App\Service\CustomerHandlerInterface; @@ -18,6 +21,8 @@ use App\Entity\Battery; use App\Entity\VehicleManufacturer; use App\Entity\BatteryManufacturer; +use DateTime; + class CMBCustomerHandler implements CustomerHandlerInterface { protected $em; @@ -147,6 +152,7 @@ class CMBCustomerHandler implements CustomerHandlerInterface $error_array[$error->getPropertyPath()] = $error->getMessage(); } + $result = []; // check if any errors were found if (!empty($error_array) || !empty($nerror_array) || !empty($verror_array)) { @@ -171,6 +177,89 @@ class CMBCustomerHandler implements CustomerHandlerInterface return $result; } + // initialize update customer form + public function initializeUpdateCustomerForm($id) + { + $params['mode'] = 'update'; + + // get row data + $em = $this->em; + $row = $em->getRepository(Customer::class)->find($id); + + // make sure this row exists + if (empty($row)) + throw new NotFoundHttpException('The item does not exist'); + + // get dropdown parameters + $this->fillDropdownParameters($params); + + // get template to display + $params['template'] = $this->getTwigTemplate('cust_update_form'); + + $params['obj'] = $row; + + return $params; + } + + // update customer and customer vehicle + public function updateCustomer(Request $req, $id) + { + // get row data + $em = $this->em; + $cust = $em->getRepository(Customer::class)->find($id); + + // make sure this row exists + if (empty($cust)) + throw $this->createNotFoundException('The item does not exist'); + + $this->setObject($cust, $req); + + // initialize error lists + $error_array = []; + $nerror_array = []; + $verror_array = []; + + // TODO: validate mobile numbers + // TODO: validate vehicles + + // custom validation for vehicles + $vehicles = json_decode($req->request->get('vehicles')); + $this->updateVehicles($em, $cust, $vehicles); + + // validate + $errors = $this->validator->validate($cust); + + // add errors to list + foreach ($errors as $error) + { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array) || !empty($nerror_array) || !empty($verror_array)) + { + // return all error_arrays + $result = [ + 'error_array' => $error_array, + 'nerror_array' => $nerror_array, + 'verror_array' => $verror_array, + ]; + + } + else + { + // validated! save the entity. do a persist anyway to save child entities + $em->persist($cust); + $em->flush(); + + $result = [ + 'id' => $cust->getID(), + ]; + } + + return $result; + } + public function getTwigTemplate($id) { if (isset($this->template_hash[$id])) @@ -225,6 +314,86 @@ class CMBCustomerHandler implements CustomerHandlerInterface $this->template_hash = []; // add all twig templates for customer to hash - $this->template_hash['cust_add_form'] = 'customer/cmb.form.html.twig'; + $this->template_hash['cust_add_form'] = 'customer/cmb.form.html.twig'; + $this->template_hash['cust_update_form'] = 'customer/cmb.form.html.twig'; + } + + 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); + } + } } } diff --git a/src/Service/CustomerHandler/ResqCustomerHandler.php b/src/Service/CustomerHandler/ResqCustomerHandler.php index 3dabaa54..6d61b985 100644 --- a/src/Service/CustomerHandler/ResqCustomerHandler.php +++ b/src/Service/CustomerHandler/ResqCustomerHandler.php @@ -7,6 +7,8 @@ use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Validator\Validator\ValidatorInterface; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + use App\Service\CustomerHandlerInterface; use App\Ramcar\CustomerClassification; @@ -21,6 +23,8 @@ use App\Entity\Battery; use App\Entity\VehicleManufacturer; use App\Entity\BatteryManufacturer; +use DateTime; + class ResqCustomerHandler implements CustomerHandlerInterface { protected $em; @@ -175,6 +179,89 @@ class ResqCustomerHandler implements CustomerHandlerInterface return $result; } + // initialize update customer form + public function initializeUpdateCustomerForm($id) + { + $params['mode'] = 'update'; + + // get row data + $em = $this->em; + $row = $em->getRepository(Customer::class)->find($id); + + // make sure this row exists + if (empty($row)) + throw new NotFoundHttpException('The item does not exist'); + + // get dropdown parameters + $this->fillDropdownParameters($params); + + // get template to display + $params['template'] = $this->getTwigTemplate('cust_update_form'); + + $params['obj'] = $row; + + return $params; + } + + // update customer and customer vehicle + public function updateCustomer(Request $req, $id) + { + // get row data + $em = $this->em; + $cust = $em->getRepository(Customer::class)->find($id); + + // make sure this row exists + if (empty($cust)) + throw $this->createNotFoundException('The item does not exist'); + + $this->setObject($cust, $req); + + // initialize error lists + $error_array = []; + $nerror_array = []; + $verror_array = []; + + // TODO: validate mobile numbers + // TODO: validate vehicles + + // custom validation for vehicles + $vehicles = json_decode($req->request->get('vehicles')); + $this->updateVehicles($em, $cust, $vehicles); + + // validate + $errors = $this->validator->validate($cust); + + // add errors to list + foreach ($errors as $error) + { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array) || !empty($nerror_array) || !empty($verror_array)) + { + // return all error_arrays + $result = [ + 'error_array' => $error_array, + 'nerror_array' => $nerror_array, + 'verror_array' => $verror_array, + ]; + + } + else + { + // validated! save the entity. do a persist anyway to save child entities + $em->persist($cust); + $em->flush(); + + $result = [ + 'id' => $cust->getID(), + ]; + } + + return $result; + } + public function getTwigTemplate($id) { if (isset($this->template_hash[$id])) @@ -185,7 +272,6 @@ class ResqCustomerHandler implements CustomerHandlerInterface return null; } - protected function setObject($obj, $req) { // set and save values @@ -232,7 +318,85 @@ 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'; } + 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($vehicle->color) + ->setStatusCondition($vehicle->status_condition) + ->setFuelType($vehicle->fuel_type) + ->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); + } + } + } } diff --git a/src/Service/CustomerHandlerInterface.php b/src/Service/CustomerHandlerInterface.php index f27e941a..19f1d3bb 100644 --- a/src/Service/CustomerHandlerInterface.php +++ b/src/Service/CustomerHandlerInterface.php @@ -12,6 +12,12 @@ interface CustomerHandlerInterface // add new customer and customer vehicle, if any public function addCustomer(Request $req); + // initialize update customer form + public function initializeUpdateCustomerForm(int $id); + + // update customer and customer vehicle + public function updateCustomer(Request $req, int $id); + // get template to display public function getTwigTemplate(string $id); }