importify various fixing
This commit is contained in:
@@ -17,6 +17,13 @@ if (count($his_info) > 0) {
|
||||
$deleteQuery->addFilter("importcode", "=", "s", "" . $importcode . "");
|
||||
$deleteQuery->execute();
|
||||
|
||||
$deleteQuery = new WA_MySQLi_Query($repnew);
|
||||
$deleteQuery->Action = "delete";
|
||||
$deleteQuery->Table = "`analysis_project`";
|
||||
|
||||
$deleteQuery->addFilter("importcode", "=", "s", "" . $importcode . "");
|
||||
$deleteQuery->execute();
|
||||
|
||||
|
||||
$deleteQuery = new WA_MySQLi_Query($repnew);
|
||||
$deleteQuery->Action = "delete";
|
||||
|
||||
@@ -44,7 +44,7 @@ if (isset($_FILES['f_csv'])) {
|
||||
$arr_associate_data->setQuery("SELECT * FROM template_associate where template_importify_id=$template_id");
|
||||
$arr_associate_data->execute();
|
||||
$arr_associate = $arr_associate_data->Results;
|
||||
|
||||
|
||||
$productsRefNumber_excelName = '';
|
||||
$reportsNumberLab_excelName = '';
|
||||
$partsCode_excelName = '';
|
||||
@@ -55,18 +55,18 @@ if (isset($_FILES['f_csv'])) {
|
||||
$arr_info = $worksheet->toArray();
|
||||
if (count($arr_info) > 1) { //check excel rows
|
||||
$arr_excel_columns = $arr_info[0];
|
||||
$arr_need_columns = array();
|
||||
foreach ($arr_associate as $item){
|
||||
if($item['column_name'] == 'products_refnumber'){
|
||||
$arr_need_columns = array();
|
||||
foreach ($arr_associate as $item) {
|
||||
if ($item['column_name'] == 'products_refnumber') {
|
||||
$productsRefNumber_excelName = $item['headerfile'];
|
||||
}
|
||||
if($item['column_name'] == 'reportsNumberLab'){
|
||||
if ($item['column_name'] == 'reportsNumberLab') {
|
||||
$reportsNumberLab_excelName = $item['headerfile'];
|
||||
}
|
||||
if($item['column_name'] == 'partsCode'){
|
||||
if ($item['column_name'] == 'partsCode') {
|
||||
$partsCode_excelName = $item['headerfile'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
array_push($arr_need_columns, $productsRefNumber_excelName);
|
||||
array_push($arr_need_columns, $reportsNumberLab_excelName);
|
||||
array_push($arr_need_columns, $partsCode_excelName);
|
||||
@@ -109,7 +109,7 @@ if (isset($_FILES['f_csv'])) {
|
||||
|
||||
if ($verify_flag) {
|
||||
//separate by products_refnumber - product
|
||||
$idx_sample_code_po = array_search($productsRefNumber_excelName, $arr_excel_columns);
|
||||
$idx_sample_code_po = array_search($productsRefNumber_excelName, $arr_excel_columns);
|
||||
|
||||
$arr_total_products = array();
|
||||
$tmp_arr_child_products = array();
|
||||
@@ -130,9 +130,9 @@ if (isset($_FILES['f_csv'])) {
|
||||
}
|
||||
if (count($tmp_arr_child_products) > 0) {
|
||||
array_push($arr_total_products, $tmp_arr_child_products);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//define importcode (timestamp)
|
||||
$importcode = time();
|
||||
|
||||
@@ -151,7 +151,6 @@ if (isset($_FILES['f_csv'])) {
|
||||
// 'importcode' => $importcode
|
||||
// ]);
|
||||
|
||||
|
||||
post_async($_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . "/../importify_bg_script.php", [
|
||||
'arr_project' => json_encode($arr_total_products),
|
||||
'arr_excel_columns' => json_encode($arr_excel_columns),
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -168,10 +168,10 @@ foreach ($arr_total_products as $product) {
|
||||
|
||||
//insert to parts table
|
||||
foreach ($arr_total_parts as $part) {
|
||||
//check exist parts item
|
||||
// Check if the part already exists
|
||||
$part_no = $part[0][$idx_part_no_po];
|
||||
$part_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
$part_query->setQuery("SELECT * FROM parts WHERE partsCode='$part_no' and idreports='$idreports' and idproducts='$idproducts'");
|
||||
$part_query->setQuery("SELECT * FROM parts WHERE partsCode='$part_no' AND idreports='$idreports' AND idproducts='$idproducts'");
|
||||
$part_query->execute();
|
||||
|
||||
$part_info = $part_query->Results;
|
||||
@@ -179,109 +179,168 @@ foreach ($arr_total_products as $product) {
|
||||
|
||||
if (count($part_info) > 0) {
|
||||
$idparts = $part_info[0]['idParts'];
|
||||
} else { // have to insert new
|
||||
} else {
|
||||
// Build the `arr_part_need_idx` array with template data
|
||||
$arr_part_need_idx = array();
|
||||
for ($i = 0; $i < count($arr_associate); $i++) {
|
||||
if ($arr_associate[$i]->table_name == "parts") {
|
||||
array_push($arr_part_need_idx, array($arr_associate[$i]->column_name, array_search($arr_associate[$i]->headerfile, $arr_excel_columns)));
|
||||
// Verifica se l'elemento ha un valore di template
|
||||
array_push($arr_part_need_idx, array($arr_associate[$i]->column_name, array_search($arr_associate[$i]->headerfile, $arr_excel_columns), 0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Insert new part
|
||||
$InsertQuery = new WA_MySQLi_Query($repnew);
|
||||
$InsertQuery->Action = "insert";
|
||||
$InsertQuery->Table = "`parts`";
|
||||
$InsertQuery->bindColumn("partsCode", "s", $part[0][$idx_part_no_po] . "", "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idproducts", "i", "" . $idproducts . "", "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idreports", "i", "" . $idreports . "", "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("importcode", "i", "" . $importcode . "", "WA_DEFAULT");
|
||||
for ($i = 0; $i < count($arr_part_need_idx); $i++) {
|
||||
$InsertQuery->bindColumn($arr_part_need_idx[$i][0], "s", $part[0][$arr_part_need_idx[$i][1]], "WA_DEFAULT");
|
||||
$InsertQuery->Table = "parts";
|
||||
$InsertQuery->bindColumn("partsCode", "s", $part_no, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idproducts", "i", $idproducts, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idreports", "i", $idreports, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("importcode", "i", $importcode, "WA_DEFAULT");
|
||||
|
||||
// Add template data for parts
|
||||
foreach ($arr_part_need_idx as $item) {
|
||||
if ($item[2] > 0) {
|
||||
$InsertQuery->bindColumn($item[0], "s", $item[1], "WA_DEFAULT");
|
||||
} else {
|
||||
$InsertQuery->bindColumn($item[0], "s", $part[0][$item[1]], "WA_DEFAULT");
|
||||
}
|
||||
}
|
||||
|
||||
$InsertQuery->saveInSession("");
|
||||
$InsertQuery->execute();
|
||||
|
||||
$part_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
$part_query->setQuery("SELECT * FROM parts WHERE partsCode='$part_no' and idreports='$idreports' and idproducts='$idproducts'");
|
||||
// Retrieve the newly inserted part ID
|
||||
$part_query->setQuery("SELECT * FROM parts WHERE partsCode='$part_no' AND idreports='$idreports' AND idproducts='$idproducts'");
|
||||
$part_query->execute();
|
||||
|
||||
$part_info = $part_query->Results;
|
||||
if (count($part_info) > 0) {
|
||||
$idparts = $part_info[0]['idParts'];
|
||||
}
|
||||
$idparts = $part_info[0]['idParts'] ?? '';
|
||||
}
|
||||
|
||||
if ($idparts == "") {
|
||||
die("server_error");
|
||||
}
|
||||
|
||||
//----------- result_project table ------------------
|
||||
// Iterate over each row in `part` to handle analysis and results
|
||||
foreach ($part as $result_project) {
|
||||
//check exist result_project item
|
||||
// $result_project_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
// $sql_result_project_query = "SELECT * FROM result_project WHERE idPart='$idparts' and idreports='$idreports' and idproducts='$idproducts'";
|
||||
//
|
||||
$arr_result_project_need_idx = array();
|
||||
// Extract analysis name from Excel and convert it into an analysis ID
|
||||
$result_TestName = $result_project[array_search("result_TestName", $arr_excel_columns)] ?? '';
|
||||
$group_code_index = array_search("analysisgroupcode", array_column($arr_associate, 'column_name'));
|
||||
$group_code_val = $result_project[array_search($arr_associate[$group_code_index]->headerfile, $arr_excel_columns)];
|
||||
$trim_group_code = str_replace("\n", "", str_replace("'", "\'", $group_code_val));
|
||||
|
||||
$analysis_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
$analysis_query->setQuery("SELECT * FROM analysisvocabulary WHERE analysiscode LIKE '$trim_group_code'");
|
||||
$analysis_query->execute();
|
||||
|
||||
$analysis_data = $analysis_query->Results;
|
||||
$idanalysisvocabulary = 0;
|
||||
|
||||
if (count($analysis_data) > 0 && $trim_group_code != "") {
|
||||
$idanalysisvocabulary = $analysis_data[0]['idanalysisvocabulary'];
|
||||
} else {
|
||||
$tmp_val = $result_project[array_search($arr_associate[$group_code_index]->headerfile, $arr_excel_columns)];
|
||||
$trim_item = str_replace("\n", "", str_replace("'", "\'", $tmp_val));
|
||||
$analysis_query->setQuery("SELECT * FROM analysisvocabulary WHERE nameanalysisvoc LIKE '$trim_item'");
|
||||
$analysis_query->execute();
|
||||
|
||||
$analysis_data = $analysis_query->Results;
|
||||
if (count($analysis_data) > 0) {
|
||||
$idanalysisvocabulary = ($analysis_data[0]['refid'] == 0) ? $analysis_data[0]['idanalysisvocabulary'] : $analysis_data[0]['refid'];
|
||||
}
|
||||
}
|
||||
|
||||
// Check if an analysis already exists with the same parameters
|
||||
$analysis_project_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
$analysis_project_query->setQuery(
|
||||
"SELECT * FROM analysis_project WHERE idPart='$idparts' AND idreports='$idreports' AND idproducts='$idproducts' AND result_TestName='$idanalysisvocabulary'"
|
||||
);
|
||||
$analysis_project_query->execute();
|
||||
$analysis_project_info = $analysis_project_query->Results;
|
||||
$idanalysis_project = "";
|
||||
|
||||
if (count($analysis_project_info) > 0) {
|
||||
$idanalysis_project = $analysis_project_info[0]['idAnalysis_Project'];
|
||||
} else {
|
||||
|
||||
// Build the `arr_analysis_project_need_idx` array with template data
|
||||
$arr_analysis_project_need_idx = array();
|
||||
for ($i = 0; $i < count($arr_associate); $i++) {
|
||||
if ($arr_associate[$i]->table_name == "analysis_project") {
|
||||
if ($arr_associate[$i]->column_name == "result_TestName") {
|
||||
// Handle `result_TestName` with the ID of the analysis as it is already mapped.
|
||||
array_push($arr_analysis_project_need_idx, array($arr_associate[$i]->column_name, $idanalysisvocabulary, 1));
|
||||
} else {
|
||||
// Map other fields using the headerfile index from the Excel columns.
|
||||
array_push($arr_analysis_project_need_idx, array($arr_associate[$i]->column_name, array_search($arr_associate[$i]->headerfile, $arr_excel_columns), 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Insert a new row into `analysis_project` with template data
|
||||
$InsertQuery = new WA_MySQLi_Query($repnew);
|
||||
$InsertQuery->Action = "insert";
|
||||
$InsertQuery->Table = "analysis_project";
|
||||
$InsertQuery->bindColumn("idPart", "i", $idparts, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idproducts", "i", $idproducts, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idreports", "i", $idreports, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("result_TestName", "i", $idanalysisvocabulary, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("importcode", "i", $importcode, "WA_DEFAULT");
|
||||
|
||||
// Add template data for `analysis_project`
|
||||
foreach ($arr_analysis_project_need_idx as $item) {
|
||||
if ($item[2] > 0) {
|
||||
$InsertQuery->bindColumn($item[0], "s", $item[1], "WA_DEFAULT");
|
||||
} else {
|
||||
$InsertQuery->bindColumn($item[0], "s", $result_project[$item[1]], "WA_DEFAULT");
|
||||
}
|
||||
}
|
||||
|
||||
$InsertQuery->saveInSession("");
|
||||
$InsertQuery->execute();
|
||||
|
||||
// Retrieve the newly inserted analysis ID
|
||||
$analysis_project_query->setQuery("SELECT LAST_INSERT_ID() as idAnalysis_Project");
|
||||
$analysis_project_query->execute();
|
||||
$analysis_project_info = $analysis_project_query->Results;
|
||||
$idanalysis_project = $analysis_project_info[0]['idAnalysis_Project'] ?? '';
|
||||
}
|
||||
|
||||
|
||||
if ($idanalysis_project == "") {
|
||||
die("server_error");
|
||||
}
|
||||
|
||||
// Build the `arr_result_project_need_idx` array with template data
|
||||
$arr_result_project_need_idx = array();
|
||||
for ($i = 0; $i < count($arr_associate); $i++) {
|
||||
if ($arr_associate[$i]->table_name == "result_project") {
|
||||
if ($arr_associate[$i]->column_name == "result_TestName") {
|
||||
$group_code_index = array_search("analysisgroupcode", array_column($arr_associate, 'column_name'));
|
||||
$group_code_val = $result_project[array_search($arr_associate[$group_code_index]->headerfile, $arr_excel_columns)];
|
||||
|
||||
$trim_group_code = str_replace("\n", "", str_replace("'", "\'", $group_code_val));
|
||||
$analysis_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
$analysis_query->setQuery("SELECT * FROM analysisvocabulary WHERE analysiscode like '$trim_group_code'");
|
||||
$analysis_query->execute();
|
||||
|
||||
$analysis_data = $analysis_query->Results;
|
||||
$ref_id = 0;
|
||||
if (count($analysis_data) > 0 && $trim_group_code != "") {
|
||||
$ref_id = $analysis_data[0]['idanalysisvocabulary'];
|
||||
} else {
|
||||
$tmp_val = $result_project[array_search($arr_associate[$i]->headerfile, $arr_excel_columns)];
|
||||
$trim_item = str_replace("\n", "", str_replace("'", "\'", $tmp_val));
|
||||
$analysis_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
$analysis_query->setQuery("SELECT * FROM analysisvocabulary WHERE nameanalysisvoc like '$trim_item'");
|
||||
$analysis_query->execute();
|
||||
|
||||
$analysis_data = $analysis_query->Results;
|
||||
$ref_id = 0;
|
||||
if (count($analysis_data) > 0) {
|
||||
if($analysis_data[0]['refid'] == 0){
|
||||
$ref_id = $analysis_data[0]['idanalysisvocabulary'];
|
||||
}else{
|
||||
$ref_id = $analysis_data[0]['refid'];
|
||||
}
|
||||
}
|
||||
}
|
||||
array_push($arr_result_project_need_idx, array($arr_associate[$i]->column_name, $ref_id, 1));
|
||||
} else if ($arr_associate[$i]->column_name == "result_AnalytsName") {
|
||||
if ($arr_associate[$i]->column_name == "result_AnalytsName") {
|
||||
// Process `result_AnalytsName` and CAS
|
||||
$cas_index = array_search("cas", array_column($arr_associate, 'column_name'));
|
||||
$cas_val = $result_project[array_search($arr_associate[$cas_index]->headerfile, $arr_excel_columns)];
|
||||
|
||||
$trim_cas_val = str_replace("\n", "", str_replace("'", "\'", $cas_val));
|
||||
$analysis_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
$analysis_query->setQuery("SELECT * FROM compundsvocabulary WHERE cascompoundvocabulary like '%$trim_cas_val%'");
|
||||
$analysis_query->execute();
|
||||
|
||||
$analysis_data = $analysis_query->Results;
|
||||
$compound_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
$compound_query->setQuery("SELECT * FROM compundsvocabulary WHERE cascompoundvocabulary LIKE '%$trim_cas_val%'");
|
||||
$compound_query->execute();
|
||||
|
||||
$compound_data = $compound_query->Results;
|
||||
$ref_id = 0;
|
||||
if (count($analysis_data) > 0 && $trim_cas_val != "" && $trim_cas_val != "-") {
|
||||
$ref_id = $analysis_data[0]['idcompoundsvocabulary'];
|
||||
|
||||
if (count($compound_data) > 0 && !empty($trim_cas_val) && $trim_cas_val != "-" && $trim_cas_val != "") {
|
||||
|
||||
$ref_id = $compound_data[0]['idcompoundsvocabulary'];
|
||||
} else {
|
||||
$tmp_val = $result_project[array_search($arr_associate[$i]->headerfile, $arr_excel_columns)];
|
||||
$trim_item = str_replace("\n", "", str_replace("'", "\'", $tmp_val));
|
||||
$analysis_query = new WA_MySQLi_RS("getquery", $repnew, 0);
|
||||
$analysis_query->setQuery("SELECT * FROM compundsvocabulary WHERE namecompoundsvocabulary like '$tmp_val'");
|
||||
$analysis_query->execute();
|
||||
$compound_query->setQuery("SELECT * FROM compundsvocabulary WHERE namecompoundsvocabulary LIKE '$trim_item'");
|
||||
$compound_query->execute();
|
||||
|
||||
$analysis_data = $analysis_query->Results;
|
||||
$ref_id = 0;
|
||||
if (count($analysis_data) > 0) {
|
||||
if($analysis_data[0]['refid'] == 0){
|
||||
$ref_id = $analysis_data[0]['idcompoundsvocabulary'];
|
||||
}else{
|
||||
$ref_id = $analysis_data[0]['refid'];
|
||||
}
|
||||
$compound_data = $compound_query->Results;
|
||||
if (count($compound_data) > 0) {
|
||||
$ref_id = ($compound_data[0]['refid'] == 0) ? $compound_data[0]['idcompoundsvocabulary'] : $compound_data[0]['refid'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,34 +350,29 @@ foreach ($arr_total_products as $product) {
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// foreach($arr_result_project_need_idx as $q) {
|
||||
// $sql_result_project_query .= " and ".$q[0]."='".$q[0]."'";
|
||||
// }
|
||||
//
|
||||
// $result_project_query->setQuery($sql_result_project_query);
|
||||
// $result_project_query->execute();
|
||||
//
|
||||
// $result_project_info = $result_project_query->Results;
|
||||
//
|
||||
// if (count($result_project_info) == 0) {
|
||||
|
||||
// Insert each result into `result_project`, linking it to `idanalysis_project`
|
||||
$InsertQuery = new WA_MySQLi_Query($repnew);
|
||||
$InsertQuery->Action = "insert";
|
||||
$InsertQuery->Table = "`result_project`";
|
||||
$InsertQuery->bindColumn("idPart", "s", $idparts . "", "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idproducts", "i", "" . $idproducts . "", "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idreports", "i", "" . $idreports . "", "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("importcode", "i", "" . $importcode . "", "WA_DEFAULT");
|
||||
for ($i = 0; $i < count($arr_result_project_need_idx); $i++) {
|
||||
if ($arr_result_project_need_idx[$i][2] > 0) {
|
||||
$InsertQuery->bindColumn($arr_result_project_need_idx[$i][0], "s", $arr_result_project_need_idx[$i][1], "WA_DEFAULT");
|
||||
$InsertQuery->Table = "result_project";
|
||||
$InsertQuery->bindColumn("idPart", "i", $idparts, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idproducts", "i", $idproducts, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idreports", "i", $idreports, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("idanalysis_project", "i", $idanalysis_project, "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn("importcode", "i", $importcode, "WA_DEFAULT");
|
||||
|
||||
// Add data for `result_project`
|
||||
foreach ($arr_result_project_need_idx as $item) {
|
||||
if ($item[2] > 0) {
|
||||
$InsertQuery->bindColumn($item[0], "s", $item[1], "WA_DEFAULT");
|
||||
} else {
|
||||
$InsertQuery->bindColumn($arr_result_project_need_idx[$i][0], "s", $result_project[$arr_result_project_need_idx[$i][1]], "WA_DEFAULT");
|
||||
$InsertQuery->bindColumn($item[0], "s", $result_project[$item[1]], "WA_DEFAULT");
|
||||
}
|
||||
}
|
||||
|
||||
// Save and execute the insert for `result_project`
|
||||
$InsertQuery->saveInSession("");
|
||||
$InsertQuery->execute();
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user