From bcc606f52af448bbaa6fb619caa4b9fa2909bec1 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sun, 27 May 2018 05:55:54 +0800 Subject: [PATCH] Add change service rider api call #132 --- src/Controller/RAPIController.php | 92 +++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 4 deletions(-) diff --git a/src/Controller/RAPIController.php b/src/Controller/RAPIController.php index 78f1fa11..740764b5 100644 --- a/src/Controller/RAPIController.php +++ b/src/Controller/RAPIController.php @@ -21,6 +21,7 @@ use App\Ramcar\WarrantyClass; use App\Ramcar\APIRiderStatus; use App\Ramcar\TransactionOrigin; use App\Ramcar\TradeInType; +use App\Ramcar\InvoiceStatus; use App\Service\InvoiceCreator; @@ -64,13 +65,13 @@ class RAPIController extends Controller if ($req->getMethod() == 'GET') { $check = $req->query->get($param); - if (empty($check)) + if ($check == null) $missing[] = $param; } else if ($req->getMethod() == 'POST') { $check = $req->request->get($param); - if (empty($check)) + if ($check == null) $missing[] = $param; } else @@ -301,11 +302,18 @@ class RAPIController extends Controller $inv_items = []; foreach ($inv->getItems() as $item) { + $item_batt = $item->getBattery(); + if ($item_batt == null) + $batt_id = null; + else + $batt_id = $item_batt->getID(); + $inv_items[] = [ 'id' => $item->getID(), 'title' => $item->getTitle(), 'qty' => $item->getQuantity(), 'price' => $item->getPrice(), + 'batt_id' => $batt_id, ]; } @@ -549,18 +557,94 @@ class RAPIController extends Controller return $res->getReturnResponse(); } - public function changeService(Request $req) + public function changeService(Request $req, InvoiceCreator $ic) { // allow rider to change service, promo, battery and trade-in options $em = $this->getDoctrine()->getManager(); - $required_params = ['jo_id', 'stype_id', 'promo_id', 'battery_id', 'trade_in']; + $required_params = ['jo_id', 'stype_id', 'promo_id', 'batt_id', 'trade_in']; $res = $this->checkJO($req, $required_params, $jo); if ($res->isError()) return $res->getReturnResponse(); + // check service type + $stype_id = $req->request->get('stype_id'); + if (!ServiceType::validate($stype_id)) + { + $res->setError(true) + ->setErrorMessage('Invalid service type - ' . $stype_id); + return $res->getReturnResponse(); + } + + // check promo id + $promo_id = $req->request->get('promo_id'); + // no promo + if ($promo_id == 0) + $promo = null; + else + { + $promo = $em->getRepository(Promo::class)->find($promo_id); + if ($promo == null) + { + $res->setError(true) + ->setErrorMessage('Invalid promo id - ' . $promo_id); + return $res->getReturnResponse(); + } + } + + // check battery id + $batt_id = $req->request->get('batt_id'); + // no battery + if ($batt_id == 0) + $battery = null; + else + { + $battery = $em->getRepository(Battery::class)->find($batt_id); + if ($battery == null) + { + $res->setError(true) + ->setErrorMessage('Invalid battery id - ' . $batt_id); + return $res->getReturnResponse(); + } + } + + // check trade in + $trade_in = $req->request->get('trade_in'); + if (!TradeInType::validate($trade_in)) + $trade_in = null; + + // generate new invoice + $crit = new InvoiceCriteria(); + $crit->setServiceType($stype_id); + + if ($promo != null) + $crit->addPromo($promo); + + if ($battery != null) + { + $crit->addEntry($battery, $trade_in, 1); + error_log('adding entry for battery - ' . $battery->getID()); + } + + $invoice = $ic->processCriteria($crit); + $invoice->setStatus(InvoiceStatus::DRAFT); + + // remove previous invoice + $old_invoice = $jo->getInvoice(); + $em->remove($old_invoice); + $em->flush(); + + // save job order + $jo->setServiceType($stype_id); + + // save invoice + $jo->setInvoice($invoice); + $em->persist($invoice); + $em->flush(); // TODO: add event // TODO: send mqtt event (?) + + return $res->getReturnResponse(); } }