<?
header("Content-Type: application/xhtml+xml; charset=iso-8859-1");
use_helper('UrlFunctions');
use_helper('UtilFunctions');
$o_ObjectBean = new PrestamoBean();
//print_r($_GET);
$tamanioPag = 20;
if($cboEstadoPrestamo == "C"){
	$tamanioPag = 150;
}

//COLOCAR CAMPOS DE LA CABECERA DEL LISTADO  
//$o_ObjectBean->setIdMercado($cboMercado); 

//FILTRO MERCADO------------------------------------------------------------------------
//Obtener Sucursal------------------------------------
//Id del Administrador--------------------------------

if($idcliente   ==""){ 
	$idcliente = "x";
}
$o_ObjectBean->setIdCliente($idcliente); 
$o_ObjectBean->setEstado("P"); 

//echo "ssd".$txtNombre;

$o_ObjectBean->page = $page;
$o_ObjectBean->tamPag = $tamanioPag;
$o_ObjectBean->sortCampos = $sortCampos;
$o_ObjectBean->sortType   = $sortType;
 
$o_class = new PrestamoBL();
$a_ObjectBean = $o_class->busquedaPrestamo($o_ObjectBean);  

$o_navigation = new navigation();
$o_navigation->setPaginaActual($page);
$o_navigation->setTamanioPagina($tamanioPag);
$o_navigation->setTotalRegistros($o_class->getCountResult($o_ObjectBean));
$valuesSort = array('sortCampos'=>$sortCampos, 'sortType'=>$sortType);

if(!$page) $page = 1;
$totalMonto = $o_class->getTotalMonto($o_ObjectBean);
ViewResponse::getInstance()->setMethodAjax();

?>
<script>
functionSort = function(campo, type){
  var typeSort      = "";
  var elmSortCampos = $('sortCampos');
  var elmSortType   = $('sortType');
  var a_sortCampos  = elmSortCampos.value!=""?elmSortCampos.value.split(";"):[];
  var a_sortType    = elmSortType.value!=""?elmSortType.value.split(";"):[];
  var pos           = a_sortCampos.indexOf(campo);
  switch (type){
     case 'A' :
       a_sortType[pos]    = "2";
     break;
     case 'D' :
       a_sortCampos = sliceByPos(a_sortCampos, pos);
       a_sortType   = sliceByPos(a_sortType, pos);
     break;
     default :
       a_sortCampos[a_sortCampos.length] = campo;
       a_sortType[a_sortType.length]     = "1";
     break;
  }
  var strSortCampos = a_sortCampos.join(";");
  var strSortType   = a_sortType.join(";");
  concatValueStr('sortCampos='+strSortCampos+'&sortType='+strSortType+"&page=<?=$page?>");
}
</script>

<?php

$strConcat = "sortCampos=".$valuesSort['sortCampos']."&sortType=".$valuesSort['sortType'];

function showImgOrder($valuesSort, $campo, $nameCampos){
  $a_valCampos = explode(";", $valuesSort['sortCampos']);
  $a_valtype   = explode(";", $valuesSort['sortType']);
  $sImg = "
            <a href=\"JavaScript:functionSort('".$campo."', '');\">".$nameCampos."</a>
            <img src=\"/images/common/buttons/arrow.gif\" alt=\"Ordenado Ascendente\" width=\"8\" height=\"10\" align=\"absmiddle\" />
          ";
  foreach ($a_valCampos as $key => $val){
    if($val == $campo){
      switch ($a_valtype[$key]){
        case 1:
          $sImg = "
                    <a href=\"JavaScript:functionSort('".$campo."', 'A');\">".$nameCampos."</a>
                    <img src=\"/images/common/buttons/arrow_up.gif\" alt=\"Ordenado Ascendente\" width=\"8\" height=\"10\" align=\"absmiddle\" />
                  ";
        break;
        case 2:
          $sImg = "
                    <a href=\"JavaScript:functionSort('".$campo."', 'D');\">".$nameCampos."</a>
                    <img src=\"/images/common/buttons/arrow_down.gif\" alt=\"Ordenado Ascendente\" width=\"8\" height=\"10\" align=\"absmiddle\" />
                  ";
        break;
      }
    }
  };
  return $sImg;
}
?>

<input type="hidden" id="sortCampos" size="100" value="<?=$valuesSort['sortCampos']?>">
<input type="hidden" id="sortType" size="100" value="<?=$valuesSort['sortType']?>">
      <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%" >Nro</th>-->
                
                <th width="3%" align="left">N.Pre.</th>
                <th width="13%" align="left" >Cliente&nbsp;</th>
			    <th width="5%" align="left">F.Inicio</th>
				<th width="5%" align="left">F.Vencim.</th>
				<th width="5%" >Capital &nbsp;</th>
                <th width="3%" >%</th>
                <th width="4%" >M.Total&nbsp;</th>
                <th width="3%" >Saldo&nbsp;</th>
                
                <th width="3%" >Monto</th>
                <th width="3%" >N.Letra</th>
				
				<th width="8%">Avance</th>
                <th width="3%" >Estado &nbsp;&nbsp;&nbsp;</th>
                <th width="3%" id="col01" onMouseOver="sort_col(this);" >Usr.Creac.</th>
                
                
              </tr>
              <? if(count($a_ObjectBean)>0){ ?>
              <? foreach ($a_ObjectBean as $iKey => $o_objBean){ 	
              	$color = '';
              	if($o_objBean->getSaldoPrestamo() >= $montoTotal){
              		//echo "entro";
              		$amortizo = $montoTotal;
              		$montoTotal = 0;
              	}else{
              		$amortizo = $o_objBean->getSaldoPrestamo();
              		$montoTotal = $montoTotal - $amortizo; 
              	}
              	if($o_objBean->getEstado()=='C' && $o_objBean->getSaldoPrestamo()> 0){
              		 $con = Conexion::getConexion();
  
					  $sql = "SELECT * FROM cr_prestamo where IdPrestamoReferencia = '".$o_objBean->getIdPrestamo()."'";
					  $result = mysql_db_query(Conexion::getNameDb(), $sql, $con);
					  if(mysql_num_rows($result) == 0) $color = "#FF6666";
              		
              	}?>
              <tr onClick="TR_click(this);" <? if($color) echo "bgcolor='".$color."'"?>>
              <tr onClick="TR_click(this);">
                
                <td align="left"><?=$o_objBean->getIdPrestamo()?></td>
                <td align="left"><?=ucwords(strtolower($o_objBean->getObjectCliente()->getNombreCompleto()))?></td>
                <td><?=$o_objBean->getFechaInicio()?></td>
                <td><?=$o_objBean->getFechaVencimiento()?></td>  
				<td align="center"><?=number_format($o_objBean->getMontoPrestado(),2)?></td>
				<td align="center"><?=$o_objBean->getInteresPorcentaje()?></td>
				<td align="center"><?=number_format($o_objBean->getMontoTotal(),2)?></td>
				<td align="right"><?php echo number_format($o_objBean->getSaldoPrestamo(),2);$total_saldo += round($o_objBean->getSaldoPrestamo(),2);?></td>
				<td align="right">
				<input type="hidden" class="montoSaldos" name="montoSaldos" value="<?php echo $o_objBean->getSaldoPrestamo();?>">
				<input type="text" class="text required montos validate-MontoAmortizado" style="text-align:right" size="6" name="monto[<?=$o_objBean->getIdPrestamo()?>]" value="<?php echo $amortizo;?>">
				</td>
				
				<td align="left" <?php if($o_objBean->getEstado()=='P') echo "style='color:#0000FF'";else echo "style='color:#FF0000'"?>><?php echo $o_objBean->getNumeroLetra();?></td>
				
				<td><?php echo pintarBarra($o_objBean->getIdPrestamo())?>
				<td align="center"><?if($o_objBean->getEstado()=='P'){echo "Pendiente";} elseif($o_objBean->getEstado()=='C') {echo "Cancelado";}?></td>
                <td align="center"><?=ucwords(strtolower($o_objBean->getUsuarioCreacion()))?></td>
                
                
              </tr>
              <? } ?>
              <tr>
             
              	<td colspan="7" align="right"></td>
              	<td align="right"><b><?php echo number_format($totalMonto,2)?></b></td>
              </tr>
              <tr>
              	<td colspan="13" align="center">
              		<table hspace="2px" width="96%" cellpadding="0" cellspacing="0">
						<tr>
							<td valign="top" colspan="2">
	                            
	                          					
									<table width="100%" border="0" cellpadding="0" cellspacing="0" style="background: #fff">
										<tr>
											<td height="0" width="100%">
											   
	
												   <table width="100%" border="0" cellspacing="0" cellpadding="0">
														<tr>
															<td><img src="/images/common/spacer.gif" width="10"></td>
														</tr>
														<tr>
														    <td align="center">
																<input type="hidden" name="typeSubmit" id="typeSubmit">
																<input class="button" type="submit" value="Grabar" onclick="$('typeSubmit').value='ok';">
																<input class="button" type="button" value="Cancelar" onclick="location.href='<?php echo buildUrl('cli/list', $s_Param."&block=001") ?>'"/>
														    </td>
														</tr>
														<tr>
															<td height="10">&nbsp;</td>
														</tr>
													</table>
												
											</td>
										</tr>
									</table>							
									
							</td>
						</tr>
					</table> 
              	</td>
              </tr>
              
              
              <? }else{ ?>
              <tr>
                <td colspan="7" align="center">No se produjo resultado</td>
              </tr>
              
              <? } ?>
            </table>
          </td>
        </tr>
       
      </table>