<?

use_helper('UrlFunctions');
$o_ObjectBean = new PrestamoBean();

if($control){
	if(is_array($txtMontoPago)){
		while(list($idPrestamo,$MontoPago) = each($txtMontoPago)){
			if($MontoPago){
				$o_PagoBean = new PagoBean();
				$o_PagoBean->setIdPrestamo($idPrestamo);
				$o_PagoBean->setMontoPago($MontoPago);
				$o_PagoBL = new PagoBL();
				$o_PagoBL->insertarPago($o_PagoBean);
				
				$o_PrestamoBean = new PrestamoBean();
				$o_PrestamoBean->setIdPrestamo($idPrestamo);
				
				$o_Prestamo = $o_PrestamoBL->busquedaPrestamo();
				
				
				$SaldoAnterior = $o_Prestamo->getSaldoPrestamo();
				$NuevoSaldo = $SaldoAnterior - $MontoPago;
				$o_PrestamoActBean = new PrestamoBean();
				
				if($NuevoSaldo <= "0"){
					$NuevoSaldo 		= 0;
					$o_PrestamoActBean->setEstado("C");
					//Enviar Alerta------------
					//-------------------------
				}
				$o_PrestamoActBean->setSaldoPrestamo($NuevoSaldo);
				$o_PrestamoActBL = new PrestamoBL();
				$o_PrestamoActBL->modificarPrestamo($o_PrestamoActBean);
				
			}
		}
	}
	
	exit(" <script language=\"JavaScript\">
      parent.objMainTab.reqBody('".buildUrl('pag/list')."','&block=001&insert=1&idMercado=".$cboMercado."');
     </script>");
}

//COLOCAR CAMPOS DE LA CABECERA DEL LISTADO  

$o_ObjectBean->setIdMercado($cboMercado); 
$o_ObjectBean->setEstado("P");
	 
 
$o_class = new PrestamoBL();
$a_ObjectBean = $o_class->busquedaPrestamo($o_ObjectBean);

?>
<script>
	function VerificarSaldo(object,Saldo){
		Pago = parseFloat(object.value);
		if(parseFloat(Saldo) < Pago){
			alert('Pago invalido');
			return false;
		}
	}
</script>
<script type="text/javascript">
  validatePass = function(){
    valid.onSubmit = false;
  }
</script>

<form id="form1" name="form1" method="POST" onsubmit="validatePass();" >
 <input type="hidden" name="control" value="1">
 <input type="hidden" name="cboMercado" value="<?=$cboMercado?>">
 <input type="hidden" name="cboMercado" value="<?=$Descripcion?>">
    <table>
    	<tr>
    		<td>Mercado</td>
    		<td>:<?=$Descripcion?></td>
    	</tr>
    </table>  
	<table class="marco" border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td>
            <table class="grid" width="100%" border="0" cellpadding="0" cellspacing="0">
              <tr height="23">
                <th width="4%" >Nš Pre.</th>
			    <th width="20%" >Cliente&nbsp;&nbsp;&nbsp;</th>
			    <th width="4%" >F. Ini&nbsp;&nbsp;&nbsp;</th>
				<th width="4%" >F. Fin&nbsp;&nbsp;&nbsp;</th>
				<th width="5%" >Capital &nbsp;&nbsp;&nbsp;</th>
                <th width="3%" >(%) &nbsp;&nbsp;&nbsp;</th>
                <th width="6%" >M. Total &nbsp;&nbsp;&nbsp;</th>
                <th width="4%" >Saldo &nbsp;&nbsp;&nbsp;</th>
                <th width="6%" >C. Pago &nbsp;&nbsp;&nbsp;</th>
                <th width="5%" >Estado &nbsp;&nbsp;&nbsp;</th>
                <th width="5%" >Ing. Pago &nbsp;&nbsp;&nbsp;</th>
               </tr>
              <? if(count($a_ObjectBean)>0){ ?>
	              <? foreach ($a_ObjectBean as $iKey => $o_objBean){ ?>
	              <tr onClick="TR_click(this);">
	                <td align="center"><?=$o_objBean->getIdPrestamo()?></td>
	                <td align="left"><?=$o_objBean->getObjectCliente()->getNombreCompleto()?></td>
	                <td><?=$o_objBean->getFechaInicio()?></td>
	                <td><?=$o_objBean->getFechaVencimiento()?></td>  
					<td align="center"><?=$o_objBean->getMontoPrestado()?></td>
					<td align="center"><?=$o_objBean->getInteresPorcentaje()?></td>
					<td align="center"><?=$o_objBean->getMontoTotal()?></td>
					<td align="center"><?=$o_objBean->getSaldoPrestamo()?></td>
					<td align="center"><?=$o_objBean->getCuotaPago()?></td>
					<td align="center"><?=$o_objBean->getEstado()?></td>
					<td nowrap="center"><input class="text" type="text" style="text-align:right;" name="txtMontoPago[<?=$o_objBean->getIdPrestamo()?>]" id="txtMontoPago[]" size="5" maxlength="5" align="right" onkeyup="return VerificarSaldo(this,'<?=$o_objBean->getSaldoPrestamo()?>');"/></td>				
	              </tr>
	              <? } ?>
	              <tr>
	              	<td colspan="11" align="right"><br><input class="button" type="submit" name="btnGrabar"  id="btnGrabar" value="Grabar Pagos"><br><br></td>
	              </tr>
              <? }else{ ?>
              <tr>
                <td colspan="7" align="center">No se produjo resultado</td>
              </tr>
              <? } ?>
            </table>
          </td>
        </tr>
        <tr>
          <td>Total Registros: <?=count($a_ObjectBean)?></td>
        </tr>
      </table>
</form>
<script type="text/javascript">
	function formCallback(result, form) {
		window.status = "valiation callback for form '" + form.id + "': result = " + result;
	}
	var valid = new Validation('form1', {immediate : true});
</script>