Fix deletion issues for related tables. #762

This commit is contained in:
Korina Cordero 2023-09-27 14:54:36 +08:00
parent 645e8a63fc
commit d78dc72c35

View file

@ -133,6 +133,7 @@ class GetJobOrderArchiveDataCommand extends Command
case 'jo_rejection': case 'jo_rejection':
case 'rider_rating': case 'rider_rating':
$query_sql = 'SELECT * FROM ' . $table_name . ' WHERE jo_id = :id'; $query_sql = 'SELECT * FROM ' . $table_name . ' WHERE jo_id = :id';
break;
default: default:
break; break;
} }
@ -171,7 +172,7 @@ class GetJobOrderArchiveDataCommand extends Command
// error_log(print_r($invoice_id_list, true)); // error_log(print_r($invoice_id_list, true));
// 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); $this->deleteData($jo_id_list, $invoice_id_list, $related_tables);
} }
protected function getRelatedArchiveData($row, $query_stmt, $table_name, $year) protected function getRelatedArchiveData($row, $query_stmt, $table_name, $year)
@ -775,17 +776,48 @@ class GetJobOrderArchiveDataCommand extends Command
} }
} }
protected function deleteData($jo_id_list, $invoice_id_list) protected function deleteData($jo_id_list, $invoice_id_list, $related_tables)
{ {
$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) . '?'; $inv_ids = str_repeat('?,', count($invoice_id_list) - 1) . '?';
$ii_del_sql = 'DELETE FROM invoice_item WHERE invoice_id IN ($inv_ids)'; $ii_del_sql = 'DELETE FROM invoice_item WHERE invoice_id IN (' . $inv_ids . ')';
$ii_stmt = $db->prepare($ii_del_sql); $ii_stmt = $db->prepare($ii_del_sql);
$ii_stmt->execute($invoice_id_list); $ii_stmt->execute($invoice_id_list);
// delete from invoice, jo_rejection, rider_rating, ticket, and jo_event
$jo_ids = str_repeat('?,', count($jo_id_list) - 1) . '?';
foreach ($related_tables as $table_name)
{
switch ($table_name) {
case 'jo_event':
case 'invoice':
case 'ticket':
$related_del_sql = 'DELETE FROM ' . $table_name . ' WHERE job_order_id IN ('. $jo_ids . ')';
break;
case 'jo_rejection':
case 'rider_rating':
$related_del_sql = 'DELETE FROM ' . $table_name . ' WHERE jo_id IN (' . $jo_ids. ')';
break;
default:
break;
}
$related_stmt = $db->prepare($related_del_sql);
$related_stmt->execute($jo_id_list);
}
// TODO: hitting a snag here if JO to be deleted is a reference JO for another JO
// delete from job order last
$jo_del_sql = 'DELETE FROM job_order WHERE id IN (' . $jo_ids . ')';
$jo_stmt = $db->prepare($jo_del_sql);
$jo_stmt->execute($jo_id_list);
} }
protected function getInvoiceIds($jo_id_list) protected function getInvoiceIds($jo_id_list)