mescole update

This commit is contained in:
2026-03-06 08:42:18 +01:00
parent 22e5b90fe4
commit 245750f057
10 changed files with 970 additions and 89 deletions
+348 -50
View File
@@ -83,7 +83,6 @@
/* --- FIX colonne lunghe: tronca con ellissi --- */
#tabellaMescole {
table-layout: fixed;
/* fondamentale: rende fisse le larghezze */
width: 100% !important;
}
@@ -92,37 +91,42 @@
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
/* una riga sola */
}
/* Larghezze consigliate (aggiusta se vuoi) */
#tabellaMescole th:nth-child(2),
#tabellaMescole td:nth-child(2) {
/* Nome Mescola */
width: 260px;
max-width: 260px;
}
#tabellaMescole th:nth-child(3),
#tabellaMescole td:nth-child(3) {
/* Nome Uscita */
width: 260px;
max-width: 260px;
}
/* facoltativo: azioni sempre leggibili */
#tabellaMescole th:nth-child(5),
#tabellaMescole td:nth-child(5) {
width: 230px;
max-width: 230px;
}
/* ID sempre piccolo */
/* ID */
#tabellaMescole th:nth-child(1),
#tabellaMescole td:nth-child(1) {
width: 70px;
max-width: 70px;
}
/* Nome Uscita */
#tabellaMescole th:nth-child(2),
#tabellaMescole td:nth-child(2) {
width: 360px;
max-width: 360px;
}
/* Q.tà totale */
#tabellaMescole th:nth-child(3),
#tabellaMescole td:nth-child(3) {
width: 140px;
max-width: 140px;
}
/* Linee */
#tabellaMescole th:nth-child(4),
#tabellaMescole td:nth-child(4) {
width: 260px;
max-width: 260px;
}
/* Azioni */
#tabellaMescole th:nth-child(5),
#tabellaMescole td:nth-child(5) {
width: 330px;
max-width: 330px;
}
</style>
</head>
@@ -153,8 +157,8 @@
<thead>
<tr>
<th>ID</th>
<th>Nome Mescola</th>
<th>Nome Uscita</th>
<th>Q. Totale</th>
<th>Linee Associate</th>
<th>Azioni</th>
</tr>
@@ -163,37 +167,61 @@
<?php
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// QTY IN SUBQUERY per evitare raddoppi dovuti alle linee
$sql = "
SELECT m.id, m.nome, m.nomeuscita,
GROUP_CONCAT(pl.name SEPARATOR ', ') AS linee
FROM mescole m
LEFT JOIN mescole_lines ml ON m.id = ml.idmescola
LEFT JOIN production_lines pl ON ml.idlinea = pl.id
GROUP BY m.id
ORDER BY m.id DESC";
SELECT
m.id,
m.nomeuscita,
IFNULL(q.qty_totale, 0) AS qty_totale,
GROUP_CONCAT(DISTINCT pl.name SEPARATOR ', ') AS linee
FROM mescole m
LEFT JOIN (
SELECT idmescola, SUM(qty) AS qty_totale
FROM mescole_supplier_lots
GROUP BY idmescola
) q ON q.idmescola = m.id
LEFT JOIN mescole_lines ml ON m.id = ml.idmescola
LEFT JOIN production_lines pl ON ml.idlinea = pl.id
GROUP BY m.id
ORDER BY m.id DESC
";
$stmt = $pdo->query($sql);
if ($stmt->rowCount() === 0) {
echo "<tr><td colspan='5' class='text-muted'>Nessuna mescola presente</td></tr>";
// DataTables-friendly: 5 TD reali
echo "<tr>
<td class='text-muted'>-</td>
<td class='text-muted'>Nessuna mescola presente</td>
<td class='text-muted'>-</td>
<td class='text-muted'>-</td>
<td class='text-muted'>-</td>
</tr>";
} else {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$linee = $row['linee'] ? htmlspecialchars($row['linee']) : '<span class="text-muted">Nessuna</span>';
$qtyTot = number_format((float)$row['qty_totale'], 3, ',', '.');
echo "<tr>
<td>{$row['id']}</td>
<td>" . htmlspecialchars($row['nome']) . "</td>
<td>" . htmlspecialchars($row['nomeuscita']) . "</td>
<td><span class='fw-semibold'>{$qtyTot}</span></td>
<td>{$linee}</td>
<td>
<button class='btn btn-sm btn-outline-primary associa-linee'
<button class='btn btn-sm btn-outline-dark associa-fornitori'
data-id='{$row['id']}'
data-nome='" . htmlspecialchars($row['nome'], ENT_QUOTES) . "'>
⚙️ Associa Linee
data-nomeuscita='" . htmlspecialchars($row['nomeuscita'], ENT_QUOTES) . "'>
🧾 Fornitori/Lotti
</button>
<button class='btn btn-sm btn-outline-primary associa-linee'
data-id='{$row['id']}'>
⚙️ Linee
</button>
<button class='btn btn-sm btn-outline-secondary edit-mescola'
data-id='{$row['id']}'
data-nome='" . htmlspecialchars($row['nome'], ENT_QUOTES) . "'
data-nomeuscita='" . htmlspecialchars($row['nomeuscita'], ENT_QUOTES) . "'>
✏️ Modifica
</button>
@@ -226,8 +254,8 @@
<div class="modal-body">
<form id="addMescolaForm">
<div class="mb-3">
<label class="form-label fw-semibold">Nome Mescola</label>
<input type="text" class="form-control" id="nomeMescola" required>
<label class="form-label fw-semibold">Nome Mescola (interno)</label>
<input type="text" class="form-control" id="nomeMescola" placeholder="opzionale / interno">
</div>
<div class="mb-3">
@@ -259,8 +287,8 @@
<input type="hidden" id="editIdMescola">
<div class="mb-3">
<label class="form-label fw-semibold">Nome Mescola</label>
<input type="text" class="form-control" id="editNomeMescola" required>
<label class="form-label fw-semibold">Nome Mescola (interno)</label>
<input type="text" class="form-control" id="editNomeMescola" placeholder="opzionale / interno">
</div>
<div class="mb-3">
@@ -305,6 +333,68 @@
</div>
</div>
<!-- MODALE ASSOCIA FORNITORI / LOTTI -->
<div class="modal fade" id="associaFornitoriModal" tabindex="-1">
<div class="modal-dialog modal-xl modal-dialog-centered">
<div class="modal-content">
<div class="modal-header" style="background-color:#cfe3ff;">
<h5 class="modal-title">Fornitori / Lotti - <span id="afNomeUscita"></span></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" id="afIdMescola">
<input type="hidden" id="afEditId">
<div class="row g-2 align-items-end mb-3">
<div class="col-md-3">
<label class="form-label fw-semibold">Fornitore</label>
<select class="form-select" id="afIdSupplier" style="width:100%;"></select>
</div>
<div class="col-md-3">
<label class="form-label fw-semibold">Nome mescola fornitore</label>
<input type="text" class="form-control" id="afSupplierMixName" placeholder="Nome specifico fornitore">
</div>
<div class="col-md-2">
<label class="form-label fw-semibold">Lotto</label>
<input type="text" class="form-control" id="afLotCode" placeholder="LOT-...">
</div>
<div class="col-md-2">
<label class="form-label fw-semibold">Scadenza</label>
<input type="date" class="form-control" id="afExpiryDate">
</div>
<div class="col-md-2">
<label class="form-label fw-semibold">Q.</label>
<input type="number" step="0.001" class="form-control" id="afQty" value="0">
</div>
<div class="col-md-12 text-end">
<button class="btn btn-add" id="afSaveBtn"> Aggiungi Riga</button>
<button class="btn btn-outline-secondary ms-2" id="afCancelEdit" type="button" style="display:none;">Annulla modifica</button>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped align-middle text-center" id="afTable">
<thead class="table-light">
<tr>
<th>ID</th>
<th>Fornitore</th>
<th>Nome mescola fornitore</th>
<th>Lotto</th>
<th>Scadenza</th>
<th>Q.</th>
<th>Azioni</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php include('jsinclude.php'); ?>
<script>
@@ -348,7 +438,7 @@
Swal.fire({
icon: "error",
title: "Errore",
text: data.message
text: data.message || "Errore salvataggio"
});
}
});
@@ -357,8 +447,9 @@
/* -------- APERTURA MODALE EDIT -------- */
$(document).on("click", ".edit-mescola", function() {
$("#editIdMescola").val($(this).data("id"));
$("#editNomeMescola").val($(this).data("nome"));
$("#editNomeUscita").val($(this).data("nomeuscita"));
// nome interno non lo abbiamo in tabella: lo lasciamo vuoto o lo recuperi se vuoi via endpoint
$("#editNomeMescola").val("");
$("#editMescolaModal").modal("show");
});
@@ -375,7 +466,7 @@
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: `id=${id}&nome=${encodeURIComponent(nome)}&nomeuscita=${encodeURIComponent(nomeuscita)}`
body: `id=${encodeURIComponent(id)}&nome=${encodeURIComponent(nome)}&nomeuscita=${encodeURIComponent(nomeuscita)}`
})
.then(r => r.json())
.then(data => {
@@ -390,7 +481,7 @@
Swal.fire({
icon: "error",
title: "Errore",
text: data.message
text: data.message || "Errore aggiornamento"
});
}
});
@@ -398,11 +489,10 @@
/* -------- MODALE ASSOCIA LINEE -------- */
$(document).on("click", ".associa-linee", function() {
const idMescola = $(this).data("id");
$("#idMescolaLinee").val(idMescola);
fetch("get_linee_mescola.php?id=" + idMescola)
fetch("get_linee_mescola.php?id=" + encodeURIComponent(idMescola))
.then(r => r.json())
.then(data => {
const select = $("#lineeSelect");
@@ -451,11 +541,219 @@
Swal.fire({
icon: "error",
title: "Errore",
text: data.message
text: data.message || "Errore salvataggio"
});
}
});
});
// ============================
// FORNITORI / LOTTI
// ============================
function afResetForm() {
$("#afEditId").val("");
$("#afIdSupplier").val("").trigger("change");
$("#afSupplierMixName").val("");
$("#afLotCode").val("");
$("#afExpiryDate").val("");
$("#afQty").val("0");
$("#afSaveBtn").text(" Aggiungi Riga");
$("#afCancelEdit").hide();
}
function afLoadSuppliers() {
return fetch("get_suppliers.php")
.then(r => r.json())
.then(data => {
const sel = $("#afIdSupplier");
sel.empty();
sel.append(`<option value="">Seleziona...</option>`);
if (data.success && Array.isArray(data.rows)) {
data.rows.forEach(s => {
sel.append(`<option value="${s.idsupplier}">${s.supplier_name}</option>`);
});
}
sel.select2({
theme: "bootstrap-5",
width: "100%",
dropdownParent: $("#associaFornitoriModal")
});
});
}
function afLoadRows(idMescola) {
fetch("get_mescola_supplier_lots.php?id=" + encodeURIComponent(idMescola))
.then(r => r.json())
.then(data => {
const tbody = $("#afTable tbody");
tbody.empty();
if (!data.success || !Array.isArray(data.rows) || data.rows.length === 0) {
tbody.append(`<tr>
<td class="text-muted">-</td>
<td class="text-muted">Nessuna associazione presente</td>
<td class="text-muted">-</td>
<td class="text-muted">-</td>
<td class="text-muted">-</td>
<td class="text-muted">-</td>
<td class="text-muted">-</td>
</tr>`);
return;
}
data.rows.forEach(row => {
const exp = row.expiry_date ? row.expiry_date : "";
tbody.append(`
<tr>
<td>${row.id}</td>
<td>${row.supplier_name}</td>
<td>${row.supplier_mix_name}</td>
<td>${row.lot_code ?? ""}</td>
<td>${exp}</td>
<td>${row.qty}</td>
<td>
<button class="btn btn-sm btn-outline-secondary af-edit"
data-id="${row.id}"
data-idsupplier="${row.idsupplier}"
data-mix="${String(row.supplier_mix_name).replace(/"/g, '&quot;')}"
data-lot="${String(row.lot_code ?? '').replace(/"/g, '&quot;')}"
data-exp="${exp}"
data-qty="${row.qty}">
✏️
</button>
<button class="btn btn-sm btn-outline-danger af-del" data-id="${row.id}">🗑️</button>
</td>
</tr>
`);
});
});
}
// Open modal
$(document).on("click", ".associa-fornitori", function() {
const idMescola = $(this).data("id");
const nomeUscita = $(this).data("nomeuscita");
$("#afIdMescola").val(idMescola);
$("#afNomeUscita").text(nomeUscita);
$("#associaFornitoriModal").modal("show");
afResetForm();
afLoadSuppliers().then(() => afLoadRows(idMescola));
});
// Edit row in modal
$(document).on("click", ".af-edit", function() {
$("#afEditId").val($(this).data("id"));
$("#afIdSupplier").val(String($(this).data("idsupplier"))).trigger("change");
$("#afSupplierMixName").val($(this).data("mix"));
$("#afLotCode").val($(this).data("lot"));
$("#afExpiryDate").val($(this).data("exp"));
$("#afQty").val($(this).data("qty"));
$("#afSaveBtn").text("💾 Salva Modifica");
$("#afCancelEdit").show();
});
$("#afCancelEdit").on("click", function() {
afResetForm();
});
// Save (insert/update)
$("#afSaveBtn").on("click", function(e) {
e.preventDefault();
const idmescola = $("#afIdMescola").val();
const editId = $("#afEditId").val();
const idsupplier = $("#afIdSupplier").val();
const supplier_mix_name = $("#afSupplierMixName").val().trim();
const lot_code = $("#afLotCode").val().trim();
const expiry_date = $("#afExpiryDate").val();
const qty = $("#afQty").val();
if (!idsupplier || !supplier_mix_name) {
Swal.fire({
icon: "warning",
title: "Attenzione",
text: "Fornitore e Nome mescola fornitore sono obbligatori"
});
return;
}
const actionUrl = editId ? "update_mescola_supplier_lot.php" : "save_mescola_supplier_lot.php";
const body =
(editId ? `id=${encodeURIComponent(editId)}&` : ``) +
`idmescola=${encodeURIComponent(idmescola)}` +
`&idsupplier=${encodeURIComponent(idsupplier)}` +
`&supplier_mix_name=${encodeURIComponent(supplier_mix_name)}` +
`&lot_code=${encodeURIComponent(lot_code)}` +
`&expiry_date=${encodeURIComponent(expiry_date)}` +
`&qty=${encodeURIComponent(qty)}`;
fetch(actionUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body
})
.then(r => r.json())
.then(data => {
if (data.success) {
afResetForm();
afLoadRows(idmescola);
// se vuoi aggiornare anche la Q.tà Totale live senza reload: lo facciamo dopo
} else {
Swal.fire({
icon: "error",
title: "Errore",
text: data.message || "Operazione non riuscita"
});
}
});
});
// Delete
$(document).on("click", ".af-del", function() {
const id = $(this).data("id");
const idmescola = $("#afIdMescola").val();
Swal.fire({
title: "Eliminare la riga?",
icon: "warning",
showCancelButton: true,
confirmButtonText: "Sì, elimina",
cancelButtonText: "Annulla",
confirmButtonColor: "#d33"
}).then((res) => {
if (!res.isConfirmed) return;
fetch("delete_mescola_supplier_lot.php", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: `id=${encodeURIComponent(id)}`
})
.then(r => r.json())
.then(data => {
if (data.success) {
afLoadRows(idmescola);
} else {
Swal.fire({
icon: "error",
title: "Errore",
text: data.message || "Cancellazione non riuscita"
});
}
});
});
});
</script>
</body>