diff --git a/src/Command/GetJobOrderArchiveDataCommand.php b/src/Command/GetJobOrderArchiveDataCommand.php index 2e8e6a41..e1bcabbd 100644 --- a/src/Command/GetJobOrderArchiveDataCommand.php +++ b/src/Command/GetJobOrderArchiveDataCommand.php @@ -42,43 +42,134 @@ class GetJobOrderArchiveDataCommand extends Command // get table to back up $year = $input->getArgument('year'); - $em = $this->em; + // get the data to archive for the following tables: + // (1) job_order + // (2) invoice (has jo foreign key) + // (3) invoice_item (has invoice foreign key) + // (4) ticket (has jo foreign key) + // (5) jo_rejection (has jo foreign key) + // (6) rider_rating (has jo foreign key) - $db = $em->getConnection(); + $jo_backup_data = $this->getJobOrderData($year); - // create the archive table - $archive_table_name = $this->createArchiveTable($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(); - // TODO: move the creation of the query_sql to a function or - // set this as a preset or a variable or something and load specific query - // according to what table is to be archived. - // sql query to retrieve the rows or entries for backup $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(); - $backup_data = []; + $jo_data = []; + $jo_event_data = []; + $invoice_data = []; + $ticket_data = []; + $jo_rejection_data = []; + $rider_rating_data = []; while ($row = $results->fetchAssociative()) { - $backup_data[] = $this->createBackupData($row); + $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']; + + $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 } - error_log('count ' . count($backup_data)); + $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, + ]; - // create the load file for the backup data - $this->createLoadDataFileForBackupData($backup_data, $archive_table_name); - - return 0; + return $backup_data; } - protected function createArchiveTable($year) + protected function getJORelatedData($id, $table_name) + { + $db = $this->em->getConnection(); + + if (($table_name == 'jo_event') || + ($table_name == 'invoice') || + ($table_name == 'ticket')) + { + $query_sql = 'SELECT * + FROM ' . $table_name . ' WHERE job_order_id = :id'; + } + else + { + $query_sql = 'SELECT * + FROM ' . $table_name . ' WHERE jo_id = :id'; + } + + $query_stmt = $db->prepare($query_sql); + $query_stmt->bindValue('id', $id); + + $results = $query_stmt->executeQuery(); + + $jo_related_data = []; + + while ($row = $results->fetchAssociative()) + { + if ($table_name == 'jo_event') + { + // 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 + } + } + + return $jo_related_data; + } + + // TODO: make this so you just call one function to create all the tables + // pass the year and the table name + // set the create sql as a constant or something + protected function createJobOrderArchiveTable($year) { // form the archive table name _archive_ $archive_table_name = 'job_order_archive_' . $year; @@ -155,7 +246,7 @@ class GetJobOrderArchiveDataCommand extends Command return $archive_table_name; } - protected function createBackupData($row) + protected function createJobOrderArchiveData($row) { // 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, @@ -316,6 +407,33 @@ 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) { // cache directory