Lims api working version with comments
This commit is contained in:
parent
412dce8941
commit
0a6fb98476
@ -187,4 +187,9 @@ class VisualLimsApiClient
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
public function getBaseUrl()
|
||||
{
|
||||
return $this->baseUrl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -13,8 +13,7 @@ try {
|
||||
throw new Exception("Missing iddatadb");
|
||||
}
|
||||
|
||||
// ⚠️ Simulation ON (change it to false to enable real API calls)
|
||||
$simulate = true;
|
||||
echo "✅ START Export for iddatadb={$iddatadb}\n";
|
||||
|
||||
// 🔹 STEP 1+2: Fetch Cliente ID + Schema ID
|
||||
$stmt = $pdo->prepare("
|
||||
@ -34,15 +33,19 @@ try {
|
||||
$clienteId = (int) $result['clienteId'];
|
||||
$schemaId = (int) $result['schemaId'];
|
||||
|
||||
// 🔹 STEP 3: Fetch Parts
|
||||
echo "✅ Cliente={$clienteId}, Schema={$schemaId}\n";
|
||||
|
||||
// 🔹 STEP 3: Fetch Parts (including idmatrice)
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT part_number, part_description, material, color, mix
|
||||
SELECT part_number, part_description, material, color, mix, idmatrice
|
||||
FROM identification_parts
|
||||
WHERE iddatadb = :iddatadb
|
||||
");
|
||||
$stmt->execute(['iddatadb' => $iddatadb]);
|
||||
$parts = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo "✅ Parts found: " . count($parts) . "\n";
|
||||
|
||||
// 🔹 STEP 4: Fetch Field Values with Labels
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT
|
||||
@ -58,169 +61,114 @@ try {
|
||||
$stmt->execute(['iddatadb' => $iddatadb]);
|
||||
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo "✅ Field rows found: " . count($rows) . "\n";
|
||||
|
||||
$fieldValues = [];
|
||||
foreach ($rows as $row) {
|
||||
$fieldValues[] = [
|
||||
"FieldLabel" => $row['field_label'],
|
||||
"FieldValue" => $row['field_value'],
|
||||
"SchemaId" => (int) $row['schema_id'],
|
||||
"FieldId" => (int) $row['field_id']
|
||||
"IdCommesseCustomFields" => (int) $row['field_id'],
|
||||
"Valore" => $row['field_value']
|
||||
];
|
||||
}
|
||||
|
||||
// 🔹 Initialize API client (even in simulation mode for consistency)
|
||||
$api = null;
|
||||
if (!$simulate) {
|
||||
$api = VisualLimsApiClient::getInstance();
|
||||
}
|
||||
// 🔹 Initialize API client
|
||||
$api = VisualLimsApiClient::getInstance();
|
||||
|
||||
// 🔹 STEP 5: Create WebOrder
|
||||
$webOrderPayload = [
|
||||
"Customer" => $clienteId
|
||||
// 🔹 STEP 5: Create CommessaWeb (NOT WebOrder)
|
||||
$commessaWebPayload = [
|
||||
"Cliente" => $clienteId,
|
||||
"SchemaCustomField" => $schemaId,
|
||||
"Richiedente" => "Test Web Import",
|
||||
"Descrizione" => "TEST CommessaWeb",
|
||||
];
|
||||
|
||||
if ($simulate) {
|
||||
// Simulate WebOrder creation
|
||||
$webOrder = [
|
||||
"Id" => 999999,
|
||||
"Code" => "TEST-WO-001",
|
||||
"Customer" => $clienteId,
|
||||
"CreatedDate" => date('c'),
|
||||
"Status" => "Draft"
|
||||
];
|
||||
} else {
|
||||
// Real API call
|
||||
$webOrder = $api->post("odata/WebOrder", $webOrderPayload);
|
||||
}
|
||||
echo "➡️ Creating CommessaWeb...\n";
|
||||
$commessaWeb = $api->post("CommessaWeb", $commessaWebPayload);
|
||||
|
||||
$webOrderId = $webOrder["Id"];
|
||||
$commessaId = $commessaWeb["IdCommessa"];
|
||||
echo "✅ CommessaWeb created: ID={$commessaId}\n";
|
||||
|
||||
// 🔹 STEP 6: Create Samples for each part
|
||||
$samples = [];
|
||||
// 🔹 STEP 6: Create Campioni (Samples) for each part
|
||||
$campioni = [];
|
||||
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"] ?? ""
|
||||
];
|
||||
$matriceId = (int) ($part["idmatrice"] ?? 0);
|
||||
|
||||
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);
|
||||
if ($matriceId <= 0) {
|
||||
throw new Exception("Invalid or missing idmatrice for part: " . ($part["part_number"] ?? "Unknown"));
|
||||
}
|
||||
|
||||
$samples[] = $sample;
|
||||
$campionePayload = [
|
||||
"Commessa" => $commessaId,
|
||||
"Matrice" => $matriceId,
|
||||
"SottoMatrice" => null,
|
||||
"SchemaCustomField" => $schemaId,
|
||||
"NoteWeb" => $part["part_description"] ?? ""
|
||||
];
|
||||
|
||||
echo "➡️ Creating Campione {$index} with Matrice={$matriceId}\n";
|
||||
$campione = $api->post("Campione", $campionePayload);
|
||||
|
||||
$campione["PartNumber"] = $part["part_number"] ?? "";
|
||||
$campione["Material"] = $part["material"] ?? "";
|
||||
$campione["Color"] = $part["color"] ?? "";
|
||||
$campione["Mix"] = $part["mix"] ?? "";
|
||||
|
||||
$campioni[] = $campione;
|
||||
echo "✅ Campione {$index} created: ID=" . ($campione["IdCampione"] ?? "N/A") . "\n";
|
||||
}
|
||||
|
||||
// 🔹 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)
|
||||
// 🔹 STEP 7: Update Custom Fields for CommessaWeb
|
||||
if (!empty($fieldValues)) {
|
||||
echo "➡️ Updating CommessaWeb Custom Fields...\n";
|
||||
$commessaWithFields = $api->get("CommessaWeb(" . $commessaId . ")?\$expand=CommesseCustomFields");
|
||||
$commessaCustomFields = [];
|
||||
foreach ($commessaWithFields["CommesseCustomFields"] as $customField) {
|
||||
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());
|
||||
if ($customField["IdCommesseCustomFields"] == $fieldValue["IdCommesseCustomFields"]) {
|
||||
$commessaCustomFields[] = [
|
||||
"IdCommesseCustomFields" => $customField["IdCommesseCustomFields"],
|
||||
"Valore" => $fieldValue["Valore"]
|
||||
];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 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());
|
||||
if (!empty($commessaCustomFields)) {
|
||||
$updatePayload = ["CommesseCustomFields" => $commessaCustomFields];
|
||||
$api->patch("CommessaWeb({$commessaId})", $updatePayload);
|
||||
echo "✅ CommessaWeb Custom Fields updated\n";
|
||||
}
|
||||
}
|
||||
|
||||
// 🔹 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
|
||||
// 🔹 STEP 9: Send CommessaWeb to laboratory
|
||||
echo "➡️ Sending CommessaWeb...\n";
|
||||
$sendResult = $api->post("CommessaWeb({$commessaId})/InviaCommessa", []);
|
||||
echo "✅ CommessaWeb sent to Lab\n";
|
||||
|
||||
// 🔹 STEP 10: Prepare final response
|
||||
$finalCommessa = [
|
||||
"Cliente" => $clienteId,
|
||||
"SchemaCustomField" => $schemaId,
|
||||
"Richiedente" => $commessaWeb["Richiedente"] ?? "Web Import",
|
||||
"Descrizione" => $commessaWeb["Descrizione"] ?? "",
|
||||
"CommesseCustomFields" => $fieldValues,
|
||||
"Campioni" => $campioni,
|
||||
"Inviata" => 1
|
||||
];
|
||||
|
||||
// 🔹 Final Response
|
||||
echo json_encode([
|
||||
"success" => true,
|
||||
"webOrder" => $finalWebOrder,
|
||||
"totalSamples" => count($samplesWithCustomFields),
|
||||
"totalCustomFields" => count($fieldValues),
|
||||
"simulation" => $simulate,
|
||||
"orderSent" => $orderSent
|
||||
"commessaWeb" => $finalCommessa,
|
||||
"totalCampioni" => count($campioni),
|
||||
"totalCustomFields" => count($fieldValues)
|
||||
]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
// Log the error for debugging
|
||||
error_log("LIMS Export Error: " . $e->getMessage() . "\nTrace: " . $e->getTraceAsString());
|
||||
|
||||
echo json_encode([
|
||||
"success" => false,
|
||||
"message" => "Export failed: " . $e->getMessage(),
|
||||
"simulation" => $simulate ?? true
|
||||
"message" => "Export failed: " . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user