Add updating of rider's active and current job orders. #762

This commit is contained in:
Korina Cordero 2023-09-28 16:00:28 +08:00 committed by Korina Cordero
parent 8ff3fe8d3c
commit d75b94693b

View file

@ -169,7 +169,13 @@ class GetJobOrderArchiveDataCommand extends Command
// need to get the list of invoice ids for deletion for invoice items
$invoice_id_list = $this->getInvoiceIds($jo_id_list);
// error_log(print_r($invoice_id_list, true));
// need to get the list of riders whose active_jo_id or current_jo_id is
// set to very old JOs
$rider_id_list = $this->findRiderIDs($jo_id_list);
// update rider's active_jo_id and current_jo_id to null
// so we can delete the old job orders (foreign key constraint)
$this->updateRiderJobOrders($rider_id_list);
// 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);
@ -812,7 +818,6 @@ class GetJobOrderArchiveDataCommand extends Command
$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);
@ -846,12 +851,51 @@ class GetJobOrderArchiveDataCommand extends Command
return $invoice_id_list;
}
protected function findRiderIDs($jo_id_list)
{
$rider_id_list = [];
$db = $this->em->getConnection();
$rider_sql = 'SELECT id FROM rider WHERE ((current_jo_id = :jo_id) OR (active_jo_id = :jo_id))';
$rider_stmt = $db->prepare($rider_sql);
// find the riders with current_jo_id or active_jo_id are still set to the old JOs
foreach ($jo_id_list as $jo_id)
{
$rider_stmt->bindValue('jo_id', $jo_id);
$results = $rider_stmt->executeQuery();
while ($row = $results->fetchAssociative())
{
$rider_id_list[] = $row['id'];
}
}
return $rider_id_list;
}
protected function updateRiderJobOrders($rider_id_list)
{
$db = $this->em->getConnection();
$update_sql = 'UPDATE rider SET current_jo_id = NULL, SET active_jo_id = NULL WHERE id = :id';
$update_stmt = $db->prepare($update_sql);
foreach ($rider_id_list as $rider_id)
{
$update_stmt->execute([
'id' => $rider_id,
]);
}
}
protected function loadDataFileForArchiveData($load_stmt)
{
$conn = $this->em->getConnection();
// this statement is for job order
// TODO: make for other tables
$stmt = $conn->prepare($load_stmt);
$result = $stmt->execute();