File "ConsultaController.php"

Full Path: C:/wamp64/www/AVIDOTAPP/controllers/ConsultaController.php
File size: 9.16 KB
MIME-type: text/x-php
Charset: utf-8

<?php
class ConsultaController {
    private $modelo;

    public function __construct() {
        require_once 'models/ConsultaModel.php';
        $this->modelo = new ConsultaModel();
    }

    // ─────────────────────────────────────────────────────────────
    // CONSULTA EPP (CONSUEPP) — BD principal
    // URL: index.php?controller=Consulta&action=index
    // ─────────────────────────────────────────────────────────────
    public function index() {
        $this->verificarSesion();

        // Datos para los selects de filtro
        $areas   = $this->modelo->getAreas();
        $ccostos = $this->modelo->getCCostos();

        // Variables de resultados (vacías por defecto)
        $resultados  = [];
        $operario    = null;
        $tipo_filtro = $_POST['tipo_filtro'] ?? null;
        $fecha_inicio = $_POST['fecha_inicio'] ?? date('Y-m-d', strtotime('-30 days'));
        $fecha_fin    = $_POST['fecha_fin']    ?? date('Y-m-d');

        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tipo_filtro'])) {
            $tipo_filtro  = $_POST['tipo_filtro'];
            $fecha_inicio = $_POST['fecha_inicio'];
            $fecha_fin    = $_POST['fecha_fin'];
            $valor        = $this->getValorFiltro($tipo_filtro, $_POST);

            $resultados = $this->modelo->buscarEntregas($tipo_filtro, $valor, $fecha_inicio, $fecha_fin);

            if ($tipo_filtro === 'cedula' && !empty($valor)) {
                $operario = $this->modelo->getDatosOperario($valor);
            }
        }

        include 'views/fconsuldota/consulta_epp.php';
    }

    // ─────────────────────────────────────────────────────────────
    // EXPORTAR EXCEL — BD principal (descarga directa)
    // URL: index.php?controller=Consulta&action=exportarExcel  (POST)
    // ─────────────────────────────────────────────────────────────
    public function exportarExcel() {
        $this->verificarSesion();

        $tipo_filtro  = $_POST['tipo_filtro_export']  ?? 'cedula';
        $fecha_inicio = $_POST['fecha_inicio_export'] ?? date('Y-m-d');
        $fecha_fin    = $_POST['fecha_fin_export']    ?? date('Y-m-d');
        $valor        = $this->getValorFiltroExport($tipo_filtro, $_POST);

        $rows = $this->modelo->exportarExcelPrincipal($tipo_filtro, $valor, $fecha_inicio, $fecha_fin);

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="entregas_filtradas.xls"');
        header('Cache-Control: max-age=0');

        include 'views/fconsuldota/excel_principal.php';
        exit();
    }

    // ─────────────────────────────────────────────────────────────
    // HISTÓRICO (BD externa)
    // URL: index.php?controller=Consulta&action=historico
    // ─────────────────────────────────────────────────────────────
    public function historico() {
        $this->verificarSesion();

        $resultados  = [];
        $operario    = null;
        $tipo_filtro = null;
        $fecha_inicio = $_POST['fecha_inicio'] ?? date('Y-m-d', strtotime('-30 days'));
        $fecha_fin    = $_POST['fecha_fin']    ?? date('Y-m-d');
        $error        = null;

        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['tipo_filtro'])) {
            $tipo_filtro  = $_POST['tipo_filtro'];
            $fecha_inicio = $_POST['fecha_inicio'];
            $fecha_fin    = $_POST['fecha_fin'];
            $valor        = $tipo_filtro === 'EMPLEADO' ? ($_POST['EMPLEADO'] ?? '') : ($_POST['item'] ?? '');

            try {
                $resultados = $this->modelo->buscarHistorico($tipo_filtro, $valor, $fecha_inicio, $fecha_fin);

                if ($tipo_filtro === 'EMPLEADO' && !empty($valor)) {
                    $operario = ['cedula' => $valor];
                }
            } catch (Exception $e) {
                $error = $e->getMessage();
            }
        }

        include 'views/fconsuldota/historico.php';
    }

    // ─────────────────────────────────────────────────────────────
    // EXPORTAR EXCEL HISTÓRICO
    // URL: index.php?controller=Consulta&action=exportarExcelHistorico  (POST)
    // ─────────────────────────────────────────────────────────────
    public function exportarExcelHistorico() {
        $this->verificarSesion();

        $tipo_filtro  = $_POST['tipo_filtro_export']  ?? 'EMPLEADO';
        $fecha_inicio = $_POST['fecha_inicio_export'] ?? date('Y-m-d');
        $fecha_fin    = $_POST['fecha_fin_export']    ?? date('Y-m-d');
        $valor        = $tipo_filtro === 'EMPLEADO'
                            ? ($_POST['EMPLEADO_export'] ?? '')
                            : ($_POST['item_export']     ?? '');

        $rows = $this->modelo->exportarExcelHistorico($tipo_filtro, $valor, $fecha_inicio, $fecha_fin);

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="historico_filtrado.xls"');
        header('Cache-Control: max-age=0');

        include 'views/fconsuldota/excel_historico.php';
        exit();
    }

    // ─────────────────────────────────────────────────────────────
    // PDF HISTÓRICO (vista imprimible por NOE)
    // URL: index.php?controller=Consulta&action=historicoPDF&noe=X
    // ─────────────────────────────────────────────────────────────
    public function historicoPDF() {
        $noe = trim($_GET['noe'] ?? '');

        if (empty($noe)) {
            die('Error: No se proporcionó el número de entrega (NOE).');
        }

        $datos_entrega = $this->modelo->getEntregaPorNOE($noe);

        if (!$datos_entrega) {
            die('Error: No se encontró la entrega con NOE: ' . htmlspecialchars($noe));
        }

        $EMPLEADO     = $datos_entrega['EMPLEADO'];
        $FECHA_ENTREGA = $datos_entrega['FECHA'];
        $DOTACION     = $datos_entrega['DOTACION'];
        $ENTREGA      = $datos_entrega['ENTREGA'];

        $FECHAD = date("d", strtotime($FECHA_ENTREGA));
        $FECHAM = date("m", strtotime($FECHA_ENTREGA));
        $FECHAY = date("Y", strtotime($FECHA_ENTREGA));

        // Datos del empleado
        $fila = $this->modelo->getEmpleadoHistorico($EMPLEADO);
        if (!$fila) {
            $fila = ['', 'EMPLEADO', 'NO REGISTRADO', '', '', ''];
        }

        // Firma
        $firma_raw = $this->modelo->getFirmaPorNOE($noe);
        $imgSrc    = ConsultaModel::procesarFirma($firma_raw);

        // Items entregados
        $items = $this->modelo->getItemsPorNOE($noe);

        include 'views/fconsuldota/historico_pdf.php';
    }

    // ─────────────────────────────────────────────────────────────
    // HELPERS PRIVADOS
    // ─────────────────────────────────────────────────────────────
    private function getValorFiltro($tipo, $post) {
        switch ($tipo) {
            case 'cedula':  return trim($post['cedula']  ?? '');
            case 'item':    return trim($post['item']    ?? '');
            case 'area':    return trim($post['area']    ?? '');
            case 'ccosto':  return trim($post['ccosto']  ?? '');
            default:        return '';
        }
    }

    private function getValorFiltroExport($tipo, $post) {
        switch ($tipo) {
            case 'cedula':  return trim($post['cedula_export']  ?? '');
            case 'item':    return trim($post['item_export']    ?? '');
            case 'area':    return trim($post['area_export']    ?? '');
            case 'ccosto':  return trim($post['ccosto_export']  ?? '');
            default:        return '';
        }
    }

    private function verificarSesion() {
        if (!isset($_SESSION['DIGITA']) || empty($_SESSION['DIGITA'])) {
            header('Location: index.php');
            exit();
        }
    }
}
?>