diff --git a/src/Command/LoadWarrantySerialCommand.php b/src/Command/LoadWarrantySerialCommand.php index 18c1ec1b..026ff251 100644 --- a/src/Command/LoadWarrantySerialCommand.php +++ b/src/Command/LoadWarrantySerialCommand.php @@ -54,7 +54,7 @@ class LoadWarrantySerialCommand extends Command // get the filenames from the queue table with status pending $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'; $ws_query_stmt = $db->prepare($ws_query_sql); @@ -62,14 +62,22 @@ class LoadWarrantySerialCommand extends Command $ws_results = $ws_query_stmt->executeQuery(); + $output_info = []; while ($row = $ws_results->fetchAssociative()) { $filename = $row['file_serial']; $user_id = $row['api_user']; + $ws_file_id = $row['id']; $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; @@ -77,6 +85,8 @@ class LoadWarrantySerialCommand extends Command protected function processWarrantySerialFile($csv_file, $user_id) { + $output_info = []; + // attempt to open file try { @@ -92,73 +102,83 @@ class LoadWarrantySerialCommand extends Command ]; $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) { - $is_valid = $this->validateRow($row, $user_id); - if ($is_valid) + $validation_result = $this->validateRow($row, $user_id); + if (!empty($validation_result)) { - // valid entry, we parse and insert - $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])); + $output_info[] = $validation_result; + continue; + } - // we are sure that this is a valid date at this point - $created_date = $this->convertDateCreate($str_date_create); + // valid entry, we parse and insert + $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 = [ - 'dispatch_status' => $dispatch_status, - 'inventory_status' => $inventory_status, - 'category_id' => $cat_id, - 'category_name' => $cat_name, - ]; + // we are sure that this is a valid date at this point + $created_date = $this->convertDateCreate($str_date_create); - $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 - $source = 'motiv'; - if ($sku == 'N/A') - $sku = null; + $info = json_encode($meta_info); - // prepared statement - $db = $this->em->getConnection(); - $insert_stmt = $db->prepare('INSERT INTO warranty_serial (id, sku, date_create, source, meta_info) - VALUES (:serial, :sku, :date_create, :source, :meta_info)'); + // prepare the data + $source = 'motiv'; + if ($sku == 'N/A') + $sku = null; - $res = $insert_stmt->execute([ - ':serial' => $serial, - ':sku' => $sku, - ':date_create' => $created_date, - ':source' => $source, - ':meta_info' => $info, - ]); + // prepared statement + $db = $this->em->getConnection(); + $insert_stmt = $db->prepare('INSERT INTO warranty_serial (id, sku, date_create, source, meta_info) + VALUES (:serial, :sku, :date_create, :source, :meta_info)'); - if (!$res) - { - // log the not successful insert - $err = $insert_stmt->errorInfo(); - $error = $err[2]; - $this->logLoadInfo($user_id, false, $serial, $error); - } - else - { - // log the successful insert - $this->logLoadInfo($user_id, true, $serial, ''); - } + $res = $insert_stmt->execute([ + ':serial' => $serial, + ':sku' => $sku, + ':date_create' => $created_date, + ':source' => $source, + ':meta_info' => $info, + ]); + + if (!$res) + { + // 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) { + $data = []; // possible lines: // (1) header in csv file - ignore // SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName @@ -189,7 +209,9 @@ class LoadWarrantySerialCommand extends Command $error = 'Empty serial'; $this->logLoadInfo($user_id, false, '', $error); - return false; + $data = $this->setOutputInfo('', 'not_uploaded', true, $error); + + return $data; } // validate the date created @@ -203,7 +225,9 @@ class LoadWarrantySerialCommand extends Command $error = 'Invalid date create'; $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 @@ -214,11 +238,13 @@ class LoadWarrantySerialCommand extends Command $error = 'Duplicate serial'; $this->logLoadInfo($user_id, false, $serial, $error); - return false; + $data = $this->setOutputInfo($serial, 'not_uploaded', true, $error) + + return $data; } - // valid entry - return true; + // valid entry, return empty + return $data; } protected function convertDateCreate($str_date_create) @@ -267,4 +293,30 @@ class LoadWarrantySerialCommand extends Command $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; + } + }