getConnection(); // Genera un UUID univoco per importreferencecode $importReferenceCode = date('YmdHis') . '-' . uniqid(); // Recupera tutti i mapping dal template, includendo is_visible_import $stmt = $pdo->prepare("SELECT id, excel_column, data_type, is_required, manual_default, is_manual, field_label, field_id, main_field, is_visible_import, auto_value FROM template_mapping WHERE template_id = ?"); $stmt->execute([$template_id]); $allMappings = $stmt->fetchAll(PDO::FETCH_ASSOC); $timeLabels = [ 'Sample Arrival Time:', 'Sample Unlock Time:', 'Login Time:', 'Presa in carico, Orario:', ]; // Returns the auto value for current session (import) based on mapping.auto_value function getImportAutoValue(array $mapping): string { $auto = $mapping['auto_value'] ?? 'none'; if ($auto === 'import_date') { return date('Y-m-d'); } if ($auto === 'import_time') { // HTML expects HH:MM (seconds optional) return date('H:i'); } return ''; } if (empty($allMappings)) { header("Location: import_xls.php?id=$template_id&status=error&message=" . urlencode("Nessun mapping trovato per il template")); exit; } // Trova il campo main_field $mainFieldMapping = null; foreach ($allMappings as $mapping) { if ($mapping['main_field'] == 1 && $mapping['is_visible_import'] == 1) { $mainFieldMapping = $mapping; break; } } // Recupera l'idclient di default dal template (se presente) $template_stmt = $pdo->prepare("SELECT idclient FROM excel_templates WHERE id = ?"); $template_stmt->execute([$template_id]); $template = $template_stmt->fetch(PDO::FETCH_ASSOC); $default_idclient = $template['idclient'] ?? null; $insertedIds = $_POST['inserted_ids'] ?? $_SESSION['inserted_ids']; // ------------------------------------------------------------ // AUTO SET: schema field_id = 244 with auth_users.lims_user_id (if present) // ------------------------------------------------------------ try { // 1) Read logged user lims_user_id $stmtUser = $pdo->prepare("SELECT lims_user_id FROM auth_users WHERE id = ? LIMIT 1"); $stmtUser->execute([(int)$user_id]); $limsUserId = $stmtUser->fetchColumn(); // normalize $limsUserId = ($limsUserId !== false && $limsUserId !== null && $limsUserId !== '') ? (string)$limsUserId : ''; if ($limsUserId !== '' && !empty($insertedIds)) { // 2) Find mapping_id for field_id = 244 in this template $stmtMap = $pdo->prepare("SELECT id FROM template_mapping WHERE template_id = ? AND field_id = 244 LIMIT 1"); $stmtMap->execute([(int)$template_id]); $mappingId244 = (int)$stmtMap->fetchColumn(); if ($mappingId244 > 0) { // 3) Upsert value into import_data_details for every inserted datadb row $pdo->beginTransaction(); $updStmt = $pdo->prepare("UPDATE import_data_details SET field_value = ? WHERE id = ? AND mapping_id = ?"); $insStmt = $pdo->prepare("INSERT INTO import_data_details (id, mapping_id, field_value) VALUES (?, ?, ?)"); foreach ($insertedIds as $iddatadb) { $iddatadb = (int)$iddatadb; if ($iddatadb <= 0) continue; $updStmt->execute([$limsUserId, $iddatadb, $mappingId244]); if ($updStmt->rowCount() === 0) { $insStmt->execute([$iddatadb, $mappingId244, $limsUserId]); } } $pdo->commit(); } } } catch (Exception $e) { // Do not block import page if this fails; just log error_log("[AUTO FIELD 244] " . $e->getMessage()); if ($pdo->inTransaction()) $pdo->rollBack(); } // Recupera i dati appena inseriti con i nomi degli utenti $stmt = $pdo->prepare(" SELECT d.*, CONCAT(u.first_name, ' ', u.last_name) AS user_name FROM datadb d LEFT JOIN auth_users u ON d.user_id = u.id WHERE d.iddatadb IN (" . implode(',', array_fill(0, count($insertedIds), '?')) . ") "); $stmt->execute($insertedIds); $importedData = $stmt->fetchAll(PDO::FETCH_ASSOC); // Recupera i dettagli manuali da import_data_details $stmt = $pdo->prepare(" SELECT d.id AS detail_id, d.id AS datadb_id, d.mapping_id, d.field_value, m.field_id, m.field_label, m.data_type, m.is_required, m.manual_default FROM import_data_details d JOIN template_mapping m ON d.mapping_id = m.id WHERE d.id IN (" . implode(',', array_fill(0, count($insertedIds), '?')) . ") "); $stmt->execute($insertedIds); $manualDetails = $stmt->fetchAll(PDO::FETCH_ASSOC); // Recupera il mapping globale per mostrare gli slug leggibili $stmt = $pdo->query("SELECT mysql_column_name, user_friendly_slug FROM column_mapping"); $slugMapping = []; foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { $slugMapping[$row['mysql_column_name']] = $row['user_friendly_slug']; } // --- FIX LABELS ONLY (do not change keys) --- $slugMapping['AnagraficaCertestObject'] = 'Object'; $slugMapping['AnagraficaCertestService'] = 'Service'; // ---------------- FIXED FIELDS (from template_fixed_mapping) ---------------- $fixedStmt = $pdo->prepare(" SELECT id, fixed_field_key, is_manual, data_type, is_required, default_value, is_visible_import FROM template_fixed_mapping WHERE template_id = ? AND is_visible_import = 1 ORDER BY id "); $fixedStmt->execute([$template_id]); $fixedFieldsRaw = $fixedStmt->fetchAll(PDO::FETCH_ASSOC); // Ordine desiderato: Cliente Responsabile prima, poi gli altri, ConsegnaRichiesta prima delle 3 speciali $desiredOrder = [ 'ClienteResponsabile', 'AnagraficaCertestObject', 'AnagraficaCertestService', 'MoltiplicatorePrezzo', 'ConsegnaRichiesta', // se ci sono altri campi fixed li mettiamo dopo ]; $fixedFields = []; $tempMap = []; foreach ($fixedFieldsRaw as $f) { $tempMap[$f['fixed_field_key']] = $f; } foreach ($desiredOrder as $key) { if (isset($tempMap[$key])) { $fixedFields[] = $tempMap[$key]; unset($tempMap[$key]); } } // Aggiunge eventuali campi fixed non elencati sopra (alla fine) foreach ($tempMap as $f) { $fixedFields[] = $f; } // Maps logical fixed_field_key → real datadb column name (mirrors JS fixedAliasMap) $fixedAliasMap = [ 'ClienteResponsabile' => 'cliente_responsabile_id', 'MoltiplicatorePrezzo' => 'moltiplicatore_prezzo_id', 'AnagraficaCertestObject' => 'anagrafica_certest_object_id', 'AnagraficaCertestService' => 'anagrafica_certest_service_id', 'ClienteFornitore' => 'cliente_fornitore_id', 'ConsegnaRichiesta' => 'consegna_richiesta', ]; // helper default (DATE can use 'today' if you already use it elsewhere) function fixedDefaultValue(array $f): string { $v = $f['default_value'] ?? ''; if ($f['data_type'] === 'DATE' && $v === 'today') return date('Y-m-d'); return (string)$v; } ?> Edit Imported Data - <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?>
"; echo ""; echo ""; echo ""; } elseif ($mainFieldMapping['data_type'] === 'Data') { echo ""; echo ""; } elseif ($mainFieldMapping['data_type'] === 'INT') { echo ""; echo ""; } else { echo ""; echo ""; } ?>
"; echo ""; echo ""; echo "
"; } else { // Show auto import date/time in header too (read-only) $autoVal = getImportAutoValue($mapping); echo "
"; if (($mapping['auto_value'] ?? 'none') === 'import_date') { echo ""; echo ""; } elseif (($mapping['auto_value'] ?? 'none') === 'import_time') { echo ""; echo ""; } else { // keep empty cell for other auto fields echo ""; } echo "
"; } $autoIndex++; $topColIndex++; } } $manualIndex = 0; foreach ($allMappings as $mapping) { if ($mapping['is_manual'] && $mapping['main_field'] != 1 && $mapping['is_visible_import'] == 1) { $fieldValue = $mapping['manual_default'] ?? ''; if ($mapping['data_type'] === 'Data' && $mapping['manual_default'] === 'today') { $fieldValue = date('Y-m-d'); } $inputClass = 'manual-input'; if ($mapping['is_required']) $inputClass .= ' required-input'; echo "
"; if ($mapping['data_type'] === 'SceltaMultipla') { echo ""; } elseif ($mapping['data_type'] === 'Data') { echo ""; } elseif ($mapping['data_type'] === 'INT') { echo ""; } elseif (in_array($mapping['field_label'], $timeLabels)) { echo ""; } else { echo ""; } echo ""; echo "
"; $manualIndex++; $topColIndex++; } } echo "
"; $topColIndex++; echo "
"; $topColIndex++; echo "
"; $topColIndex++; // ---------------- FIXED FIELDS TOP (propagate) - same order as header: fixed cols, 3 empties after ConsegnaRichiesta ---------------- if (!empty($fixedFields)) { $insertedAfterConsegnaTop = false; foreach ($fixedFields as $fx => $f) { $key = $f['fixed_field_key']; // datadb column $val = fixedDefaultValue($f); $isRequired = ((int)$f['is_required'] === 1); $inputClass = 'manual-input' . ($isRequired ? ' required-input' : ''); $topRequiredClass = ($isRequired && ($val === '' || $val === null)) ? 'missing-required' : ''; echo "
"; $topColIndex++; // Forza DATE anche se per errore nel DB è diversa $isDate = ($f['data_type'] === 'DATE' || $key === 'ConsegnaRichiesta'); if ($isDate) { echo ""; } else { $isApiField = in_array($key, [ 'MoltiplicatorePrezzo', 'ClienteResponsabile', 'AnagraficaCertestObject', 'AnagraficaCertestService' ], true); if ($isApiField) { $placeholder = ($key === 'ClienteResponsabile') ? 'Seleziona cliente prima...' : 'Seleziona...'; echo ""; } else { echo ""; } } // UNA SOLA freccia echo ""; echo "
"; if ($key === 'ConsegnaRichiesta' && !$insertedAfterConsegnaTop) { echo "
"; $topColIndex++; echo "
"; $topColIndex++; echo "
"; $topColIndex++; $insertedAfterConsegnaTop = true; } } } ?>
Actions
Client
Status
" . htmlspecialchars($mapping['field_label']) . "
"; $headerIndex++; } } foreach ($allMappings as $mapping) { if ($mapping['is_manual'] && $mapping['main_field'] != 1 && $mapping['is_visible_import'] == 1) { echo "
" . htmlspecialchars($mapping['field_label']) . "
"; $headerIndex++; } } // Aggiunta header per Tested Component echo "
Tested Component
"; $headerIndex++; echo "
AWB Number
"; $headerIndex++; echo "
Tracking Info
"; $headerIndex++; // ---------------- FIXED FIELDS HEADERS ---------------- if (!empty($fixedFields)) { $insertedAfterConsegna = false; foreach ($fixedFields as $f) { $key = $f['fixed_field_key']; $label = $slugMapping[$key] ?? $key; echo "
" . htmlspecialchars($label) . "
"; $headerIndex++; // Inseriamo le 3 colonne SOLO dopo ConsegnaRichiesta if ($key === 'ConsegnaRichiesta' && !$insertedAfterConsegna) { echo "
Import Reference Code
"; $headerIndex++; echo "
" . ($slugMapping['filename_import'] ?? 'filename_import') . "
"; $headerIndex++; echo "
" . ($slugMapping['importdate'] ?? 'importdate') . "
"; $headerIndex++; $insertedAfterConsegna = true; } } } ?>
$row): ?>
hasRole('Admin'))) : ?>
$d['mapping_id'] == $mainFieldMapping['id'] && $d['datadb_id'] == $row['iddatadb']); $detail = reset($detail) ?: ['field_value' => $mainFieldMapping['manual_default']]; $fieldValue = $detail['field_value'] ?? $mainFieldMapping['manual_default'] ?? ''; if ($mainFieldMapping['data_type'] === 'Data' && $mainFieldMapping['manual_default'] === 'today' && empty($fieldValue)) { $fieldValue = date('Y-m-d'); } $requiredClass = ($mainFieldMapping['is_required'] && (is_null($fieldValue) || $fieldValue === '')) ? 'missing-required' : ''; $inputClass = $mainFieldMapping['is_manual'] ? 'manual-input' : 'auto-input'; if ($mainFieldMapping['is_required']) $inputClass .= ' required-input'; ?>
> > >
$d['datadb_id'] == $row['iddatadb']); $autoIndex = 0; foreach ($allMappings as $mapping) { if (!$mapping['is_manual'] && $mapping['main_field'] != 1 && $mapping['is_visible_import'] == 1) { $detail = array_filter($rowDetails, fn($d) => $d['mapping_id'] == $mapping['id']); $detail = reset($detail) ?: ['field_value' => $mapping['manual_default']]; $fieldValue = $detail['field_value'] ?? $mapping['manual_default'] ?? ''; // Auto-fill import date/time only if empty if ($fieldValue === '' || $fieldValue === null) { $autoVal = getImportAutoValue($mapping); if ($autoVal !== '') { $fieldValue = $autoVal; } } $requiredClass = ($mapping['is_required'] && (is_null($fieldValue) || $fieldValue === '')) ? 'missing-required' : ''; $inputClass = 'auto-input'; if ($mapping['is_required']) $inputClass .= ' required-input'; echo "
"; if ($mapping['data_type'] === 'SceltaMultipla') { echo ""; } elseif ($mapping['data_type'] === 'Data') { echo ""; } elseif ($mapping['data_type'] === 'INT') { echo ""; } elseif (($mapping['auto_value'] ?? 'none') === 'import_time') { echo ""; } elseif (in_array($mapping['field_label'], $timeLabels)) { echo ""; } else { echo ""; } echo "
"; $cellIndex++; $autoIndex++; } } $manualIndex = 0; foreach ($allMappings as $mapping) { if ($mapping['is_manual'] && $mapping['main_field'] != 1 && $mapping['is_visible_import'] == 1) { $detail = array_filter($rowDetails, fn($d) => $d['mapping_id'] == $mapping['id']); $detail = reset($detail) ?: ['field_value' => $mapping['manual_default']]; $fieldValue = $detail['field_value'] ?? $mapping['manual_default'] ?? ''; if ($mapping['data_type'] === 'Data' && $mapping['manual_default'] === 'today' && empty($fieldValue)) { $fieldValue = date('Y-m-d'); } $requiredClass = ($mapping['is_required'] && (is_null($fieldValue) || $fieldValue === '')) ? 'missing-required' : ''; $inputClass = 'manual-input'; if ($mapping['is_required']) $inputClass .= ' required-input'; echo "
"; if ($mapping['data_type'] === 'SceltaMultipla') { echo ""; } elseif ($mapping['data_type'] === 'Data') { echo ""; } elseif ($mapping['data_type'] === 'INT') { echo ""; } elseif (in_array($mapping['field_label'], $timeLabels)) { echo ""; } else { echo ""; } echo "
"; $cellIndex++; $manualIndex++; } } // Aggiunta cella per Tested Component echo "
"; echo ""; echo ""; echo "
"; $cellIndex++; ?>
Shipment Info
"; if ($f['data_type'] === 'DATE') { echo ""; } else { $isApiField = in_array($key, ['MoltiplicatorePrezzo', 'ClienteResponsabile', 'AnagraficaCertestObject', 'AnagraficaCertestService'], true); $selectClass = $isApiField ? 'api-fixed-select' : ''; echo ""; } echo "
"; $cellIndex++; // === INSERIMENTO dopo ConsegnaRichiesta === if ($key === 'ConsegnaRichiesta') { // Import Reference Code echo "
"; echo "" . htmlspecialchars($row['importreferencecode']) . ""; echo ""; echo "
"; $cellIndex++; // filename_import echo "
"; echo "File"; echo ""; echo "
"; $cellIndex++; // importdate echo "
"; echo "" . htmlspecialchars($row['importdate']) . ""; echo ""; echo "
"; $cellIndex++; } } } ?>