From ff56db09b08d544783bbc631bdda63c3006e1e85 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 20 Dec 2019 07:30:08 +0000 Subject: [PATCH] Add command to update customer vehicle from warranty data. #290 --- .../UpdateCustomerVehicleWarrantyCommand.php | 85 +++++++++++++++++++ src/Entity/CustomerVehicle.php | 2 +- src/Service/WarrantyHandler.php | 1 + 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/Command/UpdateCustomerVehicleWarrantyCommand.php diff --git a/src/Command/UpdateCustomerVehicleWarrantyCommand.php b/src/Command/UpdateCustomerVehicleWarrantyCommand.php new file mode 100644 index 00000000..b8a1554d --- /dev/null +++ b/src/Command/UpdateCustomerVehicleWarrantyCommand.php @@ -0,0 +1,85 @@ +em = $em; + $this->wh = $wh; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('customervehicle:updatewarrantyinfo') + ->setDescription('Update customer vehicle warranty.') + ->setHelp('Update customer vehicle warranty.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + // get all warranties + // since it's possible that the same plate number will have multiple warranties, order them from earliest to latest + $warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.plate_number is not null order by w.date_purchase asc'); + $warranties = $warr_q->iterate(); + + foreach ($warranties as $row) + { + $warr = $row[0]; + + // clean plate number + $plate_number = $this->wh->cleanPlateNumber($warr->getPlateNumber()); + + // get other warranty information + $serial = $warr->getSerial(); + $expiry_date = $warr->getDateExpire(); + + // find battery + $batteries = $this->wh->getBatteriesForWarrantyPeriod($warr); + + // find customer vehicle using plate number + error_log('Finding customer vehicle with plate number ' . $plate_number); + $cust_vehicles = $this->em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); + + if (!empty($cust_vehicles)) + { + foreach ($cust_vehicles as $cv) + { + if (!empty($batteries)) + { + // set current battery to the last battery in list. + // there are cases where multiple batteries linked to an SAP code. + foreach ($batteries as $batt) + { + $cv->setCurrentBattery($batt); + } + } + $cv->setWarrantyCode($serial) + ->setWarrantyExpiration($expiry_date); + + $this->em->persist($cv); + $this->em->flush(); + } + } + $this->em->clear(); + } + } +} diff --git a/src/Entity/CustomerVehicle.php b/src/Entity/CustomerVehicle.php index 3e4c70a4..98db2ed7 100644 --- a/src/Entity/CustomerVehicle.php +++ b/src/Entity/CustomerVehicle.php @@ -81,7 +81,7 @@ class CustomerVehicle // warranty code // TODO: figure out how to check expiration /** - * @ORM\Column(type="string", length=20, nullable=true) + * @ORM\Column(type="string", length=50, nullable=true) */ protected $warranty_code; diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index 4dd4dcae..70a79d94 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -234,6 +234,7 @@ class WarrantyHandler return $warranty_period; } + // TODO: Need to rename this function public function getBatteriesForWarrantyPeriod($warr) { // find battery via sku/sap battery first