resq/utils/load_warranty_serial/bulk_load_serials.php

187 lines
5.2 KiB
PHP

<?php
require_once(__DIR__ . '/../../vendor/autoload.php');
use Symfony\Component\Dotenv\Dotenv;
// TODO: this whole thing needs to be refactored
// load csv
$csv = fopen($argv[1], 'r');
$output_file = $argv[2];
$output_fh = fopen($output_file, "a");
if (!file_exists($argv[1]))
{
$err_message = "No csv input file found." . "\n";
fwrite($output_fh, $err_message);
fclose($output_fh);
exit();
}
// get username and password
$dotenv = new Dotenv();
$dotenv->loadEnv(__DIR__.'/../../.env');
$db_info = $_ENV['DATABASE_URL'];
// sample format of db_info: mysql://db_user:db_password@127.0.0.1:3306/resq?charset=utf8
// dsn for PDO needs to be: mysql:host=127.0.0.1:3306;dbname=resq;charset=UTF8
preg_match('/^mysql:\/\/(.*):(.*)@(.*):(.*)\/(.*)\?(.*)/', $db_info, $result);
$db_type = 'mysql:host=';
$user = $result[1];
$pass = $result[2];
$ip_port = $result[3] . ':' . $result[4] . ';';
$db_name = 'dbname=' . $result[5] . ';';
$charset = $result[6];
$dsn = $db_type . $ip_port . $db_name . $charset;
//error_log($dsn);
//error_log($user);
//error_log($pass);
// connect to db
$db = new PDO($dsn, $user, $pass, array(PDO::MYSQL_ATTR_LOCAL_INFILE => true));
// go through rows
$source = 'motiv';
$data_array = [];
while (($row = fgetcsv($csv)) !== false)
{
// TODO: verify if these are still the headers if there are headers
// possible lines in output file:
// (1) header in csv file
// SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName
// (2) No available data
// (3) CH2000012071,WCHD23BL-CPN00-LX,0,2020-08-11 04:05:27.090,0,4,CHAMPION MF
// (4) Empty line
// (5) empty sku
// check if No available data
if ($row[0] == 'No available data')
{
// skip the line
error_log('No available data, skipping the line...');
continue;
}
// check if empty line
if ($row == array(null))
{
// skip
error_log('Skipping empty line...');
continue;
}
// check if empty serial
if (empty($row[0]))
{
$err_message = "Empty serial. " . "\n";
fwrite($output_fh, $err_message);
continue;
}
// sample of line in output file:
// serial number, sku, dispatch status, created date, inventory status, category id, category name
// CH2000012071,WCHD23BL-CPN00-LX,0,2020-08-11 04:05:27.090,0,4,CHAMPION MF
// MG2000313690,N/A,1,2021-05-14T23:47:30.6430000+08:00,0,10,GOLD
$serial = trim(strtoupper($row[0]));
$sku = trim(strtoupper($row[1]));
$dispatch_status = trim($row[2]);
$str_date_create = trim($row[3]);
$inventory_status = trim($row[4]);
$cat_id = trim($row[5]);
$cat_name = trim(strtoupper($row[6]));
error_log('Processing ' . $serial . ' and ' . $sku);
// since some people cannot follow simple instructions...
// check the date format on the string
// try 2021-05-15T08:35:46+08:00 format on str_date_create
$date_create = DateTime::createFromFormat('Y-m-d\TH:i:sP', $str_date_create);
if ($date_create == false)
{
// try this format: 2021-05-15T08:47:20.3330000+08:00
// get the date, time and timezone from str_date_create
$str_date_time = substr($str_date_create, 0, 19);
$str_timezone = substr($str_date_create, 27);
$str_datetime_tz = $str_date_time . $str_timezone;
// create DateTime object
// sample: 2021-05-15T12:16:06+08:00
$date_create = DateTime::createFromFormat('Y-m-d\TH:i:sP', $str_datetime_tz);
// check if datetime object was created
// if not, someone f*cked up and we have no date create
if ($date_create == false)
{
// log the serial
$message = "$serial - ERROR - " . "Invalid date format for create date." . "\n";
fwrite($output_fh, $message);
continue;
}
}
$created_date = $date_create->format('Y-m-d H:i:s');
//error_log($str_date_time);
//error_log($str_timezone);
//error_log($str_datetime_tz);
//error_log($date_create->format('Y-m-d H:i:s'));
$meta_info = [
'dispatch_status' => $dispatch_status,
'inventory_status' => $inventory_status,
'category_id' => $cat_id,
'category_name' => $cat_name,
];
$info = json_encode($meta_info);
if ($sku == 'N/A')
$sku = null;
// create data for the infile
$data_array[] = [
$serial,
$sku,
$created_date,
$source,
$info
];
}
// cache directory
$cache_dir = __DIR__ . '/../../var/cache';
$file = $cache_dir . '/warranty_serial_data.tab';
error_log('opening file for warranty serial - ' . $file);
$fp = fopen($file, 'w');
if ($fp === false)
{
error_log('could not open file for load data infile - ' . $file);
}
else
{
foreach ($data_array as $key => $data)
{
$line = implode('|', $data) . "\r\n";
fwrite($fp, $line);
}
}
fclose($fp);
error_log('Loading warranty serial data...');
$db->exec('LOAD DATA LOCAL INFILE \'' . $file . '\' INTO TABLE warranty_serial FIELDS TERMINATED BY \'|\' LINES TERMINATED BY \'\\r\\n\' (id, sku, date_create, source, meta_info)');
// close file
fclose($csv);
fclose($output_fh);