refactored api code
This commit is contained in:
@@ -47,75 +47,180 @@ try {
|
|||||||
$stmt = $pdo->prepare("
|
$stmt = $pdo->prepare("
|
||||||
SELECT
|
SELECT
|
||||||
idd.field_value,
|
idd.field_value,
|
||||||
m.field_label
|
m.field_label,
|
||||||
|
m.schema_id,
|
||||||
|
m.field_id
|
||||||
FROM
|
FROM
|
||||||
import_data_details as idd
|
import_data_details as idd
|
||||||
JOIN template_mapping m ON idd.mapping_id = m.id
|
JOIN template_mapping m ON idd.mapping_id = m.id
|
||||||
WHERE idd.id = :iddatadb
|
WHERE idd.id = :iddatadb
|
||||||
");
|
");
|
||||||
$stmt->execute([
|
$stmt->execute(['iddatadb' => $iddatadb]);
|
||||||
'iddatadb' => $iddatadb
|
|
||||||
]);
|
|
||||||
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
$fieldValues = [];
|
$fieldValues = [];
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$fieldValues[] = [
|
$fieldValues[] = [
|
||||||
"FieldLabel" => $row['field_label'],
|
"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) {
|
if ($simulate) {
|
||||||
$commessaId = 999999;
|
// Simulate WebOrder creation
|
||||||
$commessa = [
|
$webOrder = [
|
||||||
"IdCommessa" => $commessaId,
|
"Id" => 999999,
|
||||||
"CodiceCommessa" => "TEST001",
|
"Code" => "TEST-WO-001",
|
||||||
"IdCliente" => $clienteId,
|
"Customer" => $clienteId,
|
||||||
"IdSchema" => $schemaId,
|
"CreatedDate" => date('c'),
|
||||||
"Fields" => $fieldValues,
|
"Status" => "Draft"
|
||||||
"Inviato" => 1
|
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
// 🔹 REAL API FLOW
|
// Real API call
|
||||||
$api = VisualLimsApiClient::getInstance();
|
$webOrder = $api->post("odata/WebOrder", $webOrderPayload);
|
||||||
|
}
|
||||||
|
|
||||||
// 1. Create CommessaWeb
|
$webOrderId = $webOrder["Id"];
|
||||||
$commessaPayload = [
|
|
||||||
|
// 🔹 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,
|
"IdCliente" => $clienteId,
|
||||||
"IdSchema" => $schemaId,
|
"IdSchema" => $schemaId,
|
||||||
"IdSchemaCustomFields" => $schemaId,
|
"Customer" => $webOrder["Customer"] ?? $clienteId,
|
||||||
"Inviato" => 1
|
"Status" => $webOrder["Status"] ?? "Draft",
|
||||||
|
"CreatedDate" => $webOrder["CreatedDate"] ?? date('c'),
|
||||||
|
"Fields" => $fieldValues,
|
||||||
|
"Samples" => $samplesWithCustomFields,
|
||||||
|
"Inviato" => $orderSent ? 1 : 0
|
||||||
];
|
];
|
||||||
$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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 🔹 Final Response
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
"success" => true,
|
"success" => true,
|
||||||
"commessa" => $commessa,
|
"webOrder" => $finalWebOrder,
|
||||||
"parts" => $parts,
|
"totalSamples" => count($samplesWithCustomFields),
|
||||||
"simulation" => $simulate
|
"totalCustomFields" => count($fieldValues),
|
||||||
|
"simulation" => $simulate,
|
||||||
|
"orderSent" => $orderSent
|
||||||
]);
|
]);
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
// Log the error for debugging
|
||||||
|
error_log("LIMS Export Error: " . $e->getMessage() . "\nTrace: " . $e->getTraceAsString());
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
"success" => false,
|
"success" => false,
|
||||||
"message" => $e->getMessage()
|
"message" => "Export failed: " . $e->getMessage(),
|
||||||
|
"simulation" => $simulate ?? true
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -724,7 +724,7 @@ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
|||||||
},
|
},
|
||||||
success: function (response) {
|
success: function (response) {
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
alert("✅ CommessaWeb created. ID: " + response.commessa.IdCommessa);
|
alert("✅ CommessaWeb created. ID: " + response.webOrder.IdCommessa);
|
||||||
} else {
|
} else {
|
||||||
alert("❌ Error: " + response.message);
|
alert("❌ Error: " + response.message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user