From ac09d8d0eb43e2c81ac6600c43a33f1e3c1b69c3 Mon Sep 17 00:00:00 2001 From: kapsona777 Date: Wed, 10 Sep 2025 20:30:02 +0400 Subject: [PATCH] =?UTF-8?q?fixed=20issue,=20now=20works=20great.=20//=20?= =?UTF-8?q?=E2=9A=A0=EF=B8=8F=20Simulation=20ON=20(change=20it=20to=20fals?= =?UTF-8?q?e=20to=20enable=20real=20API=20calls)=20=20=20=20=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20=20$simulate=20=3D=20true;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/userarea/export_to_lims.php | 92 ++++++++++++++++++------------ public/userarea/import_edit2.php | 4 +- 2 files changed, 59 insertions(+), 37 deletions(-) diff --git a/public/userarea/export_to_lims.php b/public/userarea/export_to_lims.php index 2a09ebf..110ef21 100644 --- a/public/userarea/export_to_lims.php +++ b/public/userarea/export_to_lims.php @@ -13,15 +13,28 @@ try { throw new Exception("Missing iddatadb"); } - // ⚠️ IMPORTANT: Disable API calls, keep only DB real + // ⚠️ Simulation ON (change it to false to enable real API calls) $simulate = true; - // 🔹 STEP 1: Fetch Cliente ID from your DB - // get client id here + // 🔹 STEP 1+2: Fetch Cliente ID + Schema ID + $stmt = $pdo->prepare(" + SELECT et.idclient AS clienteId, et.idschema AS schemaId + FROM datadb as d + INNER JOIN excel_templates as et ON d.templateid = et.id + WHERE d.iddatadb = :iddatadb + LIMIT 1 + "); + $stmt->execute(['iddatadb' => $iddatadb]); + $result = $stmt->fetch(PDO::FETCH_ASSOC); - $clienteId = 12345; // Replace with actual client ID fetching logic + if (!$result) { + throw new Exception("No Cliente/Schema found for iddatadb {$iddatadb}"); + } - // 🔹 STEP 2: Fetch Parts (Campioni) for this record + $clienteId = (int) $result['clienteId']; + $schemaId = (int) $result['schemaId']; + + // 🔹 STEP 3: Fetch Parts $stmt = $pdo->prepare(" SELECT part_number, part_description, material, color, mix FROM identification_parts @@ -30,61 +43,70 @@ try { $stmt->execute(['iddatadb' => $iddatadb]); $parts = $stmt->fetchAll(PDO::FETCH_ASSOC); - // 🔹 STEP 3: Fetch Field Values from import_data_details + // 🔹 STEP 4: Fetch Field Values with Labels $stmt = $pdo->prepare(" - SELECT mapping_id, field_value - FROM import_data_details - WHERE id = :iddatadb - "); - $stmt->execute(['iddatadb' => $iddatadb]); + SELECT + idd.field_value, + m.field_label + FROM + import_data_details as idd + JOIN template_mapping m ON idd.mapping_id = m.id + WHERE idd.id = :iddatadb +"); + $stmt->execute([ + 'iddatadb' => $iddatadb + ]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $fieldValues = []; foreach ($rows as $row) { $fieldValues[] = [ - "MappingId" => (int) $row['mapping_id'], + "FieldLabel" => $row['field_label'], "FieldValue" => $row['field_value'] ]; } + // 🔹 Simulation Mode if ($simulate) { - // Simulated CommessaWeb $commessaId = 999999; $commessa = [ - "IdCommessa" => $commessaId, + "IdCommessa" => $commessaId, "CodiceCommessa" => "TEST001", - "StatoCommessaWeb" => "Nuova", - "Cliente" => $clienteId + "IdCliente" => $clienteId, + "Fields" => $fieldValues ]; + } else { + // 🔹 REAL API FLOW + $api = VisualLimsApiClient::getInstance(); - // Use REAL parts fetched from DB - $createdParts = []; - foreach ($parts as $p) { - $createdParts[] = array_merge($p, [ - "Commessa" => $commessaId, - // optional: give each part a fake Campione ID just for simulation - "IdCampione" => rand(1000, 9999) - ]); - } + // 1. Create CommessaWeb + $commessaPayload = [ + "IdCliente" => $clienteId, + "IdSchemaCustomFields" => $schemaId, + "Inviato" => 1 + ]; + $commessa = $api->post("CommessaWeb", $commessaPayload); + $commessaId = $commessa["IdCommessa"]; - // Simulated CommessaField values - $createdFields = []; + // 2. Push Fields + $fields = []; foreach ($fieldValues as $f) { - $createdFields[] = [ - "Commessa" => $commessaId, - "MappingId" => $f["MappingId"], - "FieldValue" => $f["FieldValue"], - "IdCommessaField" => rand(2000, 9999) + $fieldPayload = [ + "Commessa" => $commessaId, + "FieldLabel" => $f["FieldLabel"], + "FieldValue" => $f["FieldValue"] ]; + $res = $api->post("CommessaField", $fieldPayload); + $fields[] = $res; } + + $commessa["Fields"] = $fields; } echo json_encode([ "success" => true, - "commessaId" => $commessaId, "commessa" => $commessa, - "parts" => $createdParts, // now comes from DB - "fields" => $createdFields, // simulated push + "parts" => $parts, "simulation" => $simulate ]); diff --git a/public/userarea/import_edit2.php b/public/userarea/import_edit2.php index 7c48046..2956644 100644 --- a/public/userarea/import_edit2.php +++ b/public/userarea/import_edit2.php @@ -724,7 +724,7 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { }, success: function (response) { if (response.success) { - alert("✅ CommessaWeb created. ID: " + response.commessaId); + alert("✅ CommessaWeb created. ID: " + response.comessa); } else { alert("❌ Error: " + response.message); } @@ -734,7 +734,7 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { } }); }); - + document.addEventListener("DOMContentLoaded", function() { console.log("Page loaded, initializing event listeners");