getConnection(); // 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; // Fetch records with status='i' for this template $userFilter = $show_all_users ? '' : 'AND d.user_id = ?'; $importrefFilter = $importref !== '' ? 'AND d.importreferencecode = ?' : ''; $baseWhere = "WHERE d.templateid = ? AND d.status = 'i' {$userFilter} {$importrefFilter}"; $baseParams = [$template_id]; if (!$show_all_users) $baseParams[] = $user_id; if ($importref !== '') $baseParams[] = $importref; // Count total records $countStmt = $pdo->prepare("SELECT COUNT(*) FROM datadb d {$baseWhere}"); $countStmt->execute($baseParams); $totalRows = (int)$countStmt->fetchColumn(); $totalPages = $usePagination ? max(1, (int)ceil($totalRows / $perPage)) : 1; if ($page > $totalPages) $page = $totalPages; $limitClause = $usePagination ? 'LIMIT ' . $perPage . ' OFFSET ' . (($page - 1) * $perPage) : ''; $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 {$baseWhere} ORDER BY d.iddatadb DESC {$limitClause} "); $stmt->execute($baseParams); $importedData = $stmt->fetchAll(PDO::FETCH_ASSOC); $insertedIds = array_column($importedData, 'iddatadb'); // Fetch custom field details $manualDetails = []; if (!empty($insertedIds)) { $placeholders = implode(',', array_fill(0, count($insertedIds), '?')); $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 ({$placeholders}) "); $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', 'ClienteFornitore', 'ClienteAnalisi', 'AnagraficaCertestObject', 'AnagraficaCertestService', 'MoltiplicatorePrezzo', 'ConsegnaRichiesta', // se ci sono altri campi fixed li mettiamo dopo ]; // ClienteFornitore rendered as standalone column, exclude from fixed fields $excludeFromFixed = ['ClienteFornitore']; $fixedFields = []; $tempMap = []; foreach ($fixedFieldsRaw as $f) { if (in_array($f['fixed_field_key'], $excludeFromFixed, true)) continue; $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', 'ClienteFornitore' => 'cliente_fornitore_id', 'ClienteAnalisi' => 'clienteAnalisi', 'MoltiplicatorePrezzo' => 'moltiplicatore_prezzo_id', 'AnagraficaCertestObject' => 'anagrafica_certest_object_id', 'AnagraficaCertestService' => 'anagrafica_certest_service_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; } // ──────────────────────────────────────────────────────────── // Build gridData (JSON) — all row data for client-side rendering // ──────────────────────────────────────────────────────────── $gridDataArray = []; foreach ($importedData as $index => $row) { $rowObj = [ 'iddatadb' => (int)$row['iddatadb'], 'status' => $row['status'] ?? 'i', 'idclient' => $row['idclient'] ?? $default_idclient, 'cliente_fornitore_id' => $row['cliente_fornitore_id'] ?? null, 'tested_component' => $row['tested_component'] ?? '', 'commessaweb' => $row['commessaweb'] ?? null, 'user_name' => $row['user_name'] ?? '', 'importreferencecode' => $row['importreferencecode'] ?? '', 'filename_import' => $row['filename_import'] ?? '', 'importdate' => $row['importdate'] ?? '', ]; // Fixed fields $rowObj['fixedFields'] = []; foreach ($fixedFields as $f) { $key = $f['fixed_field_key']; $dbCol = $fixedAliasMap[$key] ?? $key; $val = $row[$dbCol] ?? ''; if ($val === '' || $val === null) { $val = fixedDefaultValue($f); } $rowObj['fixedFields'][$key] = (string)$val; } // Detail fields (from import_data_details) $rowObj['details'] = []; $rowDetails = array_filter($manualDetails, fn($d) => $d['datadb_id'] == $row['iddatadb']); foreach ($rowDetails as $d) { $rowObj['details'][(string)$d['mapping_id']] = $d['field_value'] ?? ''; } // Main field value if ($mainFieldMapping) { $mainDetail = array_filter($rowDetails, fn($d) => $d['mapping_id'] == $mainFieldMapping['id']); $mainDetail = reset($mainDetail) ?: ['field_value' => $mainFieldMapping['manual_default'] ?? '']; $rowObj['mainFieldValue'] = $mainDetail['field_value'] ?? $mainFieldMapping['manual_default'] ?? ''; } $rowObj['_dirty'] = false; $gridDataArray[] = $rowObj; } // Build columns in display order $gridColumns = []; // 1. Main field if ($mainFieldMapping) { $gridColumns[] = [ 'type' => 'main_field', 'key' => (string)$mainFieldMapping['id'], 'label' => $mainFieldMapping['field_label'], 'dataType' => $mainFieldMapping['data_type'], 'isManual' => (bool)$mainFieldMapping['is_manual'], 'isRequired' => (bool)$mainFieldMapping['is_required'], 'fieldId' => $mainFieldMapping['field_id'] ?? null, 'width' => 150, ]; } // 2. Status $gridColumns[] = ['type' => 'status', 'key' => 'status', 'label' => 'Status', 'width' => 150, 'editable' => false]; // 3. Client $gridColumns[] = ['type' => 'idclient', 'key' => 'idclient', 'label' => 'Client', 'width' => 300]; // 4. Cliente Fornitore $gridColumns[] = ['type' => 'cliente_fornitore_id', 'key' => 'cliente_fornitore_id', 'label' => $slugMapping['ClienteFornitore'] ?? 'ClienteFornitore', 'width' => 300]; // 5. Auto fields foreach ($allMappings as $mapping) { if ( !$mapping['is_manual'] && $mapping['main_field'] != 1 && $mapping['is_visible_import'] == 1 && trim((string)$mapping['field_label']) !== 'Tested Component:' ) { $gridColumns[] = [ 'type' => 'detail', 'key' => (string)$mapping['id'], 'label' => $mapping['field_label'], 'dataType' => $mapping['data_type'], 'isManual' => false, 'isRequired' => (bool)$mapping['is_required'], 'fieldId' => $mapping['field_id'] ?? null, 'autoValue' => $mapping['auto_value'] ?? 'none', 'width' => 150, ]; } } // 6. Manual fields foreach ($allMappings as $mapping) { if ( $mapping['is_manual'] && $mapping['main_field'] != 1 && $mapping['is_visible_import'] == 1 && trim((string)$mapping['field_label']) !== 'Tested Component:' ) { $gridColumns[] = [ 'type' => 'detail', 'key' => (string)$mapping['id'], 'label' => $mapping['field_label'], 'dataType' => $mapping['data_type'], 'isManual' => true, 'isRequired' => (bool)$mapping['is_required'], 'fieldId' => $mapping['field_id'] ?? null, 'manualDefault' => $mapping['manual_default'] ?? '', 'width' => 150, ]; } } // 7. Tested Component $gridColumns[] = ['type' => 'tested_component', 'key' => 'tested_component', 'label' => 'Tested Component', 'width' => 150]; // 8. AWB $gridColumns[] = ['type' => 'awb', 'key' => 'awb', 'label' => 'AWB Number', 'width' => 200]; // 9. Tracking $gridColumns[] = ['type' => 'tracking', 'key' => 'tracking', 'label' => 'Tracking Info', 'width' => 250]; // 10. Fixed fields (with importreferencecode/filename/importdate after ConsegnaRichiesta) foreach ($fixedFields as $f) { $key = $f['fixed_field_key']; $label = $slugMapping[$key] ?? $key; $gridColumns[] = [ 'type' => 'fixed', 'key' => $key, 'label' => $label, 'dataType' => $f['data_type'], 'isRequired' => (bool)$f['is_required'], 'width' => 180, ]; if ($key === 'ConsegnaRichiesta') { $gridColumns[] = ['type' => 'static', 'key' => 'importreferencecode', 'label' => $slugMapping['importreferencecode'] ?? 'Import Reference Code', 'width' => 150, 'editable' => false]; $gridColumns[] = ['type' => 'static', 'key' => 'filename_import', 'label' => $slugMapping['filename_import'] ?? 'File', 'width' => 150, 'editable' => false]; $gridColumns[] = ['type' => 'static', 'key' => 'importdate', 'label' => $slugMapping['importdate'] ?? 'Import Date', 'width' => 150, 'editable' => false]; } } $gridMeta = [ 'templateId' => $template_id, 'defaultIdclient' => $default_idclient, 'isAdmin' => Auth::user()->hasRole('Admin'), 'userId' => $user_id, 'fixedAliasMap' => $fixedAliasMap, 'slugMapping' => $slugMapping, 'timeLabels' => $timeLabels, 'columns' => $gridColumns, 'mainFieldMapping' => $mainFieldMapping, 'totalRows' => count($gridDataArray), ]; ?> Edit Imported Data - <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?>
Imported (i) To LIMS (l) ( records)
Rows per page
$lim])); ?>
1): ?>
of 1): ?> 1 2): ?>... ...