From 412dce8941563a8fc19d744169c7fa70c04ddb6c Mon Sep 17 00:00:00 2001 From: kapsona777 Date: Thu, 11 Sep 2025 18:46:47 +0400 Subject: [PATCH] refactored api code --- public/userarea/export_to_lims.php | 207 ++++++++++++++++++++++------- public/userarea/import_edit2.php | 2 +- 2 files changed, 157 insertions(+), 52 deletions(-) diff --git a/public/userarea/export_to_lims.php b/public/userarea/export_to_lims.php index 3d6f0f4..2c868ed 100644 --- a/public/userarea/export_to_lims.php +++ b/public/userarea/export_to_lims.php @@ -45,77 +45,182 @@ try { // 🔹 STEP 4: Fetch Field Values with Labels $stmt = $pdo->prepare(" - 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 - ]); + SELECT + idd.field_value, + m.field_label, + m.schema_id, + m.field_id + 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[] = [ "FieldLabel" => $row['field_label'], - "FieldValue" => $row['field_value'] + "FieldValue" => $row['field_value'], + "SchemaId" => (int) $row['schema_id'], + "FieldId" => (int) $row['field_id'] ]; } - // 🔹 Simulation Mode + // 🔹 Initialize API client (even in simulation mode for consistency) + $api = null; + if (!$simulate) { + $api = VisualLimsApiClient::getInstance(); + } + + // 🔹 STEP 5: Create WebOrder + $webOrderPayload = [ + "Customer" => $clienteId + ]; + if ($simulate) { - $commessaId = 999999; - $commessa = [ - "IdCommessa" => $commessaId, - "CodiceCommessa" => "TEST001", - "IdCliente" => $clienteId, - "IdSchema" => $schemaId, - "Fields" => $fieldValues, - "Inviato" => 1 + // Simulate WebOrder creation + $webOrder = [ + "Id" => 999999, + "Code" => "TEST-WO-001", + "Customer" => $clienteId, + "CreatedDate" => date('c'), + "Status" => "Draft" ]; } else { - // 🔹 REAL API FLOW - $api = VisualLimsApiClient::getInstance(); - - // 1. Create CommessaWeb - $commessaPayload = [ - "IdCliente" => $clienteId, - "IdSchema" => $schemaId, - "IdSchemaCustomFields" => $schemaId, - "Inviato" => 1 - ]; - $commessa = $api->post("CommessaWeb", $commessaPayload); - $commessaId = $commessa["IdCommessa"]; - - // 2. Push Fields - $fields = []; - foreach ($fieldValues as $f) { - $fieldPayload = [ - "Commessa" => $commessaId, - "FieldLabel" => $f["FieldLabel"], - "FieldValue" => $f["FieldValue"] - ]; - $res = $api->post("CommessaField", $fieldPayload); - $fields[] = $res; - } - - $commessa["Fields"] = $fields; + // Real API call + $webOrder = $api->post("odata/WebOrder", $webOrderPayload); } + $webOrderId = $webOrder["Id"]; + + // 🔹 STEP 6: Create Samples for each part + $samples = []; + foreach ($parts as $index => $part) { + $samplePayload = [ + "OrderId" => $webOrderId, + "Description" => $part["part_description"] ?? "", + "SampleCode" => $part["part_number"] ?? "", + "Material" => $part["material"] ?? "", + "Color" => $part["color"] ?? "", + "Mix" => $part["mix"] ?? "" + ]; + + if ($simulate) { + // Simulate Sample creation + $sample = [ + "Id" => 888000 + $index, + "OrderId" => $webOrderId, + "Description" => $part["part_description"] ?? "", + "SampleCode" => $part["part_number"] ?? "", + "Material" => $part["material"] ?? "", + "Color" => $part["color"] ?? "", + "Mix" => $part["mix"] ?? "", + "Status" => "Created", + "CreatedDate" => date('c') + ]; + } else { + // Real API call + $sample = $api->post("odata/Sample", $samplePayload); + } + + $samples[] = $sample; + } + + // 🔹 STEP 7: Handle Custom Fields for each sample + $samplesWithCustomFields = []; + foreach ($samples as $sample) { + $sampleId = $sample["Id"]; + + if ($simulate) { + // Simulate getting sample with custom fields + $sampleWithFields = $sample; + $sampleWithFields["CustomFieldSamples"] = []; + + // Simulate custom field updates + foreach ($fieldValues as $fieldIndex => $fieldValue) { + $customField = [ + "Id" => 777000 + $fieldIndex, + "SampleId" => $sampleId, + "FieldLabel" => $fieldValue["FieldLabel"], + "FieldValue" => $fieldValue["FieldValue"], + "UpdatedDate" => date('c') + ]; + $sampleWithFields["CustomFieldSamples"][] = $customField; + } + } else { + // Real API calls + // Get sample with custom fields expanded + $sampleWithFields = $api->get("odata/Sample({$sampleId})?$expand=CustomFieldSamples"); + + // Update custom fields (this depends on the actual API structure) + foreach ($fieldValues as $fieldValue) { + // This might need to be adjusted based on the actual Custom Fields API structure + $customFieldPayload = [ + "SampleId" => $sampleId, + "FieldLabel" => $fieldValue["FieldLabel"], + "FieldValue" => $fieldValue["FieldValue"] + ]; + + try { + $customFieldResult = $api->post("odata/CustomFieldSample", $customFieldPayload); + } catch (Exception $e) { + // Log custom field error but don't fail the entire process + error_log("Custom Field Error for Sample {$sampleId}: " . $e->getMessage()); + } + } + + // Re-fetch sample with updated custom fields + $sampleWithFields = $api->get("odata/Sample({$sampleId})?$expand=CustomFieldSamples"); + } + + $samplesWithCustomFields[] = $sampleWithFields; + } + + // 🔹 STEP 8: Optionally send the order to laboratory + $orderSent = false; + if (!$simulate) { + try { + $sendResult = $api->post("odata/WebOrder({$webOrderId})/SendOrder", []); + $orderSent = true; + } catch (Exception $e) { + // Log error but don't fail the process + error_log("Send Order Error: " . $e->getMessage()); + } + } + + // 🔹 STEP 9: Prepare final response + $finalWebOrder = [ + "IdCommessa" => $webOrderId, + "CodiceCommessa" => $webOrder["Code"] ?? "N/A", + "IdCliente" => $clienteId, + "IdSchema" => $schemaId, + "Customer" => $webOrder["Customer"] ?? $clienteId, + "Status" => $webOrder["Status"] ?? "Draft", + "CreatedDate" => $webOrder["CreatedDate"] ?? date('c'), + "Fields" => $fieldValues, + "Samples" => $samplesWithCustomFields, + "Inviato" => $orderSent ? 1 : 0 + ]; + + // 🔹 Final Response echo json_encode([ "success" => true, - "commessa" => $commessa, - "parts" => $parts, - "simulation" => $simulate + "webOrder" => $finalWebOrder, + "totalSamples" => count($samplesWithCustomFields), + "totalCustomFields" => count($fieldValues), + "simulation" => $simulate, + "orderSent" => $orderSent ]); } catch (Exception $e) { + // Log the error for debugging + error_log("LIMS Export Error: " . $e->getMessage() . "\nTrace: " . $e->getTraceAsString()); + echo json_encode([ "success" => false, - "message" => $e->getMessage() + "message" => "Export failed: " . $e->getMessage(), + "simulation" => $simulate ?? true ]); } diff --git a/public/userarea/import_edit2.php b/public/userarea/import_edit2.php index 419f056..8f85f47 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.commessa.IdCommessa); + alert("✅ CommessaWeb created. ID: " + response.webOrder.IdCommessa); } else { alert("❌ Error: " + response.message); }