Fix issues found during testing. #762

This commit is contained in:
Korina Cordero 2023-09-29 20:47:13 +08:00 committed by Korina Cordero
parent ce39e66127
commit 9128768b99

View file

@ -106,6 +106,7 @@ class GetJobOrderArchiveDataCommand extends Command
$archive_files = []; $archive_files = [];
$jo_id_list = []; $jo_id_list = [];
$ii_id_list = [];
while ($row = $results->fetchAssociative()) while ($row = $results->fetchAssociative())
{ {
@ -115,6 +116,24 @@ class GetJobOrderArchiveDataCommand extends Command
$jo_id_list[] = $row['id']; $jo_id_list[] = $row['id'];
} }
// write the array into the file
$file = $this->createDataFileRelatedArchiveData($jo_data, $jo_tname, $year, 'w');
if ($file != null)
{
$archive_files[$jo_tname] = $file;
}
// error_log('jo_data total ' . count($jo_data['job_order']));
// error_log('jo id list total ' . count($jo_id_list));
unset($jo_data);
// load the job order archive file for job order into the database
$this->loadArchiveFiles($archive_files, $year);
unset($archive_files[$jo_tname]);
// get all related data for job order // get all related data for job order
foreach ($jo_id_list as $jo_id) foreach ($jo_id_list as $jo_id)
{ {
@ -155,26 +174,16 @@ class GetJobOrderArchiveDataCommand extends Command
} }
} }
// write the array into the file // TODO: can we maybe find a way to not load the archive files successively?
$file = $this->createDataFileRelatedArchiveData($jo_data, $jo_tname, $year, 'w'); // we get the memory exception after loading the data to the last table, jo_rejection.
if ($file != null)
{
$archive_files[$jo_tname] = $file;
}
// error_log(print_r($archive_files, true));
$this->loadArchiveFiles($archive_files, $year); $this->loadArchiveFiles($archive_files, $year);
// need to get the list of invoice ids for deletion for invoice items
$invoice_id_list = $this->getInvoiceIds($jo_id_list);
// update rider's active_jo_id and current_jo_id to null // update rider's active_jo_id and current_jo_id to null
// so we can delete the old job orders (foreign key constraint) // so we can delete the old job orders (foreign key constraint)
$this->updateRiderJobOrders($jo_id_list); $this->updateRiderJobOrders($jo_id_list);
// at this point, all the job order and related data have been archived into the database // at this point, all the job order and related data have been archived into the database
$this->deleteData($jo_id_list, $invoice_id_list, $related_tables); $this->deleteData($jo_id_list, $related_tables);
} }
protected function getRelatedArchiveData($row, $query_stmt, $table_name, $year) protected function getRelatedArchiveData($row, $query_stmt, $table_name, $year)
@ -207,6 +216,7 @@ class GetJobOrderArchiveDataCommand extends Command
} }
// get the invoice items for archiving // get the invoice items for archiving
$ii_id_list = [];
foreach ($invoice_id_list as $i_id) foreach ($invoice_id_list as $i_id)
{ {
$ii_file = $this->getInvoiceItemArchiveData($i_id, $year); $ii_file = $this->getInvoiceItemArchiveData($i_id, $year);
@ -217,6 +227,8 @@ class GetJobOrderArchiveDataCommand extends Command
// write the array into the file // write the array into the file
$file = $this->createDataFileRelatedArchiveData($data, $table_name, $year, 'a'); $file = $this->createDataFileRelatedArchiveData($data, $table_name, $year, 'a');
unset($data);
if ($file != null) if ($file != null)
$files[$table_name] = $file; $files[$table_name] = $file;
@ -236,6 +248,7 @@ class GetJobOrderArchiveDataCommand extends Command
$results = $query_stmt->executeQuery(); $results = $query_stmt->executeQuery();
$ii_data = []; $ii_data = [];
$ii_id_list = [];
while ($ii_row = $results->fetchAssociative()) while ($ii_row = $results->fetchAssociative())
{ {
$id = $ii_row['id']; $id = $ii_row['id'];
@ -253,10 +266,19 @@ class GetJobOrderArchiveDataCommand extends Command
$price, $price,
$battery_id $battery_id
]; ];
$ii_id_list[] = $id;
} }
$file = $this->createDataFileRelatedArchiveData($ii_data, 'invoice_item', $year, 'a'); $file = $this->createDataFileRelatedArchiveData($ii_data, 'invoice_item', $year, 'a');
unset($ii_data);
// special case, delete the invoice items already
// so that we don't have to query for the invoice items to delete
if (count($ii_id_list) > 0)
$this->deleteInvoiceItems($ii_id_list);
return $file; return $file;
} }
@ -778,17 +800,22 @@ class GetJobOrderArchiveDataCommand extends Command
} }
} }
protected function deleteData($jo_id_list, $invoice_id_list, $related_tables) protected function deleteInvoiceItems($ii_id_list)
{ {
$db = $this->em->getConnection(); $db = $this->em->getConnection();
// delete the invoice items first // delete the invoice items first
$inv_ids = str_repeat('?,', count($invoice_id_list) - 1) . '?'; $ii_ids = str_repeat('?,', count($ii_id_list) - 1) . '?';
$ii_del_sql = 'DELETE FROM invoice_item WHERE invoice_id IN (' . $inv_ids . ')'; $ii_del_sql = 'DELETE FROM invoice_item WHERE id IN (' . $ii_ids . ')';
$ii_stmt = $db->prepare($ii_del_sql); $ii_stmt = $db->prepare($ii_del_sql);
$ii_stmt->execute($invoice_id_list); $ii_stmt->execute($ii_id_list);
}
protected function deleteData($jo_id_list, $related_tables)
{
$db = $this->em->getConnection();
// delete from invoice, jo_rejection, rider_rating, ticket, and jo_event // delete from invoice, jo_rejection, rider_rating, ticket, and jo_event
$jo_ids = str_repeat('?,', count($jo_id_list) - 1) . '?'; $jo_ids = str_repeat('?,', count($jo_id_list) - 1) . '?';
@ -821,36 +848,8 @@ class GetJobOrderArchiveDataCommand extends Command
$jo_stmt->execute($jo_id_list); $jo_stmt->execute($jo_id_list);
} }
protected function getInvoiceIds($jo_id_list)
{
$invoice_id_list = [];
$db = $this->em->getConnection();
$db->getWrappedConnection()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$query_sql = 'SELECT id FROM invoice WHERE job_order_id = :id';
$query_stmt = $db->prepare($query_sql);
foreach ($jo_id_list as $jo_id)
{
// need to get the invoice ids for the invoice items to delete
$query_stmt->bindValue('id', $jo_id);
$results = $query_stmt->executeQuery();
while ($row = $results->fetchAssociative())
{
$invoice_id_list[] = $row['id'];
}
}
return $invoice_id_list;
}
protected function updateRiderJobOrders($jo_id_list) protected function updateRiderJobOrders($jo_id_list)
{ {
// TODO: test this
$db = $this->em->getConnection(); $db = $this->em->getConnection();
$jo_ids = str_repeat('?,', count($jo_id_list) - 1) . '?'; $jo_ids = str_repeat('?,', count($jo_id_list) - 1) . '?';
@ -859,17 +858,20 @@ class GetJobOrderArchiveDataCommand extends Command
$update_curr_stmt = $db->prepare($update_curr_sql); $update_curr_stmt = $db->prepare($update_curr_sql);
$update_active_sql = 'UPDATE rider SET active_jo_id = NULL WHERE active_jo_id IN (' . $jo_ids . ')'; $update_active_sql = 'UPDATE rider SET active_jo_id = NULL WHERE active_jo_id IN (' . $jo_ids . ')';
$udpate_active_stmt = $db->prepare($update_active_sql); $update_active_stmt = $db->prepare($update_active_sql);
$update_curr_stmt->execute($jo_id_list); $update_curr_stmt->execute($jo_id_list);
$update_active_stmt->execute($jo_id_list); $update_active_stmt->execute($jo_id_list);
unset($update_curr_stmt);
unset($update_active_stmt);
} }
protected function loadDataFileForArchiveData($load_stmt) protected function loadDataFileForArchiveData($load_stmt)
{ {
$conn = $this->em->getConnection(); $conn = $this->em->getConnection();
$conn->getWrappedConnection()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
// this statement is for job order
$stmt = $conn->prepare($load_stmt); $stmt = $conn->prepare($load_stmt);
$result = $stmt->execute(); $result = $stmt->execute();