Add archiving for invoice item. Add creation of other archive tables. #762

This commit is contained in:
Korina Cordero 2023-09-15 18:19:11 +08:00
parent d7a2b25e51
commit c1578eee1f

View file

@ -51,14 +51,19 @@ class GetJobOrderArchiveDataCommand extends Command
// (6) rider_rating (has jo foreign key) // (6) rider_rating (has jo foreign key)
// create the archive tables // create the archive tables
// TODO: create the other archive tables $this->createJobOrderArchiveTables($year);
$archive_table_name = $this->createJobOrderArchiveTable($year); $this->createInvoiceArchiveTable($year);
$this->createInvoiceItemArchiveTable($year);
$this->createTicketArchiveTable($year);
$this->createJORejectionArchiveTable($year);
$this->createRiderRatingArchiveTable($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';
$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);
@ -82,52 +87,66 @@ class GetJobOrderArchiveDataCommand extends Command
$this->deleteDataFiles($tname); $this->deleteDataFiles($tname);
} }
// special since this is not directly related to JO but to invoice
$this->deleteDataFiles('invoice_item');
while ($row = $results->fetchAssociative()) while ($row = $results->fetchAssociative())
{ {
$jo_data[] = $this->createJobOrderArchiveData($row); $jo_data['job_order'][$row['id']] = $this->createJobOrderArchiveData($row);
// foreach job order id we got from the first query, we get the JO related // foreach job order id we got from the first query, we get the JO related
// data for that id from jo_event, invoice, ticket, jo_rejection, rider_rating // data for that id from jo_event, invoice, ticket, jo_rejection, rider_rating
if (is_callable($callbackJO)) if (is_callable($callbackJO))
{ {
foreach ($related_tables as $table_name) foreach ($related_tables as $table_name)
{ {
call_user_func($callbackJO, $row, $table_name); 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';
}
$db = $this->em->getConnection();
$query_stmt = $db->prepare($query_sql);
$query_stmt->bindValue('id', $row['id']);
call_user_func($callbackJO, $row, $query_stmt, $table_name);
} }
} }
} }
// TODO: load data infile for job order // write the array into the file
$file = $this->createDataFileRelatedArchiveData($jo_data, 'job_order', 'w');
if ($file != null)
{
// call load data infile
}
} }
protected function getRelatedArchiveData($row, $table_name) protected function getRelatedArchiveData($row, $query_stmt, $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', $row['id']);
$results = $query_stmt->executeQuery(); $results = $query_stmt->executeQuery();
$data = []; $data = [];
while ($q_row = $results->fetchAssociative()) while ($q_row = $results->fetchAssociative())
{ {
// check if table name is invoice because we need to get
// all invoice items for a specific invoice too
if ($table_name == 'invoice')
{
// get invoice item related data
$this->getInvoiceItemArchiveData($q_row);
}
$fields = []; $fields = [];
foreach ($q_row as $key => $value) foreach ($q_row as $key => $value)
@ -140,7 +159,46 @@ class GetJobOrderArchiveDataCommand extends Command
} }
// write the array into the file // write the array into the file
$file = $this->createDataFileRelatedArchiveData($data, $table_name); $file = $this->createDataFileRelatedArchiveData($data, $table_name, 'a');
if ($file != null)
{
// call load data infile
}
}
protected function getInvoiceItemArchiveData($row)
{
$db = $this->em->getConnection();
$query_sql = 'SELECT * FROM invoice_item WHERE invoice_id = :id';
$query_stmt = $db->prepare($query_sql);
$query_stmt->bindValue('id', $row['id']);
$results = $query_stmt->executeQuery();
$ii_data = [];
while ($ii_row = $results->fetchAssociative())
{
$id = $ii_row['id'];
$invoice_id = $ii_row['invoice_id'];
$title = $ii_row['title'];
$qty = $ii_row['qty'];
$price = $ii_row['price'];
$battery_id = $ii_row['battery_id'] ?? '\N';
$ii_data['invoice_item'][$id] = [
$id,
$invoice_id,
$title,
$qty,
$price,
$battery_id
];
}
$file = $this->createDataFileRelatedArchiveData($ii_data, 'invoice_item', 'a');
if ($file != null) if ($file != null)
{ {
@ -159,7 +217,7 @@ class GetJobOrderArchiveDataCommand extends Command
unlink($file); unlink($file);
} }
protected function createDataFileRelatedArchiveData($archive_data, $table_name) protected function createDataFileRelatedArchiveData($archive_data, $table_name, $option)
{ {
if (isset($archive_data[$table_name])) if (isset($archive_data[$table_name]))
{ {
@ -171,7 +229,7 @@ class GetJobOrderArchiveDataCommand extends Command
$file = $cache_dir . '/' . $table_name . '_archive.tab'; $file = $cache_dir . '/' . $table_name . '_archive.tab';
error_log('opening file for archive - ' . $file); error_log('opening file for archive - ' . $file);
$fp = fopen($file, 'a'); $fp = fopen($file, $option);
if ($fp === false) if ($fp === false)
{ {
error_log('could not open file for load data infile - ' . $file); error_log('could not open file for load data infile - ' . $file);
@ -193,11 +251,7 @@ class GetJobOrderArchiveDataCommand extends Command
return null; return null;
} }
// TODO: make this so you just call one function to create all the tables protected function createJobOrderArchiveTables($year) {
// 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 <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 = 'job_order_archive_' . $year; $archive_table_name = 'job_order_archive_' . $year;
@ -269,10 +323,142 @@ 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)
{
// form the archive table name <original table name>_archive_<year of data being archived>
$archive_table_name = 'invoice_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,
`user_id` int(11) DEFAULT NULL,
`job_order_id` int(11) DEFAULT NULL,
`date_create` datetime NOT NULL,
`date_paid` datetime DEFAULT NULL,
`date_cancel` datetime DEFAULT NULL,
`discount` decimal(9,2) NOT NULL,
`trade_in` decimal(9,2) NOT NULL,
`vat` decimal(9,2) NOT NULL,
`vat_exclusive_price` decimal(9,2) NOT NULL,
`total_price` decimal(9,2) NOT NULL,
`status` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
`promo_id` int(11) DEFAULT NULL,
`used_customer_tag_id` varchar(80) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`))';
$create_stmt = $db->prepare($create_sql);
$result = $create_stmt->execute();
}
protected function createInvoiceItemArchiveTable($year)
{
// form the archive table name <original table name>_archive_<year of data being archived>
$archive_table_name = 'invoice_item_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,
`invoice_id` int(11) DEFAULT NULL,
`title` varchar(80) COLLATE utf8_unicode_ci NOT NULL,
`qty` smallint(6) NOT NULL,
`price` decimal(9,2) NOT NULL,
`battery_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`))';
$create_stmt = $db->prepare($create_sql);
$result = $create_stmt->execute();
}
protected function createTicketArchiveTable($year)
{
// form the archive table name <original table name>_archive_<year of data being archived>
$archive_table_name = 'ticket_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,
`user_id` int(11) DEFAULT NULL,
`customer_id` int(11) DEFAULT NULL,
`date_create` datetime NOT NULL,
`status` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`ticket_type` varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
`other_ticket_type` varchar(80) COLLATE utf8_unicode_ci DEFAULT NULL,
`first_name` varchar(80) COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(80) COLLATE utf8_unicode_ci NOT NULL,
`contact_num` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`details` longtext COLLATE utf8_unicode_ci DEFAULT NULL,
`job_order_id` int(11) DEFAULT NULL,
`plate_number` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`ticket_type_id` int(11) DEFAULT NULL,
`subticket_type_id` int(11) DEFAULT NULL,
`source_of_awareness` varchar(80) COLLATE utf8_unicode_ci DEFAULT NULL,
`remarks` longtext COLLATE utf8_unicode_ci DEFAULT NULL,
`other_description` longtext COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`))';
$create_stmt = $db->prepare($create_sql);
$result = $create_stmt->execute();
}
protected function createJORejectionArchiveTable($year)
{
// form the archive table name <original table name>_archive_<year of data being archived>
$archive_table_name = 'jo_rejection_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,
`user_id` int(11) DEFAULT NULL,
`hub_id` int(11) DEFAULT NULL,
`jo_id` int(11) DEFAULT NULL,
`date_create` datetime NOT NULL,
`reason` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`remarks` longtext COLLATE utf8_unicode_ci DEFAULT NULL,
`contact_person` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`))';
$create_stmt = $db->prepare($create_sql);
$result = $create_stmt->execute();
}
protected function createRiderRatingArchiveTable($year)
{
// form the archive table name <original table name>_archive_<year of data being archived>
$archive_table_name = 'jo_rejection_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,
`rider_id` int(11) DEFAULT NULL,
`customer_id` int(11) DEFAULT NULL,
`jo_id` int(11) DEFAULT NULL,
`date_create` datetime NOT NULL,
`rating` int(11) NOT NULL,
`comment` longtext COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`))';
$create_stmt = $db->prepare($create_sql);
$result = $create_stmt->execute();
}
protected function createJobOrderArchiveData($row) protected function createJobOrderArchiveData($row)
{ {
// TODO: this could be shrunk further // TODO: this could be shrunk further
@ -436,31 +622,19 @@ class GetJobOrderArchiveDataCommand extends Command
return $data; return $data;
} }
protected function createLoadDataFileForArchiveData($archive_data, $table_name) // TODO: rewrite this to just load the file
protected function loadDataFileForArchiveData($file, $table_name)
{ {
// cache directory // cache directory
$cache_dir = __DIR__ . '/../../var/cache'; $cache_dir = __DIR__ . '/../../var/cache';
$file = $cache_dir . '/jo_archive.tab'; $file = $cache_dir . '/' $table_name . 'archive.tab';
error_log('opening file for jo archive - ' . $file); error_log('opening file for jo archive - ' . $file);
$fp = fopen($file, 'w');
if ($fp === false)
{
error_log('could not open file for load data infile - ' . $file);
}
else
{
foreach ($archive_data as $key => $data)
{
$line = implode('|', $data) . "\r\n";
fwrite($fp, $line);
}
}
fclose($fp);
$conn = $this->em->getConnection(); $conn = $this->em->getConnection();
// this statement is for job order
// TODO: make for other tables
$stmt = $conn->prepare('LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $table_name . ' $stmt = $conn->prepare('LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE ' . $table_name . '
FIELDS TERMINATED BY \'|\' FIELDS TERMINATED BY \'|\'
LINES TERMINATED BY \'\\r\\n\' LINES TERMINATED BY \'\\r\\n\'