fixed update fornitore
This commit is contained in:
parent
1b23885659
commit
d39e997beb
@ -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(`<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>`);
|
||||
const value = String(s.idsupplier);
|
||||
sel.append(`<option value="${value}">${s.supplier_name}</option>`);
|
||||
});
|
||||
}
|
||||
|
||||
@ -669,6 +675,12 @@
|
||||
width: "100%",
|
||||
dropdownParent: $("#associaFornitoriModal")
|
||||
});
|
||||
|
||||
if (selectedId !== "") {
|
||||
sel.val(String(selectedId)).trigger("change");
|
||||
} else {
|
||||
sel.val("").trigger("change");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -694,6 +706,7 @@
|
||||
|
||||
data.rows.forEach(row => {
|
||||
const exp = row.expiry_date ? row.expiry_date : "";
|
||||
const supplierId = String(row.idsupplier ?? "");
|
||||
tbody.append(`
|
||||
<tr>
|
||||
<td>${row.id}</td>
|
||||
@ -703,16 +716,16 @@
|
||||
<td>${exp}</td>
|
||||
<td>${row.qty}</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-outline-secondary af-edit"
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary af-edit"
|
||||
data-id="${row.id}"
|
||||
data-idsupplier="${row.idsupplier}"
|
||||
data-idsupplier="${supplierId}"
|
||||
data-mix="${String(row.supplier_mix_name).replace(/"/g, '"')}"
|
||||
data-lot="${String(row.lot_code ?? '').replace(/"/g, '"')}"
|
||||
data-exp="${exp}"
|
||||
data-qty="${row.qty}">
|
||||
✏️
|
||||
</button>
|
||||
<button class="btn btn-sm btn-outline-danger af-del" data-id="${row.id}">🗑️</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-danger af-del" data-id="${row.id}">🗑️</button>
|
||||
</td>
|
||||
</tr>
|
||||
`);
|
||||
@ -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();
|
||||
|
||||
144
public/userarea/update_mescola_supplier_lot.php
Normal file
144
public/userarea/update_mescola_supplier_lot.php
Normal file
@ -0,0 +1,144 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
ini_set('display_errors', '0');
|
||||
error_reporting(E_ALL);
|
||||
|
||||
try {
|
||||
require_once __DIR__ . '/class/db-functions.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
echo json_encode([
|
||||
'success' => 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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user