From 33c76d6d7861404752d32a52f11aeea5b2bc4778 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 13 Mar 2020 11:35:15 +0000 Subject: [PATCH] Add warranty computation service and criteria. Add command to test service. #370 --- .../TestWarrantyComputationCommand.php | 48 +++++++++++ .../Prowar/WarrantyComputationCriteria.php | 80 +++++++++++++++++++ .../Prowar/WarrantyComputationService.php | 63 +++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 src/Command/TestWarrantyComputationCommand.php create mode 100644 src/Service/Prowar/WarrantyComputationCriteria.php create mode 100644 src/Service/Prowar/WarrantyComputationService.php diff --git a/src/Command/TestWarrantyComputationCommand.php b/src/Command/TestWarrantyComputationCommand.php new file mode 100644 index 00000000..ee61eef7 --- /dev/null +++ b/src/Command/TestWarrantyComputationCommand.php @@ -0,0 +1,48 @@ +setName('test:generatewarrantycomputation') + ->setDescription('Test Prowar generate warranty computation service.') + ->setHelp('Test Prowar generate warranty computation service.'); + } + + public function __construct(WarrantyComputationService $wcs) + { + $this->wcs = $wcs; + + parent::__construct(); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $warr_criteria = new WarrantyComputationCriteria(); + + // set test values + $warr_criteria->setTotalWarranty(24) + ->setPurchaseDate(DateTime::createFromFormat('Y-m-d', '2018-06-15')) + ->setClaimDate(new DateTime()) + ->setBatteryPrice(100.00) + ->setRate(5) + ->setWarrantyLimit(12); + + $amount = $this->wcs->generateWarrantyComputation($warr_criteria); + + error_log('Customer pays ' . $amount); + } +} diff --git a/src/Service/Prowar/WarrantyComputationCriteria.php b/src/Service/Prowar/WarrantyComputationCriteria.php new file mode 100644 index 00000000..5d1de0c6 --- /dev/null +++ b/src/Service/Prowar/WarrantyComputationCriteria.php @@ -0,0 +1,80 @@ +total_warranty = $total_warranty; + return $this; + } + + public function getTotalWarranty() + { + return $this->total_warranty; + } + + public function setPurchaseDate($purchase_date) + { + $this->purchase_date = $purchase_date; + return $this; + } + + public function getPurchaseDate() + { + return $this->purchase_date; + } + + public function setClaimDate($claim_date) + { + $this->claim_date = $claim_date; + return $this; + } + + public function getClaimDate() + { + return $this->claim_date; + } + + public function setBatteryPrice($battery_price) + { + $this->battery_price = $battery_price; + return $this; + } + + public function getBatteryPrice() + { + return $this->battery_price; + } + + public function setRate($rate) + { + $this->rate = $rate; + return $this; + } + + public function getRate() + { + return $this->rate; + } + + public function setWarrantyLimit($warranty_limit) + { + $this->warranty_limit = $warranty_limit; + return $this; + } + + public function getWarrantyLimit() + { + return $this->warranty_limit; + } + +} diff --git a/src/Service/Prowar/WarrantyComputationService.php b/src/Service/Prowar/WarrantyComputationService.php new file mode 100644 index 00000000..723f2a48 --- /dev/null +++ b/src/Service/Prowar/WarrantyComputationService.php @@ -0,0 +1,63 @@ +getPurchaseDate()); + // $claim_date = DateTime::createFromFormat('Y-m-d', $criteria->getClaimDate()); + $purchase_date = $criteria->getPurchaseDate(); + $claim_date = $criteria->getClaimdate(); + + // compute number of months between purchase date and claim date + $interval = $purchase_date->diff($claim_date); + + // format the computed difference in months + days + $diff_in_years = $interval->y; + $diff_in_months = $interval->m; + $diff_in_days = $interval->d; + + // convert the difference in years to months + // then add to diff_in_months + $year_diff = $diff_in_years * 12; + $diff_in_months +=$year_diff; + + // check if difference in months is greater than the total warranty + // meaning out of warranty + $total_warranty = $criteria->getTotalWarranty(); + if ($diff_in_months > $total_warranty) + $customer_amount = $criteria->getBatteryPrice(); + else + { + // check if difference in months is greater than warranty limit + $warranty_limit = $criteria->getWarrantyLimit(); + if (($diff_in_months > $warranty_limit) && ($diff_in_days > 0)) + { + // compute pro-rate + // assuming percentage + // this whole thing can still change + $battery_price = $criteria->getBatteryPrice(); + $rate = $criteria->getRate() * 0.01; + + $total_rate = ($diff_in_months - $warranty_limit) * $rate; + + $customer_amount = $total_rate * $battery_price; + } + } + + return $customer_amount; + + } +}