121 lines
4.8 KiB
PHP
121 lines
4.8 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: auth/login.php');
|
|
exit;
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
require_once 'includes/header.php';
|
|
|
|
$pdo = db();
|
|
|
|
// 1. Obtener todas las ciudades, ordenadas por stock total descendente
|
|
$query_ciudades = '
|
|
SELECT
|
|
c.id,
|
|
c.nombre,
|
|
SUM(COALESCE(spc.stock_actual, 0)) as total_stock
|
|
FROM
|
|
ciudades c
|
|
LEFT JOIN
|
|
stock_por_ciudad spc ON c.id = spc.ciudad_id
|
|
GROUP BY
|
|
c.id, c.nombre
|
|
ORDER BY
|
|
total_stock DESC, c.orden ASC
|
|
';
|
|
$stmt_ciudades = $pdo->query($query_ciudades);
|
|
$ciudades = $stmt_ciudades->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// 2. Obtener todos los productos ordenados por la nueva columna 'orden'
|
|
$stmt_productos = $pdo->query('SELECT id, nombre FROM productos ORDER BY orden ASC');
|
|
$productos = $stmt_productos->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// 3. Obtener todo el stock y organizarlo en un array para acceso rápido
|
|
$stmt_stock = $pdo->query('SELECT producto_id, ciudad_id, stock_actual FROM stock_por_ciudad');
|
|
$stock_data = [];
|
|
while ($row = $stmt_stock->fetch(PDO::FETCH_ASSOC)) {
|
|
$stock_data[$row['producto_id']][$row['ciudad_id']] = $row['stock_actual'];
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="container-fluid px-4">
|
|
<h1 class="mt-4">Resumen de Stock</h1>
|
|
<ol class="breadcrumb mb-4">
|
|
<li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
|
|
<li class="breadcrumb-item active">Resumen de Stock</li>
|
|
</ol>
|
|
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<i class="fas fa-table me-1"></i>
|
|
Inventario por Producto y Ciudad
|
|
</div>
|
|
<div class="card-body">
|
|
<style>
|
|
.table-compact th,
|
|
.table-compact td {
|
|
padding: 0.25rem 0.5rem;
|
|
vertical-align: middle;
|
|
}
|
|
.bg-success-soft {
|
|
background-color: #d1e7dd !important; /* Verde suave */
|
|
}
|
|
.bg-danger-soft {
|
|
background-color: #f8d7da !important; /* Rojo suave */
|
|
}
|
|
</style>
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered table-striped table-hover table-compact">
|
|
<thead class="table-dark">
|
|
<tr>
|
|
<th style="width: 250px;">Producto</th>
|
|
<th class="text-center" style="width: 100px;">Orden</th>
|
|
<?php foreach ($ciudades as $ciudad): ?>
|
|
<th class="text-center"><?php echo htmlspecialchars($ciudad['nombre']); ?></th>
|
|
<?php endforeach; ?>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($productos)): ?>
|
|
<tr>
|
|
<td colspan="<?php echo count($ciudades) + 2; ?>" class="text-center">No hay productos para mostrar.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($productos as $producto): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($producto['nombre']); ?></td>
|
|
<td class="text-center">
|
|
<a href="handle_orden_productos.php?id=<?php echo $producto['id']; ?>&dir=up" class="btn btn-sm btn-outline-secondary py-0 px-1"><i class="fas fa-arrow-up"></i></a>
|
|
<a href="handle_orden_productos.php?id=<?php echo $producto['id']; ?>&dir=down" class="btn btn-sm btn-outline-secondary py-0 px-1"><i class="fas fa-arrow-down"></i></a>
|
|
</td>
|
|
<?php foreach ($ciudades as $ciudad): ?>
|
|
<?php
|
|
$stock = isset($stock_data[$producto['id']][$ciudad['id']]) ? $stock_data[$producto['id']][$ciudad['id']] : 0;
|
|
$cell_class = '';
|
|
if ($stock >= 10) {
|
|
$cell_class = 'bg-success-soft';
|
|
} elseif ($stock > 0 && $stock < 10) {
|
|
$cell_class = 'bg-danger-soft';
|
|
}
|
|
?>
|
|
<td class="text-center <?php echo $cell_class; ?>">
|
|
<?php echo $stock; ?>
|
|
</td>
|
|
<?php endforeach; ?>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
require_once 'includes/footer.php';
|
|
?>
|