<?php
use_helper('UrlFunctions');
$o_ObjectBean = new PrestamoBean();

$con = Conexion::getConexion();

$sql = "SELECT id_pre_$ind
		FROM `dw_matriz`
		WHERE 	`fecha` = '".$fecha."' 
				 and `idmercado` = '".$idmercado."' 
				 and `idsucursal` = '".$idsucursal."' 
				 and `idcolaborador` = '".$idcolaborador."' 
				";

$result = mysql_query($sql,$con);

$num_rows = mysql_num_rows($result);
    

if($num_rows > 0){
  while ($o_valor = mysql_fetch_object($result)) {
  	$idsPrestamos = $o_valor->{"id_pre_".$ind};
  	$a_idsPrestamos = explode(",",$idsPrestamos);
  }
}


$o_PrestamoBean = new PrestamoBean();
$o_PrestamoBean->setIdsPrestamos($a_idsPrestamos);

$o_class = new PrestamoBL();
$a_ObjectBean = $o_class->busquedaPrestamo($o_PrestamoBean);  ;

?>
      <table class="marco" border="0" cellpadding="0" cellspacing="0" width="100%">
        
        <tr>
          <td>
            <table class="grid" width="100%" border="1" cellpadding="0" cellspacing="0">
              <tr height="23">
                <th width="4%" >Nro</th>
                <th width="3%" >N.Clie.</th>
                <th width="3%" >N.Pre.</th>
                <th width="3%" >Origen</th>
                <th width="21%" >Cliente&nbsp;</th>
			    <th width="3%" >F.Inicio</th>
				<th width="3%" >F.Vencim.</th>
				<th width="3%" >F.Cancel.</th>
				<th width="3%" >Estado.</th>
				<th width="5%" >Capital &nbsp;</th>
				 <th width="4%" >M.Total&nbsp;</th>
				 <th width="4%" >Saldo&nbsp;</th>
                <th width="3%" >Interes</th>
                <th width="3%" >G.A.</th>
                <th width="3%" >Mora</th>
                <th width="3%" id="col01" onMouseOver="sort_col(this);" >Usr.Creac.</th>
                <th width="4%" id="col01" onMouseOver="sort_col(this);" >Fec.Creac.</th>
               
              </tr>
              <? if(count($a_ObjectBean)>0){ ?>
              <?
              	$o=1;
              	foreach ($a_ObjectBean as $iKey => $o_objBean){?>
	              <tr onClick="TR_click(this);" <? if($color) echo "bgcolor='".$color."'"?>>
	              
	                <td align="left"><?=$o;?></td>
	                <td align="left"><a style="cursor:pointer;text-decoration:underline;color:#00f;" href="#" onclick="objMainTab.modalDialog('<?php echo buildUrlComplet( 'op.php/ajax/requestPdo', 's_TypeRequest=infopopupprestamo&i_idcliente='.$o_objBean->getIdCliente().'&s_tipo=' )?>',{width:700,height:400,nameOtherWindow:'ov1'})"><?=$o_objBean->getIdCliente()?></a></td>
	                <td align="center"><a style="cursor:pointer;text-decoration:underline;color:#00f;" href="#" onclick="objMainTab.modalDialog('<?=buildUrlComplet('mnt.php/pre/estado_cuenta/id/'.$o_objBean->getIdPrestamo());?>',{title:'',width:710,height:520,nameOtherWindow:'ov'})"><?=$o_objBean->getIdPrestamo()?></a></td>
	                <td align="center"><?=$o_objBean->getOrigen()?></td>
	                <td align="left"><?=ucwords(strtolower($o_objBean->getObjectCliente()->getNombreCompleto()))?></td>
	                <td><?=$o_objBean->getFechaInicio()?></td>
	                
	                <td><?=$o_objBean->getFechaVencimiento()?></td>  
	                <td><?=substr($o_objBean->getFechaCancelacion(),0,10)?></td>
	                <td><? if($o_objBean->getEstado() == "P") echo "Pendiente";elseif($o_objBean->getEstado() == "C")  echo "Cancelado";?></td>
					<td align="center"><?=number_format($o_objBean->getMontoPrestado(),2);$totalMontoPrestado += $o_objBean->getMontoPrestado();?></td>
					<td align="center"><?=number_format($o_objBean->getMontoTotal(),2);$totalMonto += $o_objBean->getMontoTotal();?></td>
					<td align="center"><?=number_format($o_objBean->getSaldoPrestamo(),2);$totalSaldoPrestamo += $o_objBean->getSaldoPrestamo();?></td>
					<td align="center"><?=$o_objBean->getInteresMonto();$totalMontoInteres += $o_objBean->getInteresMonto();?></td>
					<td align="center"><?=$o_objBean->getGastoAdministrativo();$totalMontoGA += $o_objBean->getGastoAdministrativo();?></td>
					<td align="center"><?php 
						$o_PagoBean = new PagoBean();
	                    $o_PagoBean->setIdPrestamo($o_objBean->getIdPrestamo());
	                    $o_PagoBean->setTipo('2');
	                    $o_PagoDP = new PagoDP($o_PagoBean);
	                    $MontoPago = '0';
	                    $a_o_PagoDP = $o_PagoDP->select();
	                    if(is_array($a_o_PagoDP)){
	                    	foreach ($a_o_PagoDP as $key => $o_PagoDP){
	                    		$MontoPago += $o_PagoDP->getMontoPago();
	                    		$totalMontoMora += $MontoPago;
	                    	}
							
	                    }
	                    echo number_format($MontoPago,2,'.','');
					?></td>
					
					
					
					
	                <td align="center"><?=ucwords(strtolower($o_objBean->getUsuarioCreacion()))?></td>
	                <td align="center"><?=$o_objBean->getFechaCreacion(date("Y-m-d"))?></td>
	                
              </tr>
              <? $o++;} ?>
              <tr>
             
              	<td colspan="9" align="right">&nbsp;</td>
              	<td align="right"><b><?php echo number_format($totalMontoPrestado,2)?></b></td>
              	<td align="right"><b><?php echo number_format($totalMonto,2)?></b></td>
              	<td align="right"><b><?php echo number_format($totalSaldoPrestamo,2)?></b></td>
              	<td align="right"><b><?php echo number_format($totalMontoInteres,2)?></b></td>
              	<td align="right"><b><?php echo number_format($totalMontoGA,2)?></b></td>
              	<td align="right"><b><?php echo number_format($totalMontoMora,2)?></b></td>
              	
              	
              </tr>
              <? }else{ ?>
              <tr>
                <td colspan="13" align="center">No se produjo resultado</td>
              </tr>
              <? } ?>
            </table>
          </td>
        </tr>
        <tr>
          <td>Total Registros: <?=count($a_ObjectBean)?></td>
        </tr>
      </table>