resq/utils/load_warranty_serial/load_serials.php
2021-05-19 08:10:18 +00:00

74 lines
1.7 KiB
PHP

<?php
// load csv
$csv = fopen($argv[1], 'r');
$dsn = $argv[2];
$user = $argv[3];
$pass = $argv[4];
// 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
$counter = 0;
$source = 'motiv';
while (($row = fgetcsv($csv)) !== false)
{
// skip first line
if ($counter == 0)
{
$counter++;
continue;
}
/*
$serial = trim(strtoupper($row[0]));
$sku = trim($row[1]);
$date_create = $row[2];
$ref_id = $row[3]; */
// 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($row[1]);
$dispatch_status = trim($row[2]);
$date_create = $row[3];
$inventory_status = trim($row[4]);
$cat_id = trim($row[5]);
$cat_name = trim($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);
if ($sku == 'N/A')
$sku = null;
$res = $sth->execute([
':serial' => $serial,
':sku' => $sku,
':date_create' => $date_create,
':source' => $source,
':meta_info' => $info,
]);
if (!$res)
{
$err = $sth->errorInfo();
echo "Error ($serial) - " . $err[2] . "\n";
}
}
// close file
fclose($csv);