From 6daba0d3052d1f7848f7c63f39d88f1ef51b87fc Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 29 Nov 2019 03:41:34 +0000 Subject: [PATCH 1/3] Add command to compute expiry date for warranty. #280 --- .../ComputeWarrantyExpiryDateCommand.php | 108 ++++++++++++++++++ .../CreateCustomerFromWarrantyCommand.php | 1 - 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/Command/ComputeWarrantyExpiryDateCommand.php diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php new file mode 100644 index 00000000..4d547d3d --- /dev/null +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -0,0 +1,108 @@ +em = $em; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('warranty:computeexpirydate') + ->setDescription('Compute expiry date for existing warranties.') + ->setHelp('Comput expiry date for existing warranties.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.date_expire is null'); + $warranties = $warr_q->iterate(); + + foreach($warranties as $row) + { + $warr = $row[0]; + + error_log('Processing warranty for ' . $warr->getID()); + + $date_purchase = $warr->getDatePurchase(); + $warr_period = $this->getWarrantyPeriod($warr); + if ($warr_period != null) + { + $expiry_date = $this->computeDateExpire($date_purchase, $warr_period); + + // save expiry date + } + } + + } + + protected function getWarrantyPeriod($warr) + { + $batt_model = $warr->getBatteryModel(); + + $warranty_class = $warr->getWarrantyClass(); + $warr_period = ''; + + if (($batt_model == null) || + (empty($warranty_class))) + + { + return null; + } + + if ($batt_model != null) + { + $batteries = $batt_model->getBatteries(); + foreach($batteries as $battery) + { + // check warranty class to get warranty period + if ($warranty_class == WarrantyClass::WTY_PRIVATE) + { + $warr_period = $battery->getWarrantyPrivate(); + error_log('Warranty Period for Private: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) + { + $warr_period = $battery->getWarrantyCommercial(); + error_log('Warranty Period for Commercial: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_TNV) + { + $warr_period = $battery->getWarrantyTnv(); + error_log('Warranty Period for TNV: ' . $warr_period); + } + } + } + + return $warr_period; + } + + protected function computeDateExpire($date_create, $warranty_period) + { + $expire_date = clone $date_create; + $expire_date->add(new DateInterval('P'.$warranty_period.'M')); + return $expire_date; + } + +} diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 91ae38d1..e644fc06 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -7,7 +7,6 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Dotenv\Dotenv; use Doctrine\Common\Persistence\ObjectManager; From 243d238ad7c495d33435c7dea26313597cf28ca8 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 11 Dec 2019 08:43:15 +0000 Subject: [PATCH 2/3] Finish command to compute expiration date for existing warranties. #280 --- .../ComputeWarrantyExpiryDateCommand.php | 77 ++++++++++++------- src/Entity/BatteryModel.php | 1 + src/Entity/BatterySize.php | 1 + 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php index 4d547d3d..c2af16b2 100644 --- a/src/Command/ComputeWarrantyExpiryDateCommand.php +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -10,6 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Doctrine\Common\Persistence\ObjectManager; use App\Entity\Warranty; +use App\Entity\Battery; use App\Ramcar\WarrantyClass; @@ -31,7 +32,7 @@ class ComputeWarrantyExpiryDateCommand extends Command { $this->setName('warranty:computeexpirydate') ->setDescription('Compute expiry date for existing warranties.') - ->setHelp('Comput expiry date for existing warranties.'); + ->setHelp('Compute expiry date for existing warranties.'); } protected function execute(InputInterface $input, OutputInterface $output) @@ -50,9 +51,18 @@ class ComputeWarrantyExpiryDateCommand extends Command if ($warr_period != null) { $expiry_date = $this->computeDateExpire($date_purchase, $warr_period); - - // save expiry date } + else + { + $expiry_date = $date_purchase; + } + + // save expiry date + $warr->setDateExpire($expiry_date); + + $this->em->persist($warr); + $this->em->flush(); + $this->em->clear(); } } @@ -60,38 +70,53 @@ class ComputeWarrantyExpiryDateCommand extends Command protected function getWarrantyPeriod($warr) { $batt_model = $warr->getBatteryModel(); + $batt_size = $warr->getBatterySize(); $warranty_class = $warr->getWarrantyClass(); - $warr_period = ''; + $warr_period = 0; - if (($batt_model == null) || - (empty($warranty_class))) - + if ($batt_model == null) { + error_log('Battery model is null for warranty id ' . $warr->getID()); + return null; + } + if ($batt_size == null) + { + error_log('Battery size is null for warranty id ' . $warr->getID()); + return null; + } + if (empty($warranty_class)) + { + error_log('Warranty class is empty for warranty id ' . $warr->getID()); + return null; + } + + // find batttery using model and size + $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); + + if (empty($batteries)) + { + error_log('Battery not found for warranty id ' . $warr->getID()); return null; } - if ($batt_model != null) + foreach($batteries as $battery) { - $batteries = $batt_model->getBatteries(); - foreach($batteries as $battery) + // check warranty class to get warranty period + if ($warranty_class == WarrantyClass::WTY_PRIVATE) + { + $warr_period = $battery->getWarrantyPrivate(); + error_log('Warranty Period for Private: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) { - // check warranty class to get warranty period - if ($warranty_class == WarrantyClass::WTY_PRIVATE) - { - $warr_period = $battery->getWarrantyPrivate(); - error_log('Warranty Period for Private: ' . $warr_period); - } - if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) - { - $warr_period = $battery->getWarrantyCommercial(); - error_log('Warranty Period for Commercial: ' . $warr_period); - } - if ($warranty_class == WarrantyClass::WTY_TNV) - { - $warr_period = $battery->getWarrantyTnv(); - error_log('Warranty Period for TNV: ' . $warr_period); - } + $warr_period = $battery->getWarrantyCommercial(); + error_log('Warranty Period for Commercial: ' . $warr_period); + } + if ($warranty_class == WarrantyClass::WTY_TNV) + { + $warr_period = $battery->getWarrantyTnv(); + error_log('Warranty Period for TNV: ' . $warr_period); } } diff --git a/src/Entity/BatteryModel.php b/src/Entity/BatteryModel.php index 86f3b666..b516e8a6 100644 --- a/src/Entity/BatteryModel.php +++ b/src/Entity/BatteryModel.php @@ -68,6 +68,7 @@ class BatteryModel public function getBatteries() { + // TODO: fix this to be a proper getter function // has to return set of strings because symfony is trying to move away from role objects $str_batteries = []; foreach ($this->batteries as $battery) diff --git a/src/Entity/BatterySize.php b/src/Entity/BatterySize.php index 021e115c..9fff4d44 100644 --- a/src/Entity/BatterySize.php +++ b/src/Entity/BatterySize.php @@ -89,6 +89,7 @@ class BatterySize public function getBatteries() { + // TODO: fix this to be a proper getter function // has to return set of strings because symfony is trying to move away from role objects $str_batteries = []; foreach ($this->batteries as $battery) From c1203368c04a22acf533bfaefe2cb3a7ebb83a7e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 12 Dec 2019 08:33:29 +0000 Subject: [PATCH 3/3] Use SKU to find the battery first. #280 --- .../ComputeWarrantyExpiryDateCommand.php | 68 ++++++++++++++----- 1 file changed, 50 insertions(+), 18 deletions(-) diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php index c2af16b2..7f206cc5 100644 --- a/src/Command/ComputeWarrantyExpiryDateCommand.php +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -48,6 +48,7 @@ class ComputeWarrantyExpiryDateCommand extends Command $date_purchase = $warr->getDatePurchase(); $warr_period = $this->getWarrantyPeriod($warr); + if ($warr_period != null) { $expiry_date = $this->computeDateExpire($date_purchase, $warr_period); @@ -69,30 +70,44 @@ class ComputeWarrantyExpiryDateCommand extends Command protected function getWarrantyPeriod($warr) { + // find battery via sku/sap battery first + // if sku is null, use battery model and battery size to find battery + // if all three are null, do nothing + + $batteries = null; + + $sap_battery = $warr->getSAPBattery(); $batt_model = $warr->getBatteryModel(); $batt_size = $warr->getBatterySize(); - $warranty_class = $warr->getWarrantyClass(); - $warr_period = 0; - if ($batt_model == null) - { - error_log('Battery model is null for warranty id ' . $warr->getID()); - return null; - } - if ($batt_size == null) - { - error_log('Battery size is null for warranty id ' . $warr->getID()); - return null; - } if (empty($warranty_class)) { error_log('Warranty class is empty for warranty id ' . $warr->getID()); return null; } - - // find batttery using model and size - $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); + + if ($sap_battery != null) + { + // get the battery linked to SAP Battery using sap_battery id + $batteries = $this->em->getRepository(Battery::class)->findBy(['sap_code' => $sap_battery->getID()]); + } + else + { + if ($batt_model == null) + { + error_log('Battery model is null for warranty id ' . $warr->getID()); + return null; + } + if ($batt_size == null) + { + error_log('Battery size is null for warranty id ' . $warr->getID()); + return null; + } + + // find battery using battery model and battery size + $batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]); + } if (empty($batteries)) { @@ -100,8 +115,13 @@ class ComputeWarrantyExpiryDateCommand extends Command return null; } - foreach($batteries as $battery) + // set to -1 to show that we haven't set a warranty period yet + // cannot set initial value to 0 because warranty tnv can be 0 + $least_warranty = -1; + $warr_period = 0; + foreach ($batteries as $battery) { + // if multiple batteries, get the smallest warranty period // check warranty class to get warranty period if ($warranty_class == WarrantyClass::WTY_PRIVATE) { @@ -118,9 +138,22 @@ class ComputeWarrantyExpiryDateCommand extends Command $warr_period = $battery->getWarrantyTnv(); error_log('Warranty Period for TNV: ' . $warr_period); } + + if ($least_warranty < 0) + { + // set least warranty to the first obtained warranty period + $least_warranty = $warr_period; + } + + if ($least_warranty > $warr_period) + { + $least_warranty = $warr_period; + } } - return $warr_period; + $warranty_period = $least_warranty; + + return $warranty_period; } protected function computeDateExpire($date_create, $warranty_period) @@ -129,5 +162,4 @@ class ComputeWarrantyExpiryDateCommand extends Command $expire_date->add(new DateInterval('P'.$warranty_period.'M')); return $expire_date; } - }