diff --git a/.env.dist b/.env.dist index a9fe4b6a..b0392b88 100644 --- a/.env.dist +++ b/.env.dist @@ -28,6 +28,8 @@ RT_SHORTCODE=1234 MQTT_IP_ADDRESS=localhost MQTT_PORT=8883 MQTT_CERT=/location/of/cert/file.crt +MQTT_WS_HOST=insertiphere +MQTT_WS_PORT=8083 # redis client REDIS_CLIENT_SCHEME=tcp diff --git a/config/services.yaml b/config/services.yaml index 0b7669fd..5364d19a 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -11,6 +11,7 @@ parameters: app_acl_file: 'acl.yaml' app_access_key: 'access_keys' cvu_brand_id: "%env(CVU_BRAND_ID)%" + country_code: "%env(COUNTRY_CODE)%" services: # default configuration for services in *this* file diff --git a/src/Controller/RAPIController.php b/src/Controller/RAPIController.php index ce8631fd..a8b7a589 100644 --- a/src/Controller/RAPIController.php +++ b/src/Controller/RAPIController.php @@ -17,16 +17,19 @@ use App\Ramcar\APIResult; use App\Ramcar\JOStatus; use App\Ramcar\InvoiceCriteria; use App\Ramcar\CMBServiceType; +use App\Ramcar\ServiceType; use App\Ramcar\WarrantyClass; use App\Ramcar\APIRiderStatus; use App\Ramcar\TransactionOrigin; use App\Ramcar\CMBTradeInType; +use App\Ramcar\TradeInType; use App\Ramcar\InvoiceStatus; use App\Ramcar\ModeOfPayment; use App\Ramcar\JOEventType; use App\Service\InvoiceGeneratorInterface; use App\Service\MQTTClient; +use App\Service\WarrantyHandler; use App\Service\RedisClientProvider; use App\Service\RiderCache; @@ -50,6 +53,7 @@ use DateTime; use DateInterval; // Rider API controller +// TODO: Need to refactor this into a service class RAPIController extends Controller { protected $session; @@ -615,7 +619,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']; @@ -643,8 +647,49 @@ class RAPIController extends Controller // TODO: tag rider as unavailable + // save to customer vehicle battery record + // TODO: this has to move to JOHandler + $this->updateVehicleBattery($jo); + $em->flush(); + // create warranty + if (($jo->getServiceType() == ServiceType::BATTERY_REPLACEMENT_NEW) || + ($jo->getServiceType() == CMBServiceType::BATTERY_REPLACEMENT_NEW)) + { + $serial = null; + $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_purchase = $jo->getDateCreate(); + else + $date_purchase = $jo->getDateFulfill(); + + $plate_number = $wh->cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); + + $batt_list = array(); + $invoice = $jo->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); + } + // send mqtt event (fulfilled) $rider = $this->session->getRider(); $image_url = $req->getScheme() . '://' . $req->getHttpHost() . $req->getBasePath() . '/assets/images/user.gif'; @@ -659,12 +704,6 @@ class RAPIController extends Controller ]; $mclient->sendEvent($jo, $payload); - // create the warranty if new battery only - if ($jo->getServiceType () == CMBServiceType::BATTERY_REPLACEMENT_NEW) - { - $this->createWarranty($jo); - } - return $res->getReturnResponse(); } @@ -783,7 +822,8 @@ class RAPIController extends Controller // check service type $stype_id = $req->request->get('stype_id'); - if (!CMBServiceType::validate($stype_id)) + if ((!CMBServiceType::validate($stype_id)) || + (!ServiceType::validate($stype_id))) { $res->setError(true) ->setErrorMessage('Invalid service type - ' . $stype_id); @@ -845,7 +885,8 @@ class RAPIController extends Controller // check trade in $trade_in = $req->request->get('trade_in'); - if (!CMBTradeInType::validate($trade_in)) + if ((!CMBTradeInType::validate($trade_in)) || + (!TradeInType::validate($trade_in))) $trade_in = null; // check mode of payment @@ -899,6 +940,60 @@ class RAPIController extends Controller return $res->getReturnResponse(); } + protected function updateVehicleBattery(JobOrder $jo) + { + // check if new battery + if (($jo->getServiceType() != ServiceType::BATTERY_REPLACEMENT_NEW) || + ($jo->getServiceType() != CMBServiceType::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_months = 0; + $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); + } + protected function createWarranty($jo) { $em = $this->getDoctrine()->getManager(); @@ -972,6 +1067,4 @@ class RAPIController extends Controller $expire_date->add(new DateInterval('P'.$warranty_period.'M')); return $expire_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(); diff --git a/templates/job-order/cmb.form.onestep.html.twig b/templates/job-order/cmb.form.onestep.html.twig index 7bdcc8bb..1b043791 100644 --- a/templates/job-order/cmb.form.onestep.html.twig +++ b/templates/job-order/cmb.form.onestep.html.twig @@ -96,7 +96,7 @@