<?php

    class Expediente {

        private $con;
        private $grupoe_biblioteca_expedientes;
        private $clave_biblioteca_expedientes;
        private $codigo_vehiculo = 0;
        private $tipo_expediente_biblioteca_expedientes;
        private $orden_biblioteca_expedientes;
        private $descripcion_biblioteca_expedientes;
        private $fecha_biblioteca_expedientes;
        private $vimporte_biblioteca_expedientes = 0;
        private $vfecha_biblioteca_expedientes = NULL;
        private $etiqueta_biblioteca_expedientes = 0;

        public function __construct($con) {
            $this->con = $con;
        }

        public function set_grupoe_biblioteca_expedientes($grupoe_biblioteca_expedientes) {
            $this->grupoe_biblioteca_expedientes = $grupoe_biblioteca_expedientes;
            return $this;
        }
        public function get_grupoe_biblioteca_expedientes() {
            return $this->grupoe_biblioteca_expedientes;
        }
        public function set_clave_biblioteca_expedientes($clave_biblioteca_expedientes) {
            $this->clave_biblioteca_expedientes = $clave_biblioteca_expedientes;
            return $this;
        }
        public function get_clave_biblioteca_expedientes() {
            return $this->clave_biblioteca_expedientes;
        }
        public function set_codigo_vehiculo($codigo_vehiculo) {
            $this->codigo_vehiculo = $codigo_vehiculo;
            return $this;
        }
        public function get_codigo_vehiculo() {
            return $this->codigo_vehiculo;
        }
        public function set_tipo_expediente_biblioteca_expedientes($tipo_expediente_biblioteca_expedientes) {
            $this->tipo_expediente_biblioteca_expedientes = $tipo_expediente_biblioteca_expedientes;
            return $this;
        }
        public function get_tipo_expediente_biblioteca_expedientes() {
            return $this->tipo_expediente_biblioteca_expedientes;
        }
        public function set_orden_biblioteca_expedientes($orden_biblioteca_expedientes) {
            $this->orden_biblioteca_expedientes = $orden_biblioteca_expedientes;
            return $this;
        }
        public function get_orden_biblioteca_expedientes() {
            return $this->orden_biblioteca_expedientes;
        }
        public function set_descripcion_biblioteca_expedientes($descripcion_biblioteca_expedientes) {
            $this->descripcion_biblioteca_expedientes = $descripcion_biblioteca_expedientes;
            return $this;
        }
        public function get_descripcion_biblioteca_expedientes() {
            return $this->descripcion_biblioteca_expedientes;
        }
        public function set_fecha_biblioteca_expedientes($fecha_biblioteca_expedientes) {
            $this->fecha_biblioteca_expedientes = $fecha_biblioteca_expedientes;
            return $this;
        }
        public function get_fecha_biblioteca_expedientes() {
            return $this->fecha_biblioteca_expedientes;
        }
        public function set_vimporte_biblioteca_expedientes($vimporte_biblioteca_expedientes) {
            $this->vimporte_biblioteca_expedientes = $vimporte_biblioteca_expedientes;
            return $this;
        }
        public function get_vimporte_biblioteca_expedientes() {
            return $this->vimporte_biblioteca_expedientes;
        }
        public function set_vfecha_biblioteca_expedientes($vfecha_biblioteca_expedientes) {
            $this->vfecha_biblioteca_expedientes = $vfecha_biblioteca_expedientes;
            return $this;
        }
        public function get_vfecha_biblioteca_expedientes() {
            return $this->vfecha_biblioteca_expedientes;
        }
        public function set_etiqueta_biblioteca_expedientes($etiqueta_biblioteca_expedientes) {
            $this->etiqueta_biblioteca_expedientes = $etiqueta_biblioteca_expedientes;
            return $this;
        }
        public function get_etiqueta_biblioteca_expedientes() {
            return $this->etiqueta_biblioteca_expedientes;
        }

        


        public function check_contador_expediente ($etiqueta) {
            //$etiqueta = codigo del vehiculo
            $sql = "SELECT CLAVE FROM BIBLIOTECA_EXPEDIENTES WHERE ETIQUETA = ?";
            $stmt = $this->con->prepare($sql);
            $stmt->bind_param('i', $etiqueta);
            $stmt->execute();
            $res = $stmt->get_result();
            if($res->num_rows) {
                $row = $res->fetch_array();
                return $row['CLAVE'];
            } else {
                return FALSE;
            }
        }



        public function check_expediente ($grupoe, $cotador) {
            $sql = "SELECT CLAVE FROM BIBLIOTECA_EXPEDIENTES WHERE GRUPOE = $grupoe AND CLAVE = '$cotador'";
            $res = $this->con->query($sql);
            if($res->num_rows){
                return FALSE;
            } else {
                return TRUE;
            }
        }


        public function insert_biblioteca_expedientes_valores ($contador, $grupoe, $tipoexp, $orden, $descripcion, $vimporte = FALSE, $vfecha = FALSE, $etiqueta = FALSE) {
            $key = "";
            $value = "";
            $key .= "FECHA";
            $value .= "NOW()";
            if($vimporte)
            {
                $key .= ',VIMPORTE';
                $value .= ",$vimporte";
            }
            if($vfecha)
            {
                $key .= ',VFECHA';
                $value .= ",$vfecha";
            }
            if($etiqueta)
            {
                $key .= ',ETIQUETA';
                $value .= ",$etiqueta";
            }
            $sql = "INSERT INTO BIBLIOTECA_EXPEDIENTES(CLAVE,GRUPOE,TIPOEXP,ORDEN,DESCRIPCION,$key) VALUES('$contador',$grupoe,$tipoexp,$orden,'$descripcion',$value)";
            $this->con->query($sql);
        }


        public function check_expediente_carpeta ($expediente_tipo) {
            $sql = "SELECT * FROM BIBLIOTECA_ELEMENTOS WHERE CLAVE = '$expediente_tipo'";
            $res = $this->con->query($sql);
            if($res->num_rows) {
                return 1;
            } else {
                return 0;
            }
        }


        public function crear_carpeta_expediente ($clave,$nombre,$padre,$usuarioSession,$icono,$ubicacion,$acceso = FALSE,$expte_codigo = FALSE) {
            $carpeta_c = 'C';
            $now = date('Y-m-d H:i:s');
            $publico = 'PUB';
            $uno = 1;
            /* $sql = "INSERT INTO BIBLIOTECA_ELEMENTOS(CLAVE, NOMBRE, PADRE, TIPO, FECHA, AMBITO, CREADOR, MODIFICACION, PARTES, ICONO, UBICACION, ACCESO, EXPTE_CODIGO) VALUES('$clave','$nombre','$padre','C',NOW(),'PUB','$usuarioSession',NOW(),1, '$icono','$ubicacion','$acceso','$expte_codigo')"; */
            $sql = 'INSERT INTO BIBLIOTECA_ELEMENTOS(CLAVE, NOMBRE, PADRE, TIPO, FECHA, AMBITO, CREADOR, MODIFICACION, PARTES, ICONO, UBICACION, ACCESO, EXPTE_CODIGO)
            VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)';
            $stmt = $this->con->prepare($sql);
            $stmt->bind_param('ssssssssissss', $clave, $nombre, $padre, $carpeta_c, $now, $publico, $usuarioSession, $now, $uno, $icono, $ubicacion, $acceso, $expte_codigo);
            $stmt->execute();
        }


        public function select_acceso_biblioteca_elementos($clave)
        {
            $sql = "SELECT ACCESO FROM BIBLIOTECA_ELEMENTOS WHERE CLAVE = '$clave'";
            $res = $this->con->query($sql);
            $fila = $res->fetch_array();
            return $fila['ACCESO'];
        }


        public function insert_biblioteca_etiquetas_expediente($codigo,$clave,$descripcion,$grupoe,$esquema) {
            $sql = "INSERT INTO BIBLIOTECA_ETIQUETAS(CODIGO,CLAVE,DESCRIPCION,GRUPOE,ESQUEMA,FECHA,VALOR) VALUES('$codigo','$clave','$descripcion',$grupoe,$esquema,NOW(),'$descripcion')";
            $this->con->query($sql);
        }



        //$etiqueta es el codigo del vehiculo
        public function select_clave_padre_acceso_ubicacion_expediente ($etiqueta, $campo = FALSE) {
            $sql = "SELECT CLAVE FROM BIBLIOTECA_EXPEDIENTES WHERE ETIQUETA = ?";
            $stmt = $this->con->prepare($sql);
            $stmt->bind_param('i',$etiqueta);
            $stmt->execute();
            $res = $stmt->get_result();
            $fila = $res->fetch_array();
            $clave_etiqueta = $fila['CLAVE'];

            $sql2 = "SELECT CLAVE FROM BIBLIOTECA_ELEMENTOS WHERE EXPTE_CODIGO = '$clave_etiqueta'";
            $res2 = $this->con->query($sql2);
            $fila2 = $res2->fetch_array();
            $clave_biblioteca_elementos = $fila2['CLAVE'];

            $sqlPAU = "SELECT PADRE,ACCESO,UBICACION FROM BIBLIOTECA_ELEMENTOS WHERE CLAVE = '$clave_biblioteca_elementos'";
            $resPAU = $this->con->query($sqlPAU);
            $filaPAU = $resPAU->fetch_array();

            return $campo ? $filaPAU[$campo] : $clave_biblioteca_elementos;
        }

    }




?>