From e3ec6148df5f8aa39347b50023475cd828cbc538 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 26 May 2023 05:54:22 -0400 Subject: [PATCH] Add command to test invoice manager. #744 --- src/Command/TestInvoiceManagerCommand.php | 75 ++++++++++++++ src/Invoice/BatteryReplacementWarranty.php | 14 +-- src/Invoice/BatterySales.php | 8 +- src/Invoice/Discount.php | 34 ------- src/Invoice/DiscountType.php | 33 +++++++ src/Invoice/Fuel.php | 71 ++++++++++++-- src/Invoice/Jumpstart.php | 15 +-- src/Invoice/JumpstartWarranty.php | 22 ++--- src/Invoice/Overheat.php | 19 ++-- src/Invoice/PostRecharged.php | 17 +--- src/Invoice/PostReplacement.php | 21 ++-- src/Invoice/ServiceType.php | 7 +- src/Invoice/TireRepair.php | 22 ++--- src/Invoice/TradeIn.php | 31 ++---- src/InvoiceInterface.php | 4 +- src/Service/InvoiceManager.php | 109 ++++++++------------- 16 files changed, 269 insertions(+), 233 deletions(-) create mode 100644 src/Command/TestInvoiceManagerCommand.php delete mode 100644 src/Invoice/Discount.php create mode 100644 src/Invoice/DiscountType.php diff --git a/src/Command/TestInvoiceManagerCommand.php b/src/Command/TestInvoiceManagerCommand.php new file mode 100644 index 00000000..14c8ff90 --- /dev/null +++ b/src/Command/TestInvoiceManagerCommand.php @@ -0,0 +1,75 @@ +setName('test:generateinvoice') + ->setDescription('Test invoice manager service.') + ->setHelp('Test invoice manager service.'); + } + + public function __construct(InvoiceManager $inv_manager, EntityManagerInterface $em) + { + $this->em = $em; + $this->inv_manager = $inv_manager; + + parent::__construct(); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->testBatterySalesNoTradeInNoDiscount(); + + // TEST SCENARIO: new battery with trade in + // TEST SCENARIO: new battery with discount + // TEST SCENARIO: new battery with discount and trade-in + // TEST SCENARIO: fuel, gas + // TEST SCENARIO: fuel, diesel + // TEST SCENARIO: fuel, gas, with discount + return 0; + } + + protected function testBatterySalesNoTradeInNoDiscount() + { + $criteria = new InvoiceCriteria(); + + // TEST SCENARIO: new battery, no trade-in, no discount + $criteria->setServiceType(ServiceType::BATTERY_REPLACEMENT_NEW); + + $battery_id = 1038; + $battery = $this->em->getRepository(Battery::class)->find($battery_id); + + $criteria->addEntry($battery, null, 1); + + $rules = $this->inv_manager->check($criteria); + + // error_log(print_r($rules, true)); + + $invoice_items = $this->inv_manager->compute($criteria, $rules); + + foreach ($invoice_items as $invoice_item) + { + error_log($invoice_item->getTitle() . ' ' . $invoice_item->getQuantity() . ' ' . $invoice_item->getPrice()); + } + } +} diff --git a/src/Invoice/BatteryReplacementWarranty.php b/src/Invoice/BatteryReplacementWarranty.php index 4d571a9f..e1a1536b 100644 --- a/src/Invoice/BatteryReplacementWarranty.php +++ b/src/Invoice/BatteryReplacementWarranty.php @@ -6,10 +6,9 @@ use App\InvoiceInterface; class BatteryReplacementWarranty implements InvoiceInterface { - public function check($settings, $criteria) + public function check($criteria) { - $type_id = $settings['service_type']; - if ($type_id == $criteria->getServiceType()) + if ($this->getID() == $criteria->getServiceType()) return true; return false; @@ -25,8 +24,7 @@ class BatteryReplacementWarranty implements InvoiceInterface return 'battery_warranty'; } - // TODO: need to add service type fees as a parameter - public function compute($criteria) + public function compute($criteria, $stype_fees) { $stype = $criteria->getServiceType(); @@ -35,11 +33,13 @@ class BatteryReplacementWarranty implements InvoiceInterface { // get the entries $entries = $criteria->getEntries(); + + $stype_fee_id = $this->getID() . '_fee'; foreach($entries as $entry) { $batt = $entry['battery']; $qty = 1; - $price = 0; // need to put this in a config or something + $price = $stype_fees[$stype_fee_id]; $items[] = [ 'battery' => $batt, @@ -55,7 +55,7 @@ class BatteryReplacementWarranty implements InvoiceInterface protected function getTitle($battery) { - $title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName() . ' - Service Unit'); + $title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName() . ' - Service Unit'; return $title; } diff --git a/src/Invoice/BatterySales.php b/src/Invoice/BatterySales.php index f0b316d7..d102f950 100644 --- a/src/Invoice/BatterySales.php +++ b/src/Invoice/BatterySales.php @@ -6,10 +6,9 @@ use App\InvoiceInterface; class BatterySales implements InvoiceInterface { - public function check($settings, $criteria) + public function check($criteria) { - $type_id = $settings['service_type']; - if ($type_id == $criteria->getServiceType()) + if ($this->getID() == $criteria->getServiceType()) return true; return false; @@ -25,8 +24,7 @@ class BatterySales implements InvoiceInterface return 'battery_new'; } - // TODO: need to add service type fees as a parameter - public function compute($criteria) + public function compute($criteria, $stype_fees) { $stype = $criteria->getServiceType(); diff --git a/src/Invoice/Discount.php b/src/Invoice/Discount.php deleted file mode 100644 index 62a37eb6..00000000 --- a/src/Invoice/Discount.php +++ /dev/null @@ -1,34 +0,0 @@ -getDiscount()) - return true; - - return false; - - } - - public function getTemplate() - { - return 'invoice/discount.html.twig'; - } - - public function getID() - { - return 'discount'; - } - - public function getAmount($battery = null, $discount_list, $id) - { - return 0; - } -} - diff --git a/src/Invoice/DiscountType.php b/src/Invoice/DiscountType.php new file mode 100644 index 00000000..6ce07355 --- /dev/null +++ b/src/Invoice/DiscountType.php @@ -0,0 +1,33 @@ +getPromos())) + return true; + + return false; + + } + + public function getTemplate() + { + return 'invoice/discount_type.html.twig'; + } + + public function getID() + { + return 'discount_type'; + } + + public function compute($criteria, $stype_fees) + { + return []; + } +} + diff --git a/src/Invoice/Fuel.php b/src/Invoice/Fuel.php index 4a700f3b..ad0680a1 100644 --- a/src/Invoice/Fuel.php +++ b/src/Invoice/Fuel.php @@ -4,12 +4,13 @@ namespace App\Invoice; use App\InvoiceInterface; +use App\Ramcar\FuelType; + class Fuel implements InvoiceInterface { - public function check($settings, $criteria) + public function check($criteria) { - $type_id = $settings['service_type']; - if ($type_id == $criteria->getServiceType()) + if ($this->getID() == $criteria->getServiceType()) return true; return false; @@ -24,14 +25,66 @@ class Fuel implements InvoiceInterface return 'fuel'; } - public function getAmount($battery = null, $service_type_fees, $id) + public function compute($criteria, $stype_fees) { - if ($battery != null) - return $battery->getSellingPrice(); + $stype = $criteria->getServiceType(); - if (isset($service_type_fees[$id])) - return $service_type_fees[$id]; + $items = []; - return 0; + + if ($stype == $this->getID()) + { + // check if customer vehicle has a motolite battery + $cv = $criteria->getCustomerVehicle(); + if ($cv->hasMotoliteBattery()) + $fee = 0; + else + $fee = $stype_fees['service_fee']; + + // add the service fee to items + $qty = 1; + $items[] = [ + 'qty' => $qty, + 'title' => $this->getTitle($ftype), + 'price' => $fee, + ]; + + $ftype = $cv->getFuelType(); + + $stype_fees_id = $this->getID() . '_' . $ftype; + + switch ($ftype) + { + case FuelType::GAS: + case FuelType::DIESEL: + $qty = 1; + $price = $stype_fees[$stype_fees_id]; + $items[] = [ + 'qty' => $qty, + 'title' => $this->getTitle($ftype), + 'price' => $price, + ]; + + break; + default: + $qty = 1; + $price = 0; + $items[] = [ + 'qty' => $qty, + 'title' => $this->getTitle('Unknown'), + 'price' => $price, + ]; + break; + } + } + + return $items; + } + + protected function getTitle($fuel_type) + { + $title = '4L - ' . $fuel_type; + + return $title; } } diff --git a/src/Invoice/Jumpstart.php b/src/Invoice/Jumpstart.php index 36c25fa7..84ac7ff6 100644 --- a/src/Invoice/Jumpstart.php +++ b/src/Invoice/Jumpstart.php @@ -6,10 +6,9 @@ use App\InvoiceInterface; class Jumpstart implements InvoiceInterface { - public function check($settings, $criteria) + public function check($criteria) { - $type_id = $settings['service_type']; - if ($type_id == $criteria->getServiceType()) + if ($this->getID() == $criteria->getServiceType()) return true; return false; @@ -24,14 +23,8 @@ class Jumpstart implements InvoiceInterface return 'jumpstart_troubleshoot'; } - public function getAmount($battery = null, $service_type_fees, $id) + public function compute($criteria, $stype_fees) { - if ($battery != null) - return $battery->getSellingPrice(); - - if (isset($service_type_fees[$id])) - return $service_type_fees[$id]; - - return 0; + return []; } } diff --git a/src/Invoice/JumpstartWarranty.php b/src/Invoice/JumpstartWarranty.php index b7f89c3c..dc322036 100644 --- a/src/Invoice/JumpstartWarranty.php +++ b/src/Invoice/JumpstartWarranty.php @@ -5,15 +5,14 @@ namespace App\Invoice; use App\InvoiceInterface; class JumpstartWarranty implements InvoiceInterface -{ - public function check($settings, $criteria) +{ + public function check($criteria) { - $type_id = $settings['service_type']; - if ($type_id == $criteria->getServiceType()) + if ($this->getID() == $criteria->getServiceType()) return true; - + return false; - } + } public function getTemplate() { @@ -24,14 +23,9 @@ class JumpstartWarranty implements InvoiceInterface return 'jumpstart_warranty'; } - public function getAmount($battery = null, $service_type_fees, $id) + public function compute($criteria, $stype_fees) { - if ($battery != null) - return $battery->getSellingPrice(); - - if (isset($service_type_fees[$id])) - return $service_type_fees[$id]; - - return 0; + return []; } + } diff --git a/src/Invoice/Overheat.php b/src/Invoice/Overheat.php index dfe33cbc..5e8e0aae 100644 --- a/src/Invoice/Overheat.php +++ b/src/Invoice/Overheat.php @@ -6,15 +6,14 @@ use App\InvoiceInterface; class Overheat implements InvoiceInterface { - public function check($settings, $criteria) + public function check($criteria) { - $type_id = $settings['service_type']; - if ($type_id == $criteria->getServiceType()) + if ($this->getID() == $criteria->getServiceType()) return true; - + return false; } - + public function getTemplate() { } @@ -24,14 +23,8 @@ class Overheat implements InvoiceInterface return 'overheat'; } - public function getAmount($battery = null, $service_type_fees, $id) + public function compute($criteria, $stype_fees) { - if ($battery != null) - return $battery->getSellingPrice(); - - if (isset($service_type_fees[$id])) - return $service_type_fees[$id]; - - return 0; + return []; } } diff --git a/src/Invoice/PostRecharged.php b/src/Invoice/PostRecharged.php index 26122764..546736fd 100644 --- a/src/Invoice/PostRecharged.php +++ b/src/Invoice/PostRecharged.php @@ -6,12 +6,11 @@ use App\InvoiceInterface; class PostRecharged implements InvoiceInterface { - public function check($settings, $criteria) + public function check($criteria) { - $type_id = $settings['service_type']; - if ($type_id == $criteria->getServiceType()) + if ($this->getID() == $criteria->getServiceType()) return true; - + return false; } @@ -24,14 +23,8 @@ class PostRecharged implements InvoiceInterface return 'post_recharged'; } - public function getAmount($battery = null, $service_type_fees, $id) + public function compute($criteria, $stype_fees) { - if ($battery != null) - return $battery->getSellingPrice(); - - if (isset($service_type_fees[$id])) - return $service_type_fees[$id]; - - return 0; + return []; } } diff --git a/src/Invoice/PostReplacement.php b/src/Invoice/PostReplacement.php index bab0cb44..3d891ffb 100644 --- a/src/Invoice/PostReplacement.php +++ b/src/Invoice/PostReplacement.php @@ -5,15 +5,14 @@ namespace App\Invoice; use App\InvoiceInterface; class PostReplacement implements InvoiceInterface -{ - public function check($settings, $criteria) +{ + public function check($criteria) { - $type_id = $settings['service_type']; - if ($type_id == $criteria->getServiceType()) + if ($this->getID() == $criteria->getServiceType()) return true; - + return false; - } + } public function getTemplate() { @@ -24,14 +23,8 @@ class PostReplacement implements InvoiceInterface return 'post_replacement'; } - public function getAmount($battery = null, $service_type_fees, $id) + public function compute($criteria, $stype_fees) { - if ($battery != null) - return $battery->getSellingPrice(); - - if (isset($service_type_fees[$id])) - return $service_type_fees[$id]; - - return 0; + return []; } } diff --git a/src/Invoice/ServiceType.php b/src/Invoice/ServiceType.php index fc33c64b..120ac0a7 100644 --- a/src/Invoice/ServiceType.php +++ b/src/Invoice/ServiceType.php @@ -6,9 +6,9 @@ use App\InvoiceInterface; class ServiceType implements InvoiceInterface { - public function check($settings, $criteria) + public function check($criteria) { - if (isset($settings['service_type'])) + if ($criteria->getServiceType() != null) return true; return false; @@ -24,8 +24,7 @@ class ServiceType implements InvoiceInterface return 'service_type'; } - public function getAmount($battery = null, $service_type_fees, $id) + public function compute($criteria, $stype_fees) { - return 0; } } diff --git a/src/Invoice/TireRepair.php b/src/Invoice/TireRepair.php index c659aa31..3a903bab 100644 --- a/src/Invoice/TireRepair.php +++ b/src/Invoice/TireRepair.php @@ -5,15 +5,14 @@ namespace App\Invoice; use App\InvoiceInterface; class TireRepair implements InvoiceInterface -{ - public function check($settings, $criteria) +{ + public function check($criteria) { - $type_id = $settings['service_type']; - if ($type_id == $criteria->getServiceType()) + if ($this->getID() == $criteria->getServiceType()) return true; - + return false; - } + } public function getTemplate() { @@ -24,14 +23,9 @@ class TireRepair implements InvoiceInterface return 'tire'; } - public function getAmount($battery = null, $service_type_fees, $id) + public function compute($criteria, $stype_fees) { - if ($battery != null) - return $battery->getSellingPrice(); - - if (isset($service_type_fees[$id])) - return $service_type_fees[$id]; - - return 0; + return []; } + } diff --git a/src/Invoice/TradeIn.php b/src/Invoice/TradeIn.php index e4fb974b..ffb1ffc9 100644 --- a/src/Invoice/TradeIn.php +++ b/src/Invoice/TradeIn.php @@ -6,18 +6,17 @@ use App\InvoiceInterface; class TradeIn implements InvoiceInterface { - public function check($settings, $criteria) + public function check($criteria) { // compare specified type ids with trade in entry of criteria - $trade_ins = $settings['trade_ins']; $entries = $criteria->getEntries(); - foreach ($trade_ins as $type_id) + foreach($entries as $entry) { - foreach($entries as $entry) + if ($this->getID() == $entry['trade_in']) { - if ($type_id == $entry['trade_in']) - return true; + // just need to find one trade-in entry + return true; } } @@ -34,25 +33,9 @@ class TradeIn implements InvoiceInterface return 'trade_in'; } - public function getAmount($battery = null, $trade_in_types, $id) + public function compute($criteria, $stype_fees) { - if (isset($trade_in_types[$id])) - { - if ($battery != null) - { - switch ($trade_in) - { - case TradeInType::MOTOLITE: - return $battery->getSize()->getTIPriceMotolite(); - case TradeInType::PREMIUM: - return $battery0>getSize()->getTIPricePremium(); - case TradeInType::OTHER: - return $battery->getSize()->getTIPriceOther(); - } - } - } - - return 0; + return []; } } diff --git a/src/InvoiceInterface.php b/src/InvoiceInterface.php index 704e4e46..1812fd83 100644 --- a/src/InvoiceInterface.php +++ b/src/InvoiceInterface.php @@ -4,8 +4,8 @@ namespace App; interface InvoiceInterface { - // check if the invoice rule aka settings is in the criteria - public function check($settings, $criteria); + // check if the invoice rule is in the criteria + public function check($criteria); // display the html partial for the form public function getTemplate(); diff --git a/src/Service/InvoiceManager.php b/src/Service/InvoiceManager.php index 95831d65..bfbaa7eb 100644 --- a/src/Service/InvoiceManager.php +++ b/src/Service/InvoiceManager.php @@ -7,10 +7,11 @@ use Symfony\Component\Validator\Validator\ValidatorInterface; use Doctrine\ORM\EntityManagerInterface; +use App\Invoice; + use App\Ramcar\InvoiceCriteria; use App\Ramcar\InvoiceStatus; -use App\Entity\Invoice; use App\Entity\InvoiceItem; use App\Entity\User; use App\Entity\Battery; @@ -24,7 +25,7 @@ class InvoiceManager { $this->em = $em; - $this->available_rules = $this->getAllRules(); + $this->available_rules = $this->getAvailableRules(); } public function getAvailableRules() @@ -35,15 +36,15 @@ class InvoiceManager new Invoice\BatterySales(), new Invoice\BatteryReplacementWarranty(), new Invoice\Jumpstart(), - new Invoice\JumstartWarranty(), + new Invoice\JumpstartWarranty(), new Invoice\PostRecharged(), new Invoice\PostReplacement(), new Invoice\Overheat(), new Invoice\Fuel(), new Invoice\TireRepair(), - new Invoice\Discount(), + new Invoice\DiscountType(), new Invoice\TradeIn(), - ]; + ]; } public function getServiceTypeFees() @@ -55,54 +56,20 @@ class InvoiceManager 'jumpstart_fee' => 150, 'jumpstart_warranty_fee' => 0, 'battery_replacement_fee' => 0, - 'warranty_fee' => 0, + 'battery_warranty_fee' => 0, 'other_services_fee' => 200, - 'refuel_fee_gas' => 320, - 'refuel_fee_diesel' => 320, + 'fuel_fee_gas' => 320, + 'fuel_fee_diesel' => 320, ]; } // check what rules to use given the criteria public function check($criteria) { - // get what is in criteria - $stype = $criteria->getServiceType(); - $discount = $criteria->getDiscount(); - $entries = $criteria->getEntries(); - - $batteries = []; - $trade_ins = []; - foreach ($entries as $entry) - { - if ($entry['trade_in'] == null) - { - // battery purchase - $batteries[] = [ - 'battery' => $entry['battery'], - 'qty' => $entry['qty'], - ]; - } - else - { - // trade in - $trade_ins[] = [ - 'battery' => $entry['battery'], - 'qty' => $entry['qty'], - ]; - } - } - - $settings = [ - 'service_type' => $criteria->getServiceType(), - 'discount' => $criteria->getDiscount(), - 'batteries' => $batteries, - 'trade_ins' => $trade_ins, - ]; - $active_rules = []; foreach ($this->available_rules as $rule) { - $is_active = $rule->check($settings, $criteria); + $is_active = $rule->check($criteria); if ($is_active) $active_rules[$rule->getID()] = $rule; } @@ -111,13 +78,15 @@ class InvoiceManager } - public function compute($invoice, $criteria, $active_rules) + public function compute($criteria, $active_rules) { // get what is in criteria $stype = $criteria->getServiceType(); - $discount = $criteria->getDiscount(); $entries = $criteria->getEntries(); + // get the service type fees + $stype_fees = $this->getServiceTypeFees(); + $flag_trade_in = false; foreach ($entries as $entry) { @@ -127,48 +96,48 @@ class InvoiceManager } } + $invoice_items = []; foreach ($active_rules as $id => $active_rule) { if ($id == $stype) { + $items = []; // process trade-ins by calling the trade-in rule if there are trade-ins // since it's lumped with only the battery_new service type - $trade_in_items = []; if ($flag_trade_in) { - $trade_in_items = $active_rules['trade_in']->compute($criteria); + $tradein_items = $active_rules['trade_in']->compute($criteria); + foreach ($tradein_items as $tradein_item) + { + $items[] = $tradein_item; + } } - // TODO: need to pass service type fees to compute - $items = $active_rule->compute($criteria); + $computed_items = $active_rule->compute($criteria, $stype_fees); + foreach ($computed_items as $computed_item) + { + $items[] = $computed_item; + } foreach ($items as $item) { - $invoice_item = new InvoiceItem(); + if (isset($item['title'])) + { + $invoice_item = new InvoiceItem(); - $invoice_item->setInvoice($invoice) - ->setTitle($item['title']) - ->setQuantity($item['qty']) - ->setPrice($item['price']); + $invoice_item->setTitle($item['title']) + ->setQuantity($item['qty']) + ->setPrice($item['price']); - if (isset($item['battery'])) - $invoice_item->setBattery($item['battery']); + if (isset($item['battery'])) + $invoice_item->setBattery($item['battery']); - $invoice->addItem($invoice_item); - } - - foreach ($trade_in_items as $trade_in) - { - $invoice_item = new InvoiceItem(); - - $invoice_item->setInvoice($invoice) - ->setTitle($trade_in_item['title']) - ->setQuantity($trade_in_item['qty']) - ->setPrice($trade_initem['price']); - - $invoice->addItem($invoice_item); - } + $invoice_items[] = $invoice_item; + } + } } } + + return $invoice_items; } }