diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index 16fb4112..3186e6ae 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -23,6 +23,7 @@ use App\Entity\Hub; use App\Entity\Promo; use App\Entity\Rider; use App\Entity\JORejection; +use App\Entity\Warranty; use App\Ramcar\InvoiceCriteria; use App\Ramcar\CMBServiceType; @@ -703,6 +704,14 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface ]; $mclient->sendEvent($obj, $payload); $mclient->sendRiderEvent($obj, $payload); + + // TODO: verify if we need to create warranty for battery replacement under warranty + // create the warranty if new battery or battery replacement + if (($obj->getServiceType () == CMBServiceType::BATTERY_REPLACEMENT_NEW) || + ($obj->getServiceType () == CMBServiceType::BATTERY_REPLACEMENT_WARRANTY)) + { + $this->createWarranty($obj); + } } } @@ -2152,7 +2161,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface else if ($warr == WarrantyClass::WTY_COMMERCIAL) $warr_months = $battery->getWarrantyCommercial(); else if ($warr == WarrantyClass::WTY_TNV) - $warr_months = 12; + $warr_months = $battery->getWarrantyTnv(); $warr_date = new DateTime(); $warr_date->add(new DateInterval('P' . $warr_months . 'M')); @@ -2163,6 +2172,80 @@ 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()); + + if ($warranty_class == WarrantyClass::WTY_PRIVATE) + $warranty_period = $battery->getWarrantyPrivate(); + else if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) + $warranty_period = $battery->getWarrantyCommercial(); + else if ($warranty_class == WarrantyClass::WTY_TNV) + $warranty_period = $battery->getWarrantyTnv(); + } + } + + // 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)