From f45f7fd411dbf2b980911a10f967c70a917a87e7 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 12 Jul 2021 07:08:59 +0000 Subject: [PATCH] Modify getEstimate. #591 --- config/api_acl.yaml | 2 + src/Controller/ResqAPI/JobOrderController.php | 220 +++++------------- 2 files changed, 66 insertions(+), 156 deletions(-) diff --git a/config/api_acl.yaml b/config/api_acl.yaml index 9f280586..eae96849 100644 --- a/config/api_acl.yaml +++ b/config/api_acl.yaml @@ -146,3 +146,5 @@ access_keys: acls: - id: mobile_jo.request label: Request Job Order + - id: mobile_jo.get.estimate + label: Get Estimate diff --git a/src/Controller/ResqAPI/JobOrderController.php b/src/Controller/ResqAPI/JobOrderController.php index 1302bf21..3c7e2ea1 100644 --- a/src/Controller/ResqAPI/JobOrderController.php +++ b/src/Controller/ResqAPI/JobOrderController.php @@ -134,25 +134,7 @@ class JobOrderController extends APIController $em->flush(); - // make invoice json data - $invoice_data = [ - 'total_price' => $invoice->getTotalPrice(), - 'vat_ex_price' => $invoice->getVATExclusivePrice(), - 'vat' => $invoice->getVAT(), - 'discount' => $invoice->getDiscount(), - 'trade_in' => $invoice->getTradeIn(), - ]; - $items = $invoice->getItems(); - $items_data = []; - foreach ($items as $item) - { - $items_data[] = [ - 'title' => $item->getTitle(), - 'qty' => $item->getQuantity() + 0, - 'price' => $item->getPrice() + 0.0, - ]; - } - $invoice_data['items'] = $items_data; + $invoice_data = $this->makeInvoiceData($invoice, $mah, $req); // make job order data $data = [ @@ -272,24 +254,7 @@ class JobOrderController extends APIController $em->flush(); // make invoice json data - $invoice_data = [ - 'total_price' => $invoice->getTotalPrice(), - 'vat_ex_price' => $invoice->getVATExclusivePrice(), - 'vat' => $invoice->getVAT(), - 'discount' => $invoice->getDiscount(), - 'trade_in' => $invoice->getTradeIn(), - ]; - $items = $invoice->getItems(); - $items_data = []; - foreach ($items as $item) - { - $items_data[] = [ - 'title' => $item->getTitle(), - 'qty' => $item->getQuantity() + 0, - 'price' => $item->getPrice() + 0.0, - ]; - } - $invoice_data['items'] = $items_data; + $invoice_data = $this->makeInvoiceData($invoice, $mah, $req); // make job order data $data = [ @@ -302,143 +267,50 @@ class JobOrderController extends APIController return new APIResponse(true, 'Job order created', $data); } - // TODO: modify for MobileUser - public function getEstimate(Request $req, InvoiceGeneratorInterface $ic, EntityManagerInterface $em) + public function getEstimate(Request $req, InvoiceGeneratorInterface $ic, EntityManagerInterface $em, + MobileAPIHandler $mah) { - // $this->debugRequest($req); + $this->denyAccessUnlessGranted('mobile_jo.get.estimate', null, 'No access.'); - // check required parameters and api key + // check required parameters $required_params = [ 'service_type', 'cv_id', // 'batt_id', 'trade_in', ]; - $res = $this->checkParamsAndKey($req, $em, $required_params); - if ($res->isError()) - return $res->getReturnResponse(); + + $msg = $this->checkRequiredParameters($req, $required_params); + if ($msg) + return new APIResponse(false, $msg); + + // get capi user to link to mobile user + $user_id = $this->getUser()->getID(); + + // get mobile user + $mobile_user = $mah->findMobileUser($user_id); + + if ($mobile_user == null) + return new APIResponse(false, 'No mobile user found.'); // customer - $cust = $this->session->getCustomer(); + $cust = $mobile_user->getCustomer(); if ($cust == null) - { - $res->setError(true) - ->setErrorMessage('No customer information found'); - return $res->getReturnResponse(); - } + return new APIResponse(false, 'No customer information found'); // make invoice criteria $icrit = new InvoiceCriteria(); - $icrit->setServiceType($req->request->get('service_type')); - - // check promo - $promo_id = $req->request->get('promo_id'); - if (!empty($promo_id)) - { - $promo = $em->getRepository(Promo::class)->find($promo_id); - if ($promo == null) - { - $res->setError(true) - ->setErrorMessage('Invalid promo id'); - return $res->getReturnResponse(); - } - - // put in criteria - $icrit->addPromo($promo); - } - - // check customer vehicle - $cv = $em->getRepository(CustomerVehicle::class)->find($req->request->get('cv_id')); - if ($cv == null) - { - $res->setError(true) - ->setErrorMessage('Invalid customer vehicle id'); - return $res->getReturnResponse(); - } - $icrit->setCustomerVehicle($cv); - - // check if customer owns vehicle - if ($cust->getID() != $cv->getCustomer()->getID()) - { - $res->setError(true) - ->setErrorMessage('Customer does not own vehicle'); - return $res->getReturnResponse(); - } - - // check battery - $batt_id = $req->request->get('batt_id'); - if ($batt_id != null) - { - $batt = $em->getRepository(Battery::class)->find($batt_id); - if ($batt == null) - { - $res->setError(true) - ->setErrorMessage('Invalid battery id'); - return $res->getReturnResponse(); - } - } - else - $batt = null; - - /* - // put battery in criteria - $icrit->addBattery($batt); - */ - - // check trade-in - // only allow motolite, other, none - $trade_in = $req->request->get('trade_in'); - switch ($trade_in) - { - case TradeInType::MOTOLITE: - case TradeInType::OTHER: - break; - - default: - $trade_in = ''; - break; - } - - $icrit->addEntry($batt, $trade_in, 1); + $msg = $this->setInvoiceCriteria($em, $icrit, $req, $cust, null); + if ($msg != null) + return new APIResponse(false, $msg); // send to invoice generator $invoice = $ic->generateInvoice($icrit); // make invoice json data - $data = [ - 'total_price' => (float) $invoice->getTotalPrice(), - 'vat_ex_price' => (float) $invoice->getVATExclusivePrice(), - 'vat' => (float) $invoice->getVAT(), - 'discount' => (float) $invoice->getDiscount(), - 'trade_in' => (float) $invoice->getTradeIn(), - ]; - $items = $invoice->getItems(); - $items_data = []; - foreach ($items as $item) - { - $my_data = [ - 'title' => $item->getTitle(), - 'qty' => (int) $item->getQuantity() + 0, - 'price' => (float) $item->getPrice() + 0.0, - ]; + $invoice_data = $this->makeInvoiceData($invoice, $mah, $req); - $item_batt = $item->getBattery(); - if ($item_batt != null) - { - $my_data['image_url'] = $this->getBatteryImageURL($req, $item_batt); - } - - $items_data[] = $my_data; - } - - $data['items'] = $items_data; - - // error_log(print_r($data, true)); - - // set data - $res->setData($data); - - return $res->getReturnResponse(); + return new APIResponse(true, 'Estimate found', $invoice_data); } // TODO: modify for MobileUser @@ -1221,7 +1093,7 @@ class JobOrderController extends APIController } protected function setInvoiceCriteria(EntityManagerInterface $em, InvoiceCriteria $icrit, Request $req, - Customer $cust, JobOrder $jo) + Customer $cust, $jo) { $icrit->setServiceType($req->request->get('service_type')); @@ -1248,7 +1120,8 @@ class JobOrderController extends APIController return $msg; } $icrit->setCustomerVehicle($cv); - $jo->setCustomerVehicle($cv); + if ($jo != null) + $jo->setCustomerVehicle($cv); // check if customer owns vehicle if ($cust->getID() != $cv->getCustomer()->getID()) @@ -1434,6 +1307,41 @@ class JobOrderController extends APIController } } + protected function makeInvoiceData($invoice, $mah, $req) + { + // make invoice json data + $invoice_data = [ + 'total_price' => $invoice->getTotalPrice(), + 'vat_ex_price' => $invoice->getVATExclusivePrice(), + 'vat' => $invoice->getVAT(), + 'discount' => $invoice->getDiscount(), + 'trade_in' => $invoice->getTradeIn(), + ]; + $items = $invoice->getItems(); + $items_data = []; + foreach ($items as $item) + { + $my_data = [ + 'title' => $item->getTitle(), + 'qty' => (int) $item->getQuantity() + 0, + 'price' => (float) $item->getPrice() + 0.0, + ]; + + $item_batt = $item->getBattery(); + if ($item_batt != null) + { + // generate the url for image + $battery_image_url = $this->generateUrl('static_battery_image'); + $my_data['image_url'] = $mah->getBatteryImageURL($req, $item_batt, $battery_image_url); + } + + $items_data[] = $my_data; + } + + $invoice_data['items'] = $items_data; + return $invoice_data; + } + protected function removeCustomerTag(JobOrder $jo, CustomerVehicle $cv, PromoLogger $promo_logger, $mobile_user_id) { // check service type