From f79aa12fbc2dac8275cdb0886120e50c666e5342 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Mon, 5 Feb 2018 01:43:31 +0800 Subject: [PATCH] Add discount support to invoice generation --- src/Service/InvoiceCreator.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Service/InvoiceCreator.php b/src/Service/InvoiceCreator.php index 80ff0414..d101e802 100644 --- a/src/Service/InvoiceCreator.php +++ b/src/Service/InvoiceCreator.php @@ -4,6 +4,7 @@ namespace App\Service; use App\Ramcar\InvoiceCriteria; use App\Ramcar\TradeInType; +use App\Ramcar\DiscountApply; use App\Entity\Invoice; use App\Entity\InvoiceItem; @@ -126,6 +127,28 @@ class InvoiceCreator protected function processDiscount(&$total, InvoiceCriteria $criteria, Invoice $invoice) { + $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) + { + 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); + break; + } + + $total['discount'] = $discount; + $total['total_price'] -= $discount; } public function processCriteria(InvoiceCriteria $criteria) @@ -138,6 +161,7 @@ class InvoiceCreator 'vat_ex_price' => 0.0, 'ti_rate' => 0.0, 'total_price' => 0.0, + 'discount' => 0.0, ]; $this->processBatteries($total, $criteria, $invoice); @@ -151,6 +175,7 @@ class InvoiceCreator $invoice->setTotalPrice($total['total_price']) ->setVATExclusivePrice($total['vat_ex_price']) ->setVAT($total['vat']) + ->setDiscount($total['discount']) ->setTradeIn($total['ti_rate']);