From 9eedab0652a41d0e34ba3131080baef9a2bb53a1 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 13 Sep 2023 17:18:33 +0800 Subject: [PATCH] Add data files for the related job order tables. #762 --- src/Command/GetJobOrderArchiveDataCommand.php | 190 +++++++++--------- 1 file changed, 96 insertions(+), 94 deletions(-) diff --git a/src/Command/GetJobOrderArchiveDataCommand.php b/src/Command/GetJobOrderArchiveDataCommand.php index e1bcabbd..de602ab5 100644 --- a/src/Command/GetJobOrderArchiveDataCommand.php +++ b/src/Command/GetJobOrderArchiveDataCommand.php @@ -39,7 +39,7 @@ class GetJobOrderArchiveDataCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { - // get table to back up + // get year to archive $year = $input->getArgument('year'); // get the data to archive for the following tables: @@ -50,70 +50,59 @@ class GetJobOrderArchiveDataCommand extends Command // (5) jo_rejection (has jo foreign key) // (6) rider_rating (has jo foreign key) - $jo_backup_data = $this->getJobOrderData($year); - // create the archive tables // TODO: create the other archive tables $archive_table_name = $this->createJobOrderArchiveTable($year); - - error_log('count ' . count($jo_backup_data)); - - // create the load file for the backup data - // $this->createLoadDataFileForBackupData($jo_backup_data, $archive_table_name); - - return 0; - } - - protected function getJobOrderData($year) - { $db = $this->em->getConnection(); $query_sql = 'SELECT * FROM job_order WHERE YEAR(date_create) = :year - ORDER BY date_create'; + ORDER BY date_create'; $query_stmt = $db->prepare($query_sql); $query_stmt->bindValue('year', $year, PDO::PARAM_STR); - $results = $query_stmt->executeQuery(); + $callback = ['App\Command\GetJobOrderArchiveDataCommand', 'getRelatedArchiveData']; - $jo_data = []; - $jo_event_data = []; - $invoice_data = []; - $ticket_data = []; - $jo_rejection_data = []; - $rider_rating_data = []; + $this->getArchiveData($query_stmt, $callback); + + return 0; + } + + protected function getArchiveData($stmt, $callbackJO) + { + $results = $stmt->executeQuery(); + + $related_tables = ['jo_event', 'invoice', 'ticket', 'jo_rejection', 'rider_rating']; + + // delete the related data files + foreach ($related_tables as $tname) + { + $this->deleteDataFiles($tname); + } while ($row = $results->fetchAssociative()) { $jo_data[] = $this->createJobOrderArchiveData($row); - // get the data to archive for jo_event, invoice, ticket, jo_rejection, rider_rating - // using the job order id - $id = $row['id']; + // foreach job order id we got from the first query, we get the JO related + // data for that id from jo_event, invoice, ticket, jo_rejection, rider_rating - $jo_event_data = $this->getJORelatedData($id, 'jo_event'); - $ticket_data = $this->getJORelatedData($id, 'ticket'); - $jo_rejection_data = $this->getJORelatedData($id, 'jo_rejection'); - $rider_rating_data = $this->getJORelatedData($id, 'rider_rating'); - - // TODO: separate the invoice and invoice item data + if (is_callable($callbackJO)) + { + foreach ($related_tables as $table_name) + { + call_user_func($callbackJO, $row, $table_name); + } + } } - $backup_data = [ - 'jo' => $jo_data, - 'jo_event' => $jo_event_data, - 'invoice' => $invoice_data, - 'ticket' => $ticket_data, - 'jo_rejection' => $jo_rejection_data, - 'rider_rating' => $rider_rating_data, - ]; + // TODO: load data infile for job order - return $backup_data; } - protected function getJORelatedData($id, $table_name) + protected function getRelatedArchiveData($row, $table_name) { $db = $this->em->getConnection(); @@ -131,39 +120,77 @@ class GetJobOrderArchiveDataCommand extends Command } $query_stmt = $db->prepare($query_sql); - $query_stmt->bindValue('id', $id); + $query_stmt->bindValue('id', $row['id']); $results = $query_stmt->executeQuery(); - $jo_related_data = []; + $data = []; - while ($row = $results->fetchAssociative()) + while ($q_row = $results->fetchAssociative()) { - if ($table_name == 'jo_event') + $fields = []; + + foreach ($q_row as $key => $value) { - // create the jo event archive data - $jo_related_data = $this->createJOEventArchiveData($row); - } - if ($table_name == 'invoice') - { - // get the data to archive for invoice item - // create the invoice archive data - } - if ($table_name == 'ticket') - { - // create the ticket archive data - } - if ($table_name == 'jo_rejection') - { - // create the jo rejection archive data - } - if ($table_name == 'rider_rating') - { - // create the rider rating archive data + $cleaned_value = $this->cleanData(($value) ?? '\N'); + $fields[] = $cleaned_value; } + + $data[$table_name][$q_row['id']] = $fields; } - return $jo_related_data; + // write the array into the file + $file = $this->createDataFileRelatedArchiveData($data, $table_name); + + if ($file != null) + { + // call load data infile + } + } + + protected function deleteDataFiles($tname) + { + // cache directory + $cache_dir = __DIR__ . '/../../var/cache'; + + $file = $cache_dir . '/' . $tname . '_archive.tab'; + + if (file_exists($file)) + unlink($file); + } + + protected function createDataFileRelatedArchiveData($archive_data, $table_name) + { + if (isset($archive_data[$table_name])) + { + $adata = $archive_data[$table_name]; + + // cache directory + $cache_dir = __DIR__ . '/../../var/cache'; + + $file = $cache_dir . '/' . $table_name . '_archive.tab'; + error_log('opening file for archive - ' . $file); + + $fp = fopen($file, 'a'); + if ($fp === false) + { + error_log('could not open file for load data infile - ' . $file); + } + else + { + foreach ($adata as $key => $data) + { + $line = implode('|', $data) . "\r\n"; + fwrite($fp, $line); + } + } + + fclose($fp); + + return $file; + } + + return null; } // TODO: make this so you just call one function to create all the tables @@ -248,6 +275,8 @@ class GetJobOrderArchiveDataCommand extends Command protected function createJobOrderArchiveData($row) { + // TODO: this could be shrunk further + // get job order data // check for nulls. check the ff fields since these can be null: date_fulfill, date_cancel, date_assign, create_user_id, // assign_user_id, process_user_id, hub_id, rider_id, cancel_reason, ref_jo_id, or_num, trade_in_type, @@ -407,34 +436,7 @@ class GetJobOrderArchiveDataCommand extends Command return $data; } - protected function createJOEventArchiveData($row) - { - // fields that can be null: rider_id, create_user_id - $id = $row['id']; - - $create_user_id = $row['create_user_id'] ?? '\N'; - - $job_order_id = $row['job_order_id']; - $date_create = $row['date_create']; - $date_happen = $row['date_happen']; - $type_id = $row['type_id']; - - $rider_id = $row['rider_id'] ?? '\N'; - - $data = [ - $id, - $create_user_id, - $job_order_id, - $date_create, - $date_happen, - $type_id, - $rider_id, - ]; - - return $data; - } - - protected function createLoadDataFileForBackupData($backup_data, $table_name) + protected function createLoadDataFileForArchiveData($archive_data, $table_name) { // cache directory $cache_dir = __DIR__ . '/../../var/cache'; @@ -449,7 +451,7 @@ class GetJobOrderArchiveDataCommand extends Command } else { - foreach ($backup_data as $key => $data) + foreach ($archive_data as $key => $data) { $line = implode('|', $data) . "\r\n"; fwrite($fp, $line);