From f2de60d65a0865f71c3025ac6f4f42cdee5f5864 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 21 May 2020 09:50:42 +0000 Subject: [PATCH] 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; + + } }