File "ExistenciasController-20260419140833.php"

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

<?php
class ExistenciasController {
    private $modelo;

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

    // ─────────────────────────────────────────────────────────────
    // EXISTENCIAS NUEVO
    // URL: index.php?controller=Existencias&action=index
    // Equivale a: invenconsul/consul.php
    // ─────────────────────────────────────────────────────────────
    public function index() {
        $this->verificarSesion();
        $existencias = $this->modelo->getExistenciasNuevo();
        include 'views/invenconsul/existencias_nuevo.php';
    }

    // ─────────────────────────────────────────────────────────────
    // EXISTENCIAS USADO
    // URL: index.php?controller=Existencias&action=usado
    // Equivale a: invenconsul/consulDotaUsa.php  y  consulReposicion/consulDotaUsa.php
    // ─────────────────────────────────────────────────────────────
    public function usado() {
        $this->verificarSesion();
        $existencias = $this->modelo->getExistenciasUsado();
        include 'views/invenconsul/existencias_usado.php';
    }

    // ─────────────────────────────────────────────────────────────
    // EXCEL INVENTARIO NUEVO
    // URL: index.php?controller=Existencias&action=excelNuevo
    // Equivale a: invenconsul/excelInventa.php
    // ─────────────────────────────────────────────────────────────
    public function excelNuevo() {
        $this->verificarSesion();
        $rows = $this->modelo->getExcelNuevo();
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename=Inventario_Nuevo.xls');
        header('Pragma: no-cache');
        header('Expires: 0');
        include 'views/invenconsul/excel_nuevo.php';
        exit();
    }

    // ─────────────────────────────────────────────────────────────
    // EXCEL INVENTARIO USADO
    // URL: index.php?controller=Existencias&action=excelUsado
    // Equivale a: invenconsul/excelReposicion.php
    // ─────────────────────────────────────────────────────────────
    public function excelUsado() {
        $this->verificarSesion();
        $rows = $this->modelo->getExcelUsado();
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename=Inventario_Usado.xls');
        header('Pragma: no-cache');
        header('Expires: 0');
        include 'views/invenconsul/excel_usado.php';
        exit();
    }

    // ─────────────────────────────────────────────────────────────
    // MOVIMIENTOS
    // URL: index.php?controller=Existencias&action=movimientos
    // Equivale a: invenconsul/movimiento.php
    // ─────────────────────────────────────────────────────────────
    public function movimientos() {
        $this->verificarSesion();

        $filtros_disponibles = $this->modelo->getFiltrosMovimientos();

        $tabla = $_POST['tabla'] ?? 'regInventario';
        $filtros = [
            'CODIGO'      => $_POST['codigo']      ?? '',
            'MOVIMIENTO'  => $_POST['movimiento']  ?? '',
            'AREA'        => $_POST['area']         ?? '',
            'userEntrega' => $_POST['usuario']      ?? '',
            'fecha_inicio'=> $_POST['fecha_inicio'] ?? '',
            'fecha_fin'   => $_POST['fecha_fin']    ?? date('Y-m-d'),
        ];

        // Exportar a Excel
        if (isset($_POST['exportar'])) {
            $rows = $this->modelo->getMovimientosExcel($tabla, $filtros);
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="movimientos_' . $tabla . '_' . date('YmdHis') . '.xls"');
            header('Cache-Control: max-age=0');
            include 'views/invenconsul/excel_movimientos.php';
            exit();
        }

        $resultados     = [];
        $total          = 0;
        $buscado        = false;

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $resultados = $this->modelo->getMovimientos($tabla, $filtros);
            $total      = count($resultados);
            $buscado    = true;
        }

        include 'views/invenconsul/movimientos.php';
    }

    // ─────────────────────────────────────────────────────────────
    // FACTURAS
    // URL: index.php?controller=Existencias&action=facturas
    // Equivale a: invenconsul/consulFacturas.php
    // ─────────────────────────────────────────────────────────────
    public function facturas() {
        $this->verificarSesion();

        $filtros_disponibles = $this->modelo->getFiltrosFacturas();

        $filtros = [
            'CODIGO'      => $_POST['codigo']       ?? '',
            'nfactura'    => $_POST['nfactura']      ?? '',
            'provedor'    => $_POST['provedor']      ?? '',
            'userEntrega' => $_POST['usuario']       ?? '',
            'fecha_inicio'=> $_POST['fecha_inicio']  ?? '',
            'fecha_fin'   => $_POST['fecha_fin']     ?? date('Y-m-d'),
        ];

        // Exportar a Excel
        if (isset($_POST['exportar'])) {
            $rows = $this->modelo->getFacturas($filtros, 99999);
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="facturas_entradas_' . date('YmdHis') . '.xls"');
            header('Cache-Control: max-age=0');
            include 'views/invenconsul/excel_facturas.php';
            exit();
        }

        $resultados = [];
        $total      = 0;
        $buscado    = false;

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $resultados = $this->modelo->getFacturas($filtros);
            $total      = count($resultados);
            $buscado    = true;
        }

        include 'views/invenconsul/facturas.php';
    }

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