<?php
/* Busqueda de registros de la tabla cr_cliente_file redefinidos por el usuario. */
class Cr_cliente_fileFN extends BaseCr_cliente_fileFN
{
	
	public function selectNumRowsRep()
    {
        $s_SQLQuery = "SELECT ";
        $s_SQLQuery .= "COUNT(*) AS cnt ";
        $s_SQLQuery .= "FROM cr_cliente_file 
        				LEFT JOIN cr_cliente on cr_cliente.idcliente = cr_cliente_file.idcliente
        				WHERE ";
        $s_SQLQuery .= $this->buildConsultRep();

        $o_Resource = $this->getConnect();
        $o_SQLResult = mysql_query( $s_SQLQuery, $o_Resource );
        if ( $o_SQLResult === false )
        {
            throw new Exception( mysql_error() );
        }

        $num_rows = 0;
        if ( $a_SQLRow = mysql_fetch_object( $o_SQLResult ) )
        {
            $num_rows = $a_SQLRow->cnt;
        }
        return $num_rows;
    }

    /* Busca varios registros por los campos establecidos */
    public function selectRep( $i_Position = null, $i_Rows = null )
    {
        $s_SQLQuery = "SELECT ";
        $s_SQLQuery .= $this->getSelectColumns();
        $s_SQLQuery .= "FROM cr_cliente_file 
        				LEFT JOIN cr_cliente on cr_cliente.idcliente = cr_cliente_file.idcliente
        				WHERE ";
        $s_SQLQuery .= $this->buildConsultRep();
        if ( count( $this->a_OrderBy ) > 0 )
        {
            $s_SQLQuery = $s_SQLQuery."ORDER BY ";
            $s_Separator = "";
            foreach ( $this->a_OrderBy as $a_OrderBy )
            {
                $s_SQLQuery .= $s_Separator.$a_OrderBy[ "COLUMN" ]." ";
                if ( $a_OrderBy[ "ASC" ] === false ) $s_SQLQuery .= "DESC ";
                $s_Separator = ", ";
            }
        }
        if( !is_null( $i_Position ) && is_null( $i_Rows ) ) $s_SQLQuery .= "LIMIT ".$i_Position;
        if( !is_null( $i_Position ) && !is_null( $i_Rows ) ) $s_SQLQuery .= "LIMIT ".$i_Position.", ".$i_Rows;
        $s_SQLQuery .= ";";
//echo $s_SQLQuery."*************";
        $a_Cr_cliente_fileBN = null;
        $o_Resource = $this->getConnect();
        $o_SQLResult = mysql_query( $s_SQLQuery, $o_Resource );
        if ( $o_SQLResult === false )
        {
            throw new Exception( mysql_error() );
        }
        while ( $a_SQLRow = mysql_fetch_object( $o_SQLResult ) )
        {
            $o_Cr_cliente_fileBN = $this->insertObjectBN( $a_SQLRow );
            $a_Cr_cliente_fileBN[] = $o_Cr_cliente_fileBN;
        }
        return $a_Cr_cliente_fileBN;
    }
    
     public function buildConsultRep()
    {
        $s_SQLQuery = "";
        if ( !is_null( $this->getIdClienteFile() ) ) $s_SQLQuery .= "IdClienteFile = ".$this->getIdClienteFile()." AND ";
        if ( !is_null( $this->getIdCliente() ) ) $s_SQLQuery .= "cr_cliente_file.idcliente = ".$this->getIdCliente()." AND ";
        if ( !is_null( $this->getFileNumero() ) ) $s_SQLQuery .= "FileNumero = '".$this->getFileNumero()."' AND ";
        if ( !is_null( $this->getEstado() ) ) $s_SQLQuery .= "Estado = '".$this->getEstado()."' AND ";
        if ( !is_null( $this->getNombreFile() ) ) $s_SQLQuery .= "NombreFile = '".$this->getNombreFile()."' AND ";
        if ( !is_null( $this->getFechaEliminado() ) ) $s_SQLQuery .= "cr_cliente_file.FechaEliminado = '".$this->getFechaEliminado()."' AND ";
        if ( !is_null( $this->getUsuarioEliminado() ) ) $s_SQLQuery .= "cr_cliente_file.UsuarioEliminado = '".$this->getUsuarioEliminado()."' AND ";
        if ( !is_null( $this->getEliminado() ) ) $s_SQLQuery .= "cr_cliente_file.Eliminado = ".$this->getEliminado()." AND ";
        if ( !is_null( $this->getFechaRegistro() ) ) $s_SQLQuery .= "cr_cliente_file.FechaRegistro = '".$this->getFechaRegistro()."' AND ";
        if ( !is_null( $this->getUsuarioRegistro() ) ) $s_SQLQuery .= "cr_cliente_file.UsuarioRegistro = '".$this->getUsuarioRegistro()."' AND ";
        if ( !is_null( $this->getIdtipofile() ) ) $s_SQLQuery .= "cr_cliente_file.idtipofile = ".$this->getIdtipofile()." AND ";
       
		if($this->getIdMercado()){
			$s_SQLQuery .= " cr_cliente.idmercado  = ".$this->getIdMercado()." AND ";
		}else{
			 if($this->getIdSucursal()){
					$s_SQLQuery .= " cr_cliente_file.idcliente in 
										(
										select idcliente 
										from cr_cliente 
										where idmercado in 
														(
														select cr_mercado.idmercado 
														from cr_sucursal_mercado
														left join cr_mercado on cr_mercado.idmercado = cr_sucursal_mercado.idmercado
														where cr_sucursal_mercado.idSucursal = ".$this->getIdSucursal()."
														)
										) AND ";
			}
		}
		if($this->getApellidoPaterno()){
			$s_SQLQuery .= "  ApellidoPaterno like '".$this->getApellidoPaterno()."%' AND ";
		}
		if($this->getApellidoMaterno()){
		$s_SQLQuery .= "  ApellidoMaterno like '".$this->getApellidoMaterno()."%' AND ";
		}
		if($this->getNombres()){
		$s_SQLQuery .= "  Nombres like '%".$this->getNombres()."%' AND ";
		}
        if ( count( $this->a_Condition ) > 0 )
        {
            foreach ( $this->a_Condition as $a_Condition )
            {
                $s_SQLQuery .= $a_Condition[ "COLUMN" ]." ".$a_Condition[ "OPERATOR" ]." '".$a_Condition[ "VALUE" ]."' AND ";
            }
        }
        $s_SQLQuery .= "1 = 1 ";
        return $s_SQLQuery;
    }
}
?>