$row) { if (!isset($row['data']) || !is_array($row['data'])) { error_log("Riga non valida nello step 0, manca 'data' all'indice $index"); continue; } $combinedValue = trim((string)($row['data'][$action2_index] ?? '')); $currentStyleCode = trim((string)($row['data'][$action3_index] ?? '')); $currentStyleDescription = trim((string)($row['data'][$action4_index] ?? '')); if ($combinedValue === '') { continue; } [$parsedStyleCode, $parsedStyleDescription] = $splitStyleCombined($combinedValue); $styleCodeValues = $splitJoinedValues($currentStyleCode); $styleDescriptionValues = $splitJoinedValues($currentStyleDescription); if ($parsedStyleCode !== '' && !in_array($parsedStyleCode, $styleCodeValues, true)) { $styleCodeValues[] = $parsedStyleCode; } if ($parsedStyleDescription !== '' && !in_array($parsedStyleDescription, $styleDescriptionValues, true)) { $styleDescriptionValues[] = $parsedStyleDescription; } $excelData[$index]['data'][$action3_index] = $joinUniqueValues($styleCodeValues); $excelData[$index]['data'][$action4_index] = $joinUniqueValues($styleDescriptionValues); error_log( "Step 0 - excelrow " . ($row['excelrow'] ?? 'N/A') . " | combined: '" . $combinedValue . "'" . " | style_code_result: '" . $excelData[$index]['data'][$action3_index] . "'" . " | style_description_result: '" . $excelData[$index]['data'][$action4_index] . "'" ); } $step0_applied = true; error_log("Routine step 0 completato - Normalizzazione style applicata su " . count($excelData) . " righe"); } /** * STEP 1 * Aggregate rows by action1 using normalized STYLE CODE and STYLE DESCRIPTION * If it fails, continue with STEP 2 only */ $step1_applied = false; if ($action1_index === false || $action3_index === false || $action4_index === false) { error_log( "Unable to apply routine step 1. Package merge logic has been applied only. " . "Missing columns - action1: '$action1' (index: " . var_export($action1_index, true) . ")" . ", action3: '$action3' (index: " . var_export($action3_index, true) . ")" . ", action4: '$action4' (index: " . var_export($action4_index, true) . ")" ); } else { $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'] ?? ''); } // Collect normalized STYLE CODE values $styleCodeValues = $splitJoinedValues($row['data'][$action3_index] ?? ''); foreach ($styleCodeValues as $styleCodeValue) { if (!in_array($styleCodeValue, $grouped_data[$key]['style_codes'], true)) { $grouped_data[$key]['style_codes'][] = $styleCodeValue; } } // Collect normalized STYLE DESCRIPTION values $styleDescriptionValues = $splitJoinedValues($row['data'][$action4_index] ?? ''); foreach ($styleDescriptionValues as $styleDescriptionValue) { if (!in_array($styleDescriptionValue, $grouped_data[$key]['style_descriptions'], true)) { $grouped_data[$key]['style_descriptions'][] = $styleDescriptionValue; } } } $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] = $joinUniqueValues($group['style_codes']); $row_data[$action4_index] = $joinUniqueValues($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; $step1_applied = true; error_log("Routine step 1 completato - Righe aggregate: " . count($new_excel_data)); error_log("Excelrow aggregati step 1: " . print_r(array_column($new_excel_data, 'excelrow'), true)); } /** * STEP 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 nello step 2, manca 'data' all'indice $index"); continue; } $package_values = []; $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 ($value_other_test !== '') { $package_values[] = $value_other_test; } if ($value_only_colorfastness !== '') { $package_values[] = $value_only_colorfastness; } if ($value_only_chemical !== '') { $package_values[] = $value_only_chemical; } $package_values = array_unique($package_values); $excelData[$index]['data'][$package_index] = implode(' - ', $package_values); } error_log("Routine step 2 completato - Merge package applicato su " . count($excelData) . " righe"); if (!$step0_applied) { error_log("Warning finale: routine step 0 non applicata"); } if (!$step1_applied) { error_log("Warning finale: routine step 1 non applicata, routine step 2 applicata correttamente"); } error_log("Routine Moncler completata con successo"); } catch (Exception $e) { error_log("Eccezione nella routine Moncler: " . $e->getMessage()); throw $e; } }