diff --git a/src/Controller/CustomerTagController.php b/src/Controller/CustomerTagController.php index 680b9ae6..219e828d 100644 --- a/src/Controller/CustomerTagController.php +++ b/src/Controller/CustomerTagController.php @@ -138,9 +138,11 @@ class CustomerTagController extends Controller $em = $this->getDoctrine()->getManager(); $obj = new CustomerTag(); + $tag_details = $req->request->get('tag_details'); + $tag_details_json = json_decode($tag_details, true); $obj->setID($req->request->get('id')) ->setName($req->request->get('name')) - ->setTagDetails($req->request->get('tag_details')); + ->setTagDetails($tag_details_json); // validate $errors = $validator->validate($obj); @@ -206,9 +208,11 @@ class CustomerTagController extends Controller if (empty($obj)) throw $this->createNotFoundException('The item does not exist'); + $tag_details = $req->request->get('tag_details'); + $tag_details_json = json_decode($tag_details, true); $obj->setID($req->request->get('id')) ->setName($req->request->get('name')) - ->setTagDetails($req->request->get('tag_details')); + ->setTagDetails($tag_details_json); // validate $errors = $validator->validate($obj); diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 02f61842..d7c66525 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -638,4 +638,9 @@ class Customer return $str_cust_tags; } + public function getCustomerTagObjects() + { + return $this->customer_tags; + } + } diff --git a/src/Entity/CustomerTag.php b/src/Entity/CustomerTag.php index 6aa506da..43942a76 100644 --- a/src/Entity/CustomerTag.php +++ b/src/Entity/CustomerTag.php @@ -114,6 +114,6 @@ class CustomerTag public function getAllTagDetails() { - return $this->tag_details; + return json_encode($this->tag_details); } } diff --git a/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php b/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php index 2999d622..2c0b1abf 100644 --- a/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php +++ b/src/Service/InvoiceGenerator/ResqInvoiceGenerator.php @@ -19,6 +19,7 @@ use App\Entity\InvoiceItem; use App\Entity\User; use App\Entity\Battery; use App\Entity\Promo; +use App\Entity\Customer; use App\Service\InvoiceGeneratorInterface; @@ -66,6 +67,13 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface $stype = $criteria->getServiceType(); $cv = $criteria->getCustomerVehicle(); $has_coolant = $criteria->hasCoolant(); + + $cust_tag_info = []; + if ($stype == ServiceType::BATTERY_REPLACEMENT_NEW) + { + error_log('calling getCustomerTagInfo'); + $cust_tag_info = $this->getCustomerTagInfo($cv); + } // error_log($stype); switch ($stype) { @@ -81,7 +89,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface $this->processBatteries($total, $criteria, $invoice); $this->processTradeIns($total, $criteria, $invoice); */ - $this->processDiscount($total, $criteria, $invoice); + $this->processDiscount($total, $criteria, $invoice, $cust_tag_info); break; case ServiceType::BATTERY_REPLACEMENT_WARRANTY: @@ -404,45 +412,95 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface } } - protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice) + protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice, $cust_tag_info) { - $promos = $criteria->getPromos(); - if (count($promos) < 1) - 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) + if (empty($cust_tag_info)) { - 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; - } + error_log('empty cust tag'); + $promos = $criteria->getPromos(); + if (count($promos) < 1) + return; - // if discount is higher than 0, display in invoice - if ($discount > 0) + // 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; + } + + // if discount is higher than 0, display in invoice + if ($discount > 0) + { + $item = new InvoiceItem(); + $item->setInvoice($invoice) + ->setTitle('Promo discount') + ->setQuantity(1) + ->setPrice(-1 * $discount); + $invoice->addItem($item); + } + + $total['discount'] = $discount; + $total['total_price'] -= $discount; + + // process + $invoice->setPromo($promo); + } + else { - $item = new InvoiceItem(); - $item->setInvoice($invoice) - ->setTitle('Promo discount') - ->setQuantity(1) - ->setPrice(-1 * $discount); - $invoice->addItem($item); + // since only one promo can only be used, we prioritize the tag promos + // TODO: need to test this for multiple tags + $total_discount_amount = 0; + $total_amount = $total['total_price']; + foreach ($cust_tag_info as $ct_info) + { + // check discount type + $discount_type = ''; + $discount_value = 0; + $discount_amount = 0; + $discounted_total = 0; + if (isset($ct_info['discount_type'])) + $discount_type = $ct_info['discount_type']; + if (isset($ct_info['discount_value'])) + { + $discount_value = $ct_info['discount_value']; + } + + if ($discount_type == 'percent') + { + $discount = round(($discount_value / 100), 2); + $discount_amount = $total_amount * $discount; + $discounted_total = $total_amount - $discount_amount; + } + else + { + // assume fixed amount for this + $discount = $discount_value; + $discounted_total = $total_amount - $discount; + } + + $total_discount_amount += $discounted_total; + + $item = new InvoiceItem(); + $item->setInvoice($invoice) + ->setTitle($ct_info['invoice_display']) + ->setQuantity(1) + ->setPrice(-1 * $total_discount_amount); + $invoice->addItem($item); + } + + $total['discount'] = $total_discount_amount; + $total['total_price'] -= $total_discount_amount; } - - $total['discount'] = $discount; - $total['total_price'] -= $discount; - - // process - $invoice->setPromo($promo); } protected function processJumpstart(&$total, $invoice) @@ -663,6 +721,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface $cv = $criteria->getCustomerVehicle(); $has_coolant = $criteria->hasCoolant(); // error_log($stype); + $cust_tag_info = []; switch ($stype) { case ServiceType::JUMPSTART_TROUBLESHOOT: @@ -677,7 +736,7 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface $this->processBatteries($total, $criteria, $invoice); $this->processTradeIns($total, $criteria, $invoice); */ - $this->processDiscount($total, $criteria, $invoice); + $this->processDiscount($total, $criteria, $invoice, $cust_tag_info); break; case ServiceType::BATTERY_REPLACEMENT_WARRANTY: @@ -719,4 +778,39 @@ class ResqInvoiceGenerator implements InvoiceGeneratorInterface return $invoice; } + + protected function getCustomerTagInfo($cv) + { + $cust_tag_info = []; + // get customer and customer tags using customer vehicle + $customer = $cv->getCustomer(); + $customer_tags = $customer->getCustomerTagObjects(); + if (!empty($customer_tags)) + { + foreach ($customer_tags as $customer_tag) + { + // check tag details + $cust_tag_type = $customer_tag->getTagDetails('type'); + error_log($cust_tag_type); + if (($cust_tag_type != null) && ($cust_tag_type == 'one-time-discount')) + { + // get the discount type and discount amount + $cust_tag_discount_amount = $customer_tag->getTagDetails('discount_amount'); + + $cust_tag_invoice_display = $customer_tag->getTagDetails('invoice_display'); + + error_log($cust_tag_invoice_display); + + $cust_tag_info[] = [ + 'type' => $cust_tag_type, + 'discount_type' => $customer_tag->getTagDetails('discount_type'), + 'discount_amount' => $customer_tag->getTagDetails('discount_amount'), + 'discount_value' => $customer_tag->getTagDetails('discount_value'), + 'invoice_display' => $customer_tag->getTagDetails('invoice_display'), + ]; + } + } + } + return $cust_tag_info; + } } diff --git a/templates/customer-tag/form.html.twig b/templates/customer-tag/form.html.twig index 778378f6..e5e54544 100644 --- a/templates/customer-tag/form.html.twig +++ b/templates/customer-tag/form.html.twig @@ -61,7 +61,7 @@
- JSON format e.g. {type: 'discount', discount_type: 'percent', ...} + JSON format e.g. {"type":"discount", "discount_type":"percent", ...}