132 lines
5.2 KiB
PHP
132 lines
5.2 KiB
PHP
<?php
|
|
require_once "class/VisualLimsApiClient.class.php";
|
|
include('include/headscript.php');
|
|
|
|
$dbHandler = DBHandlerSelect::getInstance();
|
|
$pdo = $dbHandler->getConnection();
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
// 🔹 Configura directory log (creala se non esiste)
|
|
$logDir = __DIR__ . '/logs/api/';
|
|
if (!is_dir($logDir)) {
|
|
mkdir($logDir, 0755, true);
|
|
}
|
|
|
|
// 🔹 Base URL API
|
|
$apiBaseUrl = 'https://93.43.5.102/limsapi/api/odata/';
|
|
|
|
// 🔹 Hardcoded values
|
|
$iddatadb = 846;
|
|
$commessaId = 533357;
|
|
|
|
try {
|
|
// 🔹 STEP 4: Fetch Field Values with Labels (usa dati reali per iddatadb=845)
|
|
$stmt = $pdo->prepare("
|
|
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 = [];
|
|
$valueMap = []; // Mappa per field_id -> valore
|
|
foreach ($rows as $row) {
|
|
$fieldValues[] = [
|
|
"IdCommesseCustomFields" => (int) $row['field_id'],
|
|
"Valore" => $row['field_value'],
|
|
"FieldLabel" => $row['field_label']
|
|
];
|
|
$valueMap[(int) $row['field_id']] = $row['field_value']; // Mappa per ID definizione
|
|
}
|
|
|
|
// Logga i fieldValues in error_log
|
|
$logFieldValues = "FieldValues dal DB (iddatadb={$iddatadb}):\n" . json_encode($fieldValues, JSON_PRETTY_PRINT);
|
|
error_log($logFieldValues);
|
|
|
|
// 🔹 Initialize API client
|
|
$api = VisualLimsApiClient::getInstance();
|
|
|
|
// 🔹 STEP A: GET iniziale per CommesseCustomFields con espansione CustomField
|
|
$expand = "CommesseCustomFields(\$expand=CustomField)"; // Espansione come da istruzioni fornitore
|
|
$commessaWithFields = $api->get("CommessaWeb(" . $commessaId . ")?\$expand=" . $expand);
|
|
|
|
// 🔹 STEP B: Prepara payload PATCH (tutti i campi, sovrascrivi se match su CustomField.IdCustomField == field_id)
|
|
$commessaCustomFields = [];
|
|
foreach ($commessaWithFields["CommesseCustomFields"] as $customField) {
|
|
$definitionId = (int) ($customField["CustomField"]["IdCustomField"] ?? 0); // Usa IdCustomField dal CustomField
|
|
$currentValue = $customField["Valore"] ?? '';
|
|
$newValue = isset($valueMap[$definitionId]) ? $valueMap[$definitionId] : $currentValue;
|
|
|
|
$commessaCustomFields[] = [
|
|
"IdCommesseCustomFields" => (int) $customField["IdCommesseCustomFields"],
|
|
"Valore" => $newValue
|
|
];
|
|
}
|
|
|
|
// 🔹 Unico file di log per tutto
|
|
$logFile = $logDir . "commessa_{$commessaId}_patch_and_get_" . time() . ".txt";
|
|
$logContent = "FieldValues dal DB (iddatadb={$iddatadb}):\n" . json_encode($fieldValues, JSON_PRETTY_PRINT) . "\n\n";
|
|
|
|
// Log curl-like per GET iniziale
|
|
$logContent .= "GET iniziale:\n" .
|
|
"curl --location --request GET '{$apiBaseUrl}CommessaWeb({$commessaId})?\$expand=CommesseCustomFields(\$expand=CustomField)' \\\n" .
|
|
"--header 'Authorization: Bearer ••••••'\n\n" .
|
|
"RESPONSE:\n" . json_encode($commessaWithFields, JSON_PRETTY_PRINT) . "\n\n---\n";
|
|
|
|
if (!empty($commessaCustomFields)) {
|
|
$updatePayload = ["CommesseCustomFields" => $commessaCustomFields];
|
|
|
|
// Log curl-like per PATCH
|
|
$jsonPayload = json_encode($updatePayload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
$logContent .= "PATCH:\n" .
|
|
"curl --location --request PATCH '{$apiBaseUrl}CommessaWeb({$commessaId})' \\\n" .
|
|
"--header 'Content-Type: application/json' \\\n" .
|
|
"--header 'Authorization: Bearer ••••••' \\\n" .
|
|
"--data '{$jsonPayload}'\n\n";
|
|
|
|
$patchResponse = $api->patch("CommessaWeb({$commessaId})", $updatePayload);
|
|
|
|
$logContent .= "PATCH RESPONSE:\n" . json_encode($patchResponse, JSON_PRETTY_PRINT) . "\n\n---\n";
|
|
} else {
|
|
$logContent .= "PATCH: Nessun campo custom da aggiornare\n\n---\n";
|
|
}
|
|
|
|
// 🔹 STEP C: GET di controllo post-PATCH
|
|
$commessaAfterPatch = $api->get("CommessaWeb(" . $commessaId . ")?\$expand=" . $expand);
|
|
|
|
// Log curl-like per GET di controllo
|
|
$logContent .= "GET di controllo:\n" .
|
|
"curl --location --request GET '{$apiBaseUrl}CommessaWeb({$commessaId})?\$expand=CommesseCustomFields(\$expand=CustomField)' \\\n" .
|
|
"--header 'Authorization: Bearer ••••••'\n\n" .
|
|
"RESPONSE:\n" . json_encode($commessaAfterPatch, JSON_PRETTY_PRINT);
|
|
|
|
// Salva log unico
|
|
file_put_contents($logFile, $logContent);
|
|
|
|
// 🔹 Output a schermo
|
|
echo json_encode([
|
|
"success" => true,
|
|
"message" => "PATCH eseguito su commessa {$commessaId} con dati da iddatadb {$iddatadb}",
|
|
"commessaAfterPatch" => $commessaAfterPatch,
|
|
"totalCustomFieldsUpdated" => count($commessaCustomFields),
|
|
"fieldValues" => $fieldValues,
|
|
"logFile" => $logFile
|
|
]);
|
|
} catch (Exception $e) {
|
|
error_log("Patch Error: " . $e->getMessage() . "\nTrace: " . $e->getTraceAsString());
|
|
|
|
echo json_encode([
|
|
"success" => false,
|
|
"message" => "Patch failed: " . $e->getMessage(),
|
|
"logFile" => $logFile ?? 'Nessun log generato'
|
|
]);
|
|
}
|