From f08c3d845bbdc7236fc767463fd01ba7e9c50350 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Fri, 5 Mar 2021 15:02:14 +0800 Subject: [PATCH] Add API calls for warranty check and register #540 --- config/routes/api.yaml | 11 ++ src/Controller/APIController.php | 205 +++++++++++++++++++++++++++++-- src/Entity/Warranty.php | 18 +++ src/Entity/WarrantySerial.php | 102 +++++++++++++++ 4 files changed, 325 insertions(+), 11 deletions(-) create mode 100644 src/Entity/WarrantySerial.php diff --git a/config/routes/api.yaml b/config/routes/api.yaml index 4fd7d00e..a223ce10 100644 --- a/config/routes/api.yaml +++ b/config/routes/api.yaml @@ -169,3 +169,14 @@ api_schedule_option_status: path: /api/schedule_option_status controller: App\Controller\APIController::scheduleOptionStatus methods: [GET] + +# paperless warranty / qr code +api_warr_serial_check: + path: /api/warranty/{serial} + controller: App\Controller\APIController::warrantyCheck + methods: [GET] + +api_warr_serial_register: + path: /api/warranty/{serial} + controller: App\Controller\APIController::warrantyRegister + methods: [POST] diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index d31cb0e8..90fe47bb 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -51,6 +51,8 @@ use App\Entity\Partner; use App\Entity\Review; use App\Entity\PrivacyPolicy; use App\Entity\Hub; +use App\Entity\SAPBattery; +use App\Entity\WarrantySerial; use DateTime; use DateInterval; @@ -401,18 +403,8 @@ class APIController extends Controller implements LoggedController return $res->getReturnResponse(); } - public function updateInfo(Request $req) + protected function updateCustomerInfo($req, $em) { - // check required parameters and api key - $required_params = [ - 'first_name', - 'last_name', - ]; - $em = $this->getDoctrine()->getManager(); - $res = $this->checkParamsAndKey($req, $em, $required_params); - if ($res->isError()) - return $res->getReturnResponse(); - // create new customer if it's not there $cust = $this->session->getCustomer(); if ($cust == null) @@ -425,11 +417,28 @@ class APIController extends Controller implements LoggedController $cust->setFirstName($req->request->get('first_name')) ->setLastName($req->request->get('last_name')) + ->setEmail($req->request->get('email')) ->setConfirmed($this->session->isConfirmed()); // update mobile phone of customer $cust->setPhoneMobile(substr($this->session->getPhoneNumber(), 2)); + return $cust; + } + + public function updateInfo(Request $req, EntityManagerInterface $em) + { + // check required parameters and api key + $required_params = [ + 'first_name', + 'last_name', + ]; + $res = $this->checkParamsAndKey($req, $em, $required_params); + if ($res->isError()) + return $res->getReturnResponse(); + + $cust = $this->updateCustomerInfo($req, $em); + // get privacy policy for mobile $dotenv = new Dotenv(); $dotenv->loadEnv(__DIR__.'/../../.env'); @@ -2741,6 +2750,180 @@ class APIController extends Controller implements LoggedController return $res->getReturnResponse(); } + public function warrantyCheck($serial, EntityManagerInterface $em, Request $req) + { + // check required parameters and api key + $required_params = []; + $res = $this->checkParamsAndKey($req, $em, $required_params); + if ($res->isError()) + return $res->getReturnResponse(); + + // initialize data + $data = [ + 'is_valid' => false, + 'customer' => [ + 'first_name' => '', + 'last_name' => '', + 'mobile_number' => '', + 'plate_number' => '', + ], + 'battery' => [ + 'brand' => '', + 'size' => '', + ], + ]; + + // 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 warranty serial is there + if ($warr_serial != null) + { + // if we have a warranty entry for the serial already + if ($warr != null) + { + $is_registered = true; + $customer = [ + 'first_name' => $warr->getFirstName(), + 'last_name' => $warr->getLastName(), + 'mobile_number' => $warr->getMobileNumber(), + 'plate_number' => $warr->getPlateNumber(), + ]; + } + else + { + $customer = [ + 'first_name' => '', + 'last_name' => '', + 'mobile_number' => '', + 'plate_number' => '', + ]; + } + + $sku = $warr_serial->getSKU(); + $batt = $em->getRepository(SAPBattery::class)->find($sku); + if ($batt != null) + { + $battery = [ + 'brand' => $batt->getBrand()->getName(), + 'size' => $batt->getSize()->getName(), + ]; + } + else + { + $battery = [ + 'brand' => '', + 'size' => '', + ]; + } + + // populate data + $data = [ + 'is_valid' => true, + 'is_registered' => $is_registered, + 'customer' => $customer, + 'battery' => $battery, + ]; + } + + $res->setData($data); + + return $res->getReturnResponse(); + } + + public function warrantyRegister($serial, EntityManagerInterface $em, Request $req) + { + // check required parameters and api key + $required_params = [ + 'first_name', + 'last_name', + 'email', + 'plate_number', + ]; + $res = $this->checkParamsAndKey($req, $em, $required_params); + if ($res->isError()) + return $res->getReturnResponse(); + + // update customer information + $cust = $this->updateCustomerInfo($req, $em); + + // update warranty + $res = $this->updateWarranty($res, $em, $req, $serial); + + $em->flush(); + + return $res->getReturnResponse(); + } + + protected function updateWarranty($res, $em, $req, $serial) + { + // get serial + $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); + if ($warr_serial == null) + { + $res->setError(true) + ->setErrorMessage('Invalid warranty serial code.'); + return $res; + } + + // check if warranty exists already + $warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]); + + // skip warranty if it already exists + if ($warr != null) + { + $res->setError(true) + ->setErrorMessage('Warranty registration entry already exists.'); + return $res; + } + + // get sap battery + $sku = $warr_serial->getSKU(); + $sap_bty = $em->getRepository(SAPBattery::class)->find($sku); + if ($sap_bty == null) + { + $res->setError(true) + ->setErrorMessage('Could not find battery entry for warranty.'); + return $res; + } + + $date_pur = new DateTime(); + + + // create new warranty entry + $warr = new Warranty(); + $warr->setSerial($serial) + ->setFirstName($req->request->get('first_name')) + ->setLastName($req->request->get('last_name')) + ->setEmail($req->request->get('email')) + ->setPlateNumber($req->request->get('plate_number')) + // TODO: figure out how to compute date of purchase + ->setDatePurchase($date_pur) + // TODO: set status + // ->setStatus() + // TODO: set battery model and size id + // ->setBatterySize() + // ->setBatteryModel() + ->setSAPBattery($sap_bty) + ->setMobileNumber(substr($this->session->getPhoneNumber(), 2)) + ->setActivated(true); + + // TODO: check for date purchase and date expire + + $em->persist($warr); + + // TODO: check if we need to do anyting else + $data = []; + + // set data to retrun to user + $res->setData($data); + + return $res; + } + protected function findCustomerByNumber($number) { $em = $this->getDoctrine()->getManager(); diff --git a/src/Entity/Warranty.php b/src/Entity/Warranty.php index d2143966..0f3f5c6c 100644 --- a/src/Entity/Warranty.php +++ b/src/Entity/Warranty.php @@ -58,6 +58,12 @@ class Warranty */ protected $last_name; + // email + /** + * @ORM\Column(type="string", length=80, nullable=true) + */ + protected $email; + // customer mobile phone number /** * @ORM\Column(type="string", length=30, nullable=true) @@ -142,6 +148,7 @@ class Warranty $this->status = WarrantyStatus::ACTIVE; $this->date_claim = null; $this->flag_activated = false; + $this->email = ''; } public function getID() @@ -249,6 +256,17 @@ class Warranty return $this->last_name; } + public function setEmail($email) + { + $this->email = $email; + return $this; + } + + public function getEmail() + { + return $this->email; + } + public function setMobileNumber($mnum = null) { $this->mobile_number = $mnum; diff --git a/src/Entity/WarrantySerial.php b/src/Entity/WarrantySerial.php new file mode 100644 index 00000000..812e4106 --- /dev/null +++ b/src/Entity/WarrantySerial.php @@ -0,0 +1,102 @@ +date_create = new DateTime(); + } + + public function setID($id) + { + $this->id = $id; + return $this; + } + + public function getID() + { + return $this->id; + } + + public function getDateCreate() + { + return $this->date_create; + } + + public function setSKU($sku) + { + $this->sku = $sku; + return $this; + } + + public function getSKU() + { + return $this->sku; + } + + public function setBatteryID($id) + { + $this->battery_id = $id; + return $this; + } + + public function getBatteryID() + { + return $this->battery_id; + } + + public function setSource($source) + { + $this->source = $source; + return $this; + } + + public function getSource() + { + return $this->source; + } +}