fixed routine

This commit is contained in:
Claudio 2026-03-27 09:36:06 +01:00
parent 7a486e9dcf
commit 4cf03ae742

View File

@ -31,129 +31,185 @@ function applyRoutine(&$excelData, $routineData)
if (empty($headers)) {
throw new Exception("Nessun header trovato per la routine Moncler.");
}
// 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('trim', $headers);
$normalized_headers = array_map(function ($header) {
return trim((string)$header);
}, $headers);
error_log("Header normalizzati: " . print_r($normalized_headers, true));
error_log("Action values - action1: '$action1', action2: '$action2', action3: '$action3', action4: '$action4'");
// Find column indexes
$action1_index = array_search($action1, $normalized_headers);
$action2_index = array_search($action2, $normalized_headers);
$action3_index = array_search($action3, $normalized_headers);
$action4_index = array_search($action4, $normalized_headers);
$package_index = array_search($package_header, $normalized_headers);
$other_test_index = array_search($other_test_header, $normalized_headers);
$only_colorfastness_index = array_search($only_colorfastness_header, $normalized_headers);
$only_chemical_index = array_search($only_chemical_header, $normalized_headers);
if ($action1_index === false || $action2_index === false || $action3_index === false || $action4_index === false) {
throw new Exception(
"Colonne non trovate - action1: '$action1' (index: " . var_export($action1_index, true) .
"), action2: '$action2' (index: " . var_export($action2_index, true) .
"), action3: '$action3' (index: " . var_export($action3_index, true) .
"), action4: '$action4' (index: " . var_export($action4_index, true) . ")"
);
}
// Find package-related column indexes
$package_index = array_search($package_header, $normalized_headers, true);
$other_test_index = array_search($other_test_header, $normalized_headers, true);
$only_colorfastness_index = array_search($only_colorfastness_header, $normalized_headers, true);
$only_chemical_index = array_search($only_chemical_header, $normalized_headers, true);
if ($package_index === false) {
throw new Exception("Colonna PACKAGE non trovata.");
}
error_log("Indici colonne - action1: $action1_index, action2: $action2_index, action3: $action3_index, action4: $action4_index");
error_log("Indici package - PACKAGE: " . var_export($package_index, true) . ", Other test from Control Matrix: " . var_export($other_test_index, true) . ", Only Colorfastness package: " . var_export($only_colorfastness_index, true) . ", Only Chemical package: " . var_export($only_chemical_index, true));
error_log(
"Indici package - PACKAGE: " . var_export($package_index, true) .
", Other test from Control Matrix: " . var_export($other_test_index, true) .
", Only Colorfastness package: " . var_export($only_colorfastness_index, true) .
", Only Chemical package: " . var_export($only_chemical_index, true)
);
// Group rows by action1 value
$grouped_data = [];
foreach ($excelData as $row) {
/**
* PART 1
* Aggregate rows by action1 and split action2 into action3 + action4
* If it fails, continue with PART 2 only
*/
$part1_applied = false;
$action1_index = array_search($action1, $normalized_headers, true);
$action2_index = array_search($action2, $normalized_headers, true);
$action3_index = array_search($action3, $normalized_headers, true);
$action4_index = array_search($action4, $normalized_headers, true);
if ($action1_index === false || $action2_index === false || $action3_index === false || $action4_index === false) {
error_log(
"Unable to apply routine part 1. Package merge logic has been applied only. " .
"Missing columns - action1: '$action1' (index: " . var_export($action1_index, true) . ")" .
", action2: '$action2' (index: " . var_export($action2_index, true) . ")" .
", action3: '$action3' (index: " . var_export($action3_index, true) . ")" .
", action4: '$action4' (index: " . var_export($action4_index, true) . ")"
);
} else {
error_log("Indici colonne part 1 - action1: $action1_index, action2: $action2_index, action3: $action3_index, action4: $action4_index");
$grouped_data = [];
foreach ($excelData as $row) {
if (!isset($row['data']) || !is_array($row['data'])) {
error_log("Riga non valida, manca 'data' per excelrow " . ($row['excelrow'] ?? 'N/A'));
continue;
}
$key = $row['data'][$action1_index] ?? '';
$key = trim((string)$key);
$key = $key === '' ? '_empty_' : $key;
if (!isset($grouped_data[$key])) {
$grouped_data[$key] = [
'data' => $row['data'],
'excelrow' => [($row['excelrow'] ?? '')],
'style_codes' => [],
'style_descriptions' => []
];
} else {
$grouped_data[$key]['excelrow'][] = ($row['excelrow'] ?? '');
}
// Split STYLE CODE + STYLE DESCRIPTION
$action2_value = trim((string)($row['data'][$action2_index] ?? ''));
if ($action2_value !== '') {
$parts = explode(' - ', $action2_value, 2);
$style_code = trim((string)($parts[0] ?? ''));
$style_description = trim((string)($parts[1] ?? ''));
if ($style_code !== '') {
$grouped_data[$key]['style_codes'][] = $style_code;
}
if ($style_description !== '') {
$grouped_data[$key]['style_descriptions'][] = $style_description;
}
} else {
error_log("Valore vuoto in action2 per excelrow " . ($row['excelrow'] ?? 'N/A'));
}
}
$new_excel_data = [];
foreach ($grouped_data as $key => $group) {
$row_data = $group['data'];
// Update STYLE CODE and STYLE DESCRIPTION with aggregated unique values
$row_data[$action3_index] = implode(' - ', array_unique($group['style_codes']));
$row_data[$action4_index] = implode(' - ', array_unique($group['style_descriptions']));
// Concatenate excelrow values with "+"
$excelrow_clean = array_filter($group['excelrow'], function ($value) {
return $value !== null && $value !== '';
});
$excelrow_value = count($excelrow_clean) > 1
? implode('+', $excelrow_clean)
: (reset($excelrow_clean) ?: '');
$new_excel_data[] = [
'data' => $row_data,
'excelrow' => $excelrow_value
];
}
$excelData = $new_excel_data;
$part1_applied = true;
error_log("Routine part 1 completata - Righe aggregate: " . count($new_excel_data));
error_log("Excelrow aggregati part 1: " . print_r(array_column($new_excel_data, 'excelrow'), true));
}
/**
* PART 2
* Merge package-related columns into PACKAGE
* Always applied if PACKAGE exists
*/
foreach ($excelData as $index => $row) {
if (!isset($row['data']) || !is_array($row['data'])) {
error_log("Riga non valida, manca 'data' per excelrow {$row['excelrow']}");
error_log("Riga non valida nella part 2, manca 'data' all'indice $index");
continue;
}
$key = $row['data'][$action1_index] ?? '';
$key = empty($key) ? '_empty_' : $key;
if (!isset($grouped_data[$key])) {
$grouped_data[$key] = [
'data' => $row['data'],
'excelrow' => [$row['excelrow']],
'style_codes' => [],
'style_descriptions' => [],
'packages' => []
];
} else {
$grouped_data[$key]['excelrow'][] = $row['excelrow'];
}
// Split STYLE CODE + STYLE DESCRIPTION
$action2_value = $row['data'][$action2_index] ?? '';
if (!empty($action2_value)) {
$parts = explode(' - ', trim($action2_value), 2);
$style_code = $parts[0] ?? '';
$style_description = $parts[1] ?? '';
if (!empty($style_code)) {
$grouped_data[$key]['style_codes'][] = $style_code;
}
if (!empty($style_description)) {
$grouped_data[$key]['style_descriptions'][] = $style_description;
}
} else {
error_log("Valore vuoto in action2 per excelrow {$row['excelrow']}");
}
// Collect values to merge into PACKAGE
$package_values = [];
if ($package_index !== false) {
$package_values[] = trim((string)($row['data'][$package_index] ?? ''));
$value_package = trim((string)($row['data'][$package_index] ?? ''));
$value_other_test = $other_test_index !== false ? trim((string)($row['data'][$other_test_index] ?? '')) : '';
$value_only_colorfastness = $only_colorfastness_index !== false ? trim((string)($row['data'][$only_colorfastness_index] ?? '')) : '';
$value_only_chemical = $only_chemical_index !== false ? trim((string)($row['data'][$only_chemical_index] ?? '')) : '';
if ($value_package !== '') {
$package_values[] = $value_package;
}
if ($other_test_index !== false) {
$package_values[] = trim((string)($row['data'][$other_test_index] ?? ''));
if ($value_other_test !== '') {
$package_values[] = $value_other_test;
}
if ($only_colorfastness_index !== false) {
$package_values[] = trim((string)($row['data'][$only_colorfastness_index] ?? ''));
if ($value_only_colorfastness !== '') {
$package_values[] = $value_only_colorfastness;
}
if ($only_chemical_index !== false) {
$package_values[] = trim((string)($row['data'][$only_chemical_index] ?? ''));
if ($value_only_chemical !== '') {
$package_values[] = $value_only_chemical;
}
foreach ($package_values as $package_value) {
if ($package_value !== '') {
$grouped_data[$key]['packages'][] = $package_value;
}
}
$package_values = array_unique($package_values);
$excelData[$index]['data'][$package_index] = implode(' - ', $package_values);
}
// Build the new aggregated excel_data array
$new_excel_data = [];
foreach ($grouped_data as $key => $group) {
$row_data = $group['data'];
error_log("Routine part 2 completata - Merge package applicato su " . count($excelData) . " righe");
// Update STYLE CODE and STYLE DESCRIPTION with aggregated unique values
$row_data[$action3_index] = implode(' - ', array_unique($group['style_codes']));
$row_data[$action4_index] = implode(' - ', array_unique($group['style_descriptions']));
// Merge PACKAGE-related columns into PACKAGE
$row_data[$package_index] = implode(' - ', array_unique($group['packages']));
// Concatenate excelrow values with "+"
$excelrow_value = count($group['excelrow']) > 1 ? implode('+', $group['excelrow']) : $group['excelrow'][0];
$new_excel_data[] = [
'data' => $row_data,
'excelrow' => $excelrow_value
];
if (!$part1_applied) {
error_log("Warning finale: routine part 1 non applicata, routine part 2 applicata correttamente");
}
// Modify excelData in-place
$excelData = $new_excel_data;
error_log("Routine Moncler completata - Righe aggregate: " . count($new_excel_data));
error_log("Excelrow aggregati: " . print_r(array_column($new_excel_data, 'excelrow'), true));
error_log("Routine Moncler completata con successo");
} catch (Exception $e) {
error_log("Eccezione nella routine Moncler: " . $e->getMessage());
throw $e;