177 lines
7.6 KiB
PHP
177 lines
7.6 KiB
PHP
<?php
|
|
require_once 'layout_header.php';
|
|
require_once 'db/config.php';
|
|
|
|
$db = db();
|
|
$sedes = $db->query("SELECT id, nombre FROM sedes ORDER BY nombre ASC")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$selected_sede_id = isset($_GET['sede_id']) ? (int)$_GET['sede_id'] : 0;
|
|
$inventory = [];
|
|
$sede_name = '';
|
|
|
|
if ($selected_sede_id) {
|
|
$stmt = $db->prepare("SELECT nombre FROM sedes WHERE id = ?");
|
|
$stmt->execute([$selected_sede_id]);
|
|
$sede_name = $stmt->fetchColumn();
|
|
|
|
// Se añade p.id para poder usarlo en el botón
|
|
$stmt = $db->prepare("
|
|
SELECT p.id, p.nombre, p.sku, COUNT(ui.id) as quantity
|
|
FROM products p
|
|
JOIN unidades_inventario ui ON p.id = ui.producto_id
|
|
WHERE ui.sede_id = ? AND ui.estado = 'En Almacén'
|
|
GROUP BY p.id, p.nombre, p.sku
|
|
HAVING quantity > 0
|
|
ORDER BY p.nombre ASC
|
|
");
|
|
$stmt->execute([$selected_sede_id]);
|
|
$inventory = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h2 class="card-title">Auditoría de Inventario por Sede</h2>
|
|
<p class="card-subtitle mb-2 text-muted">Selecciona una sede para ver el detalle de su stock.</p>
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="GET" action="inventario_por_sede.php" class="mb-4">
|
|
<div class="row align-items-end">
|
|
<div class="col-md-4">
|
|
<label for="sede_id" class="form-label">Sede:</label>
|
|
<select name="sede_id" id="sede_id" class="form-select">
|
|
<option value="">-- Elige una sede --</option>
|
|
<?php foreach ($sedes as $sede): ?>
|
|
<option value="<?php echo $sede['id']; ?>" <?php echo $selected_sede_id == $sede['id'] ? 'selected' : ''; ?>>
|
|
<?php echo htmlspecialchars($sede['nombre']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<button type="submit" class="btn btn-primary w-100">Consultar</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
|
|
<?php if ($selected_sede_id): ?>
|
|
<hr>
|
|
<?php if ($sede_name): ?>
|
|
<h3>Inventario de: <?php echo htmlspecialchars($sede_name); ?></h3>
|
|
<?php if (count($inventory) > 0): ?>
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead class="table-dark">
|
|
<tr>
|
|
<th>Producto</th>
|
|
<th>SKU</th>
|
|
<th class="text-center">Cantidad en Stock</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($inventory as $item): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($item['nombre']); ?></td>
|
|
<td><?php echo htmlspecialchars($item['sku']); ?></td>
|
|
<td class="text-center">
|
|
<?php if ($item['quantity'] > 0): ?>
|
|
<a href="#" class="ver-codigos" data-product-id="<?php echo $item['id']; ?>" data-sede-id="<?php echo $selected_sede_id; ?>">
|
|
<?php echo $item['quantity']; ?>
|
|
</a>
|
|
<?php else: ?>
|
|
0
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="alert alert-info" role="alert">
|
|
No se encontraron productos con stock en esta sede.
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php else: ?>
|
|
<div class="alert alert-warning" role="alert">
|
|
La sede seleccionada no es válida.
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal para mostrar los códigos de barras -->
|
|
<div class="modal fade" id="codesModal" tabindex="-1" aria-labelledby="codesModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-scrollable">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="codesModalLabel">Códigos de Barras</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<p><strong>Producto:</strong> <span id="modalProductName"></span></p>
|
|
<div id="codesTableContainer"></div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
$(document).ready(function() {
|
|
$(".table").on('click', '.ver-codigos', function(e) {
|
|
e.preventDefault();
|
|
|
|
var productId = $(this).data('product-id');
|
|
var sedeId = $(this).data('sede-id');
|
|
var productName = $(this).closest('tr').find('td:first').text();
|
|
|
|
$('#modalProductName').text(productName);
|
|
$('#codesTableContainer').html('<p>Cargando...</p>');
|
|
|
|
var myModal = new bootstrap.Modal(document.getElementById('codesModal'));
|
|
myModal.show();
|
|
|
|
$.ajax({
|
|
url: 'get_barcodes_by_product_and_sede.php',
|
|
type: 'GET',
|
|
dataType: 'json',
|
|
data: {
|
|
product_id: productId,
|
|
sede_id: sedeId
|
|
},
|
|
success: function(data) {
|
|
|
|
function getBadgeClass(estado) {
|
|
if (estado === 'En Almacén') return 'bg-success';
|
|
if (estado === 'Vendido') return 'bg-danger';
|
|
if (estado === 'Generado') return 'bg-info';
|
|
return 'bg-secondary';
|
|
}
|
|
|
|
let tableHtml = '<table class="table table-bordered table-striped"><thead><tr><th>Código</th><th>Estado</th></tr></thead><tbody>';
|
|
if (data.length > 0) {
|
|
data.forEach(item => {
|
|
const badgeClass = getBadgeClass(item.estado);
|
|
tableHtml += '<tr><td>' + item.codigo_unico + '</td><td><span class="badge ' + badgeClass + '">' + item.estado + '</span></td></tr>';
|
|
});
|
|
} else {
|
|
tableHtml += '<tr><td colspan="2" class="text-center">No se encontraron unidades.</td></tr>';
|
|
}
|
|
tableHtml += '</tbody></table>';
|
|
$('#codesTableContainer').html(tableHtml);
|
|
},
|
|
error: function() {
|
|
$('#codesTableContainer').html('<p class="text-danger">Error al cargar los códigos.</p>');
|
|
}
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<?php require_once 'layout_footer.php'; ?>
|