109 lines
2.9 KiB
PHP
109 lines
2.9 KiB
PHP
<?php
|
|
|
|
// load csv
|
|
$csv = fopen($argv[1], 'r');
|
|
$dsn = $argv[2];
|
|
$user = $argv[3];
|
|
$pass = $argv[4];
|
|
$output_file = $argv[5];
|
|
|
|
$output_fh = fopen($output_file, "w");
|
|
|
|
// connect to db
|
|
$db = new PDO($dsn, $user, $pass);
|
|
|
|
// prepared statement
|
|
$sth = $db->prepare('insert into warranty_serial (id, sku, date_create, source, meta_info) values (:serial, :sku, :date_create, :source, :meta_info)');
|
|
|
|
// go through rows
|
|
$source = 'motiv';
|
|
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
|
|
|
|
// check if csv file has a header by checking contents of line
|
|
if (($row[0] == 'SerialNumber') || ($row[1] == 'Sku') ||
|
|
($row[2] == 'DispatchStatus') || ($row[3] == 'CreatedDate') ||
|
|
($row[4] == 'InventoryStatus') || ($row[5] == 'CategoryID') ||
|
|
($row[6] == 'CategoryName'))
|
|
{
|
|
// skip the header
|
|
error_log('Skipping the headers... ');
|
|
continue;
|
|
}
|
|
|
|
// 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 new line
|
|
if (empty($row[0]))
|
|
{
|
|
// skip
|
|
error_log('Skipping empty line...');
|
|
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
|
|
$serial = trim(strtoupper($row[0]));
|
|
$sku = trim(strtoupper($row[1]));
|
|
$dispatch_status = trim($row[2]);
|
|
$date_create = $row[3];
|
|
$inventory_status = trim($row[4]);
|
|
$cat_id = trim($row[5]);
|
|
$cat_name = trim(strtoupper($row[6]));
|
|
|
|
$meta_info = [
|
|
'dispatch_status' => $dispatch_status,
|
|
'inventory_status' => $inventory_status,
|
|
'category_id' => $cat_id,
|
|
'category_name' => $cat_name,
|
|
];
|
|
|
|
$info = json_encode($meta_info);
|
|
|
|
error_log('Processing ' . $serial . ' and ' . $sku);
|
|
|
|
if ($sku == 'N/A')
|
|
$sku = null;
|
|
|
|
$res = $sth->execute([
|
|
':serial' => $serial,
|
|
':sku' => $sku,
|
|
':date_create' => $date_create,
|
|
':source' => $source,
|
|
':meta_info' => $info,
|
|
]);
|
|
|
|
if (!$res)
|
|
{
|
|
// log the error
|
|
$err = $sth->errorInfo();
|
|
$err_message = "$serial - ERROR - " . $err[2] . "\n";
|
|
fwrite($output_fh, $err_message);
|
|
}
|
|
else
|
|
{
|
|
// log successful adding of serial
|
|
$message = "$serial - SUCCESS - " . "\n";
|
|
fwrite($output_fh, $message);
|
|
}
|
|
}
|
|
|
|
// close file
|
|
fclose($csv);
|
|
fclose($output_fh);
|
|
|
|
|