zibo-dashboard/public/userarea/warehouse_mescole.php

378 lines
15 KiB
PHP

<?php include('include/headscript.php'); ?>
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
<?php include('cssinclude.php'); ?>
<title>Magazzino Mescole - <?= htmlspecialchars($titlewebsite, ENT_QUOTES, 'UTF-8'); ?></title>
<!-- jQuery e Bootstrap -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<!-- DataTables -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<style>
body {
font-size: 1.05rem;
background: #f8fafc;
}
.card {
border-radius: 16px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
.back-dashboard {
background-color: #cfe3ff !important;
color: #1f2d3d !important;
border: 1px solid #bcd4f4 !important;
border-radius: 10px;
font-weight: 600;
font-size: 1rem;
padding: 10px 18px;
box-shadow: 0 3px 8px rgba(0, 0, 0, 0.1);
transition: all 0.2s ease-in-out;
}
.back-dashboard:hover {
background-color: #b9d3ff !important;
transform: translateY(-2px);
}
.table thead {
background-color: #cfe3ff;
color: #1f2d3d;
}
/* Tabella leggibile e colonne fisse */
#tabWarehouseMescole {
table-layout: fixed;
width: 100% !important;
}
#tabWarehouseMescole th,
#tabWarehouseMescole td {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
vertical-align: middle;
}
/* Colonne: stringiamo ID e quantità */
#tabWarehouseMescole th:nth-child(1),
#tabWarehouseMescole td:nth-child(1) {
width: 60px;
max-width: 60px;
}
#tabWarehouseMescole th:nth-child(6),
#tabWarehouseMescole td:nth-child(6) {
width: 120px;
max-width: 120px;
}
#tabWarehouseMescole th:nth-child(7),
#tabWarehouseMescole td:nth-child(7) {
width: 120px;
max-width: 120px;
}
#tabWarehouseMescole th:nth-child(8),
#tabWarehouseMescole td:nth-child(8) {
width: 110px;
max-width: 110px;
}
/* QTY input compatto */
.qty-input {
width: 95px;
text-align: right;
font-weight: 700;
}
/* Riga scaduta */
tr.expired {
background: #ffe5e5 !important;
}
/* Riga inattiva (mescola disattiva) */
tr.inactive-mix {
opacity: 0.65;
}
</style>
</head>
<body>
<div class="wrapper toggled">
<?php include('include/navbar.php'); ?>
<?php include('include/topbar.php'); ?>
<div class="page-wrapper">
<div class="page-content">
<div class="card p-3">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">Magazzino - Mescole</h5>
<div class="d-flex gap-2">
<button type="button" class="btn back-dashboard" onclick="location.href='warehouse_dashboard.php'">
↩️ Torna a Magazzino
</button>
</div>
</div>
<div class="card-body">
<!-- FILTRI RAPIDI -->
<div class="d-flex flex-wrap align-items-center justify-content-between gap-2 mb-3">
<div class="d-flex align-items-center gap-2">
<label class="fw-semibold mb-0">Filtro mescole:</label>
<select id="filterActive" class="form-select form-select-sm" style="width:220px;">
<option value="all">Tutte (attive + inattive)</option>
<option value="1" selected>Solo attive</option>
<option value="0">Solo inattive</option>
</select>
<div class="form-check ms-2">
<input class="form-check-input" type="checkbox" id="onlyExpired">
<label class="form-check-label" for="onlyExpired">Solo scadute</label>
</div>
</div>
<div class="text-muted small">
Suggerimento: usa la ricerca tabella per trovare lotto/fornitore
</div>
</div>
<?php
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// filtro mescole attive/inattive
$activeFilter = $_GET['active'] ?? '1';
if (!in_array($activeFilter, ['all', '0', '1'], true)) {
$activeFilter = '1';
}
$where = "";
$params = [];
if ($activeFilter !== 'all') {
$where = "WHERE m.is_active = ?";
$params[] = (int)$activeFilter;
}
// Query: tutte le righe lotto-fornitore con nome uscita
$sql = "
SELECT
msl.id AS lot_id,
m.id AS idmescola,
m.nomeuscita,
m.is_active,
s.supplier_name,
msl.supplier_mix_name,
msl.lot_code,
msl.expiry_date,
msl.qty
FROM mescole_supplier_lots msl
INNER JOIN mescole m ON m.id = msl.idmescola
INNER JOIN suppliers s ON s.idsupplier = msl.idsupplier
$where
ORDER BY m.nomeuscita ASC, s.supplier_name ASC, msl.lot_code ASC, msl.id DESC
";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$today = date('Y-m-d');
?>
<div class="table-responsive">
<table id="tabWarehouseMescole" class="table table-striped align-middle text-center" style="width:100%;">
<thead>
<tr>
<th>ID</th>
<th>Nome Uscita</th>
<th>Fornitore</th>
<th>Nome Fornitore</th>
<th>Lotto</th>
<th>Scadenza</th>
<th>Q.tà</th>
<th>Salva</th>
</tr>
</thead>
<tbody>
<?php
if (!$rows) {
echo "<tr>
<td class='text-muted'>-</td>
<td class='text-muted'>Nessuna riga 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>
<td class='text-muted'>-</td>
</tr>";
} else {
foreach ($rows as $r) {
$expiry = $r['expiry_date'] ?? '';
$isExpired = ($expiry !== '' && $expiry < $today);
$isActiveMix = ((int)$r['is_active'] === 1);
$trClass = [];
if ($isExpired) $trClass[] = 'expired';
if (!$isActiveMix) $trClass[] = 'inactive-mix';
$trClassStr = $trClass ? " class='" . implode(' ', $trClass) . "'" : "";
$qtyVal = number_format((float)$r['qty'], 3, '.', '');
$expiryShow = $expiry ? htmlspecialchars($expiry) : '<span class="text-muted">-</span>';
echo "<tr{$trClassStr}
data-lot-id='{$r['lot_id']}'
data-expired='" . ($isExpired ? "1" : "0") . "'>
<td>{$r['lot_id']}</td>
<td>" . htmlspecialchars($r['nomeuscita']) . "</td>
<td>" . htmlspecialchars($r['supplier_name']) . "</td>
<td>" . htmlspecialchars($r['supplier_mix_name']) . "</td>
<td>" . htmlspecialchars($r['lot_code'] ?? '') . "</td>
<td>{$expiryShow}</td>
<td>
<input type='number' step='0.001' class='form-control form-control-sm qty-input qty-field'
value='{$qtyVal}' />
</td>
<td>
<button class='btn btn-sm btn-outline-primary save-qty'>💾</button>
</td>
</tr>";
}
}
?>
</tbody>
</table>
</div>
<div class="text-muted small mt-2">
* Riga rossa = scaduta. * Riga opaca = mescola disattiva (ma lotto ancora presente).
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script>
let dt;
$(document).ready(function() {
// init dropdown from URL
const urlParams = new URLSearchParams(window.location.search);
const activeParam = urlParams.get('active') || '1';
$('#filterActive').val(activeParam);
dt = $('#tabWarehouseMescole').DataTable({
order: [
[1, 'asc']
],
pageLength: 50,
language: {
url: 'https://cdn.datatables.net/plug-ins/1.13.6/i18n/it-IT.json'
}
});
// filtro attive/inattive: reload server-side semplice
$('#filterActive').on('change', function() {
const v = $(this).val();
const url = new URL(window.location.href);
url.searchParams.set('active', v);
window.location.href = url.toString();
});
// filtro "solo scadute" client-side
$('#onlyExpired').on('change', function() {
const only = $(this).is(':checked');
if (!only) {
dt.rows().every(function() {
$(this.node()).show();
});
return;
}
dt.rows().every(function() {
const node = $(this.node());
const expired = node.attr('data-expired') === '1';
if (expired) node.show();
else node.hide();
});
});
});
// SALVA QTY (per riga)
$(document).on('click', '.save-qty', function() {
const tr = $(this).closest('tr');
const lotId = tr.data('lot-id');
const qty = tr.find('.qty-field').val();
fetch('update_mescola_lot_qty.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `id=${encodeURIComponent(lotId)}&qty=${encodeURIComponent(qty)}`
})
.then(async (r) => {
const text = await r.text();
try {
return JSON.parse(text);
} catch (e) {
throw new Error("Risposta non JSON:\n" + text);
}
})
.then(data => {
if (data.success) {
Swal.fire({
icon: "success",
title: "Salvato",
timer: 900,
showConfirmButton: false
});
} else {
Swal.fire({
icon: "error",
title: "Errore",
text: data.message || "Salvataggio non riuscito"
});
}
})
.catch(err => {
Swal.fire({
icon: "error",
title: "Errore (fetch)",
text: err.message
});
});
});
// SALVA con Enter dentro il campo qty
$(document).on('keypress', '.qty-field', function(e) {
if (e.which === 13) {
e.preventDefault();
$(this).closest('tr').find('.save-qty').click();
}
});
</script>
</body>
</html>