From 0ced0735a3b25cba3dfee14ec9f3b0277eb16ef7 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 5 Apr 2018 13:53:30 +0800 Subject: [PATCH] Save new battery on fulfill jo action #71 --- config/routes/vehicle.yaml | 5 ++ .../BatteryManufacturerController.php | 41 ----------- src/Controller/CustomerController.php | 1 + src/Controller/JobOrderController.php | 56 +++++++++++++++ src/Controller/VehicleController.php | 71 +++++++++++++++++++ templates/customer/form.html.twig | 11 +-- templates/job-order/form.html.twig | 2 +- 7 files changed, 141 insertions(+), 46 deletions(-) diff --git a/config/routes/vehicle.yaml b/config/routes/vehicle.yaml index 46762c0c..9f9e48e6 100644 --- a/config/routes/vehicle.yaml +++ b/config/routes/vehicle.yaml @@ -34,6 +34,11 @@ vehicle_delete: controller: App\Controller\VehicleController::destroy methods: [DELETE] +vehicle_batteries: + path: /vehicle/batteries + controller: App\Controller\VehicleController::getBatteries + methods: [POST] + # vehicle manufacturers vmfg_list: diff --git a/src/Controller/BatteryManufacturerController.php b/src/Controller/BatteryManufacturerController.php index 55419c6a..1a78605a 100644 --- a/src/Controller/BatteryManufacturerController.php +++ b/src/Controller/BatteryManufacturerController.php @@ -277,43 +277,10 @@ class BatteryManufacturerController extends BaseController // get row data $em = $this->getDoctrine()->getManager(); - $obj = $em->getRepository(BatteryManufacturer::class)->find($req->request->get('id')); - $vobj = $em->getRepository(Vehicle::class)->find($req->request->get('vehicle_id')); $all_batts = $em->getRepository(Battery::class)->findAll(); - if (empty($obj) || empty($vobj)) - throw $this->createNotFoundException('The item does not exist'); - - // build batteries array - $batteries = []; - $battery_index = []; - - // get compatible batteries from selected manufacturer - foreach ($vobj->getBatteries() as $battery) - { - if ($battery->getManufacturer()->getID() == $obj->getID()) - { - $batteries[] = [ - 'id' => $battery->getID(), - 'mfg_name' => $battery->getManufacturer()->getName(), - 'model_name' => $battery->getModel()->getName(), - 'size_name' => $battery->getSize()->getName(), - 'prod_code' => $battery->getProductCode(), - 'sell_price' => $battery->getSellingPrice(), - 'warr_private' => $battery->getWarrantyPrivate(), - 'warr_commercial' => $battery->getWarrantyCommercial(), - ]; - $battery_index[$battery->getID()] = 1; - } - } - - // add all other batteries, because they want options foreach ($all_batts as $battery) { - // if we already listed it - if (isset($battery_index[$battery->getID()])) - continue; - $batteries[] = [ 'id' => $battery->getID(), 'mfg_name' => $battery->getManufacturer()->getName(), @@ -326,14 +293,6 @@ class BatteryManufacturerController extends BaseController ]; } - /* - // NOTE: no need to order by price for control center / only for app - // order by price - usort($batteries, function ($a, $b) { - return -($a['sell_price'] <=> $b['sell_price']); - }); - */ - // response return $this->json([ 'data' => $batteries diff --git a/src/Controller/CustomerController.php b/src/Controller/CustomerController.php index ccf79fbc..74519bdf 100644 --- a/src/Controller/CustomerController.php +++ b/src/Controller/CustomerController.php @@ -155,6 +155,7 @@ class CustomerController extends BaseController $params['status_conditions'] = VehicleStatusCondition::getCollection(); $params['years'] = $this->generateYearOptions(); + $params['batteries'] = $em->getRepository(Battery::class)->findAll(); } public function addForm() diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index 7bec3f1f..9b168908 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -42,6 +42,7 @@ use CrEOF\Spatial\PHP\Types\Geometry\Point; use Mosquitto\Client as MosquittoClient; use DateTime; +use DateInterval; class JobOrderController extends BaseController { @@ -989,6 +990,58 @@ class JobOrderController extends BaseController return $this->render('job-order/form.html.twig', $params); } + protected function updateVehicleBattery(JobOrder $jo) + { + // check if new battery + if ($jo->getServiceType() != ServiceType::BATTERY_REPLACEMENT_NEW) + return; + + // customer vehicle + $cv = $jo->getCustomerVehicle(); + if ($cv == null) + return; + + // invoice + $invoice = $jo->getInvoice(); + if ($invoice == null) + return; + + // invoice items + $items = $invoice->getItems(); + if (count($items) <= 0) + return; + + // get first battery from invoice + $battery = null; + foreach ($items as $item) + { + $battery = $item->getBattery(); + if ($battery != null) + break; + } + + // no battery in order + if ($battery == null) + return; + + // warranty expiration + $warr = $jo->getWarrantyClass(); + if ($warr == WarrantyClass::WTY_PRIVATE) + $warr_months = $battery->getWarrantyPrivate(); + else if ($warr == WarrantyClass::WTY_COMMERCIAL) + $warr_months = $battery->getWarrantyCommercial(); + else if ($warr == WarrantyClass::WTY_TNV) + $warr_months = 12; + + $warr_date = new DateTime(); + $warr_date->add(new DateInterval('P' . $warr_months . 'M')); + + // update customer vehicle battery + $cv->setCurrentBattery($battery) + ->setHasMotoliteBattery(true) + ->setWarrantyExpiration($warr_date); + } + public function fulfillmentSubmit(Request $req, ValidatorInterface $validator, $id) { $this->denyAccessUnlessGranted('jo_fulfill.list', null, 'No access.'); @@ -1057,6 +1110,9 @@ class JobOrderController extends BaseController ->setJobOrder($obj); $em->persist($event); + // save to customer vehicle battery record + $this->updateVehicleBattery($obj); + // validated! save the entity $em->flush(); diff --git a/src/Controller/VehicleController.php b/src/Controller/VehicleController.php index 27339d71..dada947b 100644 --- a/src/Controller/VehicleController.php +++ b/src/Controller/VehicleController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\Ramcar\BaseController; use App\Entity\Vehicle; use App\Entity\VehicleManufacturer; +use App\Entity\Battery; use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; @@ -310,4 +311,74 @@ class VehicleController extends BaseController ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); } } + + public function getBatteries(Request $req) + { + /* + if (!$this->isGranted('customer.add') && !$this->isGranted('customer.update')) { + $exception = $this->createAccessDeniedException('No access.'); + throw $exception; + } + */ + + // get row data + $em = $this->getDoctrine()->getManager(); + $vobj = $em->getRepository(Vehicle::class)->find($req->request->get('vehicle_id')); + $all_batts = $em->getRepository(Battery::class)->findAll(); + + if (empty($vobj)) + throw $this->createNotFoundException('The item does not exist'); + + // build batteries array + $batteries = []; + $battery_index = []; + + // get compatible batteries from selected manufacturer + foreach ($vobj->getBatteries() as $battery) + { + $batteries[] = [ + 'id' => $battery->getID(), + 'mfg_name' => $battery->getManufacturer()->getName(), + 'model_name' => $battery->getModel()->getName(), + 'size_name' => $battery->getSize()->getName(), + 'prod_code' => $battery->getProductCode(), + 'sell_price' => $battery->getSellingPrice(), + 'warr_private' => $battery->getWarrantyPrivate(), + 'warr_commercial' => $battery->getWarrantyCommercial(), + ]; + $battery_index[$battery->getID()] = 1; + } + + // add all other batteries, because they want options + foreach ($all_batts as $battery) + { + // if we already listed it + if (isset($battery_index[$battery->getID()])) + continue; + + $batteries[] = [ + 'id' => $battery->getID(), + 'mfg_name' => $battery->getManufacturer()->getName(), + 'model_name' => $battery->getModel()->getName(), + 'size_name' => $battery->getSize()->getName(), + 'prod_code' => $battery->getProductCode(), + 'sell_price' => $battery->getSellingPrice(), + 'warr_private' => $battery->getWarrantyPrivate(), + 'warr_commercial' => $battery->getWarrantyCommercial(), + ]; + } + + /* + // NOTE: no need to order by price for control center / only for app + // order by price + usort($batteries, function ($a, $b) { + return -($a['sell_price'] <=> $b['sell_price']); + }); + */ + + // response + return $this->json([ + 'data' => $batteries + ]); + } } diff --git a/templates/customer/form.html.twig b/templates/customer/form.html.twig index c982b664..e6d2fd09 100644 --- a/templates/customer/form.html.twig +++ b/templates/customer/form.html.twig @@ -375,15 +375,18 @@
- + + {% for batt in batteries %} + + {% endfor %}
@@ -748,6 +751,7 @@ // set entity ids $("#vehicle").data('id', row.vehicle); $("#battery").data('id', row.battery); + $("#battery").val(row.battery); $("#cv-id").val(row.id); // set select box values @@ -755,7 +759,6 @@ if (row.battery) { $("#flag-motolite-battery").prop('checked', true).change(); - $("#bmfg").val(row.bmfg).change(); } $("#flag-active").prop('checked', row.flag_active); diff --git a/templates/job-order/form.html.twig b/templates/job-order/form.html.twig index a4594b73..fa9f1024 100644 --- a/templates/job-order/form.html.twig +++ b/templates/job-order/form.html.twig @@ -1124,7 +1124,7 @@ $(function() { // get vehicles for this manufacturer $.ajax({ method: "POST", - url: "{{ url('bmfg_batteries') }}", + url: "{{ url('vehicle_batteries') }}", data: { id: id, vehicle_id: vdata.vehicle.id