<?php
/* Busqueda de registros de la tabla cr_pedido_prestamo redefinidos por el usuario. */
class Cr_pedido_prestamoFN extends BaseCr_pedido_prestamoFN
{
	 public function buildConsult()
    {
        $s_SQLQuery = "";
        if ( !is_null( $this->getIdpedido() ) ) $s_SQLQuery .= "idpedido = ".$this->getIdpedido()." AND ";
        if ( !is_null( $this->getTipo() ) ) $s_SQLQuery .= "tipo = '".$this->getTipo()."' AND ";
        if ( !is_null( $this->getIdCliente() ) ) $s_SQLQuery .= "cr_pedido_prestamo.IdCliente = ".$this->getIdCliente()." AND ";
        if ( !is_null( $this->getIidMercado() ) ) $s_SQLQuery .= "cr_pedido_prestamo.IidMercado = ".$this->getIidMercado()." AND ";
        if ( !is_null( $this->getIdColaborador() ) ) $s_SQLQuery .= "IdColaborador = ".$this->getIdColaborador()." AND ";
		if ( !is_null( $this->getFechaTransaccion() ) ) $s_SQLQuery .= "FechaTransaccion = '".$this->getFechaTransaccion()."' AND ";
        if ( !is_null( $this->getFechaEntregado() ) ) $s_SQLQuery .= "FechaEntrega like '".$this->getFechaEntregado()."%' AND ";        
        if ( !is_null( $this->getMonto() ) ) $s_SQLQuery .= "Monto = ".$this->getMonto()." AND ";
        if ( !is_null( $this->getGastoAdministrativo() ) ) $s_SQLQuery .= "GastoAdministrativo = ".$this->getGastoAdministrativo()." AND ";
        if ( !is_null( $this->getGastoAdministrativoFinal() ) ) $s_SQLQuery .= "GastoAdministrativoFinal = ".$this->getGastoAdministrativoFinal()." AND ";
        if ( !is_null( $this->getLimiteCredito() ) ) $s_SQLQuery .= "LimiteCredito = ".$this->getLimiteCredito()." AND ";
        if ( !is_null( $this->getTelefonoContacto() ) ) $s_SQLQuery .= "TelefonoContacto = ".$this->getTelefonoContacto()." AND ";
        if ( !is_null( $this->getIdPrestamo() ) ) $s_SQLQuery .= "IdPrestamo = ".$this->getIdPrestamo()." AND ";
        if ( !is_null( $this->getIdPrestamoReferencia() ) ) $s_SQLQuery .= "IdPrestamoReferencia = ".$this->getIdPrestamoReferencia()." AND ";
        if ( !is_null( $this->getSaldoAnterior() ) ) $s_SQLQuery .= "SaldoAnterior = ".$this->getSaldoAnterior()." AND ";
        if ( !is_null( $this->getMontoExtra() ) ) $s_SQLQuery .= "MontoExtra = ".$this->getMontoExtra()." AND ";
        if ( !is_null( $this->getInteresPorcentaje() ) ) $s_SQLQuery .= "InteresPorcentaje = ".$this->getInteresPorcentaje()." AND ";
        if ( !is_null( $this->getInteresPorcentajeFinal() ) ) $s_SQLQuery .= "InteresPorcentajeFinal = ".$this->getInteresPorcentajeFinal()." AND ";
        if ( !is_null( $this->getDuracion() ) ) $s_SQLQuery .= "Duracion = ".$this->getDuracion()." AND ";
        if ( !is_null( $this->getDuracionFinal() ) ) $s_SQLQuery .= "DuracionFinal = ".$this->getDuracionFinal()." AND ";
        if ( !is_null( $this->getPeridiocidadPago() ) ) $s_SQLQuery .= "PeridiocidadPago = '".$this->getPeridiocidadPago()."' AND ";
        if ( !is_null( $this->getPeridiocidadPagoFinal() ) ) $s_SQLQuery .= "PeridiocidadPagoFinal = '".$this->getPeridiocidadPagoFinal()."' AND ";
        if ( !is_null( $this->getHoraAtencion() ) ) $s_SQLQuery .= "HoraAtencion = '".$this->getHoraAtencion()."' AND ";
        if ( !is_null( $this->getFechaCreacion() ) ) $s_SQLQuery .= "FechaCreacion = '".$this->getFechaCreacion()."' AND ";
        if ( !is_null( $this->getUsuarioCreacion() ) ) $s_SQLQuery .= "UsuarioCreacion = '".$this->getUsuarioCreacion()."' AND ";
        if ( !is_null( $this->getFechaModificacion() ) ) $s_SQLQuery .= "FechaModificacion = '".$this->getFechaModificacion()."' AND ";
        if ( !is_null( $this->getUsuarioModificacion() ) ) $s_SQLQuery .= "UsuarioModificacion = '".$this->getUsuarioModificacion()."' AND ";
        if ( !is_null( $this->getEliminado() ) ) $s_SQLQuery .= "cr_pedido_prestamo.Eliminado = ".$this->getEliminado()." AND ";
        if ( !is_null( $this->getUsuarioEliminado() ) ) $s_SQLQuery .= "UsuarioEliminado = '".$this->getUsuarioEliminado()."' AND ";
        if ( !is_null( $this->getFechaEliminado() ) ) $s_SQLQuery .= "FechaEliminado = '".$this->getFechaEliminado()."' AND ";
        if ( !is_null( $this->getEstado() ) ) $s_SQLQuery .= "cr_pedido_prestamo.Estado = '".$this->getEstado()."' AND ";
        if ( !is_null( $this->getObservacion() ) ) $s_SQLQuery .= "Observacion = '".$this->getObservacion()."' AND ";
        
        
        if ( count( $this->a_Condition ) > 0 )
        {
            foreach ( $this->a_Condition as $a_Condition )
            {
                 if( $a_Condition['FLAG_COMILLAS'] )
                {
                    $s_SQLQuery .= $a_Condition[ "COLUMN" ]." ".$a_Condition[ "OPERATOR" ]." '".$a_Condition[ "VALUE" ]."' AND ";
                }
                else
                {
                    $s_SQLQuery .= $a_Condition[ "COLUMN" ]." ".$a_Condition[ "OPERATOR" ]." ".$a_Condition[ "VALUE" ]." AND ";
                }
            }
        }
        $s_SQLQuery .= "1 = 1 ";
        return $s_SQLQuery;
    }
    
    /* Busca varios registros por los campos establecidos */
    public function select( $i_Position = null, $i_Rows = null )
    {
        $s_SQLQuery = "SELECT ";
        $s_SQLQuery .= $this->getSelectColumns();
        $s_SQLQuery .= "FROM cr_pedido_prestamo 
        				LEFT JOIN cr_cliente on cr_cliente.idcliente = cr_pedido_prestamo.idcliente
        				WHERE ";
        
        $s_SQLQuery .= $this->buildConsult();
        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_pedido_prestamoBN = null;
        $o_Resource = $this->getConnect();
        //echo $o_Resource;
        //echo $s_SQLQuery;
        $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_pedido_prestamoBN = $this->insertObjectBN( $a_SQLRow );
            $a_Cr_pedido_prestamoBN[] = $o_Cr_pedido_prestamoBN;
        }
        return $a_Cr_pedido_prestamoBN;
    }
    
    public function selectNumRows()
    {
        $s_SQLQuery = "SELECT ";
        $s_SQLQuery .= "COUNT(*) AS cnt ";
        $s_SQLQuery .= "FROM cr_pedido_prestamo 
        				LEFT JOIN cr_cliente on cr_cliente.idcliente = cr_pedido_prestamo.idcliente
        				WHERE ";
        $s_SQLQuery .= $this->buildConsult();
        
        $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;
    }

}
?>