Modify code according to code review. #762

This commit is contained in:
Korina Cordero 2023-09-12 11:35:31 +08:00
parent 7e3c392c03
commit 986181c780

View file

@ -34,91 +34,32 @@ class GetJobOrderArchiveDataCommand extends Command
$this->setName('joborder:archive') $this->setName('joborder:archive')
->setDescription('Get job order data to archive.') ->setDescription('Get job order data to archive.')
->setHelp('Get job order data to archive.') ->setHelp('Get job order data to archive.')
->addArgument('cutoff_date', InputArgument::REQUIRED, 'cutoff_date') ->addArgument('year', InputArgument::REQUIRED, 'year');
->addArgument('period', InputArgument::REQUIRED, 'period');
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
// get table to back up // get table to back up
$period = $input->getArgument('period'); $year = $input->getArgument('year');
$cutoff_date = $input->getArgument('cutoff_date');
$cutoff_date_time = $cutoff_date . ' 23:59:59';
$em = $this->em; $em = $this->em;
$db = $em->getConnection(); $db = $em->getConnection();
// create the archive table // create the archive table
$archive_table_name = $this->createArchiveTable($cutoff_date, $period); $archive_table_name = $this->createArchiveTable($year);
// 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 // sql query to retrieve the rows or entries for backup
$query_sql = 'SELECT id, $query_sql = 'SELECT *
customer_id,
cvehicle_id,
rider_id,
date_create,
date_schedule,
date_fulfill,
coordinates,
flag_advance,
service_type,
source,
date_cancel,
status,
delivery_instructions,
delivery_address,
create_user_id,
assign_user_id,
date_assign,
warranty_class,
process_user_id,
hub_id,
cancel_reason,
ref_jo_id,
tier1_notes,
tier2_notes,
mode_of_payment,
or_name,
landmark,
promo_detail,
or_num,
trade_in_type,
flag_rider_rating,
flag_coolant,
facilitated_hub_id,
facilitated_type,
coord_long,
coord_lat,
priority,
meta,
status_autoassign,
first_name,
last_name,
plate_number,
phone_mobile,
no_trade_in_reason,
will_wait,
reason_not_waiting,
not_waiting_notes,
delivery_status,
emergency_type_id,
ownership_type_id,
cust_location_id,
source_of_awareness,
remarks,
initial_concern,
initial_concern_notes,
gender,
caller_classification,
inventory_count
FROM job_order FROM job_order
WHERE DATE_CREATE <= DATE_SUB(:cutoff_date, INTERVAL ' . $period . ' 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('cutoff_date', $cutoff_date, PDO::PARAM_STR); $query_stmt->bindValue('year', $year, PDO::PARAM_STR);
$results = $query_stmt->executeQuery(); $results = $query_stmt->executeQuery();
@ -137,20 +78,15 @@ class GetJobOrderArchiveDataCommand extends Command
return 0; return 0;
} }
protected function createArchiveTable($str_date, $period) protected function createArchiveTable($year)
{ {
// form the archive table name <original table name>_archive_<year of data being archived> // form the archive table name <original table name>_archive_<year of data being archived>
$modifier = '-' . $period. 'year';
// convert the date string into DateTime
$date = new DateTime($str_date);
$year = $date->modify($modifier)->format('Y');
$archive_table_name = 'job_order_archive_' . $year; $archive_table_name = 'job_order_archive_' . $year;
// create the table if it doesn't exist // create the table if it doesn't exist
$db = $this->em->getConnection(); $db = $this->em->getConnection();
// TODO: What if table already exists?
$create_sql = 'CREATE TABLE IF NOT EXISTS `' . $archive_table_name . '` ( $create_sql = 'CREATE TABLE IF NOT EXISTS `' . $archive_table_name . '` (
`id` int(11) NOT NULL, `id` int(11) NOT NULL,
`customer_id` int(11) DEFAULT NULL, `customer_id` int(11) DEFAULT NULL,
@ -223,7 +159,7 @@ class GetJobOrderArchiveDataCommand extends Command
{ {
// 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, proces_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,
// flag_rider_rating, facilitated_type, facilitated_hub_id, status_autoassign, reason_not_waiting, // flag_rider_rating, facilitated_type, facilitated_hub_id, status_autoassign, reason_not_waiting,
// not_waiting_notes, no_trade_in_reason, delivery_status, source_of_awareness, remarks, initial_concern, // not_waiting_notes, no_trade_in_reason, delivery_status, source_of_awareness, remarks, initial_concern,
// initial_concern_notes, gender, caller_classifications, emergency_type_id, ownership_type_id, cust_location_id // initial_concern_notes, gender, caller_classifications, emergency_type_id, ownership_type_id, cust_location_id
@ -232,59 +168,34 @@ class GetJobOrderArchiveDataCommand extends Command
$cust_id = $row['customer_id']; $cust_id = $row['customer_id'];
$cv_id = $row['cvehicle_id']; $cv_id = $row['cvehicle_id'];
$rider_id = '\N'; $rider_id = $row['rider_id'] ?? '\N';
if ($row['rider_id'] != NULL)
$rider_id = $row['rider_id'];
$date_create = $row['date_create']; $date_create = $row['date_create'];
$date_schedule = $row['date_schedule']; $date_schedule = $row['date_schedule'];
$date_fulfill = '\N'; $date_fulfill = $row['date_fulfill'] ?? '\N';
if ($row['date_fulfill'] != NULL)
$date_fulfill = $row['date_schedule'];
$flag_advance = $row['flag_advance']; $flag_advance = $row['flag_advance'];
$service_type = $row['service_type']; $service_type = $row['service_type'];
$source = $row['source']; $source = $row['source'];
$date_cancel = '\N'; $date_cancel = $row['date_cancel'] ?? '\N';
if ($row['date_cancel'] != NULL)
$date_cancel = $row['date_cancel'];
$status = $row['status']; $status = $row['status'];
$del_instructions = $this->cleanData($row['delivery_instructions']); $del_instructions = $this->cleanData($row['delivery_instructions']);
$del_address = $this->cleanData($row['delivery_address']); $del_address = $this->cleanData($row['delivery_address']);
$create_user_id = '\N'; $create_user_id = $row['create_user_id'] ?? '\N';
if ($row['create_user_id'] != NULL) $assign_user_id = $row['assign_user_id'] ?? '\N';
$create_user_id = $row['create_user_id']; $date_assign = $row['date_assign'] ?? '\N';
$assign_user_id = '\N';
if ($row['assign_user_id'] != NULL)
$assign_user_id = $row['assign_user_id'];
$date_assign = '\N';
if ($row['date_assign'] != NULL)
$date_assign = $row['date_assign'];
$warr_class = $row['warranty_class']; $warr_class = $row['warranty_class'];
$process_user_id = '\N'; $process_user_id = $row['process_user_id'] ?? '\N';
if ($row['process_user_id'] != NULL) $hub_id = $row['hub_id'] ?? '\N';
$process_user_id = $row['process_user_id']; $cancel_reason = $row['cancel_reason'] ?? '\N';
$ref_jo_id = $row['ref_jo_id'] ?? '\N';
$hub_id = '\N';
if ($row['hub_id'] != NULL)
$hub_id = $row['hub_id'];
$cancel_reason = '\N';
if ($row['cancel_reason'] != NULL)
$cancel_reason = $row['cancel_reason'];
$ref_jo_id = '\N';
if ($row['ref_jo_id'] != NULL)
$ref_jo_id = $row['ref_jo_id'];
$tier1_notes = $this->cleanData($row['tier1_notes']); $tier1_notes = $this->cleanData($row['tier1_notes']);
$tier2_notes = $this->cleanData($row['tier2_notes']); $tier2_notes = $this->cleanData($row['tier2_notes']);
@ -295,99 +206,47 @@ class GetJobOrderArchiveDataCommand extends Command
$landmark = $this->cleanData($row['landmark']); $landmark = $this->cleanData($row['landmark']);
$promo_details = $row['promo_detail']; $promo_details = $row['promo_detail'];
$or_num = '\N'; $or_num = $row['or_num'] ?? '\N';
if ($row['or_num'] != NULL) $trade_in_type = $row['trade_in_type'] ?? '\N';
$or_num = $row['or_num']; $flag_rider_rating = $row['flag_rider_rating'] ?? '\N';
$trade_in_type = '\N';
if ($row['trade_in_type'] != NULL)
$trade_in_type = $row['trade_in_type'];
$flag_rider_rating = '\N';
if ($row['flag_rider_rating'] != NULL)
$flag_rider_rating = $row['flag_rider_rating'];
$flag_coolant = $row['flag_coolant']; $flag_coolant = $row['flag_coolant'];
$fac_hub_id = '\N'; $fac_hub_id = $row['facilitated_hub_id'] ?? '\N';
if ($row['facilitated_hub_id'] != NULL) $fac_type = $row['facilitated_type'] ?? '\N';
$fac_hub_id = $row['facilitated_hub_id'];
$fac_type = '\N';
if ($row['facilitated_type'] != NULL)
$fac_type = $row['facilitated_type'];
$coord_long = $row['coord_long']; $coord_long = $row['coord_long'];
$coord_lat = $row['coord_lat']; $coord_lat = $row['coord_lat'];
// coordinates needs special handling since it's a spatial column // coordinates needs special handling since it's a spatial column
$coordinates = 'POINT(' . $coord_lat . ' ' . $coord_long .')'; $geo_coordinates = 'POINT(' . $coord_lat . ' ' . $coord_long .')';
$priority = $row['priority']; $priority = $row['priority'];
$meta = $row['meta']; $meta = $row['meta'];
$status_autoassign = '\N'; $status_autoassign = $row['status_autoassign'] ?? '\N';
if ($row['status_autoassign'] != NULL)
$status_autoassign = $row['status_autoassign'];
$first_name = $row['first_name']; $first_name = $row['first_name'];
$last_name = $row['last_name']; $last_name = $row['last_name'];
$plate_number = $row['plate_number']; $plate_number = $row['plate_number'];
$phone_mobile = $row['phone_mobile']; $phone_mobile = $row['phone_mobile'];
$no_trade_in_reason = '\N'; $no_trade_in_reason = $row['no_trade_in_reason'] ?? '\N';
if ($row['no_trade_in_reason'] != NULL)
$no_trade_in_reason = $row['no_trade_in_reason'];
$will_wait = $row['will_wait']; $will_wait = $row['will_wait'];
$reason_not_waiting = '\N'; $reason_not_waiting = $row['reason_not_waiting'] ?? '\N';
if ($row['reason_not_waiting'] != NULL) $not_waiting_notes = $this->cleanData($row['not_waiting_notes']) ?? '\N';
$reason_not_waiting = $row['reason_not_waiting']; $del_status = $row['delivery_status'] ?? '\N';
$emergency_type_id = $row['emergency_type_id'] ?? '\N';
$not_waiting_notes = '\N'; $owner_type_id = $row['ownership_type_id'] ?? '\N';
if ($row['not_waiting_notes'] != NULL) $cust_location_id = $row['cust_location_id'] ?? '\N';
$not_waiting_notes = $row['not_waiting_notes']; $source_of_awareness = $row['source_of_awareness'] ?? '\N';
$remarks = $this->cleanData($row['remarks']) ?? '\N';
$del_status = '\N'; $initial_concern = $row['initial_concern'] ?? '\N';
if ($row['delivery_status'] != NULL) $initial_concern_notes = $this->cleanData($row['initial_concern_notes']) ?? '\N';
$del_status = $row['delivery_status']; $gender = $row['gender'] ?? '\N';
$caller_class = $row['caller_classification'] ?? '\N';
$emergency_type_id = '\N';
if ($row['emergency_type_id'] != NULL)
$emergency_type_id = $row['emergency_type_id'];
$owner_type_id = '\N';
if ($row['ownership_type_id'] != NULL)
$owner_type_id = $row['ownership_type_id'];
$cust_location_id = '\N';
if ($row['cust_location_id'] != NULL)
$cust_location_id = $row['cust_location_id'];
$source_of_awareness = '\N';
if ($row['source_of_awareness'] != NULL)
$source_of_awareness = $row['source_of_awareness'];
$remarks = '\N';
if ($row['remarks'] != NULL)
$remarks = $this->cleanData(row['remarks']);
$initial_concern = '\N';
if ($row['initial_concern'] != NULL)
$initial_concern = $row['initial_concern'];
$initial_concern_notes = '\N';
if ($row['initial_concern_notes'] != NULL)
$initial_concern_notes = $row['initial_concern_notes'];
$gender = '\N';
if ($row['gender'] != NULL)
$gender = $row['gender'];
$caller_class = '\N';
if ($row['caller_classification'] != NULL)
$caller_class = $row['caller_classification'];
$inv_count = $row['inventory_count']; $inv_count = $row['inventory_count'];
@ -400,7 +259,7 @@ class GetJobOrderArchiveDataCommand extends Command
$date_create, $date_create,
$date_schedule, $date_schedule,
$date_fulfill, $date_fulfill,
$coordinates, $geo_coordinates,
$flag_advance, $flag_advance,
$service_type, $service_type,
$source, $source,
@ -497,7 +356,7 @@ class GetJobOrderArchiveDataCommand extends Command
will_wait, reason_not_waiting, not_waiting_notes, delivery_status, emergency_type_id, will_wait, reason_not_waiting, not_waiting_notes, delivery_status, emergency_type_id,
ownership_type_id, cust_location_id, source_of_awareness, remarks, initial_concern, ownership_type_id, cust_location_id, source_of_awareness, remarks, initial_concern,
initial_concern_notes, gender, caller_classification, inventory_count) initial_concern_notes, gender, caller_classification, inventory_count)
SET coordinates=ST_GeomFromText(@coordinates)' SET coordinates=ST_GeomFromText(@geo_coordinates)'
); );
$result = $stmt->execute(); $result = $stmt->execute();