<?php

class Conexion {
    private $conexion;
   
    private $parametros = array();
    private $parametrosIn = array();
    private $values = array();
  
    public function Conectar() {
        $server = '161.132.55.88';
        $this->conexion = mssql_connect($server, 'inkamed1_bdtaypa_2', '46073651_Jf');
        mssql_select_db("inkamed1_bdtaypa", $this->conexion);
        if (!$this->conexion) {
            die('Error de conexión a MSSQL');
        }
    }
    
    public function Consulta($sql) {
        $query = mssql_query($sql);
        $data1 = '';
        if (mssql_num_rows($query)) {
            while ($row = mssql_fetch_array($query)) {
                $data1[] = $row;
            }
        }
        mssql_free_statement($stmt);
        return $data1;
    }

    public function Procedimiento($nombre) {
        $sql = "EXEC ".$nombre." ";
        $es_pri = true;
        foreach($this->parametrosIn as &$data) {
            if ($es_pri)
                $es_pri = false;
            else
                $sql .= ',';
            $sql .= "'" . $data . "'";
        }
        $query = mssql_query($sql);
        $data1 = array();
        // Verifica si ha devuelto algún registro
        if (mssql_num_rows($query)) {
            while ($row = mssql_fetch_array($query)) {
                $data1[] = $row;
            }
        }
        mssql_free_statement($stmt);
        return $data1;
    }

    public function reiniciarArray(){
        $this->parametros = array();
        $this->parametrosIn = array();
        $this->values = array();
    }

    public function Procedimiento2($name) {
        $sql = "{call ".$name." (";
        $es_pri = true;
        if (count($this->values) == 0) {
            $this->parametros = array();
            $this->parametrosIn = array(array());
            $this->values = array(array());
        }
        foreach($this->values as &$data) {
            if ($es_pri)
                $es_pri = false;
            else
                $sql .= ',';
            $sql .= " ?";
        }
        $sql .= ")}";
        $stmt = mssql_query($this->conexion, $sql, $this->values)or die(print_r(mssql_errors(),true));
        $affect = $num_bloqueados=mssql_rows_affected($stmt);
        $data1 = '';
        while ( $arrayDatos = $this->mssql_fetch_array($stmt) )
            $data1[] = $arrayDatos;
        return $data1;
        unset($this->values);
    }

    public function Parametro($p1, $p2){
        array_push($this->parametros, $p1);
        array_push($this->parametrosIn, $p2);
        array_push($this->values, array(&$p2, SQLSRV_PARAM_IN));
    }
    
    # METODO PARA CONTAR EL NUMERO DE RESULTADOS
    function numeroFilas($result) {
        #if(!is_resource($result)) return false;
        #return mysql_num_rows($result);
        return mssql_num_rows($result);
    }

    # METODO PARA CREAR ARRAY ASOCIATIVO DESDE UNA CONSULTA
    function fetch_assoc($result) {
        if(!is_resource($result)) return false;
        #return mysql_fetch_assoc($result);
        return mssql_fetch_array($result, SQLSRV_FETCH_ASSOC);
    }

    # METODO PARA CREAR ARRAY NUMÉRICO DESDE UNA CONSULTA
    function fetch_array($result) {
        if(!is_resource($result)) return false;
        #return mysql_fetch_assoc($result);
        return mssql_fetch_array($result, SQLSRV_FETCH_NUMERIC);
    }

    # METODO PARA CREAR ARRAY DE OBJETOS DESDE UNA CONSULTA
    function fetch_object($result) {
        if(!is_resource($result)) return false;
        #return mysql_fetch_assoc($result);
        return mssql_fetch_array($result);
    }
    
    # METODO PARA CERRAR LA CONEXION A LA BASE DE DATOS
    public function Desconectar() {
        mssql_close($this->conexion);
        #mysql_close();
    }

    public function formatFecha($fecha) {
        $m = explode("/", $fecha);
        if (count($m) > 1)
            $fecha = $m[0].'-'.$m[1].'-'.$m[2];
        $date = new DateTime($fecha);
        if ($this->formato == "d-m-y")
            $value = $date->format('d-m-Y H:i:s');
        else if ($this->formato == "m-d-y")
            $value = $date->format('m-d-Y H:i:s');
        else if ($this->formato == "m-y-d")
            $value = $date->format('m-Y-d H:i:s');
        else if ($this->formato == "d-y-m")
            $value = $date->format('d-Y-m H:i:s');
        else if ($this->formato == "y-m-d")
            $value = $date->format('Y-m-d H:i:s');
        else if ($this->formato == "y-d-m")
            $value = $date->format('Y-d-m H:i:s');
        else
            $value = 'error.';
        return $value;
    }

    public function formatFechaMDA($fecha) {
        $m = explode("/", $fecha);
        if (count($m) > 1)
            $fecha = $m[1].'-'.$m[0].'-'.$m[2];
        $date = new DateTime($fecha);
        if ($this->formato == "d-m-y")
            $value = $date->format('d-m-Y H:i:s');
        else if ($this->formato == "m-d-y")
            $value = $date->format('m-d-Y H:i:s');
        else if ($this->formato == "m-y-d")
            $value = $date->format('m-Y-d H:i:s');
        else if ($this->formato == "d-y-m")
            $value = $date->format('d-Y-m H:i:s');
        else if ($this->formato == "y-m-d")
            $value = $date->format('Y-m-d H:i:s');
        else if ($this->formato == "y-d-m")
            $value = $date->format('Y-d-m H:i:s');
        else
            $value = 'error.';
        return $value;
    }
}
