From d439a411a8180b0f3d30636d79c21cd34e84148e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 14 Feb 2020 09:40:08 +0000 Subject: [PATCH 1/3] Change computation of discount for CMB. #343 --- src/Controller/JobOrderController.php | 2 +- src/Entity/Invoice.php | 3 +- src/Ramcar/InvoiceCriteria.php | 14 +++++ .../InvoiceGenerator/CMBInvoiceGenerator.php | 54 +++++++------------ .../JobOrderHandler/CMBJobOrderHandler.php | 1 - .../job-order/cmb.form.onestep.html.twig | 15 ++---- 6 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index 15d44e1d..bdd0b2a0 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -736,7 +736,7 @@ class JobOrderController extends Controller */ // TODO: this snippet should be in the invoice generator - $error = $ic->invoicePromo($criteria, $promo_id); + $error = $ic->validateDiscount($criteria, $promo_id); if (!$error) $error = $ic->invoiceBatteries($criteria, $items); diff --git a/src/Entity/Invoice.php b/src/Entity/Invoice.php index 305c7d84..2353b925 100644 --- a/src/Entity/Invoice.php +++ b/src/Entity/Invoice.php @@ -59,7 +59,8 @@ class Invoice */ protected $items; - // total discount (amount, not %) + // total discount (amount, not %) for resq + // for cmb, discount is the percentage /** * @ORM\Column(type="decimal", precision=9, scale=2) */ diff --git a/src/Ramcar/InvoiceCriteria.php b/src/Ramcar/InvoiceCriteria.php index 5ea5e623..a1fb2568 100644 --- a/src/Ramcar/InvoiceCriteria.php +++ b/src/Ramcar/InvoiceCriteria.php @@ -12,6 +12,8 @@ class InvoiceCriteria protected $promos; protected $cv; protected $flag_coolant; + // for discount and other info + protected $meta; // entries are battery and trade-in combos protected $entries; @@ -23,6 +25,7 @@ class InvoiceCriteria $this->entries = []; $this->cv = null; $this->flag_coolant = false; + $this->meta = []; } public function setServiceType($stype) @@ -125,4 +128,15 @@ class InvoiceCriteria { return $this->flag_coolant; } + + public function addMeta($id, $value) + { + $this->meta[$id] = $value; + return $this; + } + + public function getMeta() + { + return $this->meta; + } } diff --git a/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php b/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php index f25348a4..fa13b752 100644 --- a/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php +++ b/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php @@ -17,7 +17,6 @@ use App\Ramcar\FuelType; use App\Entity\Invoice; use App\Entity\InvoiceItem; use App\Entity\Battery; -use App\Entity\Promo; use App\Entity\User; use App\Service\InvoiceGeneratorInterface; @@ -106,10 +105,6 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface // break; } - // TODO: check if any promo is applied - // apply discounts - $promos = $criteria->getPromos(); - // get current user $user = $this->security->getUser(); if ($user != null) @@ -140,7 +135,8 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface $criteria->setServiceType($jo->getServiceType()) ->setCustomerVehicle($jo->getCustomerVehicle()); - $ierror = $this->invoicePromo($criteria, $promo_id); + $discount = $promo_id; + $ierror = $this->validateDiscount($criteria, $discount); if (!$ierror && !empty($invoice_items)) { @@ -224,8 +220,9 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface return 0; } - public function invoicePromo(InvoiceCriteria $criteria, $promo_id) + public function validateDiscount(InvoiceCriteria $criteria, $discount) { + // return error if there's a problem, false otherwise // check service type $stype = $criteria->getServiceType(); @@ -233,18 +230,17 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface return null; - if (empty($promo_id)) + // check if discount is blank or 0 + if ((empty($discount)) || ($discount == 0)) { return false; } - // check if this is a valid promo - $promo = $this->em->getRepository(Promo::class)->find($promo_id); + // check if discount is greater than 50 + if ($discount > 50) + return 'Invalid discount specified'; - if (empty($promo)) - return 'Invalid promo specified.'; - - $criteria->addPromo($promo); + $criteria->addMeta('discount', $discount); return false; } @@ -389,33 +385,23 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice) { - $promos = $criteria->getPromos(); - if (count($promos) < 1) + $rate = 0; + $meta = $criteria->getMeta(); + if (isset($meta['discount'])) + $rate = $meta['discount']; + else return; - // NOTE: only get first promo because only one is applicable anyway - $promo = $promos[0]; - - $rate = $promo->getDiscountRate(); - $apply_to = $promo->getDiscountApply(); - - switch ($apply_to) - { - case DiscountApply::SRP: - $discount = round($total['sell_price'] * $rate, 2); - break; - case DiscountApply::OPL: - // $discount = round($total['sell_price'] * 0.6 / 0.7 * $rate, 2); - $discount = round($total['sell_price'] * (1 - 1.5 / 0.7 * $rate), 2); - break; - } + // compute discount + $rate = $rate * 0.01; + $discount = round($total['sell_price'] * $rate, 2); // if discount is higher than 0, display in invoice if ($discount > 0) { $item = new InvoiceItem(); $item->setInvoice($invoice) - ->setTitle('Promo discount') + ->setTitle('Discount') ->setQuantity(1) ->setPrice(-1 * $discount); $invoice->addItem($item); @@ -425,7 +411,7 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface $total['total_price'] -= $discount; // process - $invoice->setPromo($promo); + $invoice->setDiscount($discount); } protected function processJumpstart(&$total, $invoice) diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index a2727fea..ee83970f 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -2670,7 +2670,6 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $params['warranty_classes'] = CMBWarrantyClass::getCollection(); $params['modes_of_payment'] = CMBModeOfPayment::getCollection(); $params['statuses'] = JOStatus::getCollection(); - $params['discount_apply'] = DiscountApply::getCollection(); $params['trade_in_types'] = CMBTradeInType::getCollection(); $params['facilitated_types'] = FacilitatedType::getCollection(); $params['facilitated_hubs'] = $fac_hubs; diff --git a/templates/job-order/cmb.form.onestep.html.twig b/templates/job-order/cmb.form.onestep.html.twig index e0714676..b91c0a2c 100644 --- a/templates/job-order/cmb.form.onestep.html.twig +++ b/templates/job-order/cmb.form.onestep.html.twig @@ -500,22 +500,13 @@
{% if ftags.invoice_edit %} - + {% else %} - + {% endif %}
-
- - -
-
+
From a15b45173c660f0d33a10490e18d0068c44ad50e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 17 Feb 2020 01:02:41 +0000 Subject: [PATCH 2/3] Fix discount computation. #343 --- src/Entity/Invoice.php | 3 +-- src/Service/InvoiceGenerator/CMBInvoiceGenerator.php | 8 ++------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Entity/Invoice.php b/src/Entity/Invoice.php index 2353b925..305c7d84 100644 --- a/src/Entity/Invoice.php +++ b/src/Entity/Invoice.php @@ -59,8 +59,7 @@ class Invoice */ protected $items; - // total discount (amount, not %) for resq - // for cmb, discount is the percentage + // total discount (amount, not %) /** * @ORM\Column(type="decimal", precision=9, scale=2) */ diff --git a/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php b/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php index fa13b752..f1563d63 100644 --- a/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php +++ b/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php @@ -385,17 +385,13 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice) { - $rate = 0; + $discount = 0; $meta = $criteria->getMeta(); if (isset($meta['discount'])) - $rate = $meta['discount']; + $discount = $meta['discount']; else return; - // compute discount - $rate = $rate * 0.01; - $discount = round($total['sell_price'] * $rate, 2); - // if discount is higher than 0, display in invoice if ($discount > 0) { From 984b0428d0538d4e1a35a76a72fe9549ac0d26a3 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 17 Feb 2020 06:19:09 +0000 Subject: [PATCH 3/3] Fix for cmb to retain battery in invoice when editing JO. #343 --- src/Controller/JobOrderController.php | 4 ++- src/Entity/JobOrder.php | 19 ++++++++++++ src/Ramcar/InvoiceCriteria.php | 13 ++++---- .../InvoiceGenerator/CMBInvoiceGenerator.php | 14 +++------ .../InvoiceGenerator/ResqInvoiceGenerator.php | 1 + .../JobOrderHandler/CMBJobOrderHandler.php | 21 ++++++++++++- .../job-order/cmb.form.onestep.html.twig | 15 ++++++++++ templates/job-order/cmb.form.walkin.html.twig | 30 +++++++++++-------- 8 files changed, 86 insertions(+), 31 deletions(-) diff --git a/src/Controller/JobOrderController.php b/src/Controller/JobOrderController.php index bdd0b2a0..d50471fe 100644 --- a/src/Controller/JobOrderController.php +++ b/src/Controller/JobOrderController.php @@ -605,7 +605,7 @@ class JobOrderController extends Controller * @Menu(selected="jo_all") */ public function allForm($id, JobOrderHandlerInterface $jo_handler, - GISManagerInterface $gis) + GISManagerInterface $gis, EntityManagerInterface $em) { $this->denyAccessUnlessGranted('jo_all.list', null, 'No access.'); @@ -618,6 +618,8 @@ class JobOrderController extends Controller throw $this->createNotFoundException($e->getMessage()); } + $params['vmfgs'] = $em->getRepository(VehicleManufacturer::class)->findAll(); + $params['vmakes'] = $em->getRepository(Vehicle::class)->findAll(); $params['return_url'] = $this->generateUrl('jo_all'); $params['submit_url'] = ''; $params['map_js_file'] = $gis->getJSJOFile(); diff --git a/src/Entity/JobOrder.php b/src/Entity/JobOrder.php index 36834cc1..08a1f15a 100644 --- a/src/Entity/JobOrder.php +++ b/src/Entity/JobOrder.php @@ -280,6 +280,12 @@ class JobOrder */ protected $hub_rejections; + // meta + /** + * @ORM\Column(type="json") + */ + protected $meta; + public function __construct() { $this->date_create = new DateTime(); @@ -297,6 +303,8 @@ class JobOrder $this->trade_in_type = null; $this->flag_rider_rating = false; $this->flag_coolant = false; + + $this->meta = []; } public function getID() @@ -802,4 +810,15 @@ class JobOrder { return $this->hub_rejections; } + + public function addMeta($id, $value) + { + $this->meta[$id] = $value; + return $this; + } + + public function getMeta($id) + { + return $this->meta[$id]; + } } diff --git a/src/Ramcar/InvoiceCriteria.php b/src/Ramcar/InvoiceCriteria.php index a1fb2568..49290e33 100644 --- a/src/Ramcar/InvoiceCriteria.php +++ b/src/Ramcar/InvoiceCriteria.php @@ -12,8 +12,7 @@ class InvoiceCriteria protected $promos; protected $cv; protected $flag_coolant; - // for discount and other info - protected $meta; + protected $discount; // entries are battery and trade-in combos protected $entries; @@ -25,7 +24,7 @@ class InvoiceCriteria $this->entries = []; $this->cv = null; $this->flag_coolant = false; - $this->meta = []; + $this->discount = 0; } public function setServiceType($stype) @@ -129,14 +128,14 @@ class InvoiceCriteria return $this->flag_coolant; } - public function addMeta($id, $value) + public function setDiscount($discount) { - $this->meta[$id] = $value; + $this->discount = $discount; return $this; } - public function getMeta() + public function getDiscount() { - return $this->meta; + return $this->discount; } } diff --git a/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php b/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php index f1563d63..73f4524e 100644 --- a/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php +++ b/src/Service/InvoiceGenerator/CMBInvoiceGenerator.php @@ -229,18 +229,17 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface if ($stype != CMBServiceType::BATTERY_REPLACEMENT_NEW) return null; - // check if discount is blank or 0 if ((empty($discount)) || ($discount == 0)) { return false; } - // check if discount is greater than 50 - if ($discount > 50) + // check if discount is greater than 50 or negative number + if (($discount > 50) || ($discount < 0)) return 'Invalid discount specified'; - $criteria->addMeta('discount', $discount); + $criteria->setDiscount($discount); return false; } @@ -385,12 +384,7 @@ class CMBInvoiceGenerator implements InvoiceGeneratorInterface protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice) { - $discount = 0; - $meta = $criteria->getMeta(); - if (isset($meta['discount'])) - $discount = $meta['discount']; - else - return; + $discount = $criteria->getDiscount(); // if discount is higher than 0, display in invoice if ($discount > 0) diff --git a/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php b/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php index c5e9c81c..f9aad46a 100644 --- a/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php +++ b/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php @@ -18,6 +18,7 @@ use App\Entity\Invoice; use App\Entity\InvoiceItem; use App\Entity\User; use App\Entity\Battery; +use App\Entity\Promo; use App\Service\InvoiceGeneratorInterface; diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index ee83970f..cfdab814 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -522,6 +522,13 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface } } + // get discount and set to meta + $discount = $req->request->get('invoice_promo'); + + // check if discount is greater than 50 or negative number + if (($discount > 50) || ($discount < 0)) + $error_array['invoice_promo'] = 'Invalid discount specified'; + if (empty($error_array)) { // get current user @@ -551,7 +558,9 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface ->setHub($hub) ->setRider($rider); - // check if user is null, meaning call to create came from API + $jo->addMeta('discount', $discount); + + // check if user is null, meaning call to create came from API if ($user != null) { $jo->setCreatedBy($user); @@ -2488,6 +2497,13 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $hub_coordinates = $hub->getCoordinates(); } + // get discount and set to meta + $discount = $req->request->get('invoice_promo'); + + // check if discount is greater than 50 or negative number + if (($discount > 50) || ($discount < 0)) + $error_array['invoice_promo'] = 'Invalid discount specified'; + if (empty($error_array)) { // get current user @@ -2513,6 +2529,8 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface ->setCoordinates($hub_coordinates) ->setHub($hub); + $jo->addMeta('discount', $discount); + // check if user is null, meaning call to create came from API if ($user != null) { @@ -2670,6 +2688,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $params['warranty_classes'] = CMBWarrantyClass::getCollection(); $params['modes_of_payment'] = CMBModeOfPayment::getCollection(); $params['statuses'] = JOStatus::getCollection(); + $params['discount_apply'] = DiscountApply::getCollection(); $params['trade_in_types'] = CMBTradeInType::getCollection(); $params['facilitated_types'] = FacilitatedType::getCollection(); $params['facilitated_hubs'] = $fac_hubs; diff --git a/templates/job-order/cmb.form.onestep.html.twig b/templates/job-order/cmb.form.onestep.html.twig index b91c0a2c..34fe7af4 100644 --- a/templates/job-order/cmb.form.onestep.html.twig +++ b/templates/job-order/cmb.form.onestep.html.twig @@ -1268,6 +1268,21 @@ $(function() { var invoiceItems = []; + // populate invoiceItems if editing so that we don't lose the battery + {% if mode in ['open-edit', 'onestep-edit', 'walk-in-edit'] %} + {% if (obj.getInvoice and obj.getInvoice.getItems|length > 0) %} + {% for item in obj.getInvoice.getItems %} + {% if item.getBattery() %} + invoiceItems.push({ + battery: {{ item.getBattery().getID() }}, + quantity: {{ item.getQuantity() }}, + trade_in: {{ obj.getInvoice().getTradeIn }}, + }); + {% endif %} + {% endfor %} + {% endif %} + {% endif %} + // add to invoice $("#btn-add-to-invoice").click(function() { var bmfg = $("#invoice-bmfg").val(); diff --git a/templates/job-order/cmb.form.walkin.html.twig b/templates/job-order/cmb.form.walkin.html.twig index 1e3d8f9b..8f764956 100644 --- a/templates/job-order/cmb.form.walkin.html.twig +++ b/templates/job-order/cmb.form.walkin.html.twig @@ -395,22 +395,13 @@
{% if ftags.invoice_edit %} - + {% else %} - + {% endif %}
-
- - -
-
+
@@ -892,6 +883,21 @@ var vdata = false; var invoiceItems = []; + // populate invoiceItems if editing so that we don't lose the battery + {% if mode in ['open-edit', 'onestep-edit', 'walk-in-edit'] %} + {% if (obj.getInvoice and obj.getInvoice.getItems|length > 0) %} + {% for item in obj.getInvoice.getItems %} + {% if item.getBattery() %} + invoiceItems.push({ + battery: {{ item.getBattery().getID() }}, + quantity: {{ item.getQuantity() }}, + trade_in: {{ obj.getInvoice().getTradeIn }}, + }); + {% endif %} + {% endfor %} + {% endif %} + {% endif %} + // add to invoice $("#btn-add-to-invoice").click(function() { var bmfg = $("#invoice-bmfg").val();