diff --git a/src/Controller/RAPIController.php b/src/Controller/RAPIController.php index 67a598a6..9cabfa86 100644 --- a/src/Controller/RAPIController.php +++ b/src/Controller/RAPIController.php @@ -16,11 +16,11 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point; use App\Ramcar\APIResult; use App\Ramcar\JOStatus; use App\Ramcar\InvoiceCriteria; -use App\Ramcar\ServiceType; +use App\Ramcar\CMBServiceType; use App\Ramcar\WarrantyClass; use App\Ramcar\APIRiderStatus; use App\Ramcar\TransactionOrigin; -use App\Ramcar\TradeInType; +use App\Ramcar\CMBTradeInType; use App\Ramcar\InvoiceStatus; use App\Ramcar\ModeOfPayment; use App\Ramcar\JOEventType; @@ -42,9 +42,10 @@ use App\Entity\RiderRating; use App\Entity\Rider; use App\Entity\User; use App\Entity\JOEvent; +use App\Entity\Warranty; use DateTime; - +use DateInterval; // Rider API controller class RAPIController extends Controller @@ -634,6 +635,14 @@ class RAPIController extends Controller ]; $mclient->sendEvent($jo, $payload); + // TODO: verify if we need to create warranty for battery replacement under warranty + // create the warranty if new battery or battery replacement + if (($jo->getServiceType () == CMBServiceType::BATTERY_REPLACEMENT_NEW) || + ($jo->getServiceType () == CMBServiceType::BATTERY_REPLACEMENT_WARRANTY)) + { + $this->createWarranty($jo); + } + return $res->getReturnResponse(); } @@ -752,7 +761,7 @@ class RAPIController extends Controller // check service type $stype_id = $req->request->get('stype_id'); - if (!ServiceType::validate($stype_id)) + if (!CMBServiceType::validate($stype_id)) { $res->setError(true) ->setErrorMessage('Invalid service type - ' . $stype_id); @@ -814,7 +823,7 @@ class RAPIController extends Controller // check trade in $trade_in = $req->request->get('trade_in'); - if (!TradeInType::validate($trade_in)) + if (!CMBTradeInType::validate($trade_in)) $trade_in = null; // check mode of payment @@ -867,4 +876,80 @@ class RAPIController extends Controller return $res->getReturnResponse(); } + + protected function createWarranty($jo) + { + $em = $this->getDoctrine()->getManager(); + $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); + + $em->persist($warranty); + $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; + } + + }