From b4bcc06e428618434831c82f38b094854d46b89b Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 4 Jul 2023 06:05:34 -0400 Subject: [PATCH] Modify the invoice manager to be more generic. #744 --- src/Command/TestInvoiceManagerCommand.php | 303 +++--------------- src/Invoice/BatteryReplacementWarranty.php | 67 ---- src/Invoice/BatterySales.php | 77 ----- src/Invoice/ServiceType.php | 30 -- src/Invoice/Tax.php | 91 ------ src/InvoiceInterface.php | 12 - .../BatteryReplacementWarranty.php | 119 +++++++ src/InvoiceRule/BatterySales.php | 122 +++++++ src/{Invoice => InvoiceRule}/DiscountType.php | 65 +++- src/{Invoice => InvoiceRule}/Fuel.php | 61 ++-- src/{Invoice => InvoiceRule}/Jumpstart.php | 42 +-- .../JumpstartWarranty.php | 42 +-- src/{Invoice => InvoiceRule}/Overheat.php | 52 +-- .../PostRecharged.php | 40 ++- .../PostReplacement.php | 40 ++- src/InvoiceRule/Tax.php | 110 +++++++ src/{Invoice => InvoiceRule}/TireRepair.php | 40 ++- src/{Invoice => InvoiceRule}/TradeIn.php | 42 +-- src/InvoiceRuleInterface.php | 15 + src/Service/InvoiceManager.php | 284 +++++----------- 20 files changed, 744 insertions(+), 910 deletions(-) delete mode 100644 src/Invoice/BatteryReplacementWarranty.php delete mode 100644 src/Invoice/BatterySales.php delete mode 100644 src/Invoice/ServiceType.php delete mode 100644 src/Invoice/Tax.php delete mode 100644 src/InvoiceInterface.php create mode 100644 src/InvoiceRule/BatteryReplacementWarranty.php create mode 100644 src/InvoiceRule/BatterySales.php rename src/{Invoice => InvoiceRule}/DiscountType.php (57%) rename src/{Invoice => InvoiceRule}/Fuel.php (69%) rename src/{Invoice => InvoiceRule}/Jumpstart.php (59%) rename src/{Invoice => InvoiceRule}/JumpstartWarranty.php (58%) rename src/{Invoice => InvoiceRule}/Overheat.php (65%) rename src/{Invoice => InvoiceRule}/PostRecharged.php (56%) rename src/{Invoice => InvoiceRule}/PostReplacement.php (56%) create mode 100644 src/InvoiceRule/Tax.php rename src/{Invoice => InvoiceRule}/TireRepair.php (62%) rename src/{Invoice => InvoiceRule}/TradeIn.php (74%) create mode 100644 src/InvoiceRuleInterface.php diff --git a/src/Command/TestInvoiceManagerCommand.php b/src/Command/TestInvoiceManagerCommand.php index 1de193ad..f47b6b52 100644 --- a/src/Command/TestInvoiceManagerCommand.php +++ b/src/Command/TestInvoiceManagerCommand.php @@ -45,6 +45,7 @@ class TestInvoiceManagerCommand extends Command $this->testBatterySalesNoTradeInNoDiscountWithTax(); $this->testBatterySalesQuantityNoTradeInNoDiscountWithTax(); + /* // battery sales without tax $this->testBatterySalesNoTradeInNoDiscountWithoutTax(); $this->testBatterySalesQuantityNoTradeInNoDiscountWithoutTax(); @@ -153,7 +154,7 @@ class TestInvoiceManagerCommand extends Command $this->testGenerateDraftInvoiceBatterySalesNoPromo(); // test generateInvoiceInvoice call from ajax call - $this->testGenerateInvoice(); + $this->testGenerateInvoice(); */ return 0; } @@ -172,11 +173,7 @@ class TestInvoiceManagerCommand extends Command $criteria->addEntry($battery, null, 1); $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); // error_log(print_r(json_encode($invoice_data), true)); @@ -207,11 +204,7 @@ class TestInvoiceManagerCommand extends Command $criteria->addEntry($battery, null, 2); $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); // error_log(print_r(json_encode($invoice_data), true)); @@ -242,11 +235,7 @@ class TestInvoiceManagerCommand extends Command $criteria->addEntry($battery, null, 1); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); // error_log(print_r(json_encode($invoice_data), true)); @@ -276,11 +265,7 @@ class TestInvoiceManagerCommand extends Command $criteria->addEntry($battery, null, 2); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); // error_log(print_r(json_encode($invoice_data), true)); @@ -316,11 +301,7 @@ class TestInvoiceManagerCommand extends Command $criteria->addEntry($battery, 'premium', 1); $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -354,11 +335,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -392,11 +369,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -433,11 +406,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -474,11 +443,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -515,11 +480,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -556,11 +517,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -593,11 +550,7 @@ class TestInvoiceManagerCommand extends Command // add battery for trade in $criteria->addEntry($battery, 'premium', 1); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -629,11 +582,7 @@ class TestInvoiceManagerCommand extends Command // add battery for trade in $criteria->addEntry($battery, 'motolite', 1); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -665,11 +614,7 @@ class TestInvoiceManagerCommand extends Command // add battery for trade in $criteria->addEntry($battery, 'other', 1); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -704,11 +649,7 @@ class TestInvoiceManagerCommand extends Command // add battery for trade in $criteria->addEntry($trade_battery, 'premium', 1); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -743,11 +684,7 @@ class TestInvoiceManagerCommand extends Command // add battery for trade in $criteria->addEntry($trade_battery, 'motolite', 1); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -782,11 +719,7 @@ class TestInvoiceManagerCommand extends Command // add battery for trade in $criteria->addEntry($trade_battery, 'other', 1); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -821,11 +754,7 @@ class TestInvoiceManagerCommand extends Command // add battery for trade in $criteria->addEntry($trade_battery, 'other', 3); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -861,11 +790,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); // error_log(print_r(json_encode($invoice_data), true)); @@ -904,11 +829,7 @@ class TestInvoiceManagerCommand extends Command $criteria->addPromo($promo); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); // error_log(print_r(json_encode($invoice_data), true)); @@ -952,11 +873,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); // error_log(print_r(json_encode($invoice_data), true)); @@ -1003,11 +920,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1044,11 +957,7 @@ class TestInvoiceManagerCommand extends Command $criteria->addPromo($promo); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); // error_log(print_r(json_encode($invoice_data), true)); @@ -1093,11 +1002,7 @@ class TestInvoiceManagerCommand extends Command $criteria->addPromo($promo); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1128,11 +1033,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1161,11 +1062,7 @@ class TestInvoiceManagerCommand extends Command $criteria->addEntry($battery, null, 1); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1197,11 +1094,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1232,11 +1125,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1267,11 +1156,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1302,11 +1187,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1336,11 +1217,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setCustomerVehicle($cv); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1369,11 +1246,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setCustomerVehicle($cv); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1402,11 +1275,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setCustomerVehicle($cv); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1435,11 +1304,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setCustomerVehicle($cv); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1465,11 +1330,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1493,11 +1354,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setServiceType(ServiceType::JUMPSTART_TROUBLESHOOT); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1523,11 +1380,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1551,11 +1404,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setServiceType(ServiceType::JUMPSTART_WARRANTY); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1587,11 +1436,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setHasCoolant(); $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1621,11 +1466,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setCustomerVehicle($cv); $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1656,11 +1497,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setCustomerVehicle($cv); $criteria->setHasCoolant(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1689,11 +1526,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setCustomerVehicle($cv); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1719,11 +1552,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1747,11 +1576,7 @@ class TestInvoiceManagerCommand extends Command // TEST SCENARIO: post recharged with tax $criteria->setServiceType(ServiceType::POST_RECHARGED); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1777,11 +1602,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1807,11 +1628,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1843,11 +1660,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1878,11 +1691,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setIsTaxable(); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1912,11 +1721,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setCustomerVehicle($cv); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { @@ -1945,11 +1750,7 @@ class TestInvoiceManagerCommand extends Command $criteria->setCustomerVehicle($cv); - $rules = $this->inv_manager->check($criteria); - - // error_log(print_r($rules, true)); - - $invoice_data = $this->inv_manager->compute($criteria, $rules); + $invoice_data = $this->inv_manager->compute($criteria); foreach ($invoice_data as $data) { diff --git a/src/Invoice/BatteryReplacementWarranty.php b/src/Invoice/BatteryReplacementWarranty.php deleted file mode 100644 index 108016ac..00000000 --- a/src/Invoice/BatteryReplacementWarranty.php +++ /dev/null @@ -1,67 +0,0 @@ -getID() == $criteria->getServiceType()) - return true; - - return false; - } - - public function getTemplate() - { - return 'invoice/battery.html.twig'; - } - - public function getID() - { - return 'battery_warranty'; - } - - public function compute($criteria, $stype_fees, &$total) - { - $stype = $criteria->getServiceType(); - - $items = []; - if ($stype == $this->getID()) - { - // get the entries - $entries = $criteria->getEntries(); - - $stype_fee_id = $this->getID() . '_fee'; - foreach($entries as $entry) - { - $batt = $entry['battery']; - $qty = 1; - $price = $stype_fees[$stype_fee_id]; - - $items[] = [ - 'service_type' => $this->getID(), - 'battery' => $batt, - 'qty' => $qty, - 'title' => $this->getTitle($batt), - 'price' => $price, - ]; - - $qty_price = bcmul($price, $qty, 2); - $total['total_price'] = bcadd($total['total_price'], $qty_price, 2); - } - } - - return $items; - } - - protected function getTitle($battery) - { - $title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName() . ' - Service Unit'; - - return $title; - } -} - diff --git a/src/Invoice/BatterySales.php b/src/Invoice/BatterySales.php deleted file mode 100644 index e1b94282..00000000 --- a/src/Invoice/BatterySales.php +++ /dev/null @@ -1,77 +0,0 @@ -getID() == $criteria->getServiceType()) - return true; - - return false; - } - - public function getTemplate() - { - return 'invoice/battery.html.twig'; - } - - public function getID() - { - return 'battery_new'; - } - - public function compute($criteria, $stype_fees, &$total) - { - $stype = $criteria->getServiceType(); - - $items = []; - if ($stype == $this->getID()) - { - // get the entries - $entries = $criteria->getEntries(); - foreach($entries as $entry) - { - $batt = $entry['battery']; - $qty = $entry['qty']; - $trade_in = null; - - if (isset($entry['trade_in'])) - $trade_in = $entry['trade_in']; - - $size = $batt->getSize(); - - if ($trade_in == null) - { - // battery purchase - $price = $batt->getSellingPrice(); - - $items[] = [ - 'service_type' => $this->getID(), - 'battery' => $batt, - 'qty' => $qty, - 'title' => $this->getTitle($batt), - 'price' => $price, - ]; - - $qty_price = bcmul($price, $qty, 2); - - $total['sell_price'] = bcadd($total['sell_price'], $qty_price, 2); - $total['total_price'] = bcadd($total['total_price'], $qty_price, 2); - } - } - } - - return $items; - } - - protected function getTitle($battery) - { - $title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName(); - - return $title; - } -} diff --git a/src/Invoice/ServiceType.php b/src/Invoice/ServiceType.php deleted file mode 100644 index 120ac0a7..00000000 --- a/src/Invoice/ServiceType.php +++ /dev/null @@ -1,30 +0,0 @@ -getServiceType() != null) - return true; - - return false; - } - - public function getTemplate() - { - return 'invoice/service_type.html.twig'; - } - - public function getID() - { - return 'service_type'; - } - - public function compute($criteria, $stype_fees) - { - } -} diff --git a/src/Invoice/Tax.php b/src/Invoice/Tax.php deleted file mode 100644 index 45172286..00000000 --- a/src/Invoice/Tax.php +++ /dev/null @@ -1,91 +0,0 @@ -isTaxable())) - return true; - - return false; - - } - - public function getTemplate() - { - return 'invoice/tax.html.twig'; - } - - public function getID() - { - return 'tax'; - } - - public function compute($tax_rate, $items, &$total) - { - $is_battery_sales = false; - $total_price = 0; - - foreach ($items as $item) - { - $price = $item['price']; - $qty = $item['qty']; - - if (isset($item['service_type'])) - { - // battery sales is a special case since vat is computed per battery - if ($item['service_type'] == 'battery_new') - { - $is_battery_sales = true; - - // battery purchase - if (isset($item['battery'])) - { - // battery purchase - $vat = $this->getTaxAmount($price, $tax_rate); - - $qty_price = bcmul($price, $qty, 2); - $qty_vat = bcmul($vat, $qty, 2); - $price_minus_vat = bcsub($price, $vat, 2); - $qty_price_minus_vat = bcmul($price_minus_vat, $qty, 2); - - $total['vat'] = bcadd($total['vat'], $qty_vat, 2); - $total['vat_ex_price'] = bcadd($total['vat_ex_price'], $qty_price_minus_vat, 2); - } - } - } - } - - // compute VAT after adding all item costs, if service type is not battery sales - if (!$is_battery_sales) - { - $total_price = $total['total_price']; - - $vat_ex_price = $this->getTaxExclusivePrice($total_price, $tax_rate); - $vat = bcsub($total_price, $vat_ex_price, 2); - - $total['vat_ex_price'] = $vat_ex_price; - $total['vat'] = $vat; - } - } - - protected function getTaxAmount($price, $tax_rate) - { - $vat_ex_price = $this->getTaxExclusivePrice($price, $tax_rate); - $tax_amount = bcsub($price, $vat_ex_price, 2); - - return $tax_amount; - } - - protected function getTaxExclusivePrice($price, $tax_rate) - { - $tax_ex_price = bcdiv($price, bcadd(1, $tax_rate, 2), 2); - return $tax_ex_price; - } - -} - diff --git a/src/InvoiceInterface.php b/src/InvoiceInterface.php deleted file mode 100644 index 1812fd83..00000000 --- a/src/InvoiceInterface.php +++ /dev/null @@ -1,12 +0,0 @@ -em = $em; + } + + public function getID() + { + return 'battery_warranty'; + } + + public function compute($criteria, &$total) + { + $stype = $criteria->getServiceType(); + + $items = []; + if ($stype == $this->getID()) + { + // get the entries + $entries = $criteria->getEntries(); + + foreach($entries as $entry) + { + $batt = $entry['battery']; + $qty = 1; + $price = $this->getServiceTypeFee(); + + $items[] = [ + 'service_type' => $this->getID(), + 'battery' => $batt, + 'qty' => $qty, + 'title' => $this->getTitle($batt), + 'price' => $price, + ]; + + $qty_price = bcmul($price, $qty, 2); + $total['total_price'] = bcadd($total['total_price'], $qty_price, 2); + } + } + + return $items; + } + + public function getServiceTypeFee() + { + // TODO: we need to to put this somewhere like in .env + // so that if any chanages are to be made, we just edit the file + // instead of the code + return 0; + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + // check service type. Only battery sales and battery warranty should have invoice items + $stype = $criteria->getServiceType(); + if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW && $stype != ServiceType::BATTERY_REPLACEMENT_WARRANTY) + return null; + + // return error if there's a problem, false otherwise + if (!empty($invoice_items)) + { + // check if this is a valid battery + foreach ($invoice_items as $item) + { + $battery = $this->em->getRepository(Battery::class)->find($item['battery']); + + if (empty($battery)) + { + $error = 'Invalid battery specified.'; + return $error; + } + + // quantity + $qty = $item['quantity']; + if ($qty < 1) + continue; + + // if this is a trade in, add trade in + if (!empty($item['trade_in']) && TradeInType::validate($item['trade_in'])) + $trade_in = $item['trade_in']; + else + $trade_in = null; + + $criteria->addEntry($battery, $trade_in, $qty); + } + } + + return null; + } + + protected function getTitle($battery) + { + $title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName() . ' - Service Unit'; + + return $title; + } +} + diff --git a/src/InvoiceRule/BatterySales.php b/src/InvoiceRule/BatterySales.php new file mode 100644 index 00000000..533a54b4 --- /dev/null +++ b/src/InvoiceRule/BatterySales.php @@ -0,0 +1,122 @@ +em = $em; + } + + public function getID() + { + return 'battery_new'; + } + + public function compute($criteria, &$total) + { + $stype = $criteria->getServiceType(); + + $items = []; + if ($stype == $this->getID()) + { + // get the entries + $entries = $criteria->getEntries(); + foreach($entries as $entry) + { + $batt = $entry['battery']; + $qty = $entry['qty']; + $trade_in = null; + + if (isset($entry['trade_in'])) + $trade_in = $entry['trade_in']; + + $size = $batt->getSize(); + + if ($trade_in == null) + { + // battery purchase + $price = $batt->getSellingPrice(); + + $items[] = [ + 'service_type' => $this->getID(), + 'battery' => $batt, + 'qty' => $qty, + 'title' => $this->getTitle($batt), + 'price' => $price, + ]; + + $qty_price = bcmul($price, $qty, 2); + + $total['sell_price'] = bcadd($total['sell_price'], $qty_price, 2); + $total['total_price'] = bcadd($total['total_price'], $qty_price, 2); + } + } + } + + return $items; + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + // check service type. Only battery sales and battery warranty should have invoice items + $stype = $criteria->getServiceType(); + if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW && $stype != ServiceType::BATTERY_REPLACEMENT_WARRANTY) + return null; + + // return error if there's a problem, false otherwise + if (!empty($invoice_items)) + { + // check if this is a valid battery + foreach ($invoice_items as $item) + { + $battery = $this->em->getRepository(Battery::class)->find($item['battery']); + + if (empty($battery)) + { + $error = 'Invalid battery specified.'; + return $error; + } + + // quantity + $qty = $item['quantity']; + if ($qty < 1) + continue; + + // if this is a trade in, add trade in + if (!empty($item['trade_in']) && TradeInType::validate($item['trade_in'])) + $trade_in = $item['trade_in']; + else + $trade_in = null; + + $criteria->addEntry($battery, $trade_in, $qty); + } + } + + return null; + } + + protected function getTitle($battery) + { + $title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName(); + + return $title; + } +} diff --git a/src/Invoice/DiscountType.php b/src/InvoiceRule/DiscountType.php similarity index 57% rename from src/Invoice/DiscountType.php rename to src/InvoiceRule/DiscountType.php index 091ff9c6..8c89e9e7 100644 --- a/src/Invoice/DiscountType.php +++ b/src/InvoiceRule/DiscountType.php @@ -1,39 +1,40 @@ getPromos())) - return true; - - return false; - - } - - public function getTemplate() - { - return 'invoice/discount_type.html.twig'; + $this->em = $em; } public function getID() { - return 'discount_type'; + return 'discount'; } - public function compute($criteria, $stype_fees, &$total) + public function compute($criteria, &$total) { $items = []; $promos = $criteria->getPromos(); - // NOTE: only get first promo because only one is applicable anyway + if (empty($promos)) + return $items; + + // NOTE: only get first promo because only one is applicable anyway $promo = $promos[0]; $rate = $promo->getDiscountRate(); @@ -75,6 +76,36 @@ class DiscountType implements InvoiceInterface return $items; } + public function validatePromo($criteria, $promo_id) + { + // return error if there's a problem, false otherwise + // check service type + $stype = $criteria->getServiceType(); + + // discount/promo only applies for battery sales + if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW) + return null; + + if (empty($promo_id)) + { + return false; + } + + // check if this is a valid promo + $promo = $this->em->getRepository(Promo::class)->find($promo_id); + + if (empty($promo)) + return 'Invalid promo specified.'; + + $criteria->addPromo($promo); + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + protected function getTitle() { $title = 'Promo discount'; diff --git a/src/Invoice/Fuel.php b/src/InvoiceRule/Fuel.php similarity index 69% rename from src/Invoice/Fuel.php rename to src/InvoiceRule/Fuel.php index 2cc8bbeb..a5529a6f 100644 --- a/src/Invoice/Fuel.php +++ b/src/InvoiceRule/Fuel.php @@ -1,32 +1,20 @@ getID() == $criteria->getServiceType()) - return true; - - return false; - } - - public function getTemplate() - { - } - public function getID() { return 'fuel'; } - public function compute($criteria, $stype_fees, &$total) + public function compute($criteria, &$total) { $stype = $criteria->getServiceType(); @@ -39,7 +27,7 @@ class Fuel implements InvoiceInterface if ($cv->hasMotoliteBattery()) $fee = 0; else - $fee = $stype_fees['service_fee']; + $fee = $this->getServiceTypeFee(); $ftype = $cv->getFuelType(); @@ -55,14 +43,12 @@ class Fuel implements InvoiceInterface $qty_fee = bcmul($qty, $fee, 2); $total_price = $qty_fee; - $stype_fees_id = $this->getID() . '_fee_' . $ftype; - switch ($ftype) { case FuelType::GAS: case FuelType::DIESEL: $qty = 1; - $price = $stype_fees[$stype_fees_id]; + $price = $this->getFuelFee(); $items[] = [ 'service_type' => $this->getID(), 'qty' => $qty, @@ -96,6 +82,41 @@ class Fuel implements InvoiceInterface return $items; } + public function getServiceTypeFee() + { + // TODO: we need to to put this somewhere like in .env + // so that if any changes are to be made, we just edit the file + // instead of the code + return 300; + } + + public function getFuelFee($fuel_type) + { + // TODO: we need to to put this somewhere like in .env + // so that if any changes are to be made, we just edit the file + // instead of the code + if ($fuel_type == FuelType::GAS) + { + // gas fuel fee + return 320; + } + else + { + // diesel fuel fee + return 340; + } + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + protected function getTitle($fuel_type) { $title = '4L - ' . ucfirst($fuel_type); diff --git a/src/Invoice/Jumpstart.php b/src/InvoiceRule/Jumpstart.php similarity index 59% rename from src/Invoice/Jumpstart.php rename to src/InvoiceRule/Jumpstart.php index 747c1217..a97ddce7 100644 --- a/src/Invoice/Jumpstart.php +++ b/src/InvoiceRule/Jumpstart.php @@ -1,29 +1,17 @@ getID() == $criteria->getServiceType()) - return true; - - return false; - } - - public function getTemplate() - { - } - public function getID() { return 'jumpstart_troubleshoot'; } - public function compute($criteria, $stype_fees, &$total) + public function compute($criteria, &$total) { $stype = $criteria->getServiceType(); @@ -31,9 +19,7 @@ class Jumpstart implements InvoiceInterface if ($stype == $this->getID()) { - $stype_fees_id = $this->getID() . '_fee'; - - $fee = $stype_fees[$stype_fees_id]; + $fee = $this->getServiceTypeFee(); // add the service fee to items $qty = 1; @@ -51,6 +37,24 @@ class Jumpstart implements InvoiceInterface return $items; } + public function getServiceTypeFee() + { + // TODO: we need to to put this somewhere like in .env + // so that if any chanages are to be made, we just edit the file + // instead of the code + return 150; + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + protected function getServiceTitle() { $title = 'Service - Troubleshooting fee'; diff --git a/src/Invoice/JumpstartWarranty.php b/src/InvoiceRule/JumpstartWarranty.php similarity index 58% rename from src/Invoice/JumpstartWarranty.php rename to src/InvoiceRule/JumpstartWarranty.php index 634e79b8..34a331e7 100644 --- a/src/Invoice/JumpstartWarranty.php +++ b/src/InvoiceRule/JumpstartWarranty.php @@ -1,29 +1,17 @@ getID() == $criteria->getServiceType()) - return true; - - return false; - } - - public function getTemplate() - { - } - public function getID() { return 'jumpstart_warranty'; } - public function compute($criteria, $stype_fees, &$total) + public function compute($criteria, &$total) { $stype = $criteria->getServiceType(); @@ -31,9 +19,7 @@ class JumpstartWarranty implements InvoiceInterface if ($stype == $this->getID()) { - $stype_fees_id = $this->getID() . '_fee'; - - $fee = $stype_fees[$stype_fees_id]; + $fee = $this->getServiceTypeFee(); // add the service fee to items $qty = 1; @@ -51,6 +37,24 @@ class JumpstartWarranty implements InvoiceInterface return $items; } + public function getServiceTypeFee() + { + // TODO: we need to to put this somewhere like in .env + // so that if any chanages are to be made, we just edit the file + // instead of the code + return 0; + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + protected function getServiceTitle() { $title = 'Service - Troubleshooting fee'; diff --git a/src/Invoice/Overheat.php b/src/InvoiceRule/Overheat.php similarity index 65% rename from src/Invoice/Overheat.php rename to src/InvoiceRule/Overheat.php index 4700b9b2..692e5e85 100644 --- a/src/Invoice/Overheat.php +++ b/src/InvoiceRule/Overheat.php @@ -1,31 +1,19 @@ getID() == $criteria->getServiceType()) - return true; - - return false; - } - - public function getTemplate() - { - } - public function getID() { return 'overheat'; } - public function compute($criteria, $stype_fees, &$total) + public function compute($criteria, &$total) { $stype = $criteria->getServiceType(); $has_coolant = $criteria->hasCoolant(); @@ -39,7 +27,7 @@ class Overheat implements InvoiceInterface if ($cv->hasMotoliteBattery()) $fee = 0; else - $fee = $stype_fees['service_fee']; + $fee = $this->getServiceTypeFee(); // add the service fee to items $qty = 1; @@ -55,7 +43,7 @@ class Overheat implements InvoiceInterface if ($has_coolant) { - $coolant_fee = $stype_fees['coolant_fee']; + $coolant_fee = $this->getCoolantFee(); $items[] = [ 'service_type' => $this->getID(), 'qty' => $qty, @@ -73,6 +61,32 @@ class Overheat implements InvoiceInterface return $items; } + public function getServiceTypeFee() + { + // TODO: we need to to put this somewhere like in .env + // so that if any chanages are to be made, we just edit the file + // instead of the code + return 300; + } + + public function getCoolantFee() + { + // TODO: we need to to put this somewhere like in .env + // so that if any chanages are to be made, we just edit the file + // instead of the code + return 1600; + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + protected function getServiceTitle() { $title = 'Service - ' . ServiceType::getName(ServiceType::OVERHEAT_ASSISTANCE); diff --git a/src/Invoice/PostRecharged.php b/src/InvoiceRule/PostRecharged.php similarity index 56% rename from src/Invoice/PostRecharged.php rename to src/InvoiceRule/PostRecharged.php index 9984e6a2..55549aa7 100644 --- a/src/Invoice/PostRecharged.php +++ b/src/InvoiceRule/PostRecharged.php @@ -1,29 +1,17 @@ getID() == $criteria->getServiceType()) - return true; - - return false; - } - - public function getTemplate() - { - } - public function getID() { return 'post_recharged'; } - public function compute($criteria, $stype_fees, &$total) + public function compute($criteria, &$total) { $stype = $criteria->getServiceType(); @@ -31,7 +19,7 @@ class PostRecharged implements InvoiceInterface if ($stype == $this->getID()) { - $fee = $stype_fees[$this->getID() . '_fee']; + $fee = $this->getServiceTypeFee(); $qty = 1; $items[] = [ @@ -49,6 +37,24 @@ class PostRecharged implements InvoiceInterface return $items; } + public function getServiceTypeFee() + { + // TODO: we need to to put this somewhere like in .env + // so that if any chanages are to be made, we just edit the file + // instead of the code + return 300; + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + protected function getServiceTitle() { $title = 'Recharge fee'; diff --git a/src/Invoice/PostReplacement.php b/src/InvoiceRule/PostReplacement.php similarity index 56% rename from src/Invoice/PostReplacement.php rename to src/InvoiceRule/PostReplacement.php index 5d5ec7ba..a64c0fce 100644 --- a/src/Invoice/PostReplacement.php +++ b/src/InvoiceRule/PostReplacement.php @@ -1,29 +1,17 @@ getID() == $criteria->getServiceType()) - return true; - - return false; - } - - public function getTemplate() - { - } - public function getID() { return 'post_replacement'; } - public function compute($criteria, $stype_fees, &$total) + public function compute($criteria, &$total) { $stype = $criteria->getServiceType(); @@ -31,7 +19,7 @@ class PostReplacement implements InvoiceInterface if ($stype == $this->getID()) { - $fee = $stype_fees['battery_replacement_fee']; + $fee = $this->getServiceTypeFee(); $qty = 1; $items[] = [ @@ -48,6 +36,24 @@ class PostReplacement implements InvoiceInterface return $items; } + public function getServiceTypeFee() + { + // TODO: we need to to put this somewhere like in .env + // so that if any chanages are to be made, we just edit the file + // instead of the code + return 0; + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + protected function getServiceTitle() { $title = 'Battery replacement'; diff --git a/src/InvoiceRule/Tax.php b/src/InvoiceRule/Tax.php new file mode 100644 index 00000000..231c2d36 --- /dev/null +++ b/src/InvoiceRule/Tax.php @@ -0,0 +1,110 @@ +isTaxable()) + { + // nothing to compute + return []; + } + + $tax_rate = $this->getTaxRate(); + + $is_battery_sales = false; + $total_price = 0; + + // compute tax per item if service type is battery sales + $stype = $criteria->getServiceType(); + + if ($stype == ServiceType::BATTERY_REPLACEMENT_NEW) + { + // get the entries + $entries = $criteria->getEntries(); + + foreach($entries as $entry) + { + // check if entry is trade-in + if (isset($entry['trade_in'])) + { + // continue to next entry + continue; + } + + $battery = $entry['battery']; + $qty = $entry['qty']; + + $price = $battery->getSellingPrice(); + + $vat = $this->getTaxAmount($price, $tax_rate); + + $qty_price = bcmul($price, $qty, 2); + $qty_vat = bcmul($vat, $qty, 2); + $price_minus_vat = bcsub($price, $vat, 2); + $qty_price_minus_vat = bcmul($price_minus_vat, $qty, 2); + + $total['vat'] = bcadd($total['vat'], $qty_vat, 2); + $total['vat_ex_price'] = bcadd($total['vat_ex_price'], $qty_price_minus_vat, 2); + } + } + else + { + // compute VAT after adding all item costs, if service type is not battery sales + $total_price = $total['total_price']; + + $vat_ex_price = $this->getTaxExclusivePrice($total_price, $tax_rate); + $vat = bcsub($total_price, $vat_ex_price, 2); + + $total['vat_ex_price'] = $vat_ex_price; + $total['vat'] = $vat; + } + + return []; + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + + protected function getTaxAmount($price, $tax_rate) + { + $vat_ex_price = $this->getTaxExclusivePrice($price, $tax_rate); + $tax_amount = bcsub($price, $vat_ex_price, 2); + + return $tax_amount; + } + + protected function getTaxExclusivePrice($price, $tax_rate) + { + $tax_ex_price = bcdiv($price, bcadd(1, $tax_rate, 2), 2); + return $tax_ex_price; + } + + protected function getTaxRate() + { + // TODO: we need to to put this somewhere like in .env + // so that if any chanages are to be made, we just edit the file + // instead of the code + return 0.12; + } +} + diff --git a/src/Invoice/TireRepair.php b/src/InvoiceRule/TireRepair.php similarity index 62% rename from src/Invoice/TireRepair.php rename to src/InvoiceRule/TireRepair.php index 4ec85945..3ca00714 100644 --- a/src/Invoice/TireRepair.php +++ b/src/InvoiceRule/TireRepair.php @@ -1,29 +1,17 @@ getID() == $criteria->getServiceType()) - return true; - - return false; - } - - public function getTemplate() - { - } - public function getID() { return 'tire'; } - public function compute($criteria, $stype_fees, &$total) + public function compute($criteria, &$total) { $stype = $criteria->getServiceType(); @@ -36,7 +24,7 @@ class TireRepair implements InvoiceInterface if ($cv->hasMotoliteBattery()) $fee = 0; else - $fee = $stype_fees['service_fee']; + $fee = $this->getServiceTypeFee(); // add the service fee to items $qty = 1; @@ -54,6 +42,24 @@ class TireRepair implements InvoiceInterface return $items; } + protected function getServiceTypeFee() + { + // TODO: we need to to put this somewhere like in .env + // so that if any chanages are to be made, we just edit the file + // instead of the code + return 300; + } + + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + protected function getServiceTitle() { $title = 'Service - Flat Tire'; diff --git a/src/Invoice/TradeIn.php b/src/InvoiceRule/TradeIn.php similarity index 74% rename from src/Invoice/TradeIn.php rename to src/InvoiceRule/TradeIn.php index 0842fd92..8e3c6063 100644 --- a/src/Invoice/TradeIn.php +++ b/src/InvoiceRule/TradeIn.php @@ -1,41 +1,19 @@ getEntries(); - - foreach($entries as $entry) - { - if ($entry['trade_in']) - { - // just need to find one trade-in entry - return true; - } - } - - return false; - } - - public function getTemplate() - { - return 'invoice/trade_in.html.twig'; - } - public function getID() { - return 'trade_in'; + return 'trade-in'; } - public function compute($criteria, $stype_fees, &$total) + public function compute($criteria, &$total) { $items = []; @@ -72,6 +50,16 @@ class TradeIn implements InvoiceInterface return $items; } + public function validatePromo($criteria, $promo_id) + { + return false; + } + + public function validateInvoiceItems($criteria, $invoice_items) + { + return null; + } + protected function getTradeInRate($battery, $trade_in_type) { $size = $battery->getSize(); diff --git a/src/InvoiceRuleInterface.php b/src/InvoiceRuleInterface.php new file mode 100644 index 00000000..2de47dd7 --- /dev/null +++ b/src/InvoiceRuleInterface.php @@ -0,0 +1,15 @@ +em), + new InvoiceRule\BatteryReplacementWarranty($this->em), + new InvoiceRule\Jumpstart(), + new InvoiceRule\JumpstartWarranty(), + new InvoiceRule\PostRecharged(), + new InvoiceRule\PostReplacement(), + new InvoiceRule\Overheat(), + new InvoiceRule\Fuel(), + new InvoiceRule\TireRepair(), + new InvoiceRule\DiscountType($this->em), + new InvoiceRule\TradeIn(), + new InvoiceRule\Tax(), ]; } - public function getServiceTypeFees() - { - // TODO: we need to put this somewhere like a json file, so - // if any changes are to be made to fees, we just edit the file - // instead of the code - return [ - 'service_fee' => 300, - 'coolant_fee' => 1600, - 'post_recharged_fee' => 300, - 'jumpstart_troubleshoot_fee' => 150, - 'jumpstart_warranty_fee' => 0, - 'battery_replacement_fee' => 0, - 'battery_warranty_fee' => 0, - 'other_services_fee' => 200, - 'fuel_fee_gas' => 320, - 'fuel_fee_diesel' => 340, - ]; - } - - public function getTaxRate() - { - // TODO: we need to to put this somewhere like in .env - // so that if any chanages are to be made, we just edit the file - // instead of the code - return 0.12; - } - // this is called when JO is submitted public function generateInvoiceCriteria($jo, $promo_id, $invoice_items, &$error_array) { @@ -98,12 +70,26 @@ class InvoiceManager implements InvoiceGeneratorInterface // would mean adding it as a parameter to the call, impacting all calls $criteria->setIsTaxable(); - $ierror = $this->validatePromo($criteria, $promo_id); + foreach ($this->available_rules as $avail_rule) + { + $ierror = $avail_rule->validatePromo($criteria, $promo_id); + + // break out of loop when error found + if ($ierror) + break; + } if (!$ierror && !empty($invoice_items)) { // validate the invoice items (batteries and trade ins) - $ierror = $this->validateInvoiceItems($criteria, $invoice_items); + foreach ($this->available_rules as $avail_rule) + { + $ierror = $avail_rule->validateInvoiceItems($criteria, $invoice_items); + + // break out of loop when error found + if ($ierror) + break; + } } if ($ierror) @@ -142,12 +128,26 @@ class InvoiceManager implements InvoiceGeneratorInterface // this is called by JobOrderController when JS script generateInvoice is called public function generateDraftInvoice($criteria, $promo_id, $service_charges, $items) { - $ierror = $this->validatePromo($criteria, $promo_id); + foreach ($this->available_rules as $avail_rule) + { + $ierror = $avail_rule->validatePromo($criteria, $promo_id); + + // break out of loop when error found + if ($ierror) + break; + } if (!$ierror && !empty($items)) { // validate the invoice items (batteries and trade ins) - $ierror = $this->validateInvoiceItems($criteria, $items); + foreach ($this->available_rules as $avail_rule) + { + $ierror = $avail_rule->validateInvoiceItems($criteria, $items); + + // break out of loop when error found + if ($ierror) + break; + } } return $ierror; @@ -163,31 +163,14 @@ class InvoiceManager implements InvoiceGeneratorInterface { // no need to validate since generateDraftInvoice was called before this was called // generate the invoice and from APIController, the fields were validated - // check which rules to apply - $rules = $this->check($criteria); - - $invoice_data = $this->compute($criteria, $rules); + $invoice_data = $this->compute($criteria); $invoice = $this->createInvoice($invoice_data); return $invoice; } - // check what rules to use given the criteria - public function check($criteria) - { - $active_rules = []; - foreach ($this->available_rules as $rule) - { - $is_active = $rule->check($criteria); - if ($is_active) - $active_rules[$rule->getID()] = $rule; - } - - return $active_rules; - } - - public function compute($criteria, $active_rules) + public function compute($criteria) { // initialize $total = [ @@ -205,9 +188,6 @@ class InvoiceManager implements InvoiceGeneratorInterface $promos = $criteria->getPromos(); $is_taxable = $criteria->isTaxable(); - // get the service type fees - $stype_fees = $this->getServiceTypeFees(); - $flag_trade_in = false; foreach ($entries as $entry) { @@ -219,164 +199,48 @@ class InvoiceManager implements InvoiceGeneratorInterface $invoice_items = []; $data = []; - foreach ($active_rules as $id => $active_rule) + foreach ($this->available_rules as $rule) { - // each criteria can only have one service type - // find the rule for the service type - if ($id == $stype) + $items = $rule->compute($criteria, $total); + + $promo = null; + foreach ($items as $item) { - $items = []; + $title = $item['title']; + $quantity = $item['qty']; + $price = $item['price']; - $computed_items = $active_rule->compute($criteria, $stype_fees, $total); - foreach ($computed_items as $computed_item) - { - if (!empty($computed_item)) - { - $items[] = $computed_item; - } - } - - // check if tax is needed - if ($is_taxable) - { - $tax_rate = $this->getTaxRate(); - - if (isset($active_rules['tax'])) - { - $active_rules['tax']->compute($tax_rate, $items, $total); - } - } - - // process trade-ins by calling the trade-in rule if there are trade-ins among the entries - if ($flag_trade_in) - { - if (isset($active_rules['trade_in'])) - { - $tradein_items = $active_rules['trade_in']->compute($criteria, $stype_fees, $total); - foreach ($tradein_items as $tradein_item) - { - $items[] = $tradein_item; - } - } - } - - // process promos, if any - if (count($promos) > 0) - { - if (isset($active_rules['discount_type'])) - { - $discount_items = $active_rules['discount_type']->compute($criteria, $stype_fees, $total); - foreach ($discount_items as $discount_item) - { - $items[] = $discount_item; - } - } - } + $battery = null; + if (isset($item['battery'])) + $battery = $item['battery']; $promo = null; - foreach ($items as $item) - { - $title = $item['title']; - $quantity = $item['qty']; - $price = $item['price']; + if (isset($item['promo'])) + $promo = $item['promo']; - $battery = null; - if (isset($item['battery'])) - $battery = $item['battery']; - - $promo = null; - if (isset($item['promo'])) - $promo = $item['promo']; - - $invoice_items[] = [ - 'title' => $title, - 'quantity' => $quantity, - 'price' => $price, - 'battery' => $battery, - 'promo' => $promo, - ]; - } - - // also need to return the total and the promo - $data[] = [ + $invoice_items[] = [ + 'title' => $title, + 'quantity' => $quantity, + 'price' => $price, + 'battery' => $battery, 'promo' => $promo, - 'invoice_items' => $invoice_items, - 'total' => $total, ]; } + + // also need to return the total and the promo + $data[] = [ + 'promo' => $promo, + 'invoice_items' => $invoice_items, + 'total' => $total, + ]; } return $data; } - protected function validatePromo($criteria, $promo_id) - { - // return error if there's a problem, false otherwise - // check service type - $stype = $criteria->getServiceType(); - - // discount/promo only applies for battery sales - if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW) - return null; - - if (empty($promo_id)) - { - return false; - } - - // check if this is a valid promo - $promo = $this->em->getRepository(Promo::class)->find($promo_id); - - if (empty($promo)) - return 'Invalid promo specified.'; - - $criteria->addPromo($promo); - return false; - - } - - protected function validateInvoiceItems($criteria, $invoice_items) - { - // check service type. Only battery sales and battery warranty should have invoice items - $stype = $criteria->getServiceType(); - if ($stype != ServiceType::BATTERY_REPLACEMENT_NEW && $stype != ServiceType::BATTERY_REPLACEMENT_WARRANTY) - return null; - - // return error if there's a problem, false otherwise - if (!empty($invoice_items)) - { - // check if this is a valid battery - foreach ($invoice_items as $item) - { - $battery = $this->em->getRepository(Battery::class)->find($item['battery']); - - if (empty($battery)) - { - $error = 'Invalid battery specified.'; - return $error; - } - - // quantity - $qty = $item['quantity']; - if ($qty < 1) - continue; - - // if this is a trade in, add trade in - if (!empty($item['trade_in']) && TradeInType::validate($item['trade_in'])) - $trade_in = $item['trade_in']; - else - $trade_in = null; - - $criteria->addEntry($battery, $trade_in, $qty); - } - } - - return null; - } - protected function createInvoice($invoice_data) { - $invoice = new Inv(); + $invoice = new Invoice(); // get current user $user = $this->security->getUser();