From 10d9e6e5aecc4da018fdc31c036e21a9f56bf454 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 23 Dec 2019 02:04:48 +0000 Subject: [PATCH] Update customer vehicle warranty information when warranty is uploaded. #290 --- .../UpdateCustomerVehicleWarrantyCommand.php | 19 +------- src/Controller/WarrantyController.php | 35 ++------------- src/Service/WarrantyHandler.php | 44 ++++++++++++++++--- 3 files changed, 41 insertions(+), 57 deletions(-) diff --git a/src/Command/UpdateCustomerVehicleWarrantyCommand.php b/src/Command/UpdateCustomerVehicleWarrantyCommand.php index 82ce0d58..4449b54a 100644 --- a/src/Command/UpdateCustomerVehicleWarrantyCommand.php +++ b/src/Command/UpdateCustomerVehicleWarrantyCommand.php @@ -61,7 +61,7 @@ class UpdateCustomerVehicleWarrantyCommand extends Command } // find battery - $batteries = $this->wh->getBatteriesForWarrantyPeriod($warr); + $batteries = $this->wh->getBatteriesForWarranty($warr); // find customer vehicle using plate number error_log('Finding customer vehicle with plate number ' . $plate_number); @@ -69,23 +69,6 @@ class UpdateCustomerVehicleWarrantyCommand extends Command if (!empty($cust_vehicles)) { - //foreach ($cust_vehicles as $cv) - //{ - // if (!empty($batteries)) - // { - // set current battery to the first 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(); - if (!empty($batteries)) { // set current battery to the first battery in list. diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 87cb8db7..40afec1a 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -567,7 +567,7 @@ class WarrantyController extends Controller { // call service to check if warranty details is incomplete and then update warranty // using details from csv file - //error_log('Updating warranty for ' . $warr->getID()); + error_log('Updating warranty for ' . $warr->getID()); $wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase); } } @@ -582,38 +582,9 @@ class WarrantyController extends Controller continue; } - //error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number); + error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number); - $warranty = $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); - - // update customer vehicle with warranty info - // get expiry date - // TODO: test this - $expiry_date = $warranty->getDateExpire(); - - // find customer vehicle - $cust_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]); - if (!empty($cust_vehicles)) - { - if (!empty($batt_list)) - { - // set current battery to the first battery in list. - // there are cases where multiple batteries linked to an SAP code. - $battery = $batt_list[0]; - $battery_id = $battery->getID(); - } - $q = $em->createQuery('update App\Entity\CustomerVehicle cv - set cv.curr_battery = :batt_id, - cv.warranty_code = :serial, - cv.warranty_expiration = :expiry_date - where cv.plate_number = :plate_number') - ->setParameters([ - 'batt_id' => $battery_id, - 'serial' => $serial, - 'expiry_date' => $expiry_date, - 'plate_number' => $plate_number]); - $q->execute(); - } + $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } } diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index f56b0f5c..4d4b4b62 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -9,6 +9,7 @@ use App\Entity\Battery; use App\Entity\BatterySize; use App\Entity\SAPBattery; use App\Entity\BatteryModel; +use App\Entity\CustomerVehicle; use App\Ramcar\WarrantyClass; @@ -61,6 +62,7 @@ class WarrantyHandler } // compute expiry date + $date_expire = null; if ((!empty($warranty_class)) && (count($batt_list) != 0)) { @@ -80,12 +82,41 @@ class WarrantyHandler $this->em->persist($warranty); $this->em->flush(); + + // update customer vehicle with warranty info + $this->updateCustomerVehicle($serial, $batt_list, $plate_number, $date_expire); + $this->em->clear(); + } - if ($warranty == null) - error_log('warranty is null'); - - return $warranty; + public function updateCustomerVehicle($serial, $batteries, $plate_number, $date_expire) + { + // 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]); + $battery_id = null; + if (!empty($cust_vehicles)) + { + if (!empty($batteries)) + { + // set current battery to the first battery in list. + // there are cases where multiple batteries linked to an SAP code. + $battery = $batteries[0]; + $battery_id = $battery->getID(); + } + //error_log('Serial/Warranty Code = ' . $serial); + $q = $this->em->createQuery('update App\Entity\CustomerVehicle cv + set cv.curr_battery = :batt_id, + cv.warranty_code = :serial, + cv.warranty_expiration = :expiry_date + where cv.plate_number = :plate_number') + ->setParameters([ + 'batt_id' => $battery_id, + 'serial' => $serial, + 'expiry_date' => $date_expire, + 'plate_number' => $plate_number]); + $q->execute(); + } } public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $batt_list, DateTime $date_purchase) @@ -166,7 +197,7 @@ class WarrantyHandler $batteries = []; if (count($batt_list) == 0) { - $batteries = $this->getBatteriesForWarrantyPeriod($warr); + $batteries = $this->getBatteriesForWarranty($warr); } else { @@ -239,8 +270,7 @@ class WarrantyHandler return $warranty_period; } - // TODO: Need to rename this function - public function getBatteriesForWarrantyPeriod($warr) + public function getBatteriesForWarranty($warr) { // find battery via sku/sap battery first // if sku is null, use battery model and battery size to find battery