From ed14a3defd013b30b786cabf3cab91ab717662df Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 8 Jun 2021 08:38:02 +0000 Subject: [PATCH] Fix scripts for warranty serials. #576 --- utils/load_warranty_serial/load_serials.php | 74 ++++++++++++++++----- utils/warranty_motiv_local.sh | 9 +++ utils/warranty_motiv_local_bulk.sh | 68 ++++++++++--------- utils/warranty_motiv_prod.sh | 11 ++- utils/warranty_motiv_prod_bulk.sh | 40 +++++++++++ 5 files changed, 152 insertions(+), 50 deletions(-) create mode 100755 utils/warranty_motiv_local.sh create mode 100755 utils/warranty_motiv_prod_bulk.sh diff --git a/utils/load_warranty_serial/load_serials.php b/utils/load_warranty_serial/load_serials.php index 6eb1d918..9df178bc 100644 --- a/utils/load_warranty_serial/load_serials.php +++ b/utils/load_warranty_serial/load_serials.php @@ -9,6 +9,14 @@ $output_file = $argv[5]; $output_fh = fopen($output_file, "w"); +if (!file_exists($csv)) +{ + $err_message = "No csv input file found." . "\n"; + fwrite($output_fh, $err_message); + fclose($output_fh); + exit(); +} + // connect to db $db = new PDO($dsn, $user, $pass); @@ -27,17 +35,6 @@ while (($row = fgetcsv($csv)) !== false) // (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') { @@ -53,18 +50,67 @@ while (($row = fgetcsv($csv)) !== false) error_log('Skipping empty line...'); continue; } + + // 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; + } // 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]); - $date_create = $row[3]; + $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, @@ -74,15 +120,13 @@ while (($row = fgetcsv($csv)) !== false) $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, + ':date_create' => $created_date, ':source' => $source, ':meta_info' => $info, ]); diff --git a/utils/warranty_motiv_local.sh b/utils/warranty_motiv_local.sh new file mode 100755 index 00000000..403c62c2 --- /dev/null +++ b/utils/warranty_motiv_local.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#touch /tmp/warranty_download_serial.csv +proc_date=`date +%m-%d-%y -d "1 day ago"` +download_file="/tmp/warranty_download_serial_$proc_date.csv" +load_status_file="/tmp/warranty_load_status_$proc_date.txt" +echo $download_file +echo $load_status_file +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php $proc_date $download_file 1 +/usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php $download_file "mysql:host=localhost;dbname=resq;charset=UTF8" root password $load_status_file diff --git a/utils/warranty_motiv_local_bulk.sh b/utils/warranty_motiv_local_bulk.sh index 167e8566..5e1a0ab4 100755 --- a/utils/warranty_motiv_local_bulk.sh +++ b/utils/warranty_motiv_local_bulk.sh @@ -1,36 +1,40 @@ #!/bin/bash touch /tmp/warranty_download_serial.csv -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-03-2021 /tmp/warranty_download_serial.csv 1 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-04-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-05-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-06-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-07-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-08-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-09-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-10-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-11-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-12-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-13-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-14-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-15-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-16-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-17-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-18-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-19-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-20-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-21-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-22-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-23-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-24-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-25-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-26-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-27-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-28-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-29-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-30-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-31-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-01-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-02-2021 /tmp/warranty_download_serial.csv 0 -/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-03-2021 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-03-21 /tmp/warranty_download_serial.csv 1 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-04-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-05-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-06-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-07-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-08-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-09-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-10-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-11-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-12-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-13-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-14-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-15-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-16-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-17-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-18-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-19-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-20-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-21-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-22-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-23-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-24-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-25-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-26-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-27-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-28-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-29-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-30-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-31-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-01-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-02-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-03-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-04-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-05-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-06-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-07-21 /tmp/warranty_download_serial.csv 0 touch /tmp/warranty_load_status.txt /usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.csv "mysql:host=localhost;dbname=resq;charset=UTF8" root password /tmp/warranty_load_status.txt diff --git a/utils/warranty_motiv_prod.sh b/utils/warranty_motiv_prod.sh index f13a7d7d..12d00761 100755 --- a/utils/warranty_motiv_prod.sh +++ b/utils/warranty_motiv_prod.sh @@ -1,4 +1,9 @@ #!/bin/bash -touch /tmp/warranty_download_serial.txt -/usr/bin/php /var/www/resq/utils/get_warranty_serial/get_serials.php -/usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.txt "mysql:host=172.18.203.191:3306;dbname=resq;charset=UTF8" resq Motolite456 +proc_date=`date +%m-%d-%y -d "1 day ago"` +download_file="/tmp/warranty_download_serial_$proc_date.csv" +load_status_file="/tmp/warranty_load_status_$proc_date.txt" +#echo $download_file +#echo $load_status_file +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php $proc_date $download_file 1 +/usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php $download_file "mysql:host=172.18.203.191:3306;dbname=resq;charset=UTF8" resq Motolite456 $load_status_file + diff --git a/utils/warranty_motiv_prod_bulk.sh b/utils/warranty_motiv_prod_bulk.sh new file mode 100755 index 00000000..231f6bc2 --- /dev/null +++ b/utils/warranty_motiv_prod_bulk.sh @@ -0,0 +1,40 @@ +#!/bin/bash +touch /tmp/warranty_download_serial.csv +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-03-21 /tmp/warranty_download_serial.csv 1 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-04-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-05-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-06-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-07-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-08-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-09-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-10-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-11-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-12-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-13-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-14-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-15-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-16-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-17-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-18-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-19-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-20-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-21-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-22-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-23-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-24-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-25-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-26-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-27-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-28-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-29-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-30-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-31-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-01-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-02-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-03-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-04-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-05-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-06-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-07-21 /tmp/warranty_download_serial.csv 0 +touch /tmp/warranty_load_status.txt +/usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.csv "mysql:host=172.18.203.191:3306;dbname=resq;charset=UTF8" resq Motolite456 /tmp/warranty_load_status.txt