lesillaroutine

This commit is contained in:
2026-07-10 16:38:06 +02:00
parent 68f344bc06
commit def2715d38
+90
View File
@@ -0,0 +1,90 @@
<?php
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/routine_debug.log');
function applyRoutine(&$excelData, $routineData)
{
try {
// Initial log
error_log("Inizio esecuzione routine Le Silla: " . date('Y-m-d H:i:s'));
error_log("Dati routine: " . print_r($routineData, true));
error_log("Dati excel_data: " . print_r($excelData, true));
// Check if excelData is empty
if (empty($excelData)) {
throw new Exception("excelData è vuoto o non valido.");
}
// Extract routine settings with default values
$articolo_header = trim($routineData['articolo'] ?? 'ARTICOLO');
$descrizione_header = trim($routineData['descrizione'] ?? 'DESCRIZIONE');
$headers = $routineData['xls_headers'] ?? [];
if (empty($headers)) {
throw new Exception("Nessun header trovato per la routine Le Silla.");
}
// If headers arrive as JSON string, decode them
if (!is_array($headers) && is_string($headers)) {
$decoded_headers = json_decode($headers, true);
if (json_last_error() === JSON_ERROR_NONE && is_array($decoded_headers)) {
$headers = $decoded_headers;
}
}
if (!is_array($headers) || empty($headers)) {
throw new Exception("Gli header della routine non sono validi.");
}
error_log("Header ricevuti: " . print_r($headers, true));
// Normalize headers
$normalized_headers = array_map(function ($header) {
return trim((string)$header);
}, $headers);
error_log("Header normalizzati: " . print_r($normalized_headers, true));
error_log("Header values - ARTICOLO: '$articolo_header', DESCRIZIONE: '$descrizione_header'");
// Find column indexes
$articolo_index = array_search($articolo_header, $normalized_headers, true);
$descrizione_index = array_search($descrizione_header, $normalized_headers, true);
if ($articolo_index === false) {
throw new Exception("Colonna ARTICOLO non trovata.");
}
if ($descrizione_index === false) {
throw new Exception("Colonna DESCRIZIONE non trovata.");
}
error_log("Indici colonne - ARTICOLO: $articolo_index, DESCRIZIONE: $descrizione_index");
/**
* Merge ARTICOLO + DESCRIZIONE into ARTICOLO separated by " - "
*/
foreach ($excelData as $index => $row) {
if (!isset($row['data']) || !is_array($row['data'])) {
error_log("Riga non valida, manca 'data' all'indice $index");
continue;
}
$value_articolo = trim((string)($row['data'][$articolo_index] ?? ''));
$value_descrizione = trim((string)($row['data'][$descrizione_index] ?? ''));
$parts = [];
if ($value_articolo !== '') {
$parts[] = $value_articolo;
}
if ($value_descrizione !== '') {
$parts[] = $value_descrizione;
}
$excelData[$index]['data'][$articolo_index] = implode(' - ', $parts);
}
error_log("Routine Le Silla completata con successo - Merge applicato su " . count($excelData) . " righe");
} catch (Exception $e) {
error_log("Eccezione nella routine Le Silla: " . $e->getMessage());
throw $e;
}
}