Add warranty computation service and criteria. Add command to test service. #370

This commit is contained in:
Korina Cordero 2020-03-13 11:35:15 +00:00
parent 35f237f3fa
commit 33c76d6d78
3 changed files with 191 additions and 0 deletions

View file

@ -0,0 +1,48 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use App\Service\Prowar\WarrantyComputationCriteria;
use App\Service\Prowar\WarrantyComputationService;
use DateTime;
class TestWarrantyComputationCommand extends Command
{
protected $wcs;
protected function configure()
{
$this->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);
}
}

View file

@ -0,0 +1,80 @@
<?php
namespace App\Service\Prowar;
class WarrantyComputationCriteria
{
protected $total_warranty;
protected $purchase_date; // DateTime
protected $claim_date; // DateTime
protected $battery_price;
protected $rate; // percent?
protected $warranty_limit;
public function setTotalWarranty($total_warranty)
{
$this->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;
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace App\Service\Prowar;
use App\Service\Prowar\WarrantyComputationCriteria;
use DateTime;
use DateInterval;
class WarrantyComputationService
{
public function generateWarrantyComputation(WarrantyComputationCriteria $criteria)
{
$customer_amount = 0;
// get purchase dates and claim dates
// assumption is that these are already DateTime objects
// if not, convert string to DateTime objects
// $purchase_date = DateTime::createFromFormat('Y-m-d', $criteria->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;
}
}