Add processing for output. #704

This commit is contained in:
Korina Cordero 2022-09-15 09:51:07 +00:00
parent 32720ba9e7
commit cbc0b290ad

View file

@ -54,7 +54,7 @@ class LoadWarrantySerialCommand extends Command
// get the filenames from the queue table with status pending // get the filenames from the queue table with status pending
$db = $em->getConnection(); $db = $em->getConnection();
$ws_query_sql = 'SELECT file_serial, api_user FROM warranty_serial_queue $ws_query_sql = 'SELECT id, file_serial, api_user FROM warranty_serial_queue
WHERE status = :status'; WHERE status = :status';
$ws_query_stmt = $db->prepare($ws_query_sql); $ws_query_stmt = $db->prepare($ws_query_sql);
@ -62,14 +62,22 @@ class LoadWarrantySerialCommand extends Command
$ws_results = $ws_query_stmt->executeQuery(); $ws_results = $ws_query_stmt->executeQuery();
$output_info = [];
while ($row = $ws_results->fetchAssociative()) while ($row = $ws_results->fetchAssociative())
{ {
$filename = $row['file_serial']; $filename = $row['file_serial'];
$user_id = $row['api_user']; $user_id = $row['api_user'];
$ws_file_id = $row['id'];
$fname = $this->project_dir . '/public/warranty_serial_uploads/' . $filename; $fname = $this->project_dir . '/public/warranty_serial_uploads/' . $filename;
$this->processWarrantySerialFile($fname, $user_id); // TODO: need to consider what happens if file cannot be read.
// if file can be read, output_info will have an array of objects
// which can be used to transform into data
// if file cannot be read, output_info will have an array of strings
$output_info[] = $this->processWarrantySerialFile($fname, $user_id);
$this->updateWarrantySerialQueue($ws_file_id);
} }
return 0; return 0;
@ -77,6 +85,8 @@ class LoadWarrantySerialCommand extends Command
protected function processWarrantySerialFile($csv_file, $user_id) protected function processWarrantySerialFile($csv_file, $user_id)
{ {
$output_info = [];
// attempt to open file // attempt to open file
try try
{ {
@ -92,73 +102,83 @@ class LoadWarrantySerialCommand extends Command
]; ];
$this->upload_logger->logWarrantySerialUploadInfo($log_data); $this->upload_logger->logWarrantySerialUploadInfo($log_data);
error_log($error); $output_info = $this->setOutputInfo('', 'not_uploaded', true, $error);
return $output_info;
} }
while(($row = fgetcsv($fh)) !== false) while(($row = fgetcsv($fh)) !== false)
{ {
$is_valid = $this->validateRow($row, $user_id); $validation_result = $this->validateRow($row, $user_id);
if ($is_valid) if (!empty($validation_result))
{ {
// valid entry, we parse and insert $output_info[] = $validation_result;
$serial = trim(strtoupper($row[0])); continue;
$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]));
// we are sure that this is a valid date at this point // valid entry, we parse and insert
$created_date = $this->convertDateCreate($str_date_create); $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]));
$meta_info = [ // we are sure that this is a valid date at this point
'dispatch_status' => $dispatch_status, $created_date = $this->convertDateCreate($str_date_create);
'inventory_status' => $inventory_status,
'category_id' => $cat_id,
'category_name' => $cat_name,
];
$info = json_encode($meta_info); $meta_info = [
'dispatch_status' => $dispatch_status,
'inventory_status' => $inventory_status,
'category_id' => $cat_id,
'category_name' => $cat_name,
];
// prepare the data $info = json_encode($meta_info);
$source = 'motiv';
if ($sku == 'N/A')
$sku = null;
// prepared statement // prepare the data
$db = $this->em->getConnection(); $source = 'motiv';
$insert_stmt = $db->prepare('INSERT INTO warranty_serial (id, sku, date_create, source, meta_info) if ($sku == 'N/A')
VALUES (:serial, :sku, :date_create, :source, :meta_info)'); $sku = null;
$res = $insert_stmt->execute([ // prepared statement
':serial' => $serial, $db = $this->em->getConnection();
':sku' => $sku, $insert_stmt = $db->prepare('INSERT INTO warranty_serial (id, sku, date_create, source, meta_info)
':date_create' => $created_date, VALUES (:serial, :sku, :date_create, :source, :meta_info)');
':source' => $source,
':meta_info' => $info,
]);
if (!$res) $res = $insert_stmt->execute([
{ ':serial' => $serial,
// log the not successful insert ':sku' => $sku,
$err = $insert_stmt->errorInfo(); ':date_create' => $created_date,
$error = $err[2]; ':source' => $source,
$this->logLoadInfo($user_id, false, $serial, $error); ':meta_info' => $info,
} ]);
else
{ if (!$res)
// log the successful insert {
$this->logLoadInfo($user_id, true, $serial, ''); // log the not successful insert
} $err = $insert_stmt->errorInfo();
$error = $err[2];
$this->logLoadInfo($user_id, false, $serial, $error);
$output_info = $this->setOutputInfo($serial, 'not uploaded', true, $error);
}
else
{
// log the successful insert
$this->logLoadInfo($user_id, true, $serial, '');
$output_info = $this->setOutputInfo($serial, 'uploaded', false, '');
} }
} }
return true; return $output_info;
} }
protected function validateRow($row, $user_id) protected function validateRow($row, $user_id)
{ {
$data = [];
// possible lines: // possible lines:
// (1) header in csv file - ignore // (1) header in csv file - ignore
// SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName // SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName
@ -189,7 +209,9 @@ class LoadWarrantySerialCommand extends Command
$error = 'Empty serial'; $error = 'Empty serial';
$this->logLoadInfo($user_id, false, '', $error); $this->logLoadInfo($user_id, false, '', $error);
return false; $data = $this->setOutputInfo('', 'not_uploaded', true, $error);
return $data;
} }
// validate the date created // validate the date created
@ -203,7 +225,9 @@ class LoadWarrantySerialCommand extends Command
$error = 'Invalid date create'; $error = 'Invalid date create';
$this->logLoadInfo($user_id, false, $serial, $error); $this->logLoadInfo($user_id, false, $serial, $error);
return false; $data = $this->setOutputInfo($serial, 'not_uploaded', true, $error);
return $data;
} }
// check if serial is a dupe // check if serial is a dupe
@ -214,11 +238,13 @@ class LoadWarrantySerialCommand extends Command
$error = 'Duplicate serial'; $error = 'Duplicate serial';
$this->logLoadInfo($user_id, false, $serial, $error); $this->logLoadInfo($user_id, false, $serial, $error);
return false; $data = $this->setOutputInfo($serial, 'not_uploaded', true, $error)
return $data;
} }
// valid entry // valid entry, return empty
return true; return $data;
} }
protected function convertDateCreate($str_date_create) protected function convertDateCreate($str_date_create)
@ -267,4 +293,30 @@ class LoadWarrantySerialCommand extends Command
$this->load_logger->logWarrantySerialLoadInfo($log_data); $this->load_logger->logWarrantySerialLoadInfo($log_data);
} }
protected function updateWarrantySerialQueue($id)
{
// prepared statement
$db = $this->em->getConnection();
$update_stmt = $db->prepare('UPDATE warranty_serial_queue SET status = :status
WHERE id = :id');
$res = $update_stmt->execute([
':status' => 'done',
':id' => $id,
]);
}
protected function setOutputInfo($serial, $status, $has_error, $error_message)
{
$info = [
'serial' => $serial,
'status' => $status,
'has_error' => $has_error,
'error_message' => $error_message
];
return $info;
}
} }