zibo-dashboard/public/userarea/warehouse_imballaggi.php

409 lines
16 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 Imballaggi - <?= 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;
}
#tabWarehousePack {
table-layout: fixed;
width: 100% !important;
}
#tabWarehousePack th,
#tabWarehousePack td {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
vertical-align: middle;
}
/* ID */
#tabWarehousePack th:nth-child(1),
#tabWarehousePack td:nth-child(1) {
width: 60px;
max-width: 60px;
}
/* Codice */
#tabWarehousePack th:nth-child(4),
#tabWarehousePack td:nth-child(4) {
width: 120px;
max-width: 120px;
}
/* Scadenza */
#tabWarehousePack th:nth-child(7),
#tabWarehousePack td:nth-child(7) {
width: 120px;
max-width: 120px;
}
/* QTY */
#tabWarehousePack th:nth-child(8),
#tabWarehousePack td:nth-child(8) {
width: 120px;
max-width: 120px;
}
/* Salva */
#tabWarehousePack th:nth-child(9),
#tabWarehousePack td:nth-child(9) {
width: 90px;
max-width: 90px;
}
.qty-input {
width: 95px;
text-align: right;
font-weight: 700;
}
tr.expired {
background: #ffe5e5 !important;
}
tr.inactive-item {
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 - Imballaggi</h5>
<button type="button" class="btn back-dashboard" onclick="location.href='warehouse_dashboard.php'">
↩️ Torna a Magazzino
</button>
</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 flex-wrap align-items-center gap-2">
<label class="fw-semibold mb-0">Categoria:</label>
<select id="filterCategory" class="form-select form-select-sm" style="width:240px;">
<option value="all">Tutte</option>
<option value="PACKAGING_TYPE">Tipo Confezione</option>
<option value="BOX">Scatole</option>
<option value="PALLET">Pallet</option>
<option value="OTHER">Altro</option>
</select>
<label class="fw-semibold mb-0 ms-2">Stato:</label>
<select id="filterActive" class="form-select form-select-sm" style="width:220px;">
<option value="all">Tutti</option>
<option value="1" selected>Solo Attivi</option>
<option value="0">Solo Inattivi</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 scaduti</label>
</div>
</div>
<div class="text-muted small">
Cerca per codice, nome, fornitore o lotto
</div>
</div>
<?php
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
$cat = $_GET['cat'] ?? 'all';
$allowedCats = ['all', 'PACKAGING_TYPE', 'BOX', 'PALLET', 'OTHER'];
if (!in_array($cat, $allowedCats, true)) $cat = 'all';
$active = $_GET['active'] ?? '1';
if (!in_array($active, ['all', '0', '1'], true)) $active = '1';
$where = [];
$params = [];
if ($cat !== 'all') {
$where[] = "pi.category = ?";
$params[] = $cat;
}
if ($active !== 'all') {
$where[] = "pi.is_active = ?";
$params[] = (int)$active;
}
$whereSql = $where ? ("WHERE " . implode(" AND ", $where)) : "";
$sql = "
SELECT
psl.id AS stock_id,
pi.id AS item_id,
pi.category,
pi.item_name,
pi.item_code,
pi.is_active,
s.supplier_name,
psl.lot_code,
psl.expiry_date,
psl.qty
FROM packaging_stock_lots psl
INNER JOIN packaging_items pi ON pi.id = psl.idpackaging_item
INNER JOIN suppliers s ON s.idsupplier = psl.idsupplier
$whereSql
ORDER BY pi.category ASC, pi.item_name ASC, s.supplier_name ASC, psl.lot_code ASC, psl.id DESC
";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$today = date('Y-m-d');
function catLabel($c)
{
return match ($c) {
'PACKAGING_TYPE' => 'Tipo Confezione',
'BOX' => 'Scatola',
'PALLET' => 'Pallet',
default => $c
};
}
?>
<div class="table-responsive">
<table id="tabWarehousePack" class="table table-striped align-middle text-center" style="width:100%;">
<thead>
<tr>
<th>ID</th>
<th>Categoria</th>
<th>Nome</th>
<th>Codice</th>
<th>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>
<td class='text-muted'>-</td>
</tr>";
} else {
foreach ($rows as $r) {
$expiry = $r['expiry_date'] ?? '';
$isExpired = ($expiry !== '' && $expiry < $today);
$isActiveItem = ((int)$r['is_active'] === 1);
$trClass = [];
if ($isExpired) $trClass[] = 'expired';
if (!$isActiveItem) $trClass[] = 'inactive-item';
$trClassStr = $trClass ? " class='" . implode(' ', $trClass) . "'" : "";
$qtyVal = number_format((float)$r['qty'], 3, '.', '');
$expiryShow = $expiry ? htmlspecialchars($expiry) : '<span class="text-muted">-</span>';
$lotShow = htmlspecialchars($r['lot_code'] ?? '');
echo "<tr{$trClassStr} data-stock-id='{$r['stock_id']}' data-expired='" . ($isExpired ? "1" : "0") . "'>
<td>{$r['stock_id']}</td>
<td>" . htmlspecialchars(catLabel($r['category'])) . "</td>
<td>" . htmlspecialchars($r['item_name']) . "</td>
<td><span class='fw-semibold'>" . htmlspecialchars($r['item_code']) . "</span></td>
<td>" . htmlspecialchars($r['supplier_name']) . "</td>
<td>{$lotShow}</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 = item disattivo (ma stock ancora presente).
</div>
</div>
</div>
</div>
</div>
<?php include('include/footer.php'); ?>
</div>
<?php include('jsinclude.php'); ?>
<script>
let dt;
$(document).ready(function() {
const urlParams = new URLSearchParams(window.location.search);
const cat = urlParams.get('cat') || 'all';
const active = urlParams.get('active') || '1';
$('#filterCategory').val(cat);
$('#filterActive').val(active);
dt = $('#tabWarehousePack').DataTable({
order: [
[2, 'asc']
],
pageLength: 50,
language: {
url: 'https://cdn.datatables.net/plug-ins/1.13.6/i18n/it-IT.json'
}
});
$('#filterCategory, #filterActive').on('change', function() {
const url = new URL(window.location.href);
url.searchParams.set('cat', $('#filterCategory').val());
url.searchParams.set('active', $('#filterActive').val());
window.location.href = url.toString();
});
$('#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();
});
});
});
$(document).on('click', '.save-qty', function() {
const tr = $(this).closest('tr');
const stockId = tr.data('stock-id');
const qty = tr.find('.qty-field').val();
fetch('update_packaging_stock_qty.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `id=${encodeURIComponent(stockId)}&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
});
});
});
$(document).on('keypress', '.qty-field', function(e) {
if (e.which === 13) {
e.preventDefault();
$(this).closest('tr').find('.save-qty').click();
}
});
</script>
</body>
</html>