diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index 8e9c7d46..d38b7e9c 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -32,6 +32,7 @@ use App\Service\MapTools; use App\Service\HubCounter; use App\Service\MQTTClient; use App\Service\APNSClient; +use App\Service\WarrantyHandler; use Doctrine\ORM\Query; use Doctrine\DBAL\Connection; @@ -1404,7 +1405,8 @@ class JobOrderController extends Controller ->setWarrantyExpiration($warr_date); } - public function fulfillmentSubmit(Request $req, ValidatorInterface $validator, MQTTClient $mclient, $id) + public function fulfillmentSubmit(Request $req, ValidatorInterface $validator, + MQTTClient $mclient, $id, WarrantyHandler $wh) { $this->denyAccessUnlessGranted('jo_fulfill.list', null, 'No access.'); @@ -1480,6 +1482,42 @@ class JobOrderController extends Controller // save to customer vehicle battery record $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); + } + // validated! save the entity $em->flush(); 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); + } } diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index bfc30021..641658d7 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -78,7 +78,8 @@ class WarrantyHandler ->setFirstName($first_name) ->setLastName($last_name) ->setMobileNumber($mobile_number) - ->setDatePurchase($date_purchase); + ->setDatePurchase($date_purchase) + ->setWarrantyClass($warranty_class); $this->em->persist($warranty); $this->em->flush();