<?php
header("Content-Type: text/html; charset=iso-8859-1");
use_helper('UrlFunctions');

//ini_set('display_errors', 1);

$o_class = new CrPedidocabeceraFN();

if($txtFechaInicial !=""){ $o_class->setFechaInicio($txtFechaInicial); }
if($txtFechaFinal   !=""){ $o_class->setFechaFin($txtFechaFinal); }
if($s_codigoCliente !=""){ $o_class->setIdCliente($s_codigoCliente); }
if($s_cliente !=""){ $o_class->setNombreCompleto($s_cliente); }

$tamanioPag = 20;
if(!isset($page)) $page = 1;
$o_class->page = $page;
$o_class->tamPag = $tamanioPag;
if($sortCampos)
{
  $o_class->sortCampos = $sortCampos;
  $o_class->sortType   = $sortType;
}
else 
{
  $o_class->sortCampos = "Estado;Fechaini";
  $o_class->sortType   = "1,1";
}

$a_ObjectBean = $o_class->selectAll();

$o_navigation = new navigation();
$o_navigation->setPaginaActual($page);
$o_navigation->setTamanioPagina($tamanioPag);
$o_navigation->setTotalRegistros($o_class->selectNumRowsAll());
$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=<?php echo $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="<?php echo $valuesSort['sortCampos']?>">
<input type="hidden" id="sortType" size="100" value="<?php echo $valuesSort['sortType']?>">
      <table class="marco" border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td height="17" align="right" class="pager">
            <div id="contenedor">
              <?php echo $o_navigation->ContruirHTMLNavigation('contenedor', $strConcat); ?>
            </div>
          </td>
        </tr>
        <tr>
          <td>
            <table class="grid" width="100%" border="0" cellpadding="2" cellspacing="0">
              <tr height="23">
                <th width="5%" nowrap="nowrap">Nro</th>
                <th width="10%" nowrap="nowrap"><?php echo showImgOrder($valuesSort, 'Fechaoperacion','Fecha')?></th>
                <th width="10%" nowrap="nowrap"><?php echo showImgOrder($valuesSort, 'Montototal','Monto Total')?></th>
                <th width="10%" nowrap="nowrap"><?php echo showImgOrder($valuesSort, 'Montoaprobado','Monto Aprobado')?></th>
                <th width="10%" nowrap="nowrap"><?php echo showImgOrder($valuesSort, 'Montorechazado','Monto Rechazado')?></th>
                <th width="10%" nowrap="nowrap"><?php echo showImgOrder($valuesSort, 'Montoentregado','Monto Entregado')?></th>
                <th width="10%" nowrap="nowrap"><?php echo showImgOrder($valuesSort, 'Fechacreacion','Fecha Registro')?></th>
                <th width="10%" nowrap="nowrap">Usuario</th>
                <th width="25%" nowrap="nowrap">Opciones</th>
              </tr>
              <?php if(count($a_ObjectBean)>0){ ?>
              <?php foreach ($a_ObjectBean as $iKey => $o_objBean){ ?>
              <?php
              $i_idpedido = $o_objBean->getIdpedidocabecera();
              ?>
              <tr onClick="TR_click(this);">
                <td class="chk"><?php echo $iKey+1 ?></td>
                <td align="center"><?php echo substr($o_objBean->getFechaoperacion(),0,10) ?></td>
                <td align="center"><?php echo $o_objBean->getMontototal() ?></td>
                <td align="center"><?php echo $o_objBean->getMontoaprobado() ?></td>
                <td align="center"><?php echo $o_objBean->getMontorechazado() ?></td>
                <td align="center"><?php echo $o_objBean->getMontoentregado() ?></td>
                <td align="center"><?php echo substr($o_objBean->getFechacreacion(),0,10) ?></td>
                <td align="center"><?php echo $o_objBean->getUsuariocreacion() ?></td>
                <td align="center">
                    <input type="button" class="button" value="EDIT" onclick="location.href='<?=buildUrl( 'pdo/addedit', 'block=001&i_idpedido='.$i_idpedido )?>';" title=""/>
                    <?php if( myUser::getUser()->getRoleId() != "sys.ope" ){ ?>
                    <input type="button" class="button" value="EVALUAR" onclick="location.href='<?=buildUrl( 'pdo/editEval', 'block=001&i_idpedido='.$i_idpedido )?>';" title="EVALUAR"/>
                    <?php } ?>
                    <input type="button" class="button" value="ENTREGAR" onclick="location.href='<?=buildUrl( 'pdo/listEntr', 'block=001&i_idpedido='.$i_idpedido )?>';" title="ENTREGAR"/>
                </td>
              </tr>
              <?php } ?>
              <?php }else{ ?>
              <tr>
                <td colspan="7" align="center">No se produjo resultado</td>
              </tr>
              <?php } ?>
            </table>
          </td>
        </tr>
        <tr>
          <td style="background-color:#01799c; color:#FFFFFF">Total Registros: <?php echo count($a_ObjectBean)?></td>
        </tr>
      </table>