<?
header("Content-Type: text/html; charset=utf-8");
use_helper('UrlFunctions');
use_helper('UtilFunctions');

$o_ObjectBeanFN = new CrPlanillaPeriodoFN();
$o_ObjectBeanFN->setActivo('1');
if($Periodo  !=""){ $o_ObjectBeanFN->setPeriodo($Periodo); } 
if(!isset($page))$page =1;
$tamanioPag = 100;

$o_ObjectBeanFN->page = $page;
$o_ObjectBeanFN->tamPag = $tamanioPag;
$o_ObjectBeanFN->sortType   = "2;1";
$o_ObjectBeanFN->sortCampos = "Periodo;DescripcionEmpresa";

$a_o_ObjectBeanFN = $o_ObjectBeanFN->select();

$o_navigation = new navigation();
$o_navigation->setPaginaActual($page);
$o_navigation->setTamanioPagina($tamanioPag);
$o_navigation->setTotalRegistros($o_ObjectBeanFN->selectNumRows());
$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="35">
                <th width="5%" >Nro</th>
                
                <th width="10%" align="left" >Periodo</th>
                <th width="10%" align="left" >Empresa</th>
                <th width="8%" align="left" >Estado</th>
				<?php if(myUser::getUser()->getRoleId() == "sys.gen"){?>
                <th width="10%" align="right">T.Ingresos(S/.)</th>
                <th width="10%" align="right">T.Descuentos(S/.)</th>
                <th width="10%" align="right">Neto(S/.)</th>
				<?php }?>
                <th width="10%">Opciones</th>
              </tr>
              <? if(count($a_o_ObjectBeanFN)>0){ ?>
              <? foreach ($a_o_ObjectBeanFN as $iKey => $o_objBean){ ?>
              
           
               
              <tr onClick="TR_click(this);">
                <td class="chk"><?=($tamanioPag)*($page-1)+($iKey+1)?></td>
                <td align="left"><?php echo $o_objBean->getPeriodo();?></td>
                 <td align="left"><?php echo $o_objBean->getDescripcionEmpresa();?></td>
                
                <td align="left"><?php if($o_objBean->getEstado() == "A") echo "Abierto"; else echo "Cerrado"?></td>
                            
               <?php if(myUser::getUser()->getRoleId() == "sys.gen"){?>
                <td align="right"><?=$o_objBean->getTotalIngresos();$TotalIngresos += $o_objBean->getTotalIngresos();?></td>
                <td align="right"><? echo $o_objBean->getTotalDescuentos();$TotalDescuentos += $o_objBean->getTotalDescuentos(); ?></td>
                <td align="right"><? echo number_format($o_objBean->getNeto(),2);$totalNeto += $o_objBean->getNeto(); ?></td>
				<?php }?>
                <td align="center">
                	 <?php if($o_objBean->getEstado() == "A"){ ?><input type="button" onclick="parent.objMainTab.reqBody('<?=buildUrl('planilla/add')?>','001&idempresa=<?=$o_objBean->getIdEmpresa()?>&periodo=<?=$o_objBean->getPeriodo()?>')" value="Cierre" class="button"><?php }?>
                	 <?php if($o_objBean->getEstado() == "C"){ ?><input type="button" onclick="parent.objMainTab.reqBody('<?=buildUrl('planilla/show')?>','002&idempresa=<?=$o_objBean->getIdEmpresa()?>&periodo=<?=$o_objBean->getPeriodo()?>')" value="Mostrar" class="button"><?php }?>
                	 <?php if($o_objBean->getEstado() == "C"){ ?>
                	 <input type="button" onclick="deleteRegistro('<?=$o_objBean->getIdEmpresa()?>','<?=$o_objBean->getPeriodo()?>')" value="Abrir" class="button">
                	 
                	 <?php }?>
                </td>
               
              </tr>
              
              <?php } ?>
              <tr>
               <td colspan="4" align="right"><B>Totales</B></td>
			   <?php if(myUser::getUser()->getRoleId() == "sys.gen"){?>
               <td align="right"><B><?=number_format($TotalIngresos,2)?></B></td>
               <td align="right"><B><?=number_format($TotalDescuentos,2)?></B></td>
               <td align="right"><B><?=number_format($totalNeto,2)?></B></td>
              <?php }?> 
              <tr>
              <? }else{ ?>
              
                <td colspan="7" align="center">
                   <table class="marco" width="100%" border="0" cellpadding="0" cellspacing="0">
                     <tr>
                       <td>No se produjo resultado</td>
                       	
                     </tr>
                   </table> 
                </td>
              </tr>
              <? } ?>
              
            </table>
          </td>
        </tr>
        <tr>
          <td>
          <table class="marco" width="100%" border="0" cellpadding="0" cellspacing="0">
          			<?
          			$a_MontosTotales = $o_ObjectBeanFN->MontoTotal();
          			?>
                     <tr>
                       <td>Total Registros:&nbsp;<?php echo count($a_o_ObjectBeanFN)?> :: Monto Total Neto S/.<?=number_format($a_MontosTotales['1']['TotalNeto'],2);?></td>
                      
                     </tr>
                   </table> 
         </td>
        </tr>
      </table>