diff --git a/config/services.yaml b/config/services.yaml index 42044b7d..14a5e6c8 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -91,6 +91,10 @@ services: arguments: $geofence_flag: "%env(GEOFENCE_ENABLE)%" + App\Service\WarrantyHandler: + arguments: + $em: "@doctrine.orm.entity_manager" + App\Command\SetCustomerPrivacyPolicyCommand: arguments: $policy_promo: "%env(POLICY_PROMO)%" diff --git a/src/Command/ComputeWarrantyExpiryDateCommand.php b/src/Command/ComputeWarrantyExpiryDateCommand.php new file mode 100644 index 00000000..1db6fd42 --- /dev/null +++ b/src/Command/ComputeWarrantyExpiryDateCommand.php @@ -0,0 +1,75 @@ +em = $em; + $this->wh = $wh; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('warranty:computeexpirydate') + ->setDescription('Compute expiry date for existing warranties.') + ->setHelp('Compute 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(); + + $batteries = $this->wh->getBatteriesForWarrantyPeriod($warr); + if (!empty($batteries)) + { + $warranty_class = $warr->getWarrantyClass(); + + $warr_period = $this->wh->getWarrantyPeriod($batteries, $warranty_class); + + if ($warr_period != null) + { + $expiry_date = $this->wh->computeDateExpire($date_purchase, $warr_period); + } + else + { + $expiry_date = $date_purchase; + } + + // save expiry date + $warr->setDateExpire($expiry_date); + + $this->em->persist($warr); + $this->em->flush(); + } + + $this->em->clear(); + } + } +} 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; diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 4da828c6..afdd5158 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -7,10 +7,13 @@ use App\Entity\SAPBattery; use App\Entity\Battery; use App\Entity\BatteryModel; use App\Entity\BatterySize; +use App\Entity\Invoice; use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; +use App\Service\WarrantyHandler; + use Doctrine\ORM\Query; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; @@ -372,13 +375,14 @@ class WarrantyController extends Controller /** * @Menu(selected="warranty_list") */ - public function uploadSubmit(Request $req, EntityManagerInterface $em) + public function uploadSubmit(Request $req, EntityManagerInterface $em, + WarrantyHandler $wh) { // retrieve temporary info for file $file = $req->files->get('csv_file'); // process the csv file - $inv_entries = $this->processWarrantyFile($file, $em); + $inv_entries = $this->processWarrantyFile($file, $em, $wh); $resp = new StreamedResponse(); $resp->setCallback(function() use($inv_entries) { @@ -422,7 +426,8 @@ class WarrantyController extends Controller return $resp; } - protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em) + protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em, + WarrantyHandler $wh) { // attempt to open file try @@ -473,8 +478,9 @@ class WarrantyController extends Controller $serial = trim($fields[10]); $purchase_date = trim($fields[12]); $battery_id = trim($fields[16]); + $batt_invoice = trim($fields[11]); - $plate_number = $this->cleanPlateNumber($plate); + $plate_number = $wh->cleanPlateNumber($plate); // check if purchase_date or plate_number or serial is empty or if // purchase date is valid @@ -511,7 +517,7 @@ class WarrantyController extends Controller 'vehicle_year' => trim($fields[8]), 'vehicle_plate_number' => $plate_number, 'battery_serial_number' => $serial, - 'battery_sales_invoice' => trim($fields[11]), + 'battery_sales_invoice' => $batt_invoice, 'battery_date_purchase' => $purchase_date, 'distributor_name' => trim($fields[13]), 'distributor_address' => trim($fields[14]), @@ -526,81 +532,42 @@ class WarrantyController extends Controller // additional validation // check if serial number and plate number already exists $warr_results = $em->getRepository(Warranty::class)->findBy(['serial' => $serial, 'plate_number' => $plate_number]); + + // get battery via the invoice because battery_id doesn't match what's in the live data + // get job order via invoice to get the warranty class + $warranty_class = ''; + $batt_list = array(); + + // find invoice + $invoice = $em->getRepository(Invoice::class)->find($batt_invoice); + if (!empty($invoice)) + { + // get job order + $jo = $invoice->getJobOrder(); + + // get warranty class + $warranty_class = $jo->getWarrantyClass(); + + // get battery + $invoice_items = $invoice->getItems(); + foreach ($invoice_items as $item) + { + $battery = $item->getBattery(); + if ($battery != null) + { + $batt_list[] = $item->getBattery(); + } + } + } + if (!empty($warr_results)) { foreach($warr_results as $warr) { - // check if details are complete - //error_log('Updating warranty with serial number ' . $serial . ' and plate number ' . $plate_number); - if (empty($warr->getFirstName())) - { - if (!empty($first_name)) - { - $warr->setFirstName($first_name); - } - } - if (empty($warr->getLastName())) - { - if (!empty($last_name)) - { - $warr->setLastName($last_name); - } - } - if (empty($warr->getMobileNumber())) - { - if (!empty($mobile_number)) - { - $warr->setMobileNumber($mobile_number); - } - } - if ((empty($warr->getBatteryModel())) || - (empty($warr->getBatterySize()))) - { - if (!empty($battery_id)) - { - // find battery - $battery = $em->getRepository(Battery::class)->find($battery_id); - if (!empty($battery)) - { - // get the battery model and battery size - $model_id = $battery->getModel()->getID(); - $size_id = $battery->getSize()->getID(); - - $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); - $bty_size = $em->getRepository(BatterySize::class)->find($size_id); - - if ($bty_model != null) - { - $warr->setBatteryModel($bty_model); - } - if ($bty_size != null) - { - $warr->setBatterySize($bty_size); - } - $sap_code = $battery->getSAPCode(); - if (!empty($sap_code)) - { - // find sap battery - $sap_batt = $em->getRepository(SAPBattery::class)->find($sap_code); - if (!empty($sap_batt)) - { - $warr->setSAPBattery($sap_batt); - } - } - } - } - } - if (empty($warr->getDatePurchase())) - { - if (!empty($date_purchase)) - { - $warr->setDatePurchase($date_purchase); - } - } - - // TODO: compute expiry date - $em->persist($warr); - $em->flush(); + // call service to check if warranty details is incomplete and then update warranty + // using details from csv file + //error_log('Updating warranty for ' . $warr->getID()); + $wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase); } } else @@ -614,57 +581,10 @@ class WarrantyController extends Controller continue; } - //error_log('Adding warranty with serial number ' . $serial . ' and plate number ' . $plate_number); - // new warranty - $warranty = new Warranty(); + //error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number); - // get the battery purchased - // check battery first. If not found, check sap_battery - $battery = $em->getRepository(Battery::class)->find($battery_id); - if ($battery != null) - { - // get the battery model and battery size - $model_id = $battery->getModel()->getID(); - $size_id = $battery->getSize()->getID(); - - $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); - $bty_size = $em->getRepository(BatterySize::class)->find($size_id); - - if ($bty_model != null) - { - $warranty->setBatteryModel($bty_model); - } - - if ($bty_size != null) - { - $warranty->setBatterySize($bty_size); - } - } - else - { - // find battery in sap_battery - $battery = $em->getRepository(SAPBattery::class)->find($battery_id); - if ($battery != null) - { - // battery is SAPBattery - $warranty->setSAPBattery($battery); - } - } - - // TODO: compute expiry date - - // set and save values - $warranty->setSerial($serial) - ->setPlateNumber($plate_number) - ->setFirstName($first_name) - ->setLastName($last_name) - ->setMobileNumber($mobile_number) - ->setDatePurchase($date_purchase); - - $em->persist($warranty); - $em->flush(); + $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } $row_num++; 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) diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php new file mode 100644 index 00000000..4dd4dcae --- /dev/null +++ b/src/Service/WarrantyHandler.php @@ -0,0 +1,293 @@ +em = $em; + } + + public function createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, + $batt_list, DateTime $date_purchase, $warranty_class) + { + // new warranty + $warranty = new Warranty(); + + foreach ($batt_list as $battery) + { + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $this->em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warranty->setBatteryModel($bty_model); + } + if ($bty_size != null) + { + $warranty->setBatterySize($bty_size); + } + + $sap_code = $battery->getSAPCode(); + if (!empty($sap_code)) + { + // find sap battery + $sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code); + if ($sap_battery != null) + { + $warranty->setSAPBattery($sap_battery); + } + } + } + + // compute expiry date + if ((!empty($warranty_class)) && + (count($batt_list) != 0)) + { + $period = $this->getWarrantyPeriod($batt_list, $warranty_class); + $date_expire = $this->computeDateExpire($date_purchase, $period); + + $warranty->setDateExpire($date_expire); + } + + // set and save values + $warranty->setSerial($serial) + ->setPlateNumber($plate_number) + ->setFirstName($first_name) + ->setLastName($last_name) + ->setMobileNumber($mobile_number) + ->setDatePurchase($date_purchase); + + $this->em->persist($warranty); + $this->em->flush(); + $this->em->clear(); + } + + public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $batt_list, DateTime $date_purchase) + { + // TODO: add serial and plate number to update + // TODO: check if data from existing warranty matches the new data + // check if details are complete + if (empty($warr->getFirstName())) + { + if (!empty($first_name)) + { + $warr->setFirstName($first_name); + } + } + if (empty($warr->getLastName())) + { + if (!empty($last_name)) + { + $warr->setLastName($last_name); + } + } + if (empty($warr->getMobileNumber())) + { + if (!empty($mobile_number)) + { + $warr->setMobileNumber($mobile_number); + } + } + if ((empty($warr->getBatteryModel())) || + (empty($warr->getBatterySize()))) + { + if (count($batt_list) != 0) + { + foreach ($batt_list as $battery) + { + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $this->em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warranty->setBatteryModel($bty_model); + } + if ($bty_size != null) + { + $warranty->setBatterySize($bty_size); + } + + $sap_code = $battery->getSAPCode(); + if (!empty($sap_code)) + { + // find sap battery + $sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code); + if ($sap_battery != null) + { + $warranty->setSAPBattery($sap_battery); + } + } + } + } + } + + $purchase_date = $warr->getDatePurchase(); + if (empty($purchase_date)) + { + if (!empty($date_purchase)) + { + $warr->setDatePurchase($date_purchase); + } + $purchase_date = $date_purchase; + } + + if (empty($warr->getDateExpire())) + { + $batteries = []; + if (count($batt_list) == 0) + { + $batteries = $this->getBatteriesForWarrantyPeriod($warr); + } + else + { + $batteries = $batt_list; + } + + if (!empty($batteries)) + { + $period = $this->getWarrantyPeriod($batteries, $warr->getWarrantyClass()); + if (!empty($purchase_date)) + { + $expire_date = $this->computeDateExpire($purchase_date, $period); + $warr->setDateExpire($expire_date); + } + } + } + + $this->em->persist($warr); + $this->em->flush(); + $this->em->clear(); + } + + public function computeDateExpire($date_create, $warranty_period) + { + $expire_date = clone $date_create; + $expire_date->add(new DateInterval('P'.$warranty_period.'M')); + return $expire_date; + } + + public function getWarrantyPeriod($batteries, $warranty_class) + { + // 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) + { + $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); + } + + 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; + } + } + + $warranty_period = $least_warranty; + + return $warranty_period; + } + + public function getBatteriesForWarrantyPeriod($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(); + + if (empty($warranty_class)) + { + error_log('Warranty class is empty for warranty id ' . $warr->getID()); + return null; + } + + 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)) + { + error_log('Battery not found for warranty id ' . $warr->getID()); + return null; + } + + return $batteries; + } + + + public function cleanPlateNumber($plate) + { + // remove spaces and make upper case + return strtoupper(str_replace(' ', '', $plate)); + } + +}