<?php

class Feeder {

    const NOAPLICA = 1;
    const REGMER = 2;
    const ESADL = 3;
    const RNT = 4;
    const RUP = 5;
    const RUNEOL = 6;
    const VEEDURIAS = 7;
    const JUEGOSAAR = 8;
    const SOLIDARIO = 10;
    const ONGEXTRANJERA = 11;
    
    public static function busquedaRegistro($documentoIdenti, $matricula, $razonSocial){
        $datosPersona = NULL;
        $datosPersona = self::busquedaRegMER($documentoIdenti, $matricula, $razonSocial);
        if(!isset($datosPersona) and is_null($datosPersona)){
            $datosPersona = self::busquedaRegESAL($documentoIdenti, $matricula, $razonSocial);
            if(!isset($datosPersona) and is_null($datosPersona)){
                $datosPersona = self::busquedaRegRUP($documentoIdenti, $matricula, $razonSocial);
                if(!isset($datosPersona) and is_null($datosPersona)){
                    $datosPersona = self::busquedaRegRUNEOL($documentoIdenti, $matricula, $razonSocial);
                    if(!isset($datosPersona) and is_null($datosPersona)){
                        if(is_null($documentoIdenti) and isset($matricula)){
                            $datosPersona = self::busquedaRegRNT($matricula, $razonSocial);
                        }     
                    }
                }
            }
        }
        return $datosPersona;
    }
    
    public static function busquedaRegMER($documentoIdenti, $matricula, $razonSocial){
        $sqlQuery = "SELECT DatosRegMER.* FROM  DatosRegMER "
        ."WHERE (DatosRegMER.datosMercantilIDENTIFICACION=? OR DatosRegMER.datosMercantilMATRICULA=? OR DatosRegMER.datosMercantilRAZONSOCIAL=? ) "
        ."ORDER BY CAST(DatosRegMER.datosMercantilMATRICULA AS DECIMAL ) DESC  ";
        $datos = FeederDB::selectUnaFila($sqlQuery, array($documentoIdenti, $matricula, $razonSocial));
        if(isset($datos)){    
            $persona = new stdClass();
            $persona->identificacion =$datos->datosMercantilIDENTIFICACION;
            $persona->matricula =$datos->datosMercantilMATRICULA;
            $persona->nombre=$datos->datosMercantilRAZONSOCIAL;
            $persona->email=$datos->datosMercantilCORREO;
            $persona->telefono=$datos->datosMercantilTELEFONO;
            $persona->direccion=$datos->datosMercantilDIRECCION;
            $persona->celular = $datos->datosMercantilCELULAR;
            $persona->direccionDOM = "";
            $persona->tipoRegistro = $datos->datosMercantilORGANIZACION;
            return $persona;
        }
        return NULL;
    }
    
    public static function busquedaRegESAL($documentoIdenti, $matricula, $razonSocial){
        $sqlQuery = "SELECT 	* FROM  DatosRegESAL 
        WHERE (DatosRegESAL.datosEsalIDENTIFICACION=? OR DatosRegESAL.datosEsalMATRICULA=? OR DatosRegESAL.datosEsalRAZONSOCIAL=?) 
        ORDER BY CAST(DatosRegESAL.datosEsalMATRICULA AS DECIMAL ) DESC ";
        $datos = SicamDB::selectUnaFila($sqlQuery, array($documentoIdenti, $matricula, $razonSocial));
        if(isset($datos)){    
            $persona = new stdClass();
            $persona->identificacion =$datos->datosEsalIDENTIFICACION;
            $persona->matricula =$datos->datosEsalMATRICULA;
            $persona->nombre=$datos->datosEsalRAZONSOCIAL;
            $persona->email=$datos->datosEsalCORREO;
            $persona->telefono=$datos->datosEsalTELEFONO;
            $persona->direccion=$datos->datosEsalDIRECCION;
            $persona->celular=$datos->datosEsalCELULAR;
            $persona->direccionDOM = "";
            $persona->tipoRegistro = $datos->datosEsalORGANIZACION;
            return $persona;
        }
        return NULL;
    }
    
    public static function busquedaRegRNT($matricula, $razonSocial){
        $sqlQuery = "SELECT 	* FROM  DatosRegRNT WHERE (DatosRegRNT.datosRntMATRICULA=? OR DatosRegRNT.datosRntRAZONSOCIAL=?)";
        $datos = SicamDB::selectUnaFila($sqlQuery, array($matricula, $razonSocial));
        if(isset($datos)){    
            $persona = new stdClass();
            $persona->identificacion = ""; //$datos->datosEsalIDENTIFICACION;
            $persona->matricula =$datos->datosRntMATRICULA;
            $persona->nombre=$datos->datosRntRAZONSOCIAL;
            $persona->email=$datos->datosRntCORREO;
            $persona->telefono=$datos->datosRntTELEFONO;
            $persona->direccion=$datos->datosRntDIRECCION;
            $persona->celular=$datos->datosRntCELULAR;
            $persona->direccionDOM = "";
            $persona->tipoRegistro = $datos->datosRntORGANIZACION;
            return $persona;
        }
        return NULL;
    }
    
    public static function busquedaRegRUNEOL($documentoIdenti, $matricula, $razonSocial){
        $sqlQuery = "SELECT 	* FROM  DatosRegRUNEOL WHERE (DatosRegRUNEOL.datosRuneolIDENTIFICACION=? OR DatosRegRUNEOL.datosRuneolMATRICULA=? OR DatosRegRUNEOL.datosRuneolRAZONSOCIAL=?)";
        $datos = SicamDB::selectUnaFila($sqlQuery, array($documentoIdenti, $matricula, $razonSocial));
        if(isset($datos)){    
            $persona = new stdClass();
            $persona->identificacion =$datos->datosRuneolIDENTIFICACION;
            $persona->matricula =$datos->datosRuneolMATRICULA;
            $persona->nombre=$datos->datosRuneolRAZONSOCIAL;
            $persona->email=$datos->datosRuneolCORREO;
            $persona->telefono='';
            $persona->direccion='';
            $persona->celular ="";
            $persona->direccionDOM = "";
            $persona->tipoRegistro = $datos->datosRuneolORGANIACION;
            return $persona;
        }
        return NULL;
    }
    
    public static function busquedaRegRUP($documentoIdenti, $matricula, $razonSocial){
        $sqlQuery = "SELECT 	* FROM  DatosRegRUP WHERE (DatosRegRUP.datosRupIDENTIFICACION=? OR DatosRegRUP.datosRupMATRICULA=? OR DatosRegRUP.datosRupRAZONSOCIAL=?)";
        $datos = SicamDB::selectUnaFila($sqlQuery, array($documentoIdenti, $matricula, $razonSocial));
        if(isset($datos)){    
            $persona = new stdClass();
            $persona->identificacion =$datos->datosRupIDENTIFICACION;
            $persona->matricula =$datos->datosRupMATRICULA;
            //$persona->nombre= $datos->datosRupNOMBRE1 ." ". $datos->datosRupNOMBRE2 ." ". $datos->datosRupAPELLIDO1 ." ". $datos->datosRupAPELLIDO2;
            $persona->nombre= $datos->datosRupRAZONSOCIAL;
            $persona->email=$datos->datosRuneolCORREO;
            $persona->telefono=$datos->datosRupTELEFONO;
            $persona->direccion=$datos->datosRupDIRECCION;
            $persona->celular = $datos->datosRupCELULAR;
            $persona->direccionDOM = "";
            $persona->tipoRegistro = $datos->datosRupORGANIZACION;
            return $persona;
        }
        return NULL;
        
    }
    
    public static function busquedaLikeRegistro($razonSocial){
        $ObjRegistros = self::busquedaLikeRegMER($razonSocial);
        array_push($ObjRegistros, self::busquedaLikeRegESAL($razonSocial));
        array_push($ObjRegistros, self::busquedaLikeRegRNT($razonSocial));
        array_push($ObjRegistros, self::busquedaLikeRegRUNEOL($razonSocial));
        array_push($ObjRegistros, self::busquedaLikeRegRUP($razonSocial));
        return $ObjRegistros;
    }
    
    public static function busquedaLikeRegMER($razonSocial){
        $sqlQuery = "SELECT datosMercantilRAZONSOCIAL FROM DatosRegMER WHERE datosMercantilRAZONSOCIAL LIKE '". $razonSocial . "%';";
        return SicamDB::selectVariasFilas($sqlQuery);
    }
    
    public static function busquedaLikeRegESAL($razonSocial){
        $sqlQuery = "SELECT datosEsalRAZONSOCIAL FROM DatosRegESAL WHERE datosEsalRAZONSOCIAL LIKE '". $razonSocial . "%'";
        return SicamDB::selectVariasFilas($sqlQuery, array($razonSocial));
    }
    
    public static function busquedaLikeRegRNT($razonSocial){
        $sqlQuery = "SELECT datosRntRAZONSOCIAL FROM DatosRegRNT WHERE datosRntRAZONSOCIAL LIKE '". $razonSocial . "%'";
        return SicamDB::selectVariasFilas($sqlQuery);
    }
    
    public static function busquedaLikeRegRUNEOL($razonSocial){
        $sqlQuery = "SELECT datosRuneolRAZONSOCIAL FROM DatosRegRUNEOL WHERE datosRuneolRAZONSOCIAL LIKE '". $razonSocial . "%'";
        return SicamDB::selectVariasFilas($sqlQuery, array($razonSocial));
    }
    
    public static function busquedaLikeRegRUP($razonSocial){
        $sqlQuery = "SELECT datosRupRAZONSOCIAL FROM DatosRegRUP WHERE datosRupRAZONSOCIAL LIKE '". $razonSocial . "%'";
        return SicamDB::selectVariasFilas($sqlQuery, array($razonSocial));
    }    
    
    public static function listRegistros(){
        $sqlQuery = "SELECT * FROM 	RegistrosPublicosDelegados ";
        return SicamDB::selectVariasFilas($sqlQuery);
    }
    
}
