<?
header("Content-Type: application/xhtml+xml; charset=utf-8");
use_helper('UrlFunctions');
$o_ObjectBean = new PrestamoBean();
  

$tamanioPag = 100;

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

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

$o_ObjectBean->setIdMercado($cboMercado);
$o_ObjectBean->setIdColaborador($cboRepresentante);
//echo $cbolapso."---";
$a_lapso = explode("-",$cbolapso);

//print_r($a_lapso);
//echo $a_lapso[0]."*-*-*-*-";
$o_ObjectBean->setLapso1($a_lapso[0]);
if($a_lapso[1] < 9999) $o_ObjectBean->setLapso2($a_lapso[1]);
$o_ObjectBean->setFechaE(date('Y-m-d'));

//https://es.stackoverflow.com/questions/478196/en-mysql-como-sumar-valores-maximos-de-un-rango-de-fecha

//https://es.stackoverflow.com/questions/271374/seleccion-entre-fechas-mysql

$o_ObjectBean->page = $page;
$o_ObjectBean->tamPag = $tamanioPag;
$o_ObjectBean->sortCampos = $sortCampos;
$o_ObjectBean->sortType   = $sortType;

$o_PrestamoDP = new PrestamoDP($o_ObjectBean);
$a_ObjectBean = $o_PrestamoDP->selectRecaptacion();


if(!$page) $page = 1;

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


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 height="17" align="right" class="pager">
            <div id="contenedor">
              <? $o_navigation->ContruirHTMLNavigation('contenedor', $strConcat); ?>
            </div>
          </td>
        </tr>
        <tr>
          <td>
            <table class="grid" width="100%" border="0" cellpadding="0" cellspacing="0">
              <tr height="23">
               <th width="4%" >Nro</th>
               <th width="8%" align="left">Cod. Cliente</th>
                 <th width="35%" align="left" >Cliente&nbsp;</th>
                 <th width="12%" align="left" >Documento</th>
                 <th width="5%" align="left" >Dni repetidos</th>
			    <th width="4%" >IdPRestamo</th>
			    <th width="20%" >Ultimo Monto Total</th>
			    <th width="20%" align="right">Ultima F. Cancelacion</th>
				<th width="10%" >N.Prestamos</th>
				
              </tr>
              <? if(count($a_ObjectBean)>0){
				$i=1;
				foreach ($a_ObjectBean as $iKey => $o_objBean){ 
             	?>
              <tr onClick="TR_click(this);" <? if($color) echo "bgcolor='".$color."'"?>>
            
                
                <td><?=($tamanioPag*($page-1)) + $i;?></td>
                <!--<td><?=$o_objBean->getIdCliente()?></td>-->
                <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:950,height:400})"><?php echo $o_objBean->getIdCliente()?></a>
                <td><?=$o_objBean->getNombreCompleto()?></td>  
                <td><?=$o_objBean->getObservacion()?></td>  
                
                <td><?php 
                //Buscar si el dni esta repetido
                if($o_objBean->getOrigen() > 0 ){
	                $o_ClienteBeanT = new ClienteBean();
	                $o_ClienteBeanT->setDni($o_objBean->getOrigen());
	                $o_ClienteBeanT->setApellidoPaterno($o_objBean->getEstado());
	                $o_ClienteBeanT->setApellidoMaterno($o_objBean->getInteresMonto());
	                
	                $o_ClienteDP = new ClienteDP($o_ClienteBeanT);
	                $a_o_clientes = $o_ClienteDP->select();
	                
	                if(is_array($a_o_clientes) && count($a_o_clientes) > 1 ){
	                	foreach ($a_o_clientes as $key => $o_ClienteBean){
	                		//echo $o_ClienteBean->getIdCliente()."|";
	                		if($o_ClienteBean->getIdCliente() != $o_objBean->getIdCliente()){?>
	                			<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_ClienteBean->getIdCliente().'&s_tipo=' )?>',{width:700,height:400})"><?=$o_ClienteBean->getIdCliente()?></a>&nbsp;
	                		<?php
	                		}
	                	}
	                }
                }
                
				$o_PrestamoBeanT = new PrestamoBean();
				$o_PrestamoBeanT->setIdPrestamo($o_objBean->getIdPrestamo());
			
				$o_PrestamoDPT = new PrestamoDP($o_PrestamoBeanT);
				$o_prestamo = $o_PrestamoDPT->selectByPk();
				
				$MontoTotal =  $o_prestamo->getMontoTotal();
					
				                
                ?></td>  
				<td align="center"><?=$o_objBean->getIdPrestamo()?></td>  
				<td align="center"><?=$MontoTotal?></td>  
				<td align="center"><?=substr($o_objBean->getFechaCancelacion(),0,10)?></td>
				<td align="center"><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})"><?=$o_objBean->getMontoPrestado()?></a></td>
                
              </tr>
              <?php 
				$i++;
				} ?>
             
              <? }else{ ?>
              <tr>
                <td colspan="7" align="center">No se produjo resultado</td>
              </tr>
              <? } ?>
            </table>
          </td>
        </tr>
        <tr>
          <td>Total Registros: <?php echo $o_PrestamoDP->selectNumRowsRecaptacion();?></td>
        </tr>
      </table>