From 4b11b7e200750874551e327aa59d896c62995512 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 15 May 2020 05:08:32 +0000 Subject: [PATCH 1/5] Create new command to get warranties that need fixing. #407 --- src/Command/GetFlawedWarrantiesCommand.php | 248 +++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 src/Command/GetFlawedWarrantiesCommand.php diff --git a/src/Command/GetFlawedWarrantiesCommand.php b/src/Command/GetFlawedWarrantiesCommand.php new file mode 100644 index 00000000..5ba04981 --- /dev/null +++ b/src/Command/GetFlawedWarrantiesCommand.php @@ -0,0 +1,248 @@ +em = $em; + $this->wh = $wh; + + $this->loadSAPBatteries(); + + parent::__construct(); + } + + protected function configure() + { + $this->setName('warranty:getflawedwarranties') + ->setDescription('Get warranties that need to be fixed.') + ->setHelp('Get flawed warranties') + ->addArgument('start_date', InputArgument::REQUIRED, 'Start Date') + ->addArgument('end_date', InputArgument::REQUIRED, 'End Date'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $s_date = $input->getArgument('start_date'); + $e_date = $input->getArgument('end_date'); + + $start_date = DateTime::createFromFormat('Ymd', $s_date); + $end_date = DateTime::createFromFormat('Ymd', $e_date); + $end_date->setTime(23, 59); + + $missing_warranties = []; + $comm_private_warranties = []; + $invalid_plate_numbers = []; + $duplicate_warranties = []; + $sdate_expiry_wrong_warranties = []; + + $first_results_set = $this->getMissingWarranties($start_date, $end_date); + //$second_results_set = $this->getWarrantiesWithWrongEntries($start_date, $end_date); + //$third_results_set = $this->getDuplicateWarranties($start_date, $end_date); + + $missing_warranties = $first_results_set['missing']; + $invalid_plate_numbers = $first_results_set['invalid_plate_number']; + $comm_private_warranties = $first_results_set['commercial_private']; + + // our warranty error categories: missing, commercial_private, invalid plate number, duplicate, start date of expiry computation wrong + $this->outputResults($missing_warranties, $invalid_plate_numbers, $comm_prvate_warranties); + + return 0; + } + + protected function getMissingWarranties($start_date, $end_date) + { + $em = $this->em; + + // get all job orders with battery new service type within date range + $jo_query = $em->createQuery('select ii,i,jo,cv from App\Entity\InvoiceItem ii inner join ii.invoice i inner join i.job_order jo inner join jo.cus_vehicle cv join jo.customer c where ii.battery is not null and jo.service_type = :service_type and jo.date_schedule > :date_start and jo.date_schedule < :date_end'); + $jo_query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW) + ->setParameter('date_start', $start_date) + ->setParameter('date_end', $end_date); + + $jos = $jo_query->iterate(); + + $invalids = []; + $missings = []; + $comm_privates = []; + + foreach($jos as $row) + { + $invoice_item = $row[0]; + $invoice = $invoice_item->getInvoice(); + $jo = $invoice->getJobOrder(); + $cv = $jo->getCustomerVehicle(); + $customer = $jo->getCustomer(); + + if ($invoice_item != null) + { + if($invoice_item->getBattery() != null) + { + // manually retrieve the SAPBattery using the SAPCode + $battery_sap_code = $invoice_item->getBattery()->getSAPCode(); + $found_battery = $this->findSAPBattery($battery_sap_code); + $sap_code = '\'' . $battery_sap_code . '\''; + if (!$found_battery) + { + $sap_code = 'NULL'; + } + + // have to use this erroneous setting of the warranty period + // to be able to find those warranties that were added with this warranty period. + // warranties found with this warranty period will be marked as commercial_private + $warranty_period = 0; + if ($invoice_item->getBattery()->getWarrantyPrivate() != null) + { + $warranty_period = $invoice_item->getBattery()->getWarrantyPrivate(); + } + if ($invoice_item->getBattery()->getWarrantyCommercial() != null) + { + $warranty_period = $invoice_item->getBattery()->getWarrantyCommercial(); + } + if ($invoice_item->getBattery()->getWarrantyTnv() != null) + { + $warranty_period = $invoice_item->getBattery()->getWarrantyTnv(); + } + + if ($invoice->getDateCreate() != null) + { + // check if plate number is "clean". If not, do not insert into warranty + if (!(Warranty::cleanPlateNumber($cv->getPlateNumber()))) + { + // log with the dupes + $invalids[] = [ + 'jo_id' => $jo->getID(), + 'plate_number' => $cv->getPlateNumber(), + 'expiry_date' => '', + ]; + + continue; + } + + // check if warranty already exists + $cleaned_plate_number = Warranty::cleanPlateNumber($cv->getPlateNumber()); + + // use date_create of invoice as start date, as decided in group chat + $expiry_date_date_create = $this->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period); + $results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cleaned_plate_number, + 'date_expire' => $expiry_date_date_create]);; + + if (empty($results)) + { + $missings[] = [ + 'jo_id' => $jo->getID(), + 'plate_number' => $cv->getPlateNumber(), + 'expiry_date' => $expiry_date_date_create->format('Y-m-d'), + ]; + } + else + { + foreach ($results as $warranty) + { + $comm_privates[] = [ + 'warranty_id' => $warranty->getID(), + 'plate_number' => $warranty->getPlateNumber(), + 'expiry_date' => $warranty->getDateExpire()->format('Y-m-d'), + ]; + } + } + } + } + } + + $em->detach($row[0]); + $em->clear(); + } + + $res = [ + 'missing' => $missings, + 'invalid_plate_number' => $invalids, + 'commercial_private' => $comm_privates, + ]; + + return $res; + + } + + protected function getWarrantiesWithWrongEntries($start_date, $end_date) + { + $em = $this->em; + + // get all job orders with battery new service type within date range + $jo_query = $em->createQuery('select ii,i,jo,cv from App\Entity\InvoiceItem ii inner join ii.invoice i inner join i.job_order jo inner join jo.cus_vehicle cv join jo.customer c where ii.battery is not null and jo.service_type = :service_type and jo.date_schedule > :date_start and jo.date_schedule < :date_end'); + $jo_query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW) + ->setParameter('date_start', $start_date) + ->setParameter('date_end', $end_date); + + $jos = $jo_query->iterate(); + + foreach($jos as $row) + { + $invoice_item = $row[0]; + $invoice = $invoice_item->getInvoice(); + $jo = $invoice->getJobOrder(); + $cv = $jo->getCustomerVehicle(); + $customer = $jo->getCustomer(); + + $em->detach($row[0]); + $em->clear(); + } + + } + + protected function getDuplicateWarranties($start_date, $end_date) + { + } + + protected function outputResults($missing, $invalid, $commercial_private) + { + // output one file per array in csv format + } + + protected function loadSAPBatteries() + { + $this->sapbatt_hash = []; + + $sap_batteries = $this->em->getRepository(SAPBattery::class)->findAll(); + foreach($sap_batteries as $sap_batt) + { + $id = $sap_batt->getID(); + $brand_size = $sap_batt->getBrand()->getID() . " " . $sap_batt->getSize()->getID(); + $this->sapbatt_hash[$id] = $brand_size; + } + } + + protected function findSAPBattery($batt_id) + { + if (!isset($this->sapbatt_hash[$batt_id])) + { + return false; + } + + return true; + } + + + +} -- 2.43.5 From ed2e54f42897007860223a6e2a25b232a2a358cf Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 18 May 2020 07:17:58 +0000 Subject: [PATCH 2/5] Add output files for missing warranties, invalid plate numbers, and commercial private warranties. #407 --- src/Command/GetFlawedWarrantiesCommand.php | 86 +++++++++++++++++++++- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/src/Command/GetFlawedWarrantiesCommand.php b/src/Command/GetFlawedWarrantiesCommand.php index 5ba04981..b93e2151 100644 --- a/src/Command/GetFlawedWarrantiesCommand.php +++ b/src/Command/GetFlawedWarrantiesCommand.php @@ -17,6 +17,8 @@ use App\Ramcar\ServiceType; use App\Service\WarrantyHandler; +use DateTime; + class GetFlawedWarrantiesCommand extends Command { protected $em; @@ -66,7 +68,7 @@ class GetFlawedWarrantiesCommand extends Command $comm_private_warranties = $first_results_set['commercial_private']; // our warranty error categories: missing, commercial_private, invalid plate number, duplicate, start date of expiry computation wrong - $this->outputResults($missing_warranties, $invalid_plate_numbers, $comm_prvate_warranties); + $this->outputResults($s_date, $e_date, $missing_warranties, $invalid_plate_numbers, $comm_private_warranties); return 0; } @@ -130,7 +132,6 @@ class GetFlawedWarrantiesCommand extends Command // check if plate number is "clean". If not, do not insert into warranty if (!(Warranty::cleanPlateNumber($cv->getPlateNumber()))) { - // log with the dupes $invalids[] = [ 'jo_id' => $jo->getID(), 'plate_number' => $cv->getPlateNumber(), @@ -144,7 +145,7 @@ class GetFlawedWarrantiesCommand extends Command $cleaned_plate_number = Warranty::cleanPlateNumber($cv->getPlateNumber()); // use date_create of invoice as start date, as decided in group chat - $expiry_date_date_create = $this->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period); + $expiry_date_date_create = $this->wh->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period); $results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cleaned_plate_number, 'date_expire' => $expiry_date_date_create]);; @@ -215,9 +216,86 @@ class GetFlawedWarrantiesCommand extends Command { } - protected function outputResults($missing, $invalid, $commercial_private) + protected function outputResults($start_date, $end_date, $missing, $invalid, $commercial_private) { + $date_range = $start_date . '-' . $end_date; // output one file per array in csv format + // missing warranties + // we output the JOs with no warranties + $missing_csv_filename = $date_range . '-' . 'missing_warranties.csv'; + try + { + $missing_fh = fopen($missing_csv_filename, 'a'); + } + catch (Exception $e) + { + throw new Exception('The file "' . $missing_csv_filename . '" could not be opened.'); + } + + if ((count($missing)) > 0) + { + fputcsv($missing_fh, [ + 'JO ID', + 'Plate Number', + 'Expiry Date', + ]); + + foreach($missing as $missing_row) + { + fputcsv($missing_fh, $missing_row); + } + } + + // invalid plate numbers + $invalid_plates_csv_filename = $date_range . '-' . 'invalid_plate_jos.csv'; + try + { + $invalid_plates_fh = fopen($invalid_plates_csv_filename, 'a'); + } + catch (Exception $e) + { + throw new Exception('The file "' . $invalid_plates_csv_filename . '" could not be opened.'); + } + + if ((count($invalid)) > 0) + { + fputcsv($invalid_plates_fh, [ + 'JO ID', + 'Plate Number', + 'Expiry Date', + ]); + + foreach($invalid as $invalid_row) + { + fputcsv($invalid_plates_fh, $invalid_row); + } + } + + // warranties whose expiration dates were set to the commercial period + // but warranty class is private + $comm_private_csv_filename = $date_range . '-' . 'comm_private_warranties.csv'; + try + { + $comm_private_fh = fopen($comm_private_csv_filename, 'a'); + } + catch (Exception $e) + { + throw new Exception('The file "' . $comm_private_csv_filename . '" could not be opened.'); + } + + if ((count($commercial_private)) > 0) + { + fputcsv($comm_private_fh, [ + 'Warranty ID', + 'Plate Number', + 'Expiry Date', + ]); + + foreach($commercial_private as $comm_private_row) + { + fputcsv($comm_private_fh, $comm_private_row); + } + } } protected function loadSAPBatteries() -- 2.43.5 From 43b203aaefb1e87ef16453139e48f92fdddd3a87 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 18 May 2020 10:05:54 +0000 Subject: [PATCH 3/5] Find warranties with date fulfilled as the start date of expiration date computation. #407 --- src/Command/GetFlawedWarrantiesCommand.php | 137 ++++++++++++++++++++- 1 file changed, 131 insertions(+), 6 deletions(-) diff --git a/src/Command/GetFlawedWarrantiesCommand.php b/src/Command/GetFlawedWarrantiesCommand.php index b93e2151..6a3f4026 100644 --- a/src/Command/GetFlawedWarrantiesCommand.php +++ b/src/Command/GetFlawedWarrantiesCommand.php @@ -60,15 +60,17 @@ class GetFlawedWarrantiesCommand extends Command $sdate_expiry_wrong_warranties = []; $first_results_set = $this->getMissingWarranties($start_date, $end_date); - //$second_results_set = $this->getWarrantiesWithWrongEntries($start_date, $end_date); + $second_results_set = $this->getWarrantiesWithWrongEntries($start_date, $end_date); //$third_results_set = $this->getDuplicateWarranties($start_date, $end_date); $missing_warranties = $first_results_set['missing']; $invalid_plate_numbers = $first_results_set['invalid_plate_number']; $comm_private_warranties = $first_results_set['commercial_private']; + $sdate_expiry_wrong_warranties = $second_results_set['wrong_expiry_date']; // our warranty error categories: missing, commercial_private, invalid plate number, duplicate, start date of expiry computation wrong - $this->outputResults($s_date, $e_date, $missing_warranties, $invalid_plate_numbers, $comm_private_warranties); + $this->outputResults($s_date, $e_date, $missing_warranties, $invalid_plate_numbers, $comm_private_warranties, + $sdate_expiry_wrong_warranties, $duplicate_warranties); return 0; } @@ -151,6 +153,11 @@ class GetFlawedWarrantiesCommand extends Command if (empty($results)) { + // TODO: find warranty using expiration date with start date, date fulfilled, if date fulfilled is not null + // get warranty periods from battery, this time, check warranty class + // recompute expiration date with start date, date fulfilled + // if still no warranty, put JO in $missing + // if there is a warranty, put warranty in wrong expiry date $missings[] = [ 'jo_id' => $jo->getID(), 'plate_number' => $cv->getPlateNumber(), @@ -198,6 +205,8 @@ class GetFlawedWarrantiesCommand extends Command $jos = $jo_query->iterate(); + $expiries = []; + foreach($jos as $row) { $invoice_item = $row[0]; @@ -206,19 +215,88 @@ class GetFlawedWarrantiesCommand extends Command $cv = $jo->getCustomerVehicle(); $customer = $jo->getCustomer(); + $clean_plate_num = Warranty::cleanPlateNumber($cv->getPlateNumber()); + if (!($clean_plate_num)) + { + error_log('invalid plate'); + continue; + } + if ($invoice_item == null) + { + error_log('invoice item null'); + continue; + } + if ($invoice_item->getBattery() == null) + { + error_log('invoice item not a battery'); + continue; + } + + error_log('clean plate, invoice item not null, battery not null'); + // manually retrieve the SAPBattery using the SAPCode + $battery_sap_code = $invoice_item->getBattery()->getSAPCode(); + $found_battery = $this->findSAPBattery($battery_sap_code); + $sap_code = '\'' . $battery_sap_code . '\''; + if (!$found_battery) + { + $sap_code = 'NULL'; + } + + // check if warranty exists with plate number and date_create of invoice + $date = $jo->getInvoice()->getDateCreate(); + $date_create_invoice_str = $date->format('Y-m-d'); + $date_create_invoice = DateTime::createFromFormat('Y-m-d', $date_create_invoice_str); + + $warranties_date_create = $this->em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_plate_num, 'date_purchase' => $date_create_invoice]); + if (empty($warranties_date_create)) + { + // check if warranty exists with plate number and date_fulfilled + $datetime_fulfilled = $jo->getDateFulfill(); + if ($datetime_fulfilled != null) + { + $datetime_fulfilled_str = $datetime_fulfilled->format('Y-m-d'); + $date_fulfilled = DateTime::createFromFormat('Y-m-d', $datetime_fulfilled_str); + + $warranties_date_fulfilled = $this->em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_plate_num, 'date_purchase' => $date_fulfilled]); + if (!(empty($warranties_date_fulfilled))) + { + foreach ($warranties_date_fulfilled as $warranty) + { + $expiries[] = [ + 'warranty_id' => $warranty->getID(), + 'plate_number' => $warranty->getPlateNumber(), + 'expiry_date' => $warranty->getDateExpire()->format('Y-m-d'), + ]; + } + } + } + else + { + error_log('no warranty at all?'); + } + } + $em->detach($row[0]); $em->clear(); } + $res = [ + 'wrong_expiry_date' => $expiries + ]; + + return $res; + } protected function getDuplicateWarranties($start_date, $end_date) { } - protected function outputResults($start_date, $end_date, $missing, $invalid, $commercial_private) + protected function outputResults($start_date, $end_date, $missing, $invalid, $commercial_private, + $expiry_wrong, $duplicate) { $date_range = $start_date . '-' . $end_date; + // output one file per array in csv format // missing warranties // we output the JOs with no warranties @@ -296,6 +374,56 @@ class GetFlawedWarrantiesCommand extends Command fputcsv($comm_private_fh, $comm_private_row); } } + + // start of expiry date computation is not date_create of invoice + $expiry_wrong_csv_filename = $date_range . '-' . 'expiry_wrong_warranties.csv'; + try + { + $expiry_wrong_fh = fopen($expiry_wrong_csv_filename, 'a'); + } + catch (Exception $e) + { + throw new Exception('The file "' . $expiry_wrong_csv_filename . '" could not be opened.'); + } + + if ((count($expiry_wrong)) > 0) + { + fputcsv($expiry_wrong_fh, [ + 'Warranty ID', + 'Plate Number', + 'Expiry Date', + ]); + + foreach($expiry_wrong as $expiry_wrong_row) + { + fputcsv($expiry_wrong_fh, $expiry_wrong_row); + } + } + + // duplicate warranties + $duplicate_csv_filename = $date_range . '-' . 'duplicate_warranties.csv'; + try + { + $duplicate_fh = fopen($duplicate_csv_filename, 'a'); + } + catch (Exception $e) + { + throw new Exception('The file "' . $duplicate_csv_filename . '" could not be opened.'); + } + + if ((count($duplicate)) > 0) + { + fputcsv($duplicate_fh, [ + 'Warranty ID', + 'Plate Number', + 'Expiry Date', + ]); + + foreach($duplicate as $duplicate_row) + { + fputcsv($duplicate_fh, $duplicate_row); + } + } } protected function loadSAPBatteries() @@ -320,7 +448,4 @@ class GetFlawedWarrantiesCommand extends Command return true; } - - - } -- 2.43.5 From f2de60d65a0865f71c3025ac6f4f42cdee5f5864 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 21 May 2020 09:50:42 +0000 Subject: [PATCH 4/5] Add more checking for erroneous warranties. #407 --- src/Command/GetFlawedWarrantiesCommand.php | 348 +++++++++++++-------- 1 file changed, 223 insertions(+), 125 deletions(-) diff --git a/src/Command/GetFlawedWarrantiesCommand.php b/src/Command/GetFlawedWarrantiesCommand.php index 6a3f4026..2b93938b 100644 --- a/src/Command/GetFlawedWarrantiesCommand.php +++ b/src/Command/GetFlawedWarrantiesCommand.php @@ -12,8 +12,12 @@ use Doctrine\ORM\EntityManagerInterface; use App\Entity\JobOrder; use App\Entity\Warranty; use App\Entity\SAPBattery; +use App\Entity\InvoiceItem; +use App\Entity\Battery; use App\Ramcar\ServiceType; +use App\Ramcar\WarrantyClass; +use App\Ramcar\JOStatus; use App\Service\WarrantyHandler; @@ -58,38 +62,45 @@ class GetFlawedWarrantiesCommand extends Command $invalid_plate_numbers = []; $duplicate_warranties = []; $sdate_expiry_wrong_warranties = []; + $no_expiry_dates = []; + $no_sap_batteries = []; - $first_results_set = $this->getMissingWarranties($start_date, $end_date); - $second_results_set = $this->getWarrantiesWithWrongEntries($start_date, $end_date); + $first_results_set = $this->getFlawedWarranties($start_date, $end_date); //$third_results_set = $this->getDuplicateWarranties($start_date, $end_date); $missing_warranties = $first_results_set['missing']; $invalid_plate_numbers = $first_results_set['invalid_plate_number']; $comm_private_warranties = $first_results_set['commercial_private']; - $sdate_expiry_wrong_warranties = $second_results_set['wrong_expiry_date']; + $sdate_expiry_wrong_warranties = $first_results_set['wrong_expiry_date']; + $no_expiry_dates = $first_results_set['no_expiry_date']; + $no_sap_batteries = $first_results_set['no_sap_battery']; // our warranty error categories: missing, commercial_private, invalid plate number, duplicate, start date of expiry computation wrong $this->outputResults($s_date, $e_date, $missing_warranties, $invalid_plate_numbers, $comm_private_warranties, - $sdate_expiry_wrong_warranties, $duplicate_warranties); + $sdate_expiry_wrong_warranties, $no_expiry_dates, $no_sap_batteries, $duplicate_warranties); return 0; } - protected function getMissingWarranties($start_date, $end_date) + protected function getFlawedWarranties($start_date, $end_date) { $em = $this->em; // get all job orders with battery new service type within date range - $jo_query = $em->createQuery('select ii,i,jo,cv from App\Entity\InvoiceItem ii inner join ii.invoice i inner join i.job_order jo inner join jo.cus_vehicle cv join jo.customer c where ii.battery is not null and jo.service_type = :service_type and jo.date_schedule > :date_start and jo.date_schedule < :date_end'); + $jo_query = $em->createQuery('select ii,i,jo,cv from App\Entity\InvoiceItem ii inner join ii.invoice i inner join i.job_order jo inner join jo.cus_vehicle cv join jo.customer c where ii.battery is not null and jo.service_type = :service_type and jo.date_schedule > :date_start and jo.date_schedule < :date_end and jo.status != :status'); $jo_query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW) ->setParameter('date_start', $start_date) - ->setParameter('date_end', $end_date); + ->setParameter('date_end', $end_date) + ->setParameter('status', JOStatus::CANCELLED); $jos = $jo_query->iterate(); $invalids = []; $missings = []; $comm_privates = []; + $date_expiry_wrongs = []; + $no_expiries = []; + $no_saps = []; foreach($jos as $row) { @@ -131,7 +142,7 @@ class GetFlawedWarrantiesCommand extends Command if ($invoice->getDateCreate() != null) { - // check if plate number is "clean". If not, do not insert into warranty + // check if plate number is "clean". if (!(Warranty::cleanPlateNumber($cv->getPlateNumber()))) { $invalids[] = [ @@ -147,32 +158,141 @@ class GetFlawedWarrantiesCommand extends Command $cleaned_plate_number = Warranty::cleanPlateNumber($cv->getPlateNumber()); // use date_create of invoice as start date, as decided in group chat - $expiry_date_date_create = $this->wh->computeDateExpire($jo->getInvoice()->getDateCreate(), $warranty_period); - $results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cleaned_plate_number, - 'date_expire' => $expiry_date_date_create]);; + $purchase_date = $invoice->getDateCreate(); + $date_purchase_str = $purchase_date->format('Y-m-d'); + $date_purchase = DateTime::createFromFormat('Y-m-d', $date_purchase_str); - if (empty($results)) + $expiry_date_date_create = $this->wh->computeDateExpire($purchase_date, $warranty_period); + + $results_date_create = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cleaned_plate_number, + 'date_purchase' => $date_purchase]); + + if (empty($results_date_create)) { - // TODO: find warranty using expiration date with start date, date fulfilled, if date fulfilled is not null - // get warranty periods from battery, this time, check warranty class - // recompute expiration date with start date, date fulfilled - // if still no warranty, put JO in $missing - // if there is a warranty, put warranty in wrong expiry date - $missings[] = [ - 'jo_id' => $jo->getID(), - 'plate_number' => $cv->getPlateNumber(), - 'expiry_date' => $expiry_date_date_create->format('Y-m-d'), - ]; + // TODO: find warranties that used date_schedule for date_purchase + $missing_flag = true; + $jo_warranty_class = $jo->getWarrantyClass(); + + $warr_period = $this->getWarrantyPeriod($jo_warranty_class, $invoice_item); + + // get date fulfill + $datetime_fulfilled = $jo->getDateFulfill(); + if ($datetime_fulfilled != null) + { + $datetime_fulfilled_str = $datetime_fulfilled->format('Y-m-d'); + $date_fulfilled = DateTime::createFromFormat('Y-m-d', $datetime_fulfilled_str); + + $expiry_date_date_create = $this->wh->computeDateExpire($date_fulfilled, $warr_period); + + // find warranties with date fulfilled as the date_purchase + $warranties_date_fulfilled = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cleaned_plate_number, + 'date_purchase' => $date_fulfilled]); + + if (!(empty($warranties_date_fulfilled))) + { + $missing_flag = false; + + foreach ($warranties_date_fulfilled as $warranty_date_fulfilled) + { + // check if sap_code exists in battery + $warr_sap_battery = $warranty_date_fulfilled->getSAPBattery(); + if ($warr_sap_battery != null) + { + $warr_sap_code = $warr_sap_battery->getID(); + + if ($warr_sap_code != null) + { + $batteries = $em->getRepository(Battery::class)->findBy(['sap_code' => $warr_sap_code]); + if ($batteries == null) + { + $no_saps[] = [ + 'warranty_id' => $warranty_date_fulfilled->getID(), + 'plate_number' => $cleaned_plate_number, + 'sap_code' => $warr_sap_code, + ]; + } + } + } + + // wrong expiration date + $date_expiry_wrongs[] = [ + 'warranty_id' => $warranty_date_fulfilled->getID(), + 'plate_number' => $cleaned_plate_number, + 'expiry_date' => $expiry_date_date_create->format('Y-m-d'), + ]; + } + } + + } + + if ($missing_flag) + { + $missings[] = [ + 'jo_id' => $jo->getID(), + 'plate_number' => $cleaned_plate_number, + 'expiry_date' => $expiry_date_date_create->format('Y-m-d'), + ]; + } } else { - foreach ($results as $warranty) + foreach ($results_date_create as $warranty_date_create) { - $comm_privates[] = [ - 'warranty_id' => $warranty->getID(), - 'plate_number' => $warranty->getPlateNumber(), - 'expiry_date' => $warranty->getDateExpire()->format('Y-m-d'), - ]; + // check if sap_code exists in battery + $warr_sap_battery = $warranty_date_create->getSAPBattery(); + if ($warr_sap_battery != null) + { + $warr_sap_code = $warr_sap_battery->getID(); + if ($warr_sap_code != null) + { + $batteries = $em->getRepository(Battery::class)->findBy(['sap_code' => $warr_sap_code]); + if ($batteries == null) + { + $no_saps[] = [ + 'warranty_id' => $warranty_date_create->getID(), + 'plate_number' => $cleaned_plate_number, + 'sap_code' => $warr_sap_code, + ]; + } + } + } + + $exp_date = ''; + + // check if expiry date is null + if ($warranty_date_create->getDateExpire() == null) + { + $no_expiries[] = [ + 'warranty_id' => $warranty_date_create->getID(), + 'plate_number' => $cleaned_plate_number, + ]; + } + else + { + // check if warranty's expiry date == supposed to be expiry date + $jo_warranty_class = $jo->getWarrantyClass(); + + $warr_period = $this->getWarrantyPeriod($jo_warranty_class, $invoice_item); + + if ($warr_period != -1) + { + $correct_expiry_date = $this->wh->computeDateExpire($warranty_date_create->getDatePurchase(), $warr_period); + $warranty_expiry_date = $warranty_date_create->getDateExpire(); + + $correct_exp_date = $correct_expiry_date->format('Y-m-d'); + $warr_exp_date = $warranty_expiry_date->format('Y-m-d'); + + if ($correct_exp_date != $warr_exp_date) + { + $comm_privates[] = [ + 'warranty_id' => $warranty_date_create->getID(), + 'plate_number' => $cleaned_plate_number, + 'expiry_date' => $warr_exp_date, + 'supposed_expiry_date' => $correct_exp_date, + ]; + } + } + } } } } @@ -187,101 +307,9 @@ class GetFlawedWarrantiesCommand extends Command 'missing' => $missings, 'invalid_plate_number' => $invalids, 'commercial_private' => $comm_privates, - ]; - - return $res; - - } - - protected function getWarrantiesWithWrongEntries($start_date, $end_date) - { - $em = $this->em; - - // get all job orders with battery new service type within date range - $jo_query = $em->createQuery('select ii,i,jo,cv from App\Entity\InvoiceItem ii inner join ii.invoice i inner join i.job_order jo inner join jo.cus_vehicle cv join jo.customer c where ii.battery is not null and jo.service_type = :service_type and jo.date_schedule > :date_start and jo.date_schedule < :date_end'); - $jo_query->setParameter('service_type', ServiceType::BATTERY_REPLACEMENT_NEW) - ->setParameter('date_start', $start_date) - ->setParameter('date_end', $end_date); - - $jos = $jo_query->iterate(); - - $expiries = []; - - foreach($jos as $row) - { - $invoice_item = $row[0]; - $invoice = $invoice_item->getInvoice(); - $jo = $invoice->getJobOrder(); - $cv = $jo->getCustomerVehicle(); - $customer = $jo->getCustomer(); - - $clean_plate_num = Warranty::cleanPlateNumber($cv->getPlateNumber()); - if (!($clean_plate_num)) - { - error_log('invalid plate'); - continue; - } - if ($invoice_item == null) - { - error_log('invoice item null'); - continue; - } - if ($invoice_item->getBattery() == null) - { - error_log('invoice item not a battery'); - continue; - } - - error_log('clean plate, invoice item not null, battery not null'); - // manually retrieve the SAPBattery using the SAPCode - $battery_sap_code = $invoice_item->getBattery()->getSAPCode(); - $found_battery = $this->findSAPBattery($battery_sap_code); - $sap_code = '\'' . $battery_sap_code . '\''; - if (!$found_battery) - { - $sap_code = 'NULL'; - } - - // check if warranty exists with plate number and date_create of invoice - $date = $jo->getInvoice()->getDateCreate(); - $date_create_invoice_str = $date->format('Y-m-d'); - $date_create_invoice = DateTime::createFromFormat('Y-m-d', $date_create_invoice_str); - - $warranties_date_create = $this->em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_plate_num, 'date_purchase' => $date_create_invoice]); - if (empty($warranties_date_create)) - { - // check if warranty exists with plate number and date_fulfilled - $datetime_fulfilled = $jo->getDateFulfill(); - if ($datetime_fulfilled != null) - { - $datetime_fulfilled_str = $datetime_fulfilled->format('Y-m-d'); - $date_fulfilled = DateTime::createFromFormat('Y-m-d', $datetime_fulfilled_str); - - $warranties_date_fulfilled = $this->em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_plate_num, 'date_purchase' => $date_fulfilled]); - if (!(empty($warranties_date_fulfilled))) - { - foreach ($warranties_date_fulfilled as $warranty) - { - $expiries[] = [ - 'warranty_id' => $warranty->getID(), - 'plate_number' => $warranty->getPlateNumber(), - 'expiry_date' => $warranty->getDateExpire()->format('Y-m-d'), - ]; - } - } - } - else - { - error_log('no warranty at all?'); - } - } - - $em->detach($row[0]); - $em->clear(); - } - - $res = [ - 'wrong_expiry_date' => $expiries + 'wrong_expiry_date' => $date_expiry_wrongs, + 'no_expiry_date' => $no_expiries, + 'no_sap_battery' => $no_saps, ]; return $res; @@ -293,7 +321,7 @@ class GetFlawedWarrantiesCommand extends Command } protected function outputResults($start_date, $end_date, $missing, $invalid, $commercial_private, - $expiry_wrong, $duplicate) + $expiry_wrong, $no_expiry, $no_sap, $duplicate) { $date_range = $start_date . '-' . $end_date; @@ -366,7 +394,8 @@ class GetFlawedWarrantiesCommand extends Command fputcsv($comm_private_fh, [ 'Warranty ID', 'Plate Number', - 'Expiry Date', + 'Warranty Expiry Date', + 'Correct Expiry Date', ]); foreach($commercial_private as $comm_private_row) @@ -400,6 +429,55 @@ class GetFlawedWarrantiesCommand extends Command } } + // warranties with null in date_expire + $no_expiry_csv_filename = $date_range . '-' . 'no_expiry_date_warranties.csv'; + try + { + $no_expiry_fh = fopen($no_expiry_csv_filename, 'a'); + } + catch (Exception $e) + { + throw new Exception('The file "' . $no_expiry_csv_filename . '" could not be opened.'); + } + + if ((count($no_expiry)) > 0) + { + fputcsv($no_expiry_fh, [ + 'Warranty ID', + 'Plate Number', + ]); + + foreach($no_expiry as $no_expiry_row) + { + fputcsv($no_expiry_fh, $no_expiry_row); + } + } + + // sap batteries not in system + $no_sap_csv_filename = $date_range . '-' . 'no_sap_battery.csv'; + try + { + $no_sap_fh = fopen($no_sap_csv_filename, 'a'); + } + catch (Exception $e) + { + throw new Exception('The file "' . $no_sap_csv_filename . '" could not be opened.'); + } + + if ((count($no_sap)) > 0) + { + fputcsv($no_sap_fh, [ + 'Warranty ID', + 'Plate Number', + 'SAP Battery Code', + ]); + + foreach($no_sap as $no_sap_row) + { + fputcsv($no_sap_fh, $no_sap_row); + } + } + // duplicate warranties $duplicate_csv_filename = $date_range . '-' . 'duplicate_warranties.csv'; try @@ -448,4 +526,24 @@ class GetFlawedWarrantiesCommand extends Command return true; } + + protected function getWarrantyPeriod($warranty_class, InvoiceItem $invoice_item) + { + $warr_period = -1; + if ($warranty_class == WarrantyClass::WTY_PRIVATE) + { + $warr_period = $invoice_item->getBattery()->getWarrantyPrivate(); + } + if ($warranty_class == WarrantyClass::WTY_COMMERCIAL) + { + $warr_period = $invoice_item->getBattery()->getWarrantyCommercial(); + } + if ($warranty_class == WarrantyClass::WTY_TNV) + { + $warr_period = $invoice_item->getBattery()->getWarrantyTnv(); + } + + return $warr_period; + + } } -- 2.43.5 From 714aa5636dc0b5bfb7984d256d79b26f2db0cb89 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 21 May 2020 10:48:58 +0000 Subject: [PATCH 5/5] Add checking for warranties with date schedule as the start date of expiry date computation. #407 --- src/Command/GetFlawedWarrantiesCommand.php | 124 ++++++++++++++------- 1 file changed, 86 insertions(+), 38 deletions(-) diff --git a/src/Command/GetFlawedWarrantiesCommand.php b/src/Command/GetFlawedWarrantiesCommand.php index 2b93938b..73d87966 100644 --- a/src/Command/GetFlawedWarrantiesCommand.php +++ b/src/Command/GetFlawedWarrantiesCommand.php @@ -169,61 +169,109 @@ class GetFlawedWarrantiesCommand extends Command if (empty($results_date_create)) { - // TODO: find warranties that used date_schedule for date_purchase + // find warranties that used date_schedule for date_purchase $missing_flag = true; $jo_warranty_class = $jo->getWarrantyClass(); $warr_period = $this->getWarrantyPeriod($jo_warranty_class, $invoice_item); - // get date fulfill - $datetime_fulfilled = $jo->getDateFulfill(); - if ($datetime_fulfilled != null) + // get date schedule + $datetime_schedule = $jo->getDateSchedule(); + $datetime_schedule_str = $datetime_schedule->format('Y-m-d'); + $date_scheduled = DateTime::createFromFormat('Y-m-d', $datetime_schedule_str); + + $expiry_date_date_scheduled = $this->wh->computeDateExpire($date_scheduled, $warr_period); + + // find warranties with date_schedule as the date_purchase + $warranties_date_scheduled = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cleaned_plate_number, + 'date_purchase' => $date_scheduled]); + + if (!(empty($warranties_date_scheduled))) { - $datetime_fulfilled_str = $datetime_fulfilled->format('Y-m-d'); - $date_fulfilled = DateTime::createFromFormat('Y-m-d', $datetime_fulfilled_str); + $missing_flag = false; - $expiry_date_date_create = $this->wh->computeDateExpire($date_fulfilled, $warr_period); - - // find warranties with date fulfilled as the date_purchase - $warranties_date_fulfilled = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cleaned_plate_number, - 'date_purchase' => $date_fulfilled]); - - if (!(empty($warranties_date_fulfilled))) + foreach ($warranties_date_scheduled as $warranty_date_scheduled) { - $missing_flag = false; - - foreach ($warranties_date_fulfilled as $warranty_date_fulfilled) + // check if sap_code exists in battery + $warr_sap_battery = $warranty_date_scheduled->getSAPBattery(); + if ($warr_sap_battery != null) { - // check if sap_code exists in battery - $warr_sap_battery = $warranty_date_fulfilled->getSAPBattery(); - if ($warr_sap_battery != null) - { - $warr_sap_code = $warr_sap_battery->getID(); + $warr_sap_code = $warr_sap_battery->getID(); - if ($warr_sap_code != null) + if ($warr_sap_code != null) + { + $batteries = $em->getRepository(Battery::class)->findBy(['sap_code' => $warr_sap_code]); + if ($batteries == null) { - $batteries = $em->getRepository(Battery::class)->findBy(['sap_code' => $warr_sap_code]); - if ($batteries == null) - { - $no_saps[] = [ - 'warranty_id' => $warranty_date_fulfilled->getID(), - 'plate_number' => $cleaned_plate_number, - 'sap_code' => $warr_sap_code, - ]; - } + $no_saps[] = [ + 'warranty_id' => $warranty_date_scheduled->getID(), + 'plate_number' => $cleaned_plate_number, + 'sap_code' => $warr_sap_code, + ]; } } - - // wrong expiration date - $date_expiry_wrongs[] = [ - 'warranty_id' => $warranty_date_fulfilled->getID(), - 'plate_number' => $cleaned_plate_number, - 'expiry_date' => $expiry_date_date_create->format('Y-m-d'), - ]; } + + // wrong expiration date + $date_expiry_wrongs[] = [ + 'warranty_id' => $warranty_date_scheduled->getID(), + 'plate_number' => $cleaned_plate_number, + 'expiry_date' => $expiry_date_date_create->format('Y-m-d'), + ]; } } + else + { + // get date fulfill + $datetime_fulfilled = $jo->getDateFulfill(); + if ($datetime_fulfilled != null) + { + $datetime_fulfilled_str = $datetime_fulfilled->format('Y-m-d'); + $date_fulfilled = DateTime::createFromFormat('Y-m-d', $datetime_fulfilled_str); + + $expiry_date_date_create = $this->wh->computeDateExpire($date_fulfilled, $warr_period); + + // find warranties with date fulfilled as the date_purchase + $warranties_date_fulfilled = $em->getRepository(Warranty::class)->findBy(['plate_number' => $cleaned_plate_number, + 'date_purchase' => $date_fulfilled]); + + if (!(empty($warranties_date_fulfilled))) + { + $missing_flag = false; + + foreach ($warranties_date_fulfilled as $warranty_date_fulfilled) + { + // check if sap_code exists in battery + $warr_sap_battery = $warranty_date_fulfilled->getSAPBattery(); + if ($warr_sap_battery != null) + { + $warr_sap_code = $warr_sap_battery->getID(); + + if ($warr_sap_code != null) + { + $batteries = $em->getRepository(Battery::class)->findBy(['sap_code' => $warr_sap_code]); + if ($batteries == null) + { + $no_saps[] = [ + 'warranty_id' => $warranty_date_fulfilled->getID(), + 'plate_number' => $cleaned_plate_number, + 'sap_code' => $warr_sap_code, + ]; + } + } + } + + // wrong expiration date + $date_expiry_wrongs[] = [ + 'warranty_id' => $warranty_date_fulfilled->getID(), + 'plate_number' => $cleaned_plate_number, + 'expiry_date' => $expiry_date_date_create->format('Y-m-d'), + ]; + } + } + } + } if ($missing_flag) { -- 2.43.5