<?
header("Content-Type: application/xhtml+xml; charset=iso-8859-1");
use_helper('UrlFunctions');
$o_ObjectBean = new SobranteFaltanteBean();
  
$tamanioPag = 10;
  
if($Idsofa   !=""){ $o_ObjectBean->setIdSofa($IdSofa); }
if($cboColaborador  !=""){ $o_ObjectBean->setIdColaborador($cboColaborador); } 
if($cboEstado   !=""){ $o_ObjectBean->setEstado($cboEstado); }
if($txtFechaInicial   !=""){ $o_ObjectBean->setFechaSofa($txtFechaInicial); }
if($txtFechaFinal   !=""){ $o_ObjectBean->setFechaFinal($txtFechaFinal); }

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

$o_class = new SobranteFaltanteBL();


if(myUser::getUser()->getIdPadre()=="1"){
	
	$o_ObjectBean->setIdColaboradorPrestamo(myUser::getUser()->getIdColaborador());
	
  	$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);
  	$idAdministrador 			= $o_ColaboradorBeanResult[0]->getIdPadre();
  	
 }if(myUser::getUser()->getRoleId() == "sys.res"){ 
	$idAdministrador = myUser::getUser()->getIdColaborador();
}

if($idAdministrador){
	$o_ColaboradorBean = new ColaboradorBean();
	$o_ColaboradorBean->setIdPadre($idAdministrador);
	$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();
		}
	}
	$o_ObjectBean->setIdColaboradores($a_Colaborador);
	
}
$a_ObjectBean = $o_class->busquedaSobranteFaltante($o_ObjectBean);

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

if(!$page) $page = 1;



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="5%" >Nro</th>
                <th width="20%" align="left" >Representante</th>
                <th width="10%" align="right">Monto(S/.)</th>
                <th width="8%" >Estado&nbsp;&nbsp;&nbsp;</th>
                <th width="8%" >Sobrante/Faltante&nbsp;&nbsp;&nbsp;</th>
                <th width="12%" >Fecha&nbsp;&nbsp;&nbsp;</th>
                <th width="8%" id="col01" onMouseOver="sort_col(this);" >Usr.Creac.</th>
                <th width="8%" id="col01" onMouseOver="sort_col(this);" >Fec.Creac.</th>
              
                <th width="10%">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 align="left"><?=ucwords(strtolower($o_objBean->getNombreCompleto()))?></td>
                <td align="right"><?php echo number_format($o_objBean->getMonto(),2);$total +=$o_objBean->getMonto();?></td>
                <td align="center"><?=$o_objBean->getEstado()?></td>
                <td align="center"><? if($o_objBean->getTipoSofa()=='s'){echo Sobrante ;}elseif($o_objBean->getTipoSofa()=='f'){echo Faltante;}?></td>
                <td align="center"><?=$o_objBean->getFechaSofa()?></td>
                <td align="center"><?=$o_objBean->getUsuarioCreacion()?></td>
                <td align="center"><?=$o_objBean->getFechaCreacion()?></td>
                <td align="center" valign="top">
                   <img src="/images/common/buttons/btnReview.gif" style="cursor:pointer;" title="Mostrar" onclick="parent.objMainTab.modalDialog('<?=buildUrl('sof/show/id/'.$o_objBean->getIdSofa());?>','block=001')">&nbsp;
                   <input type="image" name="Editar" src="/images/common/buttons/btnEdit.gif" title="Modificar" onclick="parent.objMainTab.modalDialog('<?=buildUrl('sof/edit/id/'.$o_objBean->getIdSofa())?>','block=001')">&nbsp;
                   <input type="image" name="Eliminar" src="/images/common/buttons/btnDelete.gif" title="Eliminar" onclick="deleteRegistro('<?=$o_objBean->getIdSofa()?>','<?=$o_objBean->getIdSofa()?>')">&nbsp;
                </td>
              </tr>
              <? } ?>
               <tr>
               <td colspan="3" align="right"><B>S/.<?=number_format($totalMonto,2)?></B></td>
              <tr>
              <? }else{ ?>
              <tr>
                <td colspan="7" align="center">No se produjo resultado</td>
              </tr>
              <? } ?>
            </table>
          </td>
        </tr>
        <tr>
          <td>Total Registros: <?=count($a_ObjectBean)?></td>
        </tr>
      </table>