Add data files for the related job order tables. #762

This commit is contained in:
Korina Cordero 2023-09-13 17:18:33 +08:00
parent 35ed917336
commit d7a2b25e51

View file

@ -39,7 +39,7 @@ class GetJobOrderArchiveDataCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
// get table to back up // get year to archive
$year = $input->getArgument('year'); $year = $input->getArgument('year');
// get the data to archive for the following tables: // get the data to archive for the following tables:
@ -50,70 +50,59 @@ class GetJobOrderArchiveDataCommand extends Command
// (5) jo_rejection (has jo foreign key) // (5) jo_rejection (has jo foreign key)
// (6) rider_rating (has jo foreign key) // (6) rider_rating (has jo foreign key)
$jo_backup_data = $this->getJobOrderData($year);
// create the archive tables // create the archive tables
// TODO: create the other archive tables // TODO: create the other archive tables
$archive_table_name = $this->createJobOrderArchiveTable($year); $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(); $db = $this->em->getConnection();
$query_sql = 'SELECT * $query_sql = 'SELECT *
FROM job_order FROM job_order
WHERE YEAR(date_create) = :year WHERE YEAR(date_create) = :year
ORDER BY date_create'; ORDER BY date_create';
$query_stmt = $db->prepare($query_sql); $query_stmt = $db->prepare($query_sql);
$query_stmt->bindValue('year', $year, PDO::PARAM_STR); $query_stmt->bindValue('year', $year, PDO::PARAM_STR);
$results = $query_stmt->executeQuery(); $callback = ['App\Command\GetJobOrderArchiveDataCommand', 'getRelatedArchiveData'];
$jo_data = []; $this->getArchiveData($query_stmt, $callback);
$jo_event_data = [];
$invoice_data = []; return 0;
$ticket_data = []; }
$jo_rejection_data = [];
$rider_rating_data = []; 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()) while ($row = $results->fetchAssociative())
{ {
$jo_data[] = $this->createJobOrderArchiveData($row); $jo_data[] = $this->createJobOrderArchiveData($row);
// get the data to archive for jo_event, invoice, ticket, jo_rejection, rider_rating // foreach job order id we got from the first query, we get the JO related
// using the job order id // data for that id from jo_event, invoice, ticket, jo_rejection, rider_rating
$id = $row['id'];
$jo_event_data = $this->getJORelatedData($id, 'jo_event'); if (is_callable($callbackJO))
$ticket_data = $this->getJORelatedData($id, 'ticket'); {
$jo_rejection_data = $this->getJORelatedData($id, 'jo_rejection'); foreach ($related_tables as $table_name)
$rider_rating_data = $this->getJORelatedData($id, 'rider_rating'); {
call_user_func($callbackJO, $row, $table_name);
// TODO: separate the invoice and invoice item data }
}
} }
$backup_data = [ // TODO: load data infile for job order
'jo' => $jo_data,
'jo_event' => $jo_event_data,
'invoice' => $invoice_data,
'ticket' => $ticket_data,
'jo_rejection' => $jo_rejection_data,
'rider_rating' => $rider_rating_data,
];
return $backup_data;
} }
protected function getJORelatedData($id, $table_name) protected function getRelatedArchiveData($row, $table_name)
{ {
$db = $this->em->getConnection(); $db = $this->em->getConnection();
@ -131,39 +120,77 @@ class GetJobOrderArchiveDataCommand extends Command
} }
$query_stmt = $db->prepare($query_sql); $query_stmt = $db->prepare($query_sql);
$query_stmt->bindValue('id', $id); $query_stmt->bindValue('id', $row['id']);
$results = $query_stmt->executeQuery(); $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 $cleaned_value = $this->cleanData(($value) ?? '\N');
$jo_related_data = $this->createJOEventArchiveData($row); $fields[] = $cleaned_value;
}
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
} }
$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 // 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) protected function createJobOrderArchiveData($row)
{ {
// TODO: this could be shrunk further
// get job order data // 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, // 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, // 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; return $data;
} }
protected function createJOEventArchiveData($row) protected function createLoadDataFileForArchiveData($archive_data, $table_name)
{
// 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)
{ {
// cache directory // cache directory
$cache_dir = __DIR__ . '/../../var/cache'; $cache_dir = __DIR__ . '/../../var/cache';
@ -449,7 +451,7 @@ class GetJobOrderArchiveDataCommand extends Command
} }
else else
{ {
foreach ($backup_data as $key => $data) foreach ($archive_data as $key => $data)
{ {
$line = implode('|', $data) . "\r\n"; $line = implode('|', $data) . "\r\n";
fwrite($fp, $line); fwrite($fp, $line);