diff --git a/src/Command/UpdateWarrantyDealerInfoCommand.php b/src/Command/UpdateWarrantyDealerInfoCommand.php new file mode 100644 index 00000000..b3ecd06c --- /dev/null +++ b/src/Command/UpdateWarrantyDealerInfoCommand.php @@ -0,0 +1,184 @@ +em = $em; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('warranty:updatedealerinfo') + ->setDescription('Update warranty dealer information.') + ->setHelp('Update warranty dealer information.') + ->addArgument('input_file', InputArgument::REQUIRED, 'Path to the CSV file with the warranty info.') + ->addArgument('output_file', InputArgument::REQUIRED, 'Output filename'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + // for now, hardcode new dealer info (G-SIX SUMULONG) + $new_dealer = [ + 'name' => 'G-SIX SUMULONG', + 'address' => '175 SUMULONG HIGHWAY MAYAMOT RIZAL_X000D_', + 'branch_code' => 'ZN03587644', + ]; + + $csv_file = $input->getArgument('input_file'); + $output_file = $input->getArgument('output_file'); + + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be opened.'); + } + + // start at 1 since 0 has the headers + $row_num = 1; + $output_info = []; + while (($fields = fgetcsv($fh)) !== false) + { + // ignore first row + if ($row_num == 1) + { + $row_num++; + continue; + } + + // process row + $output_info[] = $this->processRow($fields, $new_dealer); + + $row_num++; + } + + // write to output file + $this->outputWarrantyInfo($output_file, $output_info); + + fclose($fh); + return 0; + } + + protected function processRow($fields, $new_dealer) + { + // get the warranty id + $warranty_id = trim($fields[SELF::F_ID]); + + // find the warranty + $warranty = $this->em->getRepository(Warranty::class)->find($warranty_id); + + if ($warranty == null) + { + // log warranty not found + return $this->setOutputInfo($fields, 'NOT UPDATED', 'Warranty not found'); + } + + // update warranty dealer information + $dealer_name = $new_dealer['name']; + $dealer_address = $new_dealer['address']; + $dealer_branch_code = $new_dealer['branch_code']; + + $warranty->setDealerName($dealer_name) + ->setDealerAddress($dealer_address) + ->setDealerBranchCode($dealer_branch_code); + + $this->em->flush(); + + // log successful update + return $this->setOutputInfo($fields, 'UPDATED', ''); + } + + protected function setOutputInfo($fields, $status, $reason) + { + $warranty_id = trim($fields[SELF::F_ID]); + + return [ + $warranty_id, + $status, + $reason, + ]; + } + + protected function outputWarrantyInfo($output_file, $entries) + { + try + { + $fh = fopen($output_file, "w"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $report_file . '" could be opened.'); + } + + // write the headers + fputcsv($fh, [ + 'Warranty ID', + 'Status', + 'Reason', + ]); + + foreach ($entries as $row) + { + fputcsv($fh, $row); + } + + fclose($fh); + } +}