diff --git a/config/routes/api.yaml b/config/routes/api.yaml index a47df74b..50e50957 100644 --- a/config/routes/api.yaml +++ b/config/routes/api.yaml @@ -100,6 +100,11 @@ api_jo_history: controller: App\Controller\APIController:getJOHistory methods: [GET] +api_jo_invoice: + path: /api/job_order/invoice + controller: App\Controller\APIController:getJOInvoice + methods: [GET] + api_device_id: path: /api/device_id controller: App\Controller\APIController:updateDeviceID diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 5ea8a578..7dc867f8 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -1278,6 +1278,81 @@ class APIController extends Controller return $res->getReturnResponse(); } + public function getJOInvoice(Request $req) + { + $required_params = [ + 'jo_id', + ]; + $em = $this->getDoctrine()->getManager(); + $res = $this->checkParamsAndKey($req, $em, $required_params); + if ($res->isError()) + return $res->getReturnResponse(); + + // get job order + $jo_id = $req->query->get('jo_id'); + $jo = $em->getRepository(JobOrder::class)->find($jo_id); + if ($jo == null) + { + $res->setError(true) + ->setErrorMessage('No job order found'); + return $res->getReturnResponse(); + } + + // get customer + $cust = $this->session->getCustomer(); + if ($cust == null) + { + $res->setError(true) + ->setErrorMessage('No customer information found'); + return $res->getReturnResponse(); + } + + // check that the customer owns the job order + $jo_cust = $jo->getCustomer(); + if ($jo_cust->getID() != $cust->getID()) + { + $res->setError(true) + ->setErrorMessage('Job order was not initiated by customer'); + return $res->getReturnResponse(); + } + + $inv = $jo->getInvoice(); + + // invoice items + $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, + ]; + } + + $data = [ + 'invoice' => [ + 'discount' => $inv->getDiscount(), + 'trade_in' => $inv->getTradeIn(), + 'total_price' => $inv->getTotalPrice(), + 'vat' => $inv->getVat(), + 'items' => $inv_items, + ], + ]; + + + $res->setData($data); + + return $res->getReturnResponse(); + } + public function cancelJobOrder(Request $req, MQTTClient $mclient) { $required_params = [