<?
header("Content-Type: application/xhtml+xml; charset=utf-8");
use_helper('UrlFunctions');
$o_cierreHipoteca = new CrCierrehipotecaCFN();
$tamanioPag = 10;
$o_cierreHipoteca->page = $page;
$o_cierreHipoteca->tamPag = $tamanioPag;
$o_cierreHipoteca->sortCampos = $sortCampos;
$o_cierreHipoteca->sortType   = $sortType;



if(myUser::getUser()->getRoleId() == "sys.res"){
 	$idAdministrador = myUser::getUser()->getIdColaborador();
}elseif(myUser::getUser()->getIdPadre()){
  	$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();
  	
  	$o_ColaboradorBean = new ColaboradorBean();
	$o_ColaboradorBean->setIdPadre($IdPadre);
	$o_ColaboradorBL = new ColaboradorBL();	
  	
  	$a_o_Colaborador = $o_ColaboradorBL->busquedaColaborador($o_ColaboradorBean);
  	
	if(is_array($a_o_Colaborador)){
		foreach ($a_o_Colaborador as $key => $o_colaborador){
			$a_Colaborador[] = $o_colaborador->getIdColaborador();
		}
	}
}elseif(myUser::getUser()->getIdPadre() == "0"){
	
}else{//Solo los Hijos del Colaborador
  	$IdPadre = myUser::getUser()->getIdColaborador();
  	 	
  	$o_ColaboradorBean = new ColaboradorBean();
	$o_ColaboradorBean->setIdPadre($IdPadre);
	$o_ColaboradorBL = new ColaboradorBL();	
  	
  	$a_o_Colaborador = $o_ColaboradorBL->busquedaColaborador($o_ColaboradorBean);
	if(is_array($a_o_Colaborador)){
		foreach ($a_o_Colaborador as $key => $o_colaborador){
			$a_Colaborador[] = $o_colaborador->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);

foreach ($a_o_sucursal as $key => $o_sucursal){
	$IdSucursal 	= $o_sucursal->getIdSucursal();
	$NombreSucursal = $o_sucursal->getDescripcion();
}




if(myUser::getUser()->getRoleId() == "sys.gen"){
	
	if($cboSucursal   !=""){ $o_cierreHipoteca->setIdSucursal($cboSucursal); }
}else{
	$o_cierreHipoteca->setIdSucursal($IdSucursal);
}
if($txtFechaInicial   !=""){ $o_cierreHipoteca->setFechaInicial($txtFechaInicial); }
if($txtFechaFinal   !=""){ $o_cierreHipoteca->setFechaFinal($txtFechaFinal); }

if(!isset($sortCampos))$sortCampos= "Fecha";
if(!isset($sortType))  $sortType ="2";

$o_cierreHipoteca->sortCampos = $sortCampos;
$o_cierreHipoteca->sortType   = $sortType;

$a_ObjectBean = $o_cierreHipoteca->select();


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

if(!is_array($a_ObjectBean) && isset($a_ObjectBean)){
  $obj = $a_ObjectBean;
  $a_ObjectBean = null;
  $a_ObjectBean[] = $obj;  
}


?>
<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;
}
?>

<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="2%" >Nro</th>
                <th width="8%" align="left" id="col01" onMouseOver="sort_col(this);" >Sucursal</th>
                <th width="8%" align="left" id="col01" onMouseOver="sort_col(this);" >F.Balance</th>
                
                <th width="8%" align="left" id="col01" onMouseOver="sort_col(this);" >Comentario</th>
                <th width="4%" align="left" id="col01" onMouseOver="sort_col(this);" >Moneda</th>
				<th width="5%" align="right" id="col01" onMouseOver="sort_col(this);" >Salida</th>
				<th width="5%" align="right" id="col01" onMouseOver="sort_col(this);" >Ingreso Bruto</th>
				<th width="5%" align="right" id="col01" onMouseOver="sort_col(this);" >Ingreso Dep.</th>
				<th width="5%" align="right" id="col01" onMouseOver="sort_col(this);" >Ingreso Neto</th>
				<th width="2%" align="left" id="col01" onMouseOver="sort_col(this);">&nbsp;</td>
				<th width="8%" align="left" id="col01" onMouseOver="sort_col(this);" >U.Registro</th>
				<th width="8%" align="left" id="col01" onMouseOver="sort_col(this);" >F.Registro</th>
                <th width="5%">Opciones</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->getObservacion()?></td>
                   <td><?if($o_objBean->getMoneda()=="S") echo "Soles"; else echo "Dolares"; ?></td>
                  <td align="right"><?php 
                  if($o_objBean->getMoneda()=="D"){
                  	$totalS += $o_objBean->getSalidatotal();
                  }
                  if($o_objBean->getMoneda()=="S"){
                  	$totalSS += $o_objBean->getSalidatotal();
                  }
                  echo number_format($o_objBean->getSalidatotal(),2)?></td>
                  <td align="right"><?php 
                  if($o_objBean->getMoneda()=="D"){
                  	$totalI += $o_objBean->getIngresototal();
                  }
                  if($o_objBean->getMoneda()=="S"){
                  	$totalIS += $o_objBean->getIngresototal();
                  }
                  echo number_format($o_objBean->getIngresototal()+$o_objBean->getIngresototalDeposito(),2)?></td>
                  <td align="right"><?php 
                  if($o_objBean->getMoneda()=="D"){
                  	$totalIDep += $o_objBean->getIngresototalDeposito();
                  }
                  if($o_objBean->getMoneda()=="S"){
                  	$totalISDep += $o_objBean->getIngresototalDeposito();
                  }
                  echo number_format($o_objBean->getIngresototalDeposito(),2)?></td> 
                  <td align="right"><?php 
                  if($o_objBean->getMoneda()=="D"){
                  	$totalINeto += $o_objBean->getIngresototal();
                  }
                  if($o_objBean->getMoneda()=="S"){
                  	$totalISNeto += $o_objBean->getIngresototal();
                  }
                  echo number_format($o_objBean->getIngresototal(),2)?></td>
                  <td>&nbsp;</td>
                  <td><?=$o_objBean->getUsuarioregistro()?></td>
                  <td><?=$o_objBean->getFecharegistro()?></td>
                 
                  <td align="center" valign="top">
                     <input title="Mostrar" type="image" name="Mostrar" src="/images/common/buttons/btnReview.gif" onclick="parent.objMainTab.reqBody('<?=buildUrl('hip/show')?>','001&id=<?=$o_objBean->getIdSucursal()?>&Fecha=<?=$o_objBean->getFecha();?>&moneda=<?=$o_objBean->getMoneda();?>')">&nbsp;
                     <input title="Abrir Periodo" type="image" name="Abrir" src="/images/common/buttons/btnDelete.gif" onclick="deleteRegistro('<?=$o_objBean->getIdSucursal()?>','<?=$o_objBean->getFecha()?>')">&nbsp;                     
                     <!--<img src="/images/common/buttons/btnPdf.gif" title="Balance del Día - Detalle " onClick="parent.objMainTab.reqBody('<?=buildUrl('hip/pdf_det/idSucursal/'.$o_objBean->getIdSucursal().'/fecha/'.$o_objBean->getFecha());?>','001');";" style="cursor:pointer" />-->
                  </td>
                </tr>
                <? } ?>
              <? }else{ ?>
              <tr>
                <td colspan="7" align="center">No se produjo resultado</td>
              </tr>
              <? } ?>
              
			    <tr>
		      	 <td>&nbsp;</td>
		      	 <td>&nbsp;</td>
		      	 <td>&nbsp;</td>
		      	 <td>&nbsp;</td>
		      	 <td align="right"><b>Total Soles:</b></td>
		         <td align="right"><b><?=number_format($totalSS,2);?></b></td>
		         <td align="right"><b><?=number_format($totalIS,2);?></b></td>
		         <td align="right"><b><?=number_format($totalISDep,2);?></b></td>
		         <td align="right"><b><?=number_format($totalISNeto,2);?></b></td>
		         <td>&nbsp;</td>
		         <td>&nbsp;</td>
		         <td>&nbsp;</td>
		         <td>&nbsp;</td>
			    </tr>
			     <tr>
		      	 <td>&nbsp;</td>
		      	 <td>&nbsp;</td>
		      	 <td>&nbsp;</td>
		      	 
		      	 <td align="right" colspan="2"><b>Total Dolares:</b></td>
		         <td align="right"><b><?=number_format($totalS,2);?></b></td>
		         <td align="right"><b><?=number_format($totalI,2);?></b></td>
		         <td align="right"><b><?=number_format($totalIDep,2);?></b></td>
		         <td align="right"><b><?=number_format($totalINeto,2);?></b></td>
		         <td>&nbsp;</td>
		         <td>&nbsp;</td>
		         <td>&nbsp;</td>
		         <td>&nbsp;</td>
			    </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td>Total Registros: <?=count($a_ObjectBean)?></td>
        </tr>
      </table>