moncler supplier fabric routine

This commit is contained in:
Claudio 2026-03-27 15:54:22 +01:00
parent e96f538a47
commit 490786731a

View File

@ -0,0 +1,212 @@
<?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 nuova routine: " . 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.");
}
// Get headers from routine data
$headers = $routineData['xls_headers'] ?? [];
if (empty($headers)) {
throw new Exception("Nessun header trovato per la routine.");
}
// 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));
/**
* Define source and target headers
*/
$parte_moncler_header = 'PARTE MONCLER';
$parte_fornitore_header = 'PARTE FORNITORE';
$descr_parte_moncler_header = 'DESCRIZIONE PARTE MONCLER';
$descr_parte_fornitore_header = 'DESCRIZIONE PARTE FORNITORE';
$colore_moncler_header = 'COLORE MONCLER';
$colore_fornitore_header = 'COLORE FORNITORE';
$composizione_tessile_header = 'COMPOSIZIONE TESSILE';
$composizione_totale_header = 'COMPOSIZIONE TOTALE';
$pacchetti_test_header = 'PACCHETTI TEST';
$test_addizionali_header = 'TEST ADDIZIONALI (A5 solo se richiesti dal cliente)';
/**
* Find indexes
*/
$parte_moncler_index = array_search($parte_moncler_header, $normalized_headers, true);
$parte_fornitore_index = array_search($parte_fornitore_header, $normalized_headers, true);
$descr_parte_moncler_index = array_search($descr_parte_moncler_header, $normalized_headers, true);
$descr_parte_fornitore_index = array_search($descr_parte_fornitore_header, $normalized_headers, true);
$colore_moncler_index = array_search($colore_moncler_header, $normalized_headers, true);
$colore_fornitore_index = array_search($colore_fornitore_header, $normalized_headers, true);
$composizione_tessile_index = array_search($composizione_tessile_header, $normalized_headers, true);
$composizione_totale_index = array_search($composizione_totale_header, $normalized_headers, true);
$pacchetti_test_index = array_search($pacchetti_test_header, $normalized_headers, true);
$test_addizionali_index = array_search($test_addizionali_header, $normalized_headers, true);
error_log(
"Indici trovati - " .
"PARTE MONCLER: " . var_export($parte_moncler_index, true) .
", PARTE FORNITORE: " . var_export($parte_fornitore_index, true) .
", DESCRIZIONE PARTE MONCLER: " . var_export($descr_parte_moncler_index, true) .
", DESCRIZIONE PARTE FORNITORE: " . var_export($descr_parte_fornitore_index, true) .
", COLORE MONCLER: " . var_export($colore_moncler_index, true) .
", COLORE FORNITORE: " . var_export($colore_fornitore_index, true) .
", COMPOSIZIONE TESSILE: " . var_export($composizione_tessile_index, true) .
", COMPOSIZIONE TOTALE: " . var_export($composizione_totale_index, true) .
", PACCHETTI TEST: " . var_export($pacchetti_test_index, true) .
", TEST ADDIZIONALI: " . var_export($test_addizionali_index, true)
);
// Required target columns must exist
if ($parte_moncler_index === false) {
throw new Exception("Colonna PARTE MONCLER non trovata.");
}
if ($descr_parte_moncler_index === false) {
throw new Exception("Colonna DESCRIZIONE PARTE MONCLER non trovata.");
}
if ($colore_moncler_index === false) {
throw new Exception("Colonna COLORE MONCLER non trovata.");
}
if ($composizione_tessile_index === false) {
throw new Exception("Colonna COMPOSIZIONE TESSILE non trovata.");
}
if ($pacchetti_test_index === false) {
throw new Exception("Colonna PACCHETTI TEST non trovata.");
}
/**
* Apply merge row by row
*/
foreach ($excelData as $index => $row) {
if (!isset($row['data']) || !is_array($row['data'])) {
error_log("Riga non valida, manca 'data' all'indice $index");
continue;
}
// 1. PARTE MONCLER + PARTE FORNITORE -> PARTE MONCLER
$parte_values = [];
$value_parte_moncler = trim((string)($row['data'][$parte_moncler_index] ?? ''));
$value_parte_fornitore = $parte_fornitore_index !== false ? trim((string)($row['data'][$parte_fornitore_index] ?? '')) : '';
if ($value_parte_moncler !== '') {
$parte_values[] = $value_parte_moncler;
}
if ($value_parte_fornitore !== '') {
$parte_values[] = $value_parte_fornitore;
}
$parte_values = array_unique($parte_values);
$excelData[$index]['data'][$parte_moncler_index] = implode(' - ', $parte_values);
// 2. DESCRIZIONE PARTE MONCLER + DESCRIZIONE PARTE FORNITORE -> DESCRIZIONE PARTE MONCLER
$descr_values = [];
$value_descr_moncler = trim((string)($row['data'][$descr_parte_moncler_index] ?? ''));
$value_descr_fornitore = $descr_parte_fornitore_index !== false ? trim((string)($row['data'][$descr_parte_fornitore_index] ?? '')) : '';
if ($value_descr_moncler !== '') {
$descr_values[] = $value_descr_moncler;
}
if ($value_descr_fornitore !== '') {
$descr_values[] = $value_descr_fornitore;
}
$descr_values = array_unique($descr_values);
$excelData[$index]['data'][$descr_parte_moncler_index] = implode(' - ', $descr_values);
// 3. COLORE MONCLER + COLORE FORNITORE -> COLORE MONCLER
$colore_values = [];
$value_colore_moncler = trim((string)($row['data'][$colore_moncler_index] ?? ''));
$value_colore_fornitore = $colore_fornitore_index !== false ? trim((string)($row['data'][$colore_fornitore_index] ?? '')) : '';
if ($value_colore_moncler !== '') {
$colore_values[] = $value_colore_moncler;
}
if ($value_colore_fornitore !== '') {
$colore_values[] = $value_colore_fornitore;
}
$colore_values = array_unique($colore_values);
$excelData[$index]['data'][$colore_moncler_index] = implode(' - ', $colore_values);
// 4. COMPOSIZIONE TESSILE + COMPOSIZIONE TOTALE -> COMPOSIZIONE TESSILE
$composizione_values = [];
$value_composizione_tessile = trim((string)($row['data'][$composizione_tessile_index] ?? ''));
$value_composizione_totale = $composizione_totale_index !== false ? trim((string)($row['data'][$composizione_totale_index] ?? '')) : '';
if ($value_composizione_tessile !== '') {
$composizione_values[] = $value_composizione_tessile;
}
if ($value_composizione_totale !== '') {
$composizione_values[] = $value_composizione_totale;
}
$composizione_values = array_unique($composizione_values);
$excelData[$index]['data'][$composizione_tessile_index] = implode(' - ', $composizione_values);
// 5. PACCHETTI TEST + TEST ADDIZIONALI -> PACCHETTI TEST
$pacchetti_values = [];
$value_pacchetti_test = trim((string)($row['data'][$pacchetti_test_index] ?? ''));
$value_test_addizionali = $test_addizionali_index !== false ? trim((string)($row['data'][$test_addizionali_index] ?? '')) : '';
if ($value_pacchetti_test !== '') {
$pacchetti_values[] = $value_pacchetti_test;
}
if ($value_test_addizionali !== '') {
$pacchetti_values[] = $value_test_addizionali;
}
$pacchetti_values = array_unique($pacchetti_values);
$excelData[$index]['data'][$pacchetti_test_index] = implode(' - ', $pacchetti_values);
}
error_log("Nuova routine completata con successo. Righe elaborate: " . count($excelData));
} catch (Exception $e) {
error_log("Eccezione nella nuova routine: " . $e->getMessage());
throw $e;
}
}