From 9d04ff7879ac2a486d4d57595d8928ea17ab59dd Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Wed, 7 Feb 2018 01:40:13 +0800 Subject: [PATCH] Add API call for get estimate --- src/Controller/APIController.php | 99 +++++++++++++++++++++++++++++++- src/Entity/Invoice.php | 2 +- 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index c13a7023..ab61f116 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -12,6 +12,9 @@ use Symfony\Component\HttpFoundation\JsonResponse; use App\Ramcar\APIResult; use App\Ramcar\JOStatus; +use App\Ramcar\InvoiceCriteria; + +use App\Service\InvoiceCreator; use App\Entity\MobileSession; use App\Entity\Customer; @@ -19,6 +22,8 @@ use App\Entity\VehicleManufacturer; use App\Entity\Vehicle; use App\Entity\CustomerVehicle; use App\Entity\JobOrder; +use App\Entity\Promo; +use App\Entity\Battery; use DateTime; @@ -673,15 +678,105 @@ class APIController extends Controller return $res->getReturnResponse(); } - public function getEstimate(Request $req) + public function getEstimate(Request $req, InvoiceCreator $ic) { // check required parameters and api key - $required_params = []; + $required_params = [ + 'cv_id', + 'batt_id', + 'trade_in', + ]; $em = $this->getDoctrine()->getManager(); $res = $this->checkParamsAndKey($req, $em, $required_params); if ($res->isError()) return $res->getReturnResponse(); + // customer + $cust = $this->session->getCustomer(); + if ($cust == null) + { + $res->setError(true) + ->setErrorMessage('No customer information found'); + return $res->getReturnResponse(); + } + + // make invoice criteria + $icrit = new InvoiceCriteria(); + + // 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(); + } + + // 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 = $em->getRepository(Battery::class)->find($req->request->get('batt_id')); + if ($batt == null) + { + $res->setError(true) + ->setErrorMessage('Invalid battery id'); + return $res->getReturnResponse(); + } + + // put battery in criteria + $icrit->addBattery($batt); + + // TODO: check trade-in + + // send to invoice generator + $invoice = $ic->processCriteria($icrit); + + // make invoice json data + $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(), + 'price' => $item->getPrice(), + ]; + } + + $data['items'] = $items_data; + + // set data + $res->setData($data); + return $res->getReturnResponse(); } diff --git a/src/Entity/Invoice.php b/src/Entity/Invoice.php index 4f018c89..a5e70b24 100644 --- a/src/Entity/Invoice.php +++ b/src/Entity/Invoice.php @@ -41,7 +41,7 @@ class Invoice // user that created the invoice /** * @ORM\ManyToOne(targetEntity="User", inversedBy="invoices") - * @ORM\JoinColumn(name="user_id", referencedColumnName="id") + * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true) */ protected $created_by;