Merge branch '576-fix-warranty-loading-from-motiv' into 'master-fix'

Resolve "Fix warranty loading from motiv"

See merge request jankstudio/resq!690
This commit is contained in:
Kendrick Chan 2021-06-09 08:52:46 +00:00
commit dcf8df3f2e
9 changed files with 285 additions and 30 deletions

View file

@ -80,3 +80,7 @@ API_VERSION=insert_api_version_here
#SSL_ENABLE for websockets
SSL_ENABLE=set_to_true_or_false
# db
DB_USER=db_username
DB_PASSWORD=db_password

View file

@ -20,14 +20,20 @@ $conn_string = "BlobEndpoint=$blob_url;\nSharedAccessSignature=$sas_token";
$blob_client = BlobRestProxy::createBlobService($conn_string);
$current_date = new DateTime();
$current_date->modify("-1 day");
// get date argument
$proc_date = $argv[1];
$date = $current_date->format('m-d-y');
error_log($proc_date);
$filename = 'warrantylogs' . $date . '.csv';
//print_r($filename);
//$current_date = new DateTime();
//$current_date->modify("-1 day");
//$date = $current_date->format('m-d-Y');
$filename = 'warrantylogs' . $proc_date . '.csv';
error_log($filename);
/*
try {
// NOTE: via download blob
$res = $blob_client->getBlob('warranty', $filename);
@ -36,8 +42,7 @@ try {
file_put_contents("/tmp/warranty_download_serial.txt", $res->getContentStream());
} catch (Exception $e) {
file_put_contents("/tmp/serial_download_error.txt", $filename . "\n" . $e->getMessage() . "\n" . "\n", FILE_APPEND);
}
} */
/*
// NOTE: getting via url

View file

@ -0,0 +1,49 @@
<?php
// NOTE: reference: https://github.com/Azure/azure-storage-php/blob/master/samples/BlobSamples.php
require_once(__DIR__ . '/../../vendor/autoload.php');
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\ServiceException;
$blob_url = 'https://popappshopprodstorage.blob.core.windows.net';
$sas_token = 'sp=r&st=2021-04-13T03:48:30Z&se=2022-04-01T11:48:30Z&spr=https&sv=2020-02-10&sr=c&sig=L6VDl40qRXhQb7w8JVkj3r7x2Xkt72pQaQ8AH2M5CRk%3D';
$conn_string = "BlobEndpoint=$blob_url;\nSharedAccessSignature=$sas_token";
$blob_client = BlobRestProxy::createBlobService($conn_string);
// get date argument, output file argument, overwrite_flag argument
$proc_date = $argv[1];
$output_file = $argv[2];
$flag_overwrite = $argv[3];
error_log($proc_date);
$filename = 'warrantylogs' . $proc_date . '.csv';
error_log($filename);
try {
// NOTE: via download blob
$res = $blob_client->getBlob('warranty', $filename);
// print_r($res);
if ($flag_overwrite > 0)
{
file_put_contents($output_file, $res->getContentStream());
}
else
{
file_put_contents($output_file, "\r\n", FILE_APPEND);
file_put_contents($output_file, $res->getContentStream(), FILE_APPEND);
}
} catch (Exception $e) {
file_put_contents("/tmp/serial_download_error.txt", $filename . "\n" . $e->getMessage() . "\n" . "\n", FILE_APPEND);
}

View file

@ -1,10 +1,48 @@
<?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');
$dsn = $argv[2];
$user = $argv[3];
$pass = $argv[4];
$output_file = $argv[2];
$output_fh = fopen($output_file, "w");
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
// 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 = 'charset=UTF8';
$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);
@ -13,33 +51,90 @@ $db = new PDO($dsn, $user, $pass);
$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)
// 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')
{
$counter++;
// skip the line
error_log('No available data, skipping the line...');
continue;
}
/*
$serial = trim(strtoupper($row[0]));
$sku = trim($row[1]);
$date_create = $row[2];
$ref_id = $row[3]; */
// 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($row[1]);
$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($row[6]);
$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,
@ -56,19 +151,27 @@ while (($row = fgetcsv($csv)) !== false)
$res = $sth->execute([
':serial' => $serial,
':sku' => $sku,
':date_create' => $date_create,
':date_create' => $created_date,
':source' => $source,
':meta_info' => $info,
]);
if (!$res)
{
// log the error
$err = $sth->errorInfo();
echo "Error ($serial) - " . $err[2] . "\n";
$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);

View file

@ -1,4 +1,4 @@
#!/bin/bash
touch /tmp/warranty_download_serial.txt
/usr/bin/php /usr/share/nginx/html/resqapi/utils/get_warranty_serial/get_serials.php
/usr/bin/php /usr/share/nginx/html/resqapi/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.txt "mysql:host=localhost;dbname=resq;charset=UTF8" resq Motolite456
/usr/bin/php /usr/share/nginx/html/resqapi/utils/get_warranty_serial/get_serials.php `date +%m-%d-%Y`
/usr/bin/php /usr/share/nginx/html/resqapi/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.txt "mysql:host=localhost;dbname=resq;charset=UTF8"

9
utils/warranty_motiv_local.sh Executable file
View file

@ -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 $load_status_file

View file

@ -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 /tmp/warranty_load_status.txt

View file

@ -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 $load_status_file

View file

@ -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 /tmp/warranty_load_status.txt