<?
$r = "../";
require_once($r."inc/funciones.php");
validNotSession($r);
$sql="";
foreach ($_GET as $key => $value)
{
    if($key!="fecha1" && $key!='fecha2' && $key!='id'){
        if($value!=""){$sql .= $key . "=". $value.' AND ';}
    }
    if($key=="fecha1" or $key=='fecha2'){
        if($key=="fecha1"){
            if($value!=""){
            $fecha1=$value;
            $fecha2=$value;
            $fechai= "fecha BETWEEN '$fecha1 00:00:00' AND '$fecha2 23:59:59' AND ";
            
            }
        }
        if($key=="fecha2"){
            if($value!=""){
            $fecha2=$value;
            $fechaf= "fecha BETWEEN '$fecha1 00:00:00' AND '$fecha2 23:59:59' AND ";
            }
        }
    }
   
}
if($fechaf==""){$sql.=$fechai;}else{$sql.=$fechaf;}
$mysqli = connectDB($r);
$row1 = $mysqli->query("SELECT * FROM z_pedidos WHERE $sql id!=0");
?>
<!DOCTYPE html>
<html lang="en">
  <?php include $r."inc/head.php"; ?>
<body id="page-top">
  <?php include "nav.php"; ?>
  
    <div class="container-fluid">
      <div class="card mb-3">
        <div class="h3 card-header">
          <i class="fa fa-table"></i> RESULTADO CONSULTA</div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>Nombres y apellidos usuario</th>
                  <th>Cliente</th>
                  <th>Domiciliario</th>
                  <th>Fecha entrega </th>
                  <th>Codigo </th>
                  <th>Estado </th>
                  <th>valor</th>
                  <th>Acción</th>
                </tr>
              </thead>
              <tbody>
                    <?php
                    while($row2 = $row1->fetch_assoc()) {
                        
                    ?>
                      <tr>
                        <td><?php $usuario = suser($row2["iduser"]); echo $usuario['nombre']; ?></td>
                        <td><?php $cliente = sclien($row2["idcliente"]); echo $cliente['nombre'];?></td>
                        <td><?php $rutero = srute($row2["idrutero"]); echo $rutero['nombre']; ?></td>
                        <td><?php echo $row2["fechaentrega"] ?></td>
                        <td><?php echo $row2["codigopedido"] ?></td>
                        <? 
                        $code=$row2["codigopedido"];
                        $row3=$mysqli->query("SELECT * FROM z_pedido INNER JOIN z_pedidos ON z_pedido.codigopedido='$code' AND z_pedidos.codigopedido='$code'");
            while($row4 = $row3->fetch_assoc()){
                $des=100-$row4['descuento'];
                $cantidad=$row4['cantidad']-$row4['devolucion'];
                if($si==0){$abono=$row4['abono'];$bono=$row4['bono'];$si=1;}
                $total=$precio+$row4['precio']*$cantidad/100*$des;$tota=$tota+$total;
                }$si=0;
                $total=$tota+$adicional-$abono;
                $subtotal=$subtotal+$total;
                        ?>
                        <td><?php   if($row2["pago"]!=1){
                        if($row2["estado"]==0){echo 'Por aprovar';}
                        if($row2["estado"]==1){echo 'Por entregar';}
                        if($row2["estado"]==2){echo 'En ruta';}
                        if($row2["estado"]==3){echo 'finalizado';}
                        if($row2["estado"]==4){echo 'Cancelado';}
                        if($row2["estado"]==5){echo 'Devuelto';}}else{echo '<spam class="text-warning">Bono</spam>';}?></td>
                        
                        
                        <?php   if($row2["pago"]!=1){ ?>
                        <td class="h6 <? if($total<0){echo 'text-danger';}elseif($total>0){echo 'text-success';}elseif($total=0){echo 'text-warning';}?> text-right">$ <? echo $total;?>COP</td>
                        <? }else{echo '<td><spam class="h6 text-warning text-right">$ '.$total.'COP</spam></td>';}?>
                        <?php $total=0;$tota=0;$adicional=0;?>
                        <td><? if($row2["estado"]=2){ ?><a class="btn_default text-primary h6" href="ver.php?id=<?php echo $row2["codigopedido"] ?>"><i class="fa fa-eye"></i></a> <? }?>
                        <? if($row2["estado"]==2){ ?><a class="btn_default text-info h6" onClick="return popitup('../caja/imprimir.php?code=<?php echo $row2["codigopedido"] ?>')" href="../caja/imprimir.php?code=<?php echo $row2["codigopedido"] ?>" target="_blank"><i class="fa fa-print"></i></a> 
                        <a class="btn_default text-success h5"  href="my.bluetoothprint.scheme://https://quimiforen.com/admin/movil/a.php?code=<?php echo $row2["codigopedido"] ?>" target="_blank"><i class="fa fa-print"></i></a> <? }?></td>
                        </tr>
                    <?php
                    }
                    ?>
              <tbody>
                  
              <tfoot>
                <tr>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th>TOTAL</th>
                  <th class=" text-right text-primary"><? if($spe==$pad or $spe==$psu){ ?>$ <? echo $subtotal;?>COP<? } ?><th>
                </tr>
              </tfoot>
            </table>
          </div>
        </div>
      </div>
      <input type="button" class="btn btn-primary float-left" value="Atras" name="Back2" onclick="history.back()" />
    </div>
  </div>
  <?php include "foot.php"; ?>
 
<script>
    function popitup(url){
	    newwindow=window.open(url,'name','height=600,width=320'); 
	    if (window.focus) {newwindow.print();}
	    return false; 
    }
</script> 
<script>
$('#dataTable').DataTable( {
        "order": [[ 3, "desc" ]],
        "lengthMenu": [[100, 500, 1000, -1], [100, 500, 1000, "All"]]
    } );
  </script>
</body>
</html>   