225 lines
9.5 KiB
PHP
225 lines
9.5 KiB
PHP
<?php
|
|
include('../../Connections/repnew.php');
|
|
// Database connection
|
|
$host = $servername;
|
|
$db = $database;
|
|
$user = $username;
|
|
$pass = $password;
|
|
|
|
try {
|
|
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (PDOException $e) {
|
|
die("Connection failed: " . $e->getMessage());
|
|
}
|
|
|
|
// Fetch unprocessed JSON files
|
|
$query = "SELECT * FROM temp_json_queue WHERE processed = 0";
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute();
|
|
$jsonEntries = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
foreach ($jsonEntries as $entry) {
|
|
$data = json_decode($entry['json_data'], true);
|
|
|
|
// Validate lab reference
|
|
$labId = getLaboratoryId($data['reflab'], $pdo);
|
|
if (!$labId) {
|
|
markForIntervention($entry['id'], $pdo);
|
|
continue;
|
|
}
|
|
|
|
// Validate and insert product
|
|
$productId = insertProduct($data['product'], $pdo);
|
|
|
|
if ($productId) {
|
|
// Insert reports, parts, analyses, and results
|
|
foreach ($data['product']['reports'] as $report) {
|
|
$reportId = insertReport($report, $productId, $labId, $pdo);
|
|
|
|
foreach ($report['parts'] as $part) {
|
|
$partId = insertPart($part, $reportId, $pdo);
|
|
|
|
foreach ($part['analyses'] as $analysis) {
|
|
$analysisId = insertAnalysis($analysis, $partId, $pdo);
|
|
|
|
foreach ($analysis['results'] as $result) {
|
|
insertResult($result, $analysisId, $pdo);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
markAsProcessed($entry['id'], $pdo);
|
|
} else {
|
|
markForIntervention($entry['id'], $pdo);
|
|
}
|
|
}
|
|
|
|
// Function definitions
|
|
|
|
function getLaboratoryId($reflab, $pdo) {
|
|
$query = "SELECT idlab FROM laboratories WHERE reflab = :reflab";
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute(['reflab' => $reflab]);
|
|
return $stmt->fetchColumn();
|
|
}
|
|
|
|
function insertProduct($product, $pdo) {
|
|
$query = "SELECT idproducts FROM products WHERE products_refnumber = :refnumber";
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute(['refnumber' => $product['products_refnumber']]);
|
|
$productId = $stmt->fetchColumn();
|
|
|
|
if (!$productId) {
|
|
$query = "INSERT INTO products (products_refnumber, products_description, products_style, products_sku, products_color, products_season, products_market, products_order, product_buyer, product_claimedfiber, products_fibercontentresult, dateprod, namesupplier, agerange, products_billto, products_billtocde, products_codeanddesc, products_division, products_phase, products_country, products_region)
|
|
VALUES (:refnumber, :description, :style, :sku, :color, :season, :market, :order, :buyer, :claimedfiber, :fibercontentresult, :dateprod, :supplier, :agerange, :billto, :billtocde, :codeanddesc, :division, :phase, :country, :region)";
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute([
|
|
'refnumber' => $product['products_refnumber'],
|
|
'description' => $product['products_description'],
|
|
'style' => $product['products_style'],
|
|
'sku' => $product['products_sku'],
|
|
'color' => $product['products_color'],
|
|
'season' => $product['products_season'],
|
|
'market' => $product['products_market'],
|
|
'order' => $product['products_order'],
|
|
'buyer' => $product['product_buyer'],
|
|
'claimedfiber' => $product['product_claimedfiber'],
|
|
'fibercontentresult' => $product['products_fibercontentresult'],
|
|
'dateprod' => $product['dateprod'],
|
|
'supplier' => $product['namesupplier'],
|
|
'agerange' => $product['agerange'],
|
|
'billto' => $product['products_billto'],
|
|
'billtocde' => $product['products_billtocde'],
|
|
'codeanddesc' => $product['products_codeanddesc'],
|
|
'division' => $product['products_division'],
|
|
'phase' => $product['products_phase'],
|
|
'country' => $product['products_country'],
|
|
'region' => $product['products_region'],
|
|
]);
|
|
return $pdo->lastInsertId();
|
|
}
|
|
return $productId; // Return existing product ID
|
|
}
|
|
|
|
function insertReport($report, $productId, $labId, $pdo) {
|
|
// Check if the report already exists
|
|
$query = "SELECT idreports FROM reports WHERE reportsNumberLab = :reportsNumberLab AND idLabs = :idlaboratories";
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute([
|
|
'reportsNumberLab' => $report['reportsNumberLab'],
|
|
'idlaboratories' => $labId
|
|
]);
|
|
$reportId = $stmt->fetchColumn();
|
|
|
|
if ($reportId) {
|
|
// Report already exists, return the existing ID
|
|
return $reportId;
|
|
} else {
|
|
// Insert new report
|
|
$query = "INSERT INTO reports (reportsNumberLab, reportDateIn, reportsDateOut, reportsDateDue, reportsRating, reportsRating2, pdffilename, reports_LabName, reports_testype, reports_invoicenumber, reports_invoiceamount, reports_invoiceadate, idproducts, idLabs)
|
|
VALUES (:reportsNumberLab, :reportDateIn, :reportsDateOut, :reportsDateDue, :reportsRating, :reportsRating2, :pdffilename, :reports_LabName, :reports_testype, :reports_invoicenumber, :reports_invoiceamount, :reports_invoiceadate, :idproducts, :idlaboratories)";
|
|
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute([
|
|
'reportsNumberLab' => $report['reportsNumberLab'],
|
|
'reportDateIn' => $report['reportDateIn'],
|
|
'reportsDateOut' => $report['reportsDateOut'],
|
|
'reportsDateDue' => $report['reportsDateDue'],
|
|
'reportsRating' => $report['reportsRating'],
|
|
'reportsRating2' => $report['reportsRating2'],
|
|
'pdffilename' => $report['pdffilename'],
|
|
'reports_LabName' => $report['reports_LabName'],
|
|
'reports_testype' => $report['reports_testype'],
|
|
'reports_invoicenumber' => $report['reports_invoicenumber'],
|
|
'reports_invoiceamount' => $report['reports_invoiceamount'],
|
|
'reports_invoiceadate' => $report['reports_invoiceadate'],
|
|
'idproducts' => $productId,
|
|
'idlaboratories' => $labId,
|
|
]);
|
|
return $pdo->lastInsertId();
|
|
}
|
|
}
|
|
|
|
function insertPart($part, $reportId, $pdo) {
|
|
// Check if the part already exists
|
|
$query = "SELECT idparts FROM parts WHERE partsCode = :partsCode AND idreports = :idreports";
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute([
|
|
'partsCode' => $part['partsCode'],
|
|
'idreports' => $reportId
|
|
]);
|
|
$partId = $stmt->fetchColumn();
|
|
|
|
if ($partId) {
|
|
// Part already exists, return the existing ID
|
|
return $partId;
|
|
} else {
|
|
// Insert new part
|
|
$query = "INSERT INTO parts (partsCode, partsDescription, partsColor, partsMaterial, idreports)
|
|
VALUES (:partsCode, :partsDescription, :partsColor, :partsMaterial, :idreports)";
|
|
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute([
|
|
'partsCode' => $part['partsCode'],
|
|
'partsDescription' => $part['partsDescription'],
|
|
'partsColor' => $part['partsColor'],
|
|
'partsMaterial' => $part['partsMaterial'],
|
|
'idreports' => $reportId,
|
|
]);
|
|
return $pdo->lastInsertId();
|
|
}
|
|
}
|
|
|
|
|
|
function insertAnalysis($analysis, $partId, $pdo) {
|
|
$query = "INSERT INTO analysis_project (result_TestName, test_Rating, test_Rating2, requirements, reference, analysisgroupcode, idPart)
|
|
VALUES (:result_TestName, :test_Rating, :test_Rating2, :requirements, :reference, :analysisgroupcode, :idparts)";
|
|
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute([
|
|
'result_TestName' => $analysis['result_TestName'],
|
|
'test_Rating' => $analysis['test_Rating'],
|
|
'test_Rating2' => $analysis['test_Rating2'],
|
|
'requirements' => $analysis['requirements'],
|
|
'reference' => $analysis['reference'],
|
|
'analysisgroupcode' => $analysis['analysisgroupcode'],
|
|
'idparts' => $partId,
|
|
]);
|
|
return $pdo->lastInsertId();
|
|
}
|
|
|
|
function insertResult($result, $analysisId, $pdo) {
|
|
$query = "INSERT INTO result_project (result_AnalytsName, result_AnalytsRating, result_Value, result_Comment, test_Rating, test_Rating2, result_UnitofMeasure, cas, requirements, reference, idanalysis_project)
|
|
VALUES (:result_AnalytsName, :result_AnalytsRating, :result_Value, :result_Comment, :test_Rating, :test_Rating2, :result_UnitofMeasure, :cas, :requirements, :reference, :idanalysis_project)";
|
|
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute([
|
|
'result_AnalytsName' => $result['result_AnalytsName'],
|
|
'result_AnalytsRating' => $result['result_AnalytsRating'],
|
|
'result_Value' => $result['result_Value'],
|
|
'result_Comment' => $result['result_Comment'],
|
|
'test_Rating' => $result['test_Rating'],
|
|
'test_Rating2' => $result['test_Rating2'],
|
|
'result_UnitofMeasure' => $result['result_UnitofMeasure'],
|
|
'cas' => $result['cas'],
|
|
'requirements' => $result['requirements'],
|
|
'reference' => $result['reference'],
|
|
'idanalysis_project' => $analysisId,
|
|
]);
|
|
}
|
|
|
|
function markAsProcessed($id, $pdo) {
|
|
$query = "UPDATE temp_json_queue SET processed = 1 WHERE id = :id";
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute(['id' => $id]);
|
|
}
|
|
|
|
function markForIntervention($id, $pdo) {
|
|
$query = "UPDATE temp_json_queue SET processed = 2 WHERE id = :id";
|
|
$stmt = $pdo->prepare($query);
|
|
$stmt->execute(['id' => $id]);
|
|
}
|
|
?>
|