From fb2d716721302f92bfd47a190c583fb142fe9386 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 21 Jan 2020 08:38:52 +0000 Subject: [PATCH] Add update of customer vehicle and warranty creation after rider sets JO to fulfilled. #304 --- src/Controller/RAPIController.php | 95 ++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/src/Controller/RAPIController.php b/src/Controller/RAPIController.php index ddd66d5e..a1d709a6 100644 --- a/src/Controller/RAPIController.php +++ b/src/Controller/RAPIController.php @@ -27,6 +27,7 @@ use App\Ramcar\JOEventType; use App\Service\InvoiceCreator; use App\Service\MQTTClient; +use App\Service\WarrantyHandler; use App\Entity\RiderSession; use App\Entity\Customer; @@ -590,7 +591,7 @@ class RAPIController extends Controller return $res->getReturnResponse(); } - public function payment(Request $req, MQTTClient $mclient) + public function payment(Request $req, MQTTClient $mclient, WarrantyHandler $wh) { $em = $this->getDoctrine()->getManager(); $required_params = ['jo_id']; @@ -618,6 +619,46 @@ class RAPIController extends Controller // TODO: tag rider as unavailable + // save to customer vehicle battery record + // TODO: this has to move to JOHandler + $this->updateVehicleBattery($obj); + + // create warranty + if ($obj->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) + { + $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 = $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(); + } + } + } + + $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); + } + $em->flush(); // send mqtt event (fulfilled) @@ -868,4 +909,56 @@ class RAPIController extends Controller return $res->getReturnResponse(); } + + protected function updateVehicleBattery(JobOrder $jo) + { + // check if new battery + if ($jo->getServiceType() != ServiceType::BATTERY_REPLACEMENT_NEW) + return; + + // customer vehicle + $cv = $jo->getCustomerVehicle(); + if ($cv == null) + return; + + // invoice + $invoice = $jo->getInvoice(); + if ($invoice == null) + return; + + // invoice items + $items = $invoice->getItems(); + if (count($items) <= 0) + return; + + // get first battery from invoice + $battery = null; + foreach ($items as $item) + { + $battery = $item->getBattery(); + if ($battery != null) + break; + } + + // no battery in order + if ($battery == null) + return; + + // warranty expiration + $warr = $jo->getWarrantyClass(); + if ($warr == WarrantyClass::WTY_PRIVATE) + $warr_months = $battery->getWarrantyPrivate(); + else if ($warr == WarrantyClass::WTY_COMMERCIAL) + $warr_months = $battery->getWarrantyCommercial(); + else if ($warr == WarrantyClass::WTY_TNV) + $warr_months = 12; + + $warr_date = new DateTime(); + $warr_date->add(new DateInterval('P' . $warr_months . 'M')); + + // update customer vehicle battery + $cv->setCurrentBattery($battery) + ->setHasMotoliteBattery(true) + ->setWarrantyExpiration($warr_date); + } }