fixed product line view 2 for new

This commit is contained in:
2026-09-09 18:14:02 +02:00
parent da5c4b8777
commit 4241d8e62b
+110
View File
@@ -410,6 +410,39 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['create_quick'])) {
}
// --- AJAX: mescole collegate a una matrice ---
if (!empty($_GET['mescole_by_matrice'])) {
header('Content-Type: application/json; charset=utf-8');
$idMatrice = (int)($_GET['id_matrice'] ?? 0);
if ($idMatrice <= 0) {
echo json_encode(['success' => false, 'rows' => []]);
exit;
}
$stmt = $pdo->prepare("
SELECT me.id, me.nome
FROM matrici_mescole mm
JOIN mescole me ON me.id = mm.idmescola
WHERE mm.idmatrice = :idm AND me.is_active = 1
ORDER BY me.nome
");
$stmt->execute(['idm' => $idMatrice]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Linee associate a questa matrice
$stmtL = $pdo->prepare("
SELECT ml.idlinea AS id
FROM matrici_lines ml
WHERE ml.idmatrice = :idm
");
$stmtL->execute(['idm' => $idMatrice]);
$lineIds = array_map('intval', $stmtL->fetchAll(PDO::FETCH_COLUMN));
echo json_encode(['success' => true, 'rows' => $rows, 'lines' => $lineIds]);
exit;
}
// --- AJAX PRINCIPALE: carica i record della data selezionata ---
if (!empty($_GET['ajax'])) {
$lineRaw = $_GET['line'] ?? '';
@@ -2455,11 +2488,88 @@ if (!empty($_GET['ajax'])) {
width: '100%',
closeOnSelect: false
});
$('#qcLinea').select2({
dropdownParent: $('#quickCreateModal .modal-content'),
placeholder: 'Seleziona linea...',
allowClear: true,
width: '100%'
});
// Salvo l'HTML originale delle linee per poterlo ricostruire ad ogni cambio matrice
if (!window._qcLineaOriginalHtml) {
window._qcLineaOriginalHtml = $('#qcLinea').html();
}
// Ricostruisce il select linea raggruppando le associate in cima
function rebuildLineSelect(associatedIds) {
const current = $('#qcLinea').val();
const assocSet = new Set((associatedIds || []).map(String));
// Ripristino tutte le option originali
const $tmp = $('<select>').html(window._qcLineaOriginalHtml);
const allOptions = [];
$tmp.find('option').each(function() {
const v = $(this).val();
if (v === '') return; // scarto il placeholder
allOptions.push({
value: v,
label: $(this).text()
});
});
let html = '<option value="">Seleziona linea</option>';
const assoc = allOptions.filter(o => assocSet.has(String(o.value)));
const others = allOptions.filter(o => !assocSet.has(String(o.value)));
if (assoc.length) {
html += '<optgroup label="⭐ Consigliate per questa matrice">';
assoc.forEach(o => html += `<option value="${o.value}">⭐ ${o.label}</option>`);
html += '</optgroup>';
}
if (others.length) {
html += '<optgroup label="Altre linee">';
others.forEach(o => html += `<option value="${o.value}">${o.label}</option>`);
html += '</optgroup>';
}
$('#qcLinea').html(html);
// Preseleziono se è associata UNA sola linea, altrimenti mantengo la scelta corrente
if (assoc.length === 1) {
$('#qcLinea').val(String(assoc[0].value));
} else {
$('#qcLinea').val(current || '');
}
$('#qcLinea').trigger('change.select2');
}
// Quando cambio matrice, precarico mescole e riordino le linee
$('#qcMatrice').off('change.autoMescole').on('change.autoMescole', function() {
const idMatrice = $(this).val();
if (!idMatrice) {
$('#qcMescola').val(null).trigger('change');
rebuildLineSelect([]);
return;
}
$.getJSON('', {
mescole_by_matrice: 1,
id_matrice: idMatrice
}, function(r) {
if (r.success && Array.isArray(r.rows)) {
const ids = r.rows.map(m => String(m.id));
$('#qcMescola').val(ids).trigger('change');
}
rebuildLineSelect(r.lines || []);
});
});
}
function destroyQuickSelects() {
if ($('#qcMatrice').data('select2')) $('#qcMatrice').select2('destroy');
if ($('#qcMescola').data('select2')) $('#qcMescola').select2('destroy');
if ($('#qcLinea').data('select2')) $('#qcLinea').select2('destroy');
}
function closeQuickModal() {