From c5b395d720589b6296c58bc158a80e211d8a3e7d Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 24 Jan 2024 04:02:14 -0500 Subject: [PATCH] Add price tier for battery replacement warranty. #782 --- .../BatteryReplacementWarranty.php | 41 ++++++++++++++++++- src/InvoiceRule/BatterySales.php | 2 - src/Service/InvoiceManager.php | 2 +- src/Service/PriceTierManager.php | 4 +- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/InvoiceRule/BatteryReplacementWarranty.php b/src/InvoiceRule/BatteryReplacementWarranty.php index ee1497b5..8c2dafd4 100644 --- a/src/InvoiceRule/BatteryReplacementWarranty.php +++ b/src/InvoiceRule/BatteryReplacementWarranty.php @@ -11,14 +11,19 @@ use App\Ramcar\TradeInType; use App\Entity\Battery; use App\Entity\ServiceOffering; +use App\Entity\ItemType; + +use App\Service\PriceTierManager; class BatteryReplacementWarranty implements InvoiceRuleInterface { protected $em; + protected $pt_manager; - public function __construct(EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, PriceTierManager $pt_manager) { $this->em = $em; + $this->pt_manager = $pt_manager; } public function getID() @@ -29,6 +34,7 @@ class BatteryReplacementWarranty implements InvoiceRuleInterface public function compute($criteria, &$total) { $stype = $criteria->getServiceType(); + $pt_id = $criteria->getPriceTier(); $items = []; if ($stype == $this->getID()) @@ -40,7 +46,14 @@ class BatteryReplacementWarranty implements InvoiceRuleInterface { $batt = $entry['battery']; $qty = 1; - $price = $this->getServiceTypeFee(); + + // check if price tier has item price + $pt_price = $this->getPriceTierItemPrice($pt_id); + + if ($pt_price == null) + $price = $this->getServiceTypeFee(); + else + $price = $pt_price; $items[] = [ 'service_type' => $this->getID(), @@ -117,6 +130,30 @@ class BatteryReplacementWarranty implements InvoiceRuleInterface return null; } + protected function getPriceTierItemPrice($pt_id) + { + // price_tier is default + if ($pt_id == 0) + return null; + + // find the item type for service offering + $item_type = $this->em->getRepository(ItemType::class)->findOneBy(['code' => 'service_offering']); + if ($item_type == null) + return null; + + // find the service offering + $code = 'battery_replacement_warranty_fee'; + $service = $this->em->getRepository(ServiceOffering::class)->findOneBy(['code' => $code]); + + $item_type_id = $item_type->getID(); + $item_id = $service->getID(); + + $price = $this->pt_manager->getItemPrice($pt_id, $item_type_id, $item_id); + + return $price; + } + + protected function getTitle($battery) { $title = $battery->getModel()->getName() . ' ' . $battery->getSize()->getName() . ' - Service Unit'; diff --git a/src/InvoiceRule/BatterySales.php b/src/InvoiceRule/BatterySales.php index 3ff9a995..645e545b 100644 --- a/src/InvoiceRule/BatterySales.php +++ b/src/InvoiceRule/BatterySales.php @@ -35,8 +35,6 @@ class BatterySales implements InvoiceRuleInterface $stype = $criteria->getServiceType(); $pt = $criteria->getPriceTier(); - error_log('price tier ' . $pt); - $items = []; if ($stype == $this->getID()) { diff --git a/src/Service/InvoiceManager.php b/src/Service/InvoiceManager.php index 93d6bafd..6e37a207 100644 --- a/src/Service/InvoiceManager.php +++ b/src/Service/InvoiceManager.php @@ -46,7 +46,7 @@ class InvoiceManager implements InvoiceGeneratorInterface // TODO: get list of invoice rules from .env or a json file? return [ new InvoiceRule\BatterySales($this->em, $this->pt_manager), - new InvoiceRule\BatteryReplacementWarranty($this->em), + new InvoiceRule\BatteryReplacementWarranty($this->em, $this->pt_manager), new InvoiceRule\Jumpstart($this->em), new InvoiceRule\JumpstartWarranty($this->em), new InvoiceRule\PostRecharged($this->em), diff --git a/src/Service/PriceTierManager.php b/src/Service/PriceTierManager.php index e4174e78..c572af76 100644 --- a/src/Service/PriceTierManager.php +++ b/src/Service/PriceTierManager.php @@ -35,7 +35,9 @@ class PriceTierManager $ip_result = $ip_stmt->executeQuery(); - $actual_price = 0; + // results found + $actual_price = null; + // go through rows while ($row = $ip_result->fetchAssociative()) {