first commit
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
<?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 Moncler: " . 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
|
||||
$action1 = trim($routineData['action1'] ?? 'K');
|
||||
$action2 = trim($routineData['action2'] ?? 'STYLE CODE + STYLE DESCRIPTION');
|
||||
$action3 = trim($routineData['action3'] ?? 'STYLE CODE');
|
||||
$action4 = trim($routineData['action4'] ?? 'STYLE DESCRIPTION');
|
||||
$headers = $routineData['xls_headers'] ?? [];
|
||||
|
||||
// Package-related headers
|
||||
$package_header = 'PACKAGE';
|
||||
$other_test_header = 'Other test from Control Matrix';
|
||||
$only_colorfastness_header = 'Only Colorfastness package';
|
||||
$only_chemical_header = 'Only Chemical package';
|
||||
|
||||
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(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 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 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)
|
||||
);
|
||||
|
||||
/**
|
||||
* 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 nella part 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 part 2 completata - Merge package applicato su " . count($excelData) . " righe");
|
||||
|
||||
if (!$part1_applied) {
|
||||
error_log("Warning finale: routine part 1 non applicata, routine part 2 applicata correttamente");
|
||||
}
|
||||
|
||||
error_log("Routine Moncler completata con successo");
|
||||
} catch (Exception $e) {
|
||||
error_log("Eccezione nella routine Moncler: " . $e->getMessage());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
<?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 Moncler: " . 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
|
||||
$action1 = trim($routineData['action1'] ?? 'K');
|
||||
$action2 = trim($routineData['action2'] ?? 'STYLE CODE + STYLE DESCRIPTION');
|
||||
$action3 = trim($routineData['action3'] ?? 'STYLE CODE');
|
||||
$action4 = trim($routineData['action4'] ?? 'STYLE DESCRIPTION');
|
||||
$headers = $routineData['xls_headers'] ?? [];
|
||||
|
||||
// Package-related headers
|
||||
$package_header = 'PACKAGE';
|
||||
$other_test_header = 'Other test from Control Matrix';
|
||||
$only_colorfastness_header = 'Only Colorfastness package';
|
||||
$only_chemical_header = 'Only Chemical package';
|
||||
|
||||
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(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 main indexes
|
||||
$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);
|
||||
|
||||
// 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: " . var_export($action1_index, true) .
|
||||
", action2: " . var_export($action2_index, true) .
|
||||
", action3: " . var_export($action3_index, true) .
|
||||
", action4: " . var_export($action4_index, true) .
|
||||
", 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)
|
||||
);
|
||||
|
||||
// Helper: split a value already joined by " - "
|
||||
$splitJoinedValues = function ($value) {
|
||||
$value = trim((string)$value);
|
||||
if ($value === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$parts = preg_split('/\s*-\s*/', $value);
|
||||
$parts = array_map('trim', $parts);
|
||||
$parts = array_filter($parts, function ($item) {
|
||||
return $item !== '';
|
||||
});
|
||||
|
||||
return array_values(array_unique($parts));
|
||||
};
|
||||
|
||||
// Helper: join unique values with " - "
|
||||
$joinUniqueValues = function (array $values) {
|
||||
$clean = [];
|
||||
foreach ($values as $value) {
|
||||
$value = trim((string)$value);
|
||||
if ($value !== '' && !in_array($value, $clean, true)) {
|
||||
$clean[] = $value;
|
||||
}
|
||||
}
|
||||
return implode(' - ', $clean);
|
||||
};
|
||||
|
||||
// Helper: split STYLE CODE + STYLE DESCRIPTION into code + description
|
||||
$splitStyleCombined = function ($value) {
|
||||
$value = trim((string)$value);
|
||||
|
||||
if ($value === '') {
|
||||
return ['', ''];
|
||||
}
|
||||
|
||||
// Accept both "CODE - DESC" and "CODE-DESC"
|
||||
$parts = preg_split('/\s*-\s*/', $value, 2);
|
||||
|
||||
$styleCode = trim((string)($parts[0] ?? ''));
|
||||
$styleDescription = trim((string)($parts[1] ?? ''));
|
||||
|
||||
return [$styleCode, $styleDescription];
|
||||
};
|
||||
|
||||
/**
|
||||
* STEP 0
|
||||
* Normalize STYLE CODE and STYLE DESCRIPTION before grouping by K
|
||||
*
|
||||
* Rules:
|
||||
* - Read STYLE CODE + STYLE DESCRIPTION
|
||||
* - If it contains code/description, fill missing values
|
||||
* - If target field already contains a different value, add it instead of replacing it
|
||||
* - Avoid duplicates
|
||||
*/
|
||||
$step0_applied = false;
|
||||
|
||||
if ($action2_index === false || $action3_index === false || $action4_index === false) {
|
||||
error_log(
|
||||
"Unable to apply routine step 0. Missing columns - 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 {
|
||||
foreach ($excelData as $index => $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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Routine: build_field_347_from_x_columns
|
||||
*
|
||||
* Purpose:
|
||||
* For each imported XLS row:
|
||||
* - check columns P to AT
|
||||
* - when a cell contains "x", take the related column title from row 6
|
||||
* - append the free text value from column AU
|
||||
* - save the final comma-separated text into column P
|
||||
*
|
||||
* Target:
|
||||
* Column P must be mapped to field_id 347.
|
||||
*/
|
||||
|
||||
function applyRoutine(&$excelData, $routineData = [])
|
||||
{
|
||||
/*
|
||||
* Excel column indexes are zero-based:
|
||||
*
|
||||
* P = 15
|
||||
* AT = 45
|
||||
* AU = 46
|
||||
*/
|
||||
$targetColumnIndex = 15; // P
|
||||
$startColumnIndex = 15; // P
|
||||
$endColumnIndex = 45; // AT
|
||||
$extraColumnIndex = 46; // AU
|
||||
|
||||
/*
|
||||
* Headers must come from XLS row 6.
|
||||
* Usually they are passed inside $routineData['xls_headers'].
|
||||
*/
|
||||
$headers = $routineData['xls_headers'] ?? [];
|
||||
|
||||
if (empty($headers) || !is_array($headers)) {
|
||||
error_log("Routine field_id 347: missing XLS headers from row 6.");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($excelData as $rowIndex => &$row) {
|
||||
if (!isset($row['data']) || !is_array($row['data'])) {
|
||||
error_log("Routine field_id 347: invalid row structure at index {$rowIndex}.");
|
||||
continue;
|
||||
}
|
||||
|
||||
$selectedValues = [];
|
||||
|
||||
/*
|
||||
* Check columns from P to AT.
|
||||
* If the cell contains x, take the related column header.
|
||||
*/
|
||||
for ($columnIndex = $startColumnIndex; $columnIndex <= $endColumnIndex; $columnIndex++) {
|
||||
$cellValue = strtolower(trim((string)($row['data'][$columnIndex] ?? '')));
|
||||
|
||||
if ($cellValue === 'x') {
|
||||
$headerTitle = trim((string)($headers[$columnIndex] ?? ''));
|
||||
|
||||
if ($headerTitle !== '') {
|
||||
$selectedValues[] = $headerTitle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Add free text from column AU.
|
||||
*/
|
||||
$extraText = '';
|
||||
|
||||
if (isset($row['data'][$extraColumnIndex])) {
|
||||
$extraText = trim((string)$row['data'][$extraColumnIndex]);
|
||||
} elseif (isset($row['data']['AU'])) {
|
||||
$extraText = trim((string)$row['data']['AU']);
|
||||
}
|
||||
|
||||
error_log(
|
||||
"Routine field_id 347: row " .
|
||||
($row['excelrow'] ?? $rowIndex) .
|
||||
" AU index {$extraColumnIndex} value: " .
|
||||
print_r($row['data'][$extraColumnIndex] ?? null, true) .
|
||||
" | AU key value: " .
|
||||
print_r($row['data']['AU'] ?? null, true)
|
||||
);
|
||||
|
||||
if ($extraText !== '') {
|
||||
$selectedValues[] = $extraText;
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove empty and duplicate values.
|
||||
*/
|
||||
$selectedValues = array_values(array_unique(array_filter($selectedValues, function ($value) {
|
||||
return trim((string)$value) !== '';
|
||||
})));
|
||||
|
||||
/*
|
||||
* Save final value into column P.
|
||||
* Column P must be mapped to field_id 347 in the template mapping.
|
||||
*/
|
||||
$row['data'][$targetColumnIndex] = implode(', ', $selectedValues);
|
||||
|
||||
error_log(
|
||||
"Routine field_id 347: row " .
|
||||
($row['excelrow'] ?? $rowIndex) .
|
||||
" generated value: " .
|
||||
$row['data'][$targetColumnIndex]
|
||||
);
|
||||
}
|
||||
|
||||
unset($row);
|
||||
|
||||
error_log("Routine field_id 347 completed.");
|
||||
}
|
||||
Reference in New Issue
Block a user