34786-vm/liquidaciones_por_fecha.php
2025-12-12 16:33:10 +00:00

109 lines
4.4 KiB
PHP

<?php
require_once 'includes/header.php';
$fecha_inicio = isset($_GET['fecha_inicio']) ? $_GET['fecha_inicio'] : '';
$fecha_fin = isset($_GET['fecha_fin']) ? $_GET['fecha_fin'] : '';
$resultados = [];
if ($fecha_inicio && $fecha_fin) {
$pdo = db();
$stmt = $pdo->prepare("
SELECT
p.nombre AS producto,
c.nombre AS ciudad,
SUM(m.cantidad_pedidos) AS cantidad_pedidos,
SUM(m.cantidad) AS cantidad,
SUM(m.precio_liquidacion) AS precio_liquidaciones
FROM
movimientos m
JOIN
productos p ON m.producto_id = p.id
JOIN
ciudades c ON m.ciudad_origen_id = c.id
WHERE
m.tipo = 'Salida' AND DATE(m.fecha) BETWEEN :fecha_inicio AND :fecha_fin
GROUP BY
p.nombre, c.nombre
ORDER BY
precio_liquidaciones DESC
");
$stmt->execute([
':fecha_inicio' => $fecha_inicio,
':fecha_fin' => $fecha_fin
]);
$resultados = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
<h1 class="mb-4">Liquidaciones por Fecha</h1>
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Filtrar por Fecha</h5>
<form method="GET" action="liquidaciones_por_fecha.php" class="row g-3">
<div class="col-md-5">
<label for="fecha_inicio" class="form-label">Fecha de Inicio</label>
<input type="date" class="form-control" id="fecha_inicio" name="fecha_inicio" value="<?php echo htmlspecialchars($fecha_inicio); ?>" required>
</div>
<div class="col-md-5">
<label for="fecha_fin" class="form-label">Fecha de Fin</label>
<input type="date" class="form-control" id="fecha_fin" name="fecha_fin" value="<?php echo htmlspecialchars($fecha_fin); ?>" required>
</div>
<div class="col-md-2 d-flex align-items-end">
<button type="submit" class="btn btn-primary w-100">Calcular</button>
</div>
</form>
</div>
</div>
<?php if ($fecha_inicio && $fecha_fin): ?>
<h2 class="mb-3">Resultados del <?php echo htmlspecialchars(date('d/m/Y', strtotime($fecha_inicio))); ?> al <?php echo htmlspecialchars(date('d/m/Y', strtotime($fecha_fin))); ?></h2>
<?php if (empty($resultados)): ?>
<div class="alert alert-info">No se encontraron liquidaciones para el rango de fechas seleccionado.</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead class="table-dark">
<tr>
<th>Producto</th>
<th>Ciudad</th>
<th>Cantidad de Pedidos</th>
<th>Cantidad</th>
<th>Precio Liquidaciones</th>
</tr>
</thead>
<tbody>
<?php
$total_pedidos = 0;
$total_cantidad = 0;
$total_precio = 0;
foreach ($resultados as $fila):
$total_pedidos += $fila['cantidad_pedidos'];
$total_cantidad += $fila['cantidad'];
$total_precio += $fila['precio_liquidaciones'];
?>
<tr>
<td><?php echo htmlspecialchars($fila['producto']); ?></td>
<td><?php echo htmlspecialchars($fila['ciudad']); ?></td>
<td><?php echo htmlspecialchars($fila['cantidad_pedidos']); ?></td>
<td><?php echo htmlspecialchars($fila['cantidad']); ?></td>
<td><?php echo htmlspecialchars(number_format($fila['precio_liquidaciones'], 2)); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr class="table-secondary">
<th colspan="2" class="text-end">Totales:</th>
<th><?php echo $total_pedidos; ?></th>
<th><?php echo $total_cantidad; ?></th>
<th><?php echo number_format($total_precio, 2); ?></th>
</tr>
</tfoot>
</table>
</div>
<?php endif; ?>
<?php endif; ?>
<?php require_once 'includes/footer.php'; ?>