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; } - - - }