Add loading of data file. #762

This commit is contained in:
Korina Cordero 2023-09-18 18:10:06 +08:00
parent c1578eee1f
commit 3773a664d1

View file

@ -57,25 +57,27 @@ class GetJobOrderArchiveDataCommand extends Command
$this->createTicketArchiveTable($year); $this->createTicketArchiveTable($year);
$this->createJORejectionArchiveTable($year); $this->createJORejectionArchiveTable($year);
$this->createRiderRatingArchiveTable($year); $this->createRiderRatingArchiveTable($year);
$this->createJOEventArchiveTable($year);
$db = $this->em->getConnection(); $db = $this->em->getConnection();
$query_sql = 'SELECT * $query_sql = 'SELECT *
FROM job_order FROM job_order
WHERE YEAR(date_create) = :year WHERE YEAR(date_create) = :year
ORDER BY date_create'; ORDER BY date_create
LIMIT 100';
$query_stmt = $db->prepare($query_sql); $query_stmt = $db->prepare($query_sql);
$query_stmt->bindValue('year', $year, PDO::PARAM_STR); $query_stmt->bindValue('year', $year, PDO::PARAM_STR);
$callback = ['App\Command\GetJobOrderArchiveDataCommand', 'getRelatedArchiveData']; $callback = ['App\Command\GetJobOrderArchiveDataCommand', 'getRelatedArchiveData'];
$this->getArchiveData($query_stmt, $callback); $this->getArchiveData($query_stmt, $callback, 'job_order', $year);
return 0; return 0;
} }
protected function getArchiveData($stmt, $callbackJO) protected function getArchiveData($stmt, $callbackJO, $jo_tname, $year)
{ {
$results = $stmt->executeQuery(); $results = $stmt->executeQuery();
@ -117,21 +119,42 @@ class GetJobOrderArchiveDataCommand extends Command
$query_stmt = $db->prepare($query_sql); $query_stmt = $db->prepare($query_sql);
$query_stmt->bindValue('id', $row['id']); $query_stmt->bindValue('id', $row['id']);
call_user_func($callbackJO, $row, $query_stmt, $table_name); call_user_func($callbackJO, $row, $query_stmt, $table_name, $year);
} }
} }
} }
// write the array into the file // write the array into the file
$file = $this->createDataFileRelatedArchiveData($jo_data, 'job_order', 'w'); $file = $this->createDataFileRelatedArchiveData($jo_data, $jo_tname, 'w');
if ($file != null) if ($file != null)
{ {
$archive_tname = $jo_tname . '_archive_' . $year;
// load statement for job order
$load_stmt = 'LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $archive_tname . '
FIELDS TERMINATED BY \'|\'
LINES TERMINATED BY \'\\r\\n\'
(id, 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)
SET coordinates=ST_GeomFromText(@geo_coordinates)';
// call load data infile // call load data infile
$this->loadDataFileForArchiveData($load_stmt);
} }
} }
protected function getRelatedArchiveData($row, $query_stmt, $table_name) protected function getRelatedArchiveData($row, $query_stmt, $table_name, $year)
{ {
$results = $query_stmt->executeQuery(); $results = $query_stmt->executeQuery();
@ -144,7 +167,7 @@ class GetJobOrderArchiveDataCommand extends Command
if ($table_name == 'invoice') if ($table_name == 'invoice')
{ {
// get invoice item related data // get invoice item related data
$this->getInvoiceItemArchiveData($q_row); $this->getInvoiceItemArchiveData($q_row, $year);
} }
$fields = []; $fields = [];
@ -161,13 +184,56 @@ class GetJobOrderArchiveDataCommand extends Command
// write the array into the file // write the array into the file
$file = $this->createDataFileRelatedArchiveData($data, $table_name, 'a'); $file = $this->createDataFileRelatedArchiveData($data, $table_name, 'a');
// TODO: this needs to move or something because we are writing duplicate rows into the db
if ($file != null) if ($file != null)
{ {
$archive_tname = $table_name . '_archive_' . $year;
if ($table_name == 'jo_event')
{
$load_stmt = 'LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $archive_tname . '
FIELDS TERMINATED BY \'|\'
LINES TERMINATED BY \'\\r\\n\'
(id, create_user_id, job_order_id, date_create, date_happen, type_id, rider_id)';
}
if ($table_name == 'jo_rejection')
{
$load_stmt = 'LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $archive_tname . '
FIELDS TERMINATED BY \'|\'
LINES TERMINATED BY \'\\r\\n\'
(id, user_id, hub_id, jo_id, date_create, reason, remarks, contact_person)';
}
if ($table_name == 'invoice')
{
$load_stmt = 'LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $archive_tname . '
FIELDS TERMINATED BY \'|\'
LINES TERMINATED BY \'\\r\\n\'
(id, user_id, job_order_id, date_create, date_paid, discount, trade_in, vat, vat_exclusive_price,
total_price, status, promo_id, used_customer_tag_id)';
}
if ($table_name == 'rider_rating')
{
$load_stmt = 'LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $archive_tname . '
FIELDS TERMINATED BY \'|\'
LINES TERMINATED BY \'\\r\\n\'
(id, rider_id, customer_id, jo_id, date_create, rating, comment)';
}
if ($table_name == 'ticket')
{
$load_stmt = 'LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $archive_tname . '
FIELDS TERMINATED BY \'|\'
LINES TERMINATED BY \'\\r\\n\'
(id, user_id, customer_id, date_create, status, ticket_type, other_ticket_type, first_name, last_name,
contact_num, details, job_order_id, plate_number, ticket_type_id, subticket_type_id,
source_of_awareness, remarks, other_description)';
}
// call load data infile // call load data infile
$this->loadDataFileForArchiveData($load_stmt);
} }
} }
protected function getInvoiceItemArchiveData($row) protected function getInvoiceItemArchiveData($row, $year)
{ {
$db = $this->em->getConnection(); $db = $this->em->getConnection();
@ -202,6 +268,12 @@ class GetJobOrderArchiveDataCommand extends Command
if ($file != null) if ($file != null)
{ {
$archive_tname = 'invoice_item_archive' . $year;
$load_stmt = 'LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $archive_tname . '
FIELDS TERMINATED BY \'|\'
LINES TERMINATED BY \'\\r\\n\'
(id, invoice_id, title, qty, price, battery_id)';
// call load data infile // call load data infile
} }
} }
@ -323,6 +395,8 @@ class GetJobOrderArchiveDataCommand extends Command
$create_stmt = $db->prepare($create_sql); $create_stmt = $db->prepare($create_sql);
$result = $create_stmt->execute(); $result = $create_stmt->execute();
return $archive_table_name;
} }
protected function createInvoiceArchiveTable($year) protected function createInvoiceArchiveTable($year)
@ -438,7 +512,7 @@ class GetJobOrderArchiveDataCommand extends Command
protected function createRiderRatingArchiveTable($year) protected function createRiderRatingArchiveTable($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>
$archive_table_name = 'jo_rejection_archive_' . $year; $archive_table_name = 'rider_rating_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();
@ -458,6 +532,29 @@ class GetJobOrderArchiveDataCommand extends Command
$result = $create_stmt->execute(); $result = $create_stmt->execute();
} }
protected function createJOEventArchiveTable($year)
{
// form the archive table name <original table name>_archive_<year of data being archived>
$archive_table_name = 'jo_event_archive_' . $year;
// create the table if it doesn't exist
$db = $this->em->getConnection();
// TODO: What if table already exists?
$create_sql = 'CREATE TABLE IF NOT EXISTS `' . $archive_table_name . '` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`create_user_id` int(11) DEFAULT NULL,
`job_order_id` int(11) DEFAULT NULL,
`date_create` datetime NOT NULL,
`date_happen` datetime NOT NULL,
`type_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`rider_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`))';
$create_stmt = $db->prepare($create_sql);
$result = $create_stmt->execute();
}
protected function createJobOrderArchiveData($row) protected function createJobOrderArchiveData($row)
{ {
@ -622,36 +719,13 @@ class GetJobOrderArchiveDataCommand extends Command
return $data; return $data;
} }
// TODO: rewrite this to just load the file protected function loadDataFileForArchiveData($load_stmt)
protected function loadDataFileForArchiveData($file, $table_name)
{ {
// cache directory
$cache_dir = __DIR__ . '/../../var/cache';
$file = $cache_dir . '/' $table_name . 'archive.tab';
error_log('opening file for jo archive - ' . $file);
$conn = $this->em->getConnection(); $conn = $this->em->getConnection();
// this statement is for job order // this statement is for job order
// TODO: make for other tables // TODO: make for other tables
$stmt = $conn->prepare('LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $table_name . ' $stmt = $conn->prepare($load_stmt);
FIELDS TERMINATED BY \'|\'
LINES TERMINATED BY \'\\r\\n\'
(id, 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)
SET coordinates=ST_GeomFromText(@geo_coordinates)'
);
$result = $stmt->execute(); $result = $stmt->execute();