diff --git a/public/userarea/mescole.php b/public/userarea/mescole.php
index 8200bc0..f84b753 100644
--- a/public/userarea/mescole.php
+++ b/public/userarea/mescole.php
@@ -650,17 +650,23 @@
$("#afCancelEdit").hide();
}
- function afLoadSuppliers() {
+ function afLoadSuppliers(selectedId = "") {
return fetch("get_suppliers.php")
.then(r => r.json())
.then(data => {
const sel = $("#afIdSupplier");
+
+ if (sel.hasClass("select2-hidden-accessible")) {
+ sel.select2("destroy");
+ }
+
sel.empty();
sel.append(``);
if (data.success && Array.isArray(data.rows)) {
data.rows.forEach(s => {
- sel.append(``);
+ const value = String(s.idsupplier);
+ sel.append(``);
});
}
@@ -669,6 +675,12 @@
width: "100%",
dropdownParent: $("#associaFornitoriModal")
});
+
+ if (selectedId !== "") {
+ sel.val(String(selectedId)).trigger("change");
+ } else {
+ sel.val("").trigger("change");
+ }
});
}
@@ -694,25 +706,26 @@
data.rows.forEach(row => {
const exp = row.expiry_date ? row.expiry_date : "";
+ const supplierId = String(row.idsupplier ?? "");
tbody.append(`
-
- | ${row.id} |
- ${row.supplier_name} |
- ${row.supplier_mix_name} |
- ${row.lot_code ?? ""} |
- ${exp} |
- ${row.qty} |
-
-
-
+ |
+ | ${row.id} |
+ ${row.supplier_name} |
+ ${row.supplier_mix_name} |
+ ${row.lot_code ?? ""} |
+ ${exp} |
+ ${row.qty} |
+
+
+
|
`);
@@ -728,19 +741,30 @@
$("#afIdMescola").val(idMescola);
$("#afNomeUscita").text(nomeUscita);
- $("#associaFornitoriModal").modal("show");
afResetForm();
- afLoadSuppliers().then(() => afLoadRows(idMescola));
+
+ afLoadSuppliers("")
+ .then(() => {
+ $("#associaFornitoriModal").modal("show");
+ 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"));
+ const editId = $(this).attr("data-id");
+ const supplierId = $(this).attr("data-idsupplier");
+ const mix = $(this).attr("data-mix");
+ const lot = $(this).attr("data-lot");
+ const exp = $(this).attr("data-exp");
+ const qty = $(this).attr("data-qty");
+
+ $("#afEditId").val(editId);
+ $("#afIdSupplier").val(String(supplierId)).trigger("change");
+ $("#afSupplierMixName").val(mix);
+ $("#afLotCode").val(lot);
+ $("#afExpiryDate").val(exp);
+ $("#afQty").val(qty);
$("#afSaveBtn").text("💾 Salva Modifica");
$("#afCancelEdit").show();
@@ -757,7 +781,7 @@
const idmescola = $("#afIdMescola").val();
const editId = $("#afEditId").val();
- const idsupplier = $("#afIdSupplier").val();
+ const idsupplier = String($("#afIdSupplier").val() || "").trim();
const supplier_mix_name = $("#afSupplierMixName").val().trim();
const lot_code = $("#afLotCode").val().trim();
const expiry_date = $("#afExpiryDate").val();
diff --git a/public/userarea/update_mescola_supplier_lot.php b/public/userarea/update_mescola_supplier_lot.php
new file mode 100644
index 0000000..368a72c
--- /dev/null
+++ b/public/userarea/update_mescola_supplier_lot.php
@@ -0,0 +1,144 @@
+ false,
+ 'message' => 'Metodo non consentito'
+ ]);
+ exit;
+ }
+
+ $id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
+ $idmescola = isset($_POST['idmescola']) ? (int)$_POST['idmescola'] : 0;
+ $idsupplier = isset($_POST['idsupplier']) ? (int)$_POST['idsupplier'] : 0;
+ $supplier_mix_name = trim($_POST['supplier_mix_name'] ?? '');
+ $lot_code = trim($_POST['lot_code'] ?? '');
+ $expiry_date = trim($_POST['expiry_date'] ?? '');
+ $qty = isset($_POST['qty']) ? str_replace(',', '.', trim((string)$_POST['qty'])) : '0';
+
+ if ($id <= 0) {
+ echo json_encode([
+ 'success' => false,
+ 'message' => 'ID record non valido'
+ ]);
+ exit;
+ }
+
+ if ($idmescola <= 0) {
+ echo json_encode([
+ 'success' => false,
+ 'message' => 'ID mescola non valido'
+ ]);
+ exit;
+ }
+
+ if ($idsupplier <= 0) {
+ echo json_encode([
+ 'success' => false,
+ 'message' => 'Fornitore obbligatorio'
+ ]);
+ exit;
+ }
+
+ if ($supplier_mix_name === '') {
+ echo json_encode([
+ 'success' => false,
+ 'message' => 'Il nome mescola fornitore è obbligatorio'
+ ]);
+ exit;
+ }
+
+ if ($qty === '' || !is_numeric($qty)) {
+ echo json_encode([
+ 'success' => false,
+ 'message' => 'Quantità non valida'
+ ]);
+ exit;
+ }
+
+ $qty = (float)$qty;
+
+ if ($expiry_date === '') {
+ $expiry_date = null;
+ } else {
+ $dt = DateTime::createFromFormat('Y-m-d', $expiry_date);
+ if (!$dt || $dt->format('Y-m-d') !== $expiry_date) {
+ echo json_encode([
+ 'success' => false,
+ 'message' => 'Data scadenza non valida'
+ ]);
+ exit;
+ }
+ }
+
+ $db = DBHandlerSelect::getInstance();
+ $pdo = $db->getConnection();
+
+ // Verifica che il record esista
+ $check = $pdo->prepare("
+ SELECT id
+ FROM mescole_supplier_lots
+ WHERE id = ?
+ LIMIT 1
+ ");
+ $check->execute([$id]);
+
+ if (!$check->fetch(PDO::FETCH_ASSOC)) {
+ echo json_encode([
+ 'success' => false,
+ 'message' => 'Record non trovato'
+ ]);
+ exit;
+ }
+
+ $sql = "
+ UPDATE mescole_supplier_lots
+ SET
+ idmescola = :idmescola,
+ idsupplier = :idsupplier,
+ supplier_mix_name = :supplier_mix_name,
+ lot_code = :lot_code,
+ expiry_date = :expiry_date,
+ qty = :qty
+ WHERE id = :id
+ LIMIT 1
+ ";
+
+ $stmt = $pdo->prepare($sql);
+ $ok = $stmt->execute([
+ ':idmescola' => $idmescola,
+ ':idsupplier' => $idsupplier,
+ ':supplier_mix_name' => $supplier_mix_name,
+ ':lot_code' => $lot_code !== '' ? $lot_code : null,
+ ':expiry_date' => $expiry_date,
+ ':qty' => $qty,
+ ':id' => $id
+ ]);
+
+ if ($ok) {
+ echo json_encode([
+ 'success' => true,
+ 'message' => 'Record aggiornato correttamente'
+ ]);
+ exit;
+ }
+
+ echo json_encode([
+ 'success' => false,
+ 'message' => 'Aggiornamento non riuscito'
+ ]);
+ exit;
+} catch (Throwable $e) {
+ echo json_encode([
+ 'success' => false,
+ 'message' => 'Errore server: ' . $e->getMessage()
+ ]);
+ exit;
+}