From 864714d198bb642f215b6767c7491ca73018a660 Mon Sep 17 00:00:00 2001 From: Claudio Date: Wed, 24 Sep 2025 14:18:31 +0200 Subject: [PATCH] routine scripts --- public/userarea/apply_routine.php | 50 ++++ public/userarea/import_insert.php | 85 +++--- public/userarea/import_xls2.php | 335 +++++++++++++++--------- public/userarea/process_import_xls2.php | 48 +++- 4 files changed, 342 insertions(+), 176 deletions(-) create mode 100644 public/userarea/apply_routine.php diff --git a/public/userarea/apply_routine.php b/public/userarea/apply_routine.php new file mode 100644 index 0000000..601fb8f --- /dev/null +++ b/public/userarea/apply_routine.php @@ -0,0 +1,50 @@ + '', 'rows' => [], 'columns' => [], 'template_id' => 0, 'filename' => '', 'excel_data' => []]; + +try { + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $input = json_decode(file_get_contents('php://input'), true); + $template_id = isset($input['template_id']) ? intval($input['template_id']) : 0; + $filename = $input['routine_data']['filename'] ?? ''; + $headerrow = $input['routine_data']['headerrow'] ?? 1; + $excelData = $input['excel_data'] ?? []; + $routineData = $input['routine_data'] ?? []; + + if (!$filename || empty($excelData)) { + throw new Exception("Dati della routine mancanti."); + } + + $routineFile = __DIR__ . '/routines/' . $filename; + if (file_exists($routineFile)) { + include_once $routineFile; + $routineData['xls_headers'] = $_SESSION['headers'] ?? []; + applyRoutine($excelData, $routineData); // Modifica $excelData in place + error_log("Routine {$routineData['name']} applicata (file: {$filename}) per template {$template_id}, header row: {$headerrow}"); + } else { + throw new Exception("File della routine non trovato: $routineFile"); + } + + // Aggiorna la sessione con i dati modificati + $_SESSION['excel_data'] = $excelData; + + $response['excel_data'] = $excelData; + $response['rows'] = array_column($excelData, 'data'); + $response['columns'] = $_SESSION['headers']; + $response['template_id'] = $template_id; + $response['filename'] = $input['filename'] ?? ''; + } else { + $response['error'] = "Richiesta non valida."; + } +} catch (Exception $e) { + $response['error'] = "Errore durante l'applicazione della routine: " . $e->getMessage(); + error_log("Exception in apply_routine.php: " . $e->getMessage()); +} + +ob_end_clean(); +header('Content-Type: application/json'); +echo json_encode($response); +exit; diff --git a/public/userarea/import_insert.php b/public/userarea/import_insert.php index 7a8d012..44ca645 100644 --- a/public/userarea/import_insert.php +++ b/public/userarea/import_insert.php @@ -17,23 +17,27 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['template_id']) || !i header("Location: xlstemplates_grid.php?status=error&message=" . urlencode("Richiesta non valida")); exit; } + $template_id = intval($_POST['template_id']); -$selected_rows = $_POST['selected_rows']; -$columns = json_decode($_POST['columns'], true); // Header dell'XLS -$rows = json_decode($_POST['rows'], true); // Dati dell'XLS +$selected_rows = array_map('intval', $_POST['selected_rows']); +$columns = json_decode($_POST['columns'], true); +$rows = json_decode($_POST['rows'], true); +$excelrows = json_decode($_POST['excelrows'], true); $newFilename = htmlspecialchars($_POST['filename']); $_SESSION['template_id'] = $template_id; $_SESSION['selected_rows'] = $selected_rows; $_SESSION['columns'] = $columns; $_SESSION['rows'] = $rows; +$_SESSION['excelrows'] = $excelrows; $_SESSION['filename'] = $newFilename; error_log("Received Data - Template ID: $template_id, Selected Rows: " . json_encode($selected_rows)); error_log("Columns: " . json_encode($columns)); error_log("Rows: " . json_encode($rows)); +error_log("Excelrows: " . json_encode($excelrows)); -$user_id = $iduserlogin ?? 1; // Default a 1 se non definito +$user_id = $iduserlogin ?? 1; $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); @@ -60,30 +64,38 @@ foreach ($allMappings as $mapping) { } } -// Inserisci le righe selezionate in datadb (solo campi generici con templateid) +// Inserisci le righe selezionate in datadb $insertedIds = []; foreach ($selected_rows as $rowIndex) { - $row = $rows[$rowIndex]; + $row = $rows[$rowIndex] ?? null; + $excelrow = $excelrows[$rowIndex] ?? null; + + if ($row === null || $excelrow === null) { + error_log("Errore: riga o excelrow mancante per rowIndex $rowIndex"); + continue; + } + $values = [ - $template_id, // templateid - $importReferenceCode, // importreferencecode - $newFilename, // filename_import - 'i', // status - $user_id, // user_id - null, // limscode - date('Y-m-d') // importdate + $template_id, + $importReferenceCode, + $newFilename, + 'i', + $user_id, + null, + date('Y-m-d'), + $excelrow // Aggiunto excelrow per la colonna excelrow ]; - $sql = "INSERT INTO datadb (templateid, importreferencecode, filename_import, status, user_id, limscode, importdate) VALUES (?, ?, ?, ?, ?, ?, ?)"; + $sql = "INSERT INTO datadb (templateid, importreferencecode, filename_import, status, user_id, limscode, importdate, excelrow) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute($values); $iddatadb = $pdo->lastInsertId(); $insertedIds[] = $iddatadb; - // Inserisci tutti i campi (automatici e manuali) in import_data_details + // Inserisci tutti i campi in import_data_details foreach ($allMappings as $mapping) { $fieldValue = null; - if (!$mapping['is_manual']) { // Campi automatici dall'XLS + if (!$mapping['is_manual']) { $excelColumn = trim($mapping['excel_column']); $excelColumnIndex = array_search($excelColumn, array_map('trim', $columns)); if ($excelColumnIndex !== false && isset($row[$excelColumnIndex]) && $row[$excelColumnIndex] !== '') { @@ -109,7 +121,7 @@ foreach ($selected_rows as $rowIndex) { $fieldValue = !empty($fieldValue) ? htmlspecialchars((string)$fieldValue) : ($mapping['manual_default'] ?? ''); break; } - } else { // Campi manuali + } else { $fieldValue = $mapping['manual_default'] ?? ''; if ($mapping['data_type'] === 'DATE' && $mapping['manual_default'] === 'today') { $fieldValue = date('Y-m-d'); @@ -129,29 +141,26 @@ $_SESSION['inserted_ids'] = $insertedIds; $params = [ 'template_id' => $template_id, - 'filename' => $newFilename, + 'filename' => $newFilename, ]; ?> -
- - - - - - - - - - - - - -
- +
+ + + + + + + + + + + +
+ \ No newline at end of file diff --git a/public/userarea/import_xls2.php b/public/userarea/import_xls2.php index 47cadd7..e1e3b40 100644 --- a/public/userarea/import_xls2.php +++ b/public/userarea/import_xls2.php @@ -21,6 +21,11 @@ if (!$template) { exit; } +// Verifica i mapping +$stmt = $pdo->prepare("SELECT id FROM template_mapping WHERE template_id = ?"); +$stmt->execute([$id]); +$hasMappings = $stmt->fetch(PDO::FETCH_ASSOC); + // Debug del template error_log("Loaded template: " . print_r($template, true)); ?> @@ -33,6 +38,7 @@ error_log("Loaded template: " . print_r($template, true)); +