fixed issue, now works great. // ⚠️ Simulation ON (change it to false to enable real API calls)
$simulate = true;
This commit is contained in:
parent
33e3ae059d
commit
ac09d8d0eb
@ -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
|
||||
]);
|
||||
|
||||
|
||||
@ -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");
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user