<?php class ReporteModel { private $conexion; public function __construct() { $database = new Database(); $this->conexion = $database->connect(); } /** * Obtener registros de entregasst filtrados por rango de fechas * JOIN con reginventario para traer precio y con empleado para traer tpContrato */ public function obtenerEntregasPorRangoFechas($fecha_inicio, $fecha_fin) { if (empty($fecha_inicio) || empty($fecha_fin)) { throw new Exception("Las fechas de inicio y fin son obligatorias"); } $fecha_inicio = mysqli_real_escape_string($this->conexion, $fecha_inicio); $fecha_fin = mysqli_real_escape_string($this->conexion, $fecha_fin); if (strtotime($fecha_inicio) > strtotime($fecha_fin)) { throw new Exception("La fecha de inicio no puede ser mayor que la fecha final"); } $query = "SELECT e.cedula, e.nombre, e.ccosto, e.area, e.cargo, e.fEntrega, e.tpEstado, e.codigo, e.epp, e.cantidad, e.userEntrega, e.tpEntrega, COALESCE( (SELECT i.precio FROM reginventario i WHERE i.CODIGO = e.codigo LIMIT 1), '' ) AS precio, COALESCE( (SELECT em.tpContrato FROM empleado em WHERE em.cedula = e.cedula LIMIT 1), '' ) AS tpContrato FROM entregasst e WHERE e.fEntrega BETWEEN '$fecha_inicio' AND '$fecha_fin' ORDER BY e.fEntrega DESC, e.cedula ASC"; $result = mysqli_query($this->conexion, $query); if (!$result) { throw new Exception("Error en la consulta: " . mysqli_error($this->conexion)); } $registros = []; while ($row = mysqli_fetch_assoc($result)) { $registros[] = $row; } return $registros; } /** * Obtener estadísticas del rango de fechas */ public function obtenerEstadisticas($fecha_inicio, $fecha_fin) { $fecha_inicio = mysqli_real_escape_string($this->conexion, $fecha_inicio); $fecha_fin = mysqli_real_escape_string($this->conexion, $fecha_fin); $query = "SELECT COUNT(DISTINCT cedula) as total_empleados, COUNT(*) as total_entregas, SUM(cantidad) as total_items FROM entregasst WHERE fEntrega BETWEEN '$fecha_inicio' AND '$fecha_fin'"; $result = mysqli_query($this->conexion, $query); if (!$result) { throw new Exception("Error al obtener estadísticas: " . mysqli_error($this->conexion)); } return mysqli_fetch_assoc($result); } /** * Validar que existan registros en el rango de fechas */ public function validarExistenciaRegistros($fecha_inicio, $fecha_fin) { $fecha_inicio = mysqli_real_escape_string($this->conexion, $fecha_inicio); $fecha_fin = mysqli_real_escape_string($this->conexion, $fecha_fin); $query = "SELECT COUNT(*) as total FROM entregasst WHERE fEntrega BETWEEN '$fecha_inicio' AND '$fecha_fin'"; $result = mysqli_query($this->conexion, $query); if (!$result) { return false; } $row = mysqli_fetch_assoc($result); return ($row['total'] > 0); } } ?>