diff --git a/config/routes/capi.yaml b/config/routes/capi.yaml index 0c2e5ac1..243f0c39 100644 --- a/config/routes/capi.yaml +++ b/config/routes/capi.yaml @@ -149,3 +149,9 @@ capi_customer_register: path: /capi/quick_registration controller: App\Controller\CAPI\CustomerController::register methods: [POST] + +# customer warranty api +capi_cwarr_check: + path: /capi/customer_warranty/{serial} + controller: App\Controller\CAPI\CustomerWarrantyController::check + methods: [GET] diff --git a/src/Controller/CAPI/CustomerWarrantyController.php b/src/Controller/CAPI/CustomerWarrantyController.php new file mode 100644 index 00000000..fcc5e675 --- /dev/null +++ b/src/Controller/CAPI/CustomerWarrantyController.php @@ -0,0 +1,208 @@ +acl_gen = $acl_gen; + } + + protected function checkMissingParameters(Request $req, $params = []) + { + $missing = []; + + // check if parameters are there + foreach ($params as $param) + { + if ($req->getMethod() == 'GET') + { + $check = $req->query->get($param); + if (empty($check)) + $missing[] = $param; + } + else if ($req->getMethod() == 'POST') + { + $check = $req->request->get($param); + if (empty($check)) + $missing[] = $param; + } + else + return $params; + } + + return $missing; + } + + protected function checkRequiredParams(Request $req, $params) + { + // check required parameters + $missing = $this->checkMissingParameters($req, $params); + if (count($missing) > 0) + { + $miss_string = implode(', ', $missing); + return new APIResponse(false, 'Missing parameter(s): ' . $miss_string); + } + + return true; + } + + protected function cleanSerial($serial) + { + return trim(strtoupper($serial)); + } + + public function check($serial, EntityManagerInterface $em, Request $req) + { + // check required parameters + $required_params = []; + $res = $this->checkRequiredParams($req, $required_params); + if (!$res) + return $res; + + // check if warranty serial is there + $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); + $warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]); + $batt = null; + $is_registered = false; + + if ($warr_serial == null) + { + return new APIResponse(false, 'Invalid warranty serial code.'); + } + + $today = new DateTime(); + + // if we have a warranty entry for the serial already + if ($warr != null) + { + $warr_plate = $warr->getPlateNumber(); + $is_registered = true; + + // null mobile number should be blank string instead + if ($warr->getMobileNumber() == null) + $mobile_num = ''; + else + $mobile_num = $warr->getMobileNumber(); + + // purchase date of customer + if ($warr->getDatePurchaseCustomer() != null) + $date_purchase_cust = $warr->getDatePurchaseCustomer()->format('Y-m-d'); + else + $date_purchase_cust = $today->format('Y-m-d'); + + + // invoice + if ($warr->getFileInvoice() != null) + $invoice_url = $req->getSchemeAndHttpHost() . '/warranty_uploads/' . $warr->getFileInvoice(); + else + $invoice_url = ''; + + // warranty card + if ($warr->getFileWarrantyCard() != null) + $warr_card_url = $req->getSchemeAndHttpHost() . '/warranty_uploads/' . $warr->getFileWarrantyCard(); + else + $warr_card_url = ''; + + $customer = [ + 'first_name' => $warr->getFirstName(), + 'last_name' => $warr->getLastName(), + 'mobile_number' => $mobile_num, + 'plate_number' => $warr_plate, + 'email' => $warr->getEmail(), + ]; + $other_data = [ + 'odometer' => $warr->getOdometer(), + 'date_purchase' => $date_purchase_cust, + 'invoice' => $invoice_url, + 'warr_card' => $warr_card_url, + ]; + } + else + { + $customer = [ + 'first_name' => '', + 'last_name' => '', + 'mobile_number' => '', + 'plate_number' => '', + 'email' => '', + ]; + $other_data = [ + 'odometer' => 0, + 'date_purchase' => $today->format('Y-m-d'), + 'invoice' => '', + 'warr_card' => '', + ]; + } + + $sku = $warr_serial->getSKU(); + $batt = $em->getRepository(SAPBattery::class)->find($sku); + // TODO: put this in a config file + $image_url = $req->getSchemeAndHttpHost() . '/battery/generic.png'; + if ($batt != null) + { + $battery = [ + 'brand' => $batt->getBrand()->getName(), + 'size' => $batt->getSize()->getName(), + 'image_url' => $image_url, + ]; + } + else + { + $battery = [ + 'brand' => '', + 'size' => '', + 'image_url' => '', + ]; + } + + // populate data + $data = [ + 'is_valid' => true, + 'is_registered' => $is_registered, + 'customer' => $customer, + 'battery' => $battery, + 'odometer' => $other_data['odometer'], + 'invoice' => $other_data['invoice'], + 'warr_card' => $other_data['warr_card'], + 'date_purchase' => $other_data['date_purchase'], + ]; + + return new APIResponse(true, 'Warranty found.', $data); + } + +}