From 2c9f5180e3dcb8d0743adc8a172d1ce07029d6ed Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 23 Jan 2020 07:47:32 +0000 Subject: [PATCH] Move warranty creation when JO is fulfilled to the warranty handler for CMB. #308 --- .../JobOrderHandler/CMBJobOrderHandler.php | 110 ++++++------------ 1 file changed, 35 insertions(+), 75 deletions(-) diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index 89dc9663..4803e1ec 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -40,6 +40,7 @@ use App\Ramcar\JORejectionReason; use App\Service\InvoiceGeneratorInterface; use App\Service\JobOrderHandlerInterface; use App\Service\RiderAssignmentHandlerInterface; +use App\Service\WarrantyHandler; use App\Service\MQTTClient; use App\Service\APNSClient; use App\Service\MapTools; @@ -62,13 +63,14 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface protected $translator; protected $rah; protected $country_code; + protected $wh; protected $template_hash; public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah, - string $country_code) + string $country_code, WarrantyHandler $wh) { $this->em = $em; $this->ic = $ic; @@ -77,6 +79,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $this->translator = $translator; $this->rah = $rah; $this->country_code = $country_code; + $this->wh = $wh; $this->loadTemplates(); } @@ -842,7 +845,37 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface // create the warranty if new battery only if ($obj->getServiceType () == CMBServiceType::BATTERY_REPLACEMENT_NEW) { - $this->createWarranty($obj); + $serial = null; + $warranty_class = $obj->getWarrantyClass(); + $first_name = $obj->getCustomer()->getFirstName(); + $last_name = $obj->getCustomer()->getLastName(); + $mobile_number = $obj->getCustomer()->getPhoneMobile(); + + // check if date fulfilled is null + if ($obj->getDateFulfill() == null) + $date_purchase = $obj->getDateCreate(); + else + $date_purchase = $obj->getDateFulfill(); + + $plate_number = $this->wh->cleanPlateNumber($obj->getCustomerVehicle()->getPlateNumber()); + + $batt_list = array(); + $invoice = $obj->getInvoice(); + if (!empty($invoice)) + { + // get battery + $invoice_items = $invoice->getItems(); + foreach ($invoice_items as $item) + { + $battery = $item->getBattery(); + if ($battery != null) + { + $batt_list[] = $item->getBattery(); + } + } + } + + $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } } } @@ -2395,79 +2428,6 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface ->setWarrantyExpiration($warr_date); } - protected function createWarranty($jo) - { - $warranty = new Warranty(); - - $warranty_class = $jo->getWarrantyClass(); - $first_name = $jo->getCustomer()->getFirstName(); - $last_name = $jo->getCustomer()->getLastName(); - $mobile_number = $jo->getCustomer()->getPhoneMobile(); - - // check if date fulfilled is null - if ($jo->getDateFulfill() == null) - $date_create = $jo->getDateCreate(); - else - $date_create = $jo->getDateFulfill(); - - // normalize the plate number - $plate_number = $this->normalizePlateNumber($jo->getCustomerVehicle()->getPlateNumber()); - - // get battery and its warranty periods - $warranty_period = 0; - $invoice_items = $jo->getInvoice()->getItems(); - foreach ($invoice_items as $item) - { - if ($item->getBattery() != null) - { - $battery = $item->getBattery(); - $warranty->setBatteryModel($battery->getModel()); - $warranty->setBatterySize($battery->getSize()); - - // use getWarrantyPrivate for passenger warranty - if ($warranty_class == CMBWarrantyClass::WTY_PASSENGER) - $warranty_period = $battery->getWarrantyPrivate(); - else if ($warranty_class == CMBWarrantyClass::WTY_COMMERCIAL) - $warranty_period = $battery->getWarrantyCommercial(); - } - } - - // compute expiry date - $expiry_date = $this->computeDateExpire($date_create, $warranty_period); - - $warranty->setWarrantyClass($warranty_class) - ->setFirstName($first_name) - ->setLastName($last_name) - ->setMobileNumber($mobile_number) - ->setDatePurchase($date_create) - ->setDateExpire($expiry_date) - ->setPlateNumber($plate_number); - - $this->em->persist($warranty); - $this->em->flush(); - - } - - protected function normalizePlateNumber($plate_number) - { - // make it upper case - $plate_number = trim(strtoupper($plate_number)); - - // remove special characters and spaces - $plate_number = preg_replace('/[^A-Za-z0-9]/', '', $plate_number); - - //error_log('plate number ' . $plate_number); - - return $plate_number; - } - - protected function computeDateExpire($date_create, $warranty_period) - { - $expire_date = clone $date_create; - $expire_date->add(new DateInterval('P'.$warranty_period.'M')); - return $expire_date; - } - // TODO: re-enable search, figure out how to group the orWhere filters into one, so can execute that plus the pending filter // check if datatable filter is present and append to query protected function setQueryFilters($datatable, &$query, $qb, $hubs, $tier, $status)