<?
header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=busqueda_promedios.xls");
ViewResponse::getInstance()->setMethodAjax();
$o_ObjectBean = new PromedioPrestamoBean();
$tamanioPag = 10;

$o_ObjectBean->sortCampos = $sortCampos;
$o_ObjectBean->sortType   = $sortType;


if(myUser::getUser()->getIdPadre() == "1"){
  	$o_ColaboradorBean_Temp = new ColaboradorBean();
  	$o_ColaboradorBean_Temp->setIdColaborador(myUser::getUser()->getIdColaborador()); 
  	$o_ColaboradorBL_Temp = new ColaboradorBL();
  	$o_ColaboradorBeanResult = $o_ColaboradorBL_Temp->busquedaColaborador($o_ColaboradorBean_Temp);
  	 	
  	$IdPadre = $o_ColaboradorBeanResult[0]->getIdPadre();
  	
}else{//Solo los Hijos del Colaborador
  	$IdPadre = myUser::getUser()->getIdColaborador();
}

//Sacar el nombre de la sucursal
$o_SucursalBean = new SucursalBean();
$o_SucursalBean->setIdAdministrador($IdPadre);


$o_SucursalBL = new SucursalBL();
$a_o_sucursal = $o_SucursalBL->busquedaSucursal($o_SucursalBean);
if(is_array($a_o_sucursal)){
	foreach ($a_o_sucursal as $key => $o_sucursal){
		$IdSucursal 	= $o_sucursal->getIdSucursal();
		$NombreSucursal = $o_sucursal->getDescripcion();
		$FechaIniCiclico = $o_sucursal->getFechaSaldoIni();
	}
}

$o_ObjectBean->setIdSucursal($IdSucursal); 
if($cboColaborador   !="")	{ $o_ObjectBean->setIdRepresentante($cboColaborador); }
if($txtFechaInicial   !="")	{ $o_ObjectBean->setFecha($txtFechaInicial); }
if($txtFechaFinal   !="")	{ $o_ObjectBean->setFechaFinal($txtFechaFinal); }
$o_PromedioPrestamo = new PromedioPrestamoDP($o_ObjectBean);
$a_ObjectBean = $o_PromedioPrestamo->select();


?>
<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
$page = 1;
$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;
}
?>
<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="2%" >Nro</th>
                <th width="8%" align="left"  onMouseOver="sort_col(this);" >Sucursal</th>
                <th width="8%" align="left"  onMouseOver="sort_col(this);" >Fecha</th>
                <th width="20%" align="left"  onMouseOver="sort_col(this);" >Representante</th>
                <th width="8%" align="right"  onMouseOver="sort_col(this);" >P.Capital</th>
                <th width="3%" align="right"  onMouseOver="sort_col(this);" >P.Monto</th>
               	 <th width="8%" align="right"  onMouseOver="sort_col(this);" >Cap.Efec.</th>
                <th width="8%" align="right"  onMouseOver="sort_col(this);" >P.%&nbsp;Inter&eacute;s</th>
                <th width="8%" align="right"  onMouseOver="sort_col(this);" >P.#&nbsp;Prestamos</th>
                 <th width="8%" align="center"  onMouseOver="sort_col(this);" >P.S.V.</th>
               
                <!--<th width="8%" align="right"  onMouseOver="sort_col(this);" >#D&iacute;as Prom.</th>-->
				<th width="3%"></th>
              </tr>
              <? if(count($a_ObjectBean)>0){ ?>
                <? foreach ($a_ObjectBean as $iKey => $o_objBean){ 
                	
                
                ?>
                <tr onClick="TR_click(this);">
                  <td class="chk"><?=($tamanioPag)*($page-1)+($iKey+1)?></td>
                  <td><?=$o_objBean->getNombreSucursal() ?></td>
                  <td><?=$o_objBean->getFecha() ?></td>
                  <td><?=$o_objBean->getNombreRepresentante() ?></td>
                  <td align="right"><?=number_format($o_objBean->getCapital(),2); $s_Capital += round($o_objBean->getCapital(),2) ?></td>
                  <td align="right"><?=number_format($o_objBean->getMontoTotal(),2) ;$s_MontoTotal += round($o_objBean->getMontoTotal(),2)?></td>
                   <td align="right"><?=number_format($o_objBean->getPrestamosNuevos(),2); $s_PrestamosNuevos += round($o_objBean->getPrestamosNuevos(),2) ?></td>
                  <td align="right"><?=number_format($o_objBean->getInteres(),2); $s_Interes += round($o_objBean->getInteres(),2) ?></td>
                  <td align="right"><?=number_format($o_objBean->getNumeroPrestamo(),0); $s_NumeroPrestamo += round($o_objBean->getNumeroPrestamo(),0) ?></td>
                  <td align="center"><?=number_format($o_objBean->getSaldoPrestamos(),2); $s_SV += round($o_objBean->getSaldoPrestamos(),2) ?></td>
                 <!--<td align="right"><?=number_format($o_objBean->getDiasPromedio(),2); $s_DiasPromedio += round($o_objBean->getDiasPromedio(),2) ?></td>-->
                </tr>
                
                <? } ?>
				<tr>
					 <td>&nbsp;</td>
					 <td>&nbsp;</td>
					 <td>&nbsp;</td>
					 <td align="right">&nbsp;<b>Ponderado de Promedios :</b></td>
					 <td align="right"><strong><?=number_format($s_Capital/count($a_ObjectBean),2);?></strong></td>
					 <td align="right"><strong><?=number_format($s_MontoTotal/count($a_ObjectBean),2);?></strong></td>
					 <td align="right"><strong><?=number_format($s_PrestamosNuevos/count($a_ObjectBean),2);?></strong></td>
					 <td align="right"><strong><?=number_format($s_Interes/count($a_ObjectBean),2);?></strong></td>
					 <td align="right"><strong><?=number_format($s_NumeroPrestamo/count($a_ObjectBean),2);?></strong></td>
					 <td align="right"><strong><?=number_format($s_SV/count($a_ObjectBean),2);?></strong></td>
					 
					 
				 	 <!--<td>&nbsp;</td>-->
				
				</tr>
              <? }else{ ?>
         		   <tr>
	                <td colspan="5" align="center">No se produjo resultado</td>
	              </tr>
              <? } ?>
              
            </table>
      </td>
    </tr>
   
    <tr>
      <td>Total Registros: <?=count($a_ObjectBean)?></td>
    </tr>
  </table>