<?php $r="../";
require($r."vendor/fpdf181/fpdf.php");
$idrutero=base64_decode($_GET['idrutero']);
require_once($r."inc/funciones.php");
validNotSession($r);
$mysqli = connectDB($r);
//if($spe!=$pru){ exit(header("Location:".$r."dashboard.php?error=NO TIENES PERMISO PARA ESTA SECCION"));}
$row7 = $mysqli->query("SELECT * FROM z_pagos WHERE idrutero='$idrutero' ORDER BY id DESC");
$row8 = $row7->fetch_assoc();
$fechai=$row8['fecha'];
if($fechai==""){$fecha1='2018-08-11';}
$fecha4= date("Y-m-d H:i:s");
$row1 = $mysqli->query("SELECT * FROM z_pedidos WHERE idrutero='$idrutero' AND fechaentrega BETWEEN '$fechai' AND '$fecha4'");
$diaspagar = array();
while($row2 = $row1->fetch_assoc()){
    $fecha1 = $row2['fechaentrega'];
    if (in_array($fecha1, $diaspagar)) {}else{array_push($diaspagar,$fecha1);}
    
}

foreach($diaspagar as $key => $item){ 
   $days++;
}

/*$off = $new->num_rows;
if($off>0){exit(header("Location:index.php?error=Este Pedido ya Fue Entregado"));}
$row1 = $mysqli->query("SELECT * FROM z_pedido INNER JOIN z_pedidos ON z_pedido.codigopedido='$code' AND z_pedidos.codigopedido='$code' AND z_pedidos.estado=1");*/
$arraycodigopedidos = array();
$arrayidproductos = array();
$arraycantidadproductos = array();
$arrayprecioproducto= array();
$endproductos= array();
$arraybono= array();

$row1 = $mysqli->query("SELECT * FROM z_pedidos WHERE idrutero='$idrutero' AND fechasalida BETWEEN '$fechai' AND '$fecha4 23:59:59'");
while($row2 = $row1->fetch_assoc()) {
    array_push($arraycodigopedidos,$row2['codigopedido']);
}
foreach($arraycodigopedidos as $key => $item){
    $pedido = $mysqli->query("SELECT * FROM z_pedido WHERE codigopedido='$item'");
    while($pedid = $pedido->fetch_assoc()){
    array_push($arrayidproductos,$pedid['idproducto']);
    array_push($arraycantidadproductos,$pedid['cantidad']);
    array_push($arrayprecioproducto,$pedid['precio']);
    array_push($arraybono,$pedid['bono']);}
}
foreach($arrayidproductos as $key => $item){ 
    if (in_array($item, $endproductos)) {}else{array_push($endproductos,$item);}
}
/*?>
<!DOCTYPE html>
<html lang="en">
  <?php include $r."inc/head.php"; ?>
  <style>
      .container-fluid{padding-right:5px;padding-left:5px;}
  </style>
<body>
  <?php include "nav.php"; ?>
    <div class="container-fluid">
      <div class="card mb-3">
        <div class="h3 card-header">
          <i class="fa fa-table"></i> PEDIDOS POR ENTREGAR <? $rutero=srute($idrutero); echo $rutero['nombre']; ?></div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>id</th>
                  <th>Producto</th>
                  <th>cantidad</th>
                </tr>
              </thead>
              <tbody>
                  
              </tbody>
                  
                <tfoot>
                    <tr>
                    <td></td>
                    <td class="text-right h6"><b>SUMA TOTAL VENTA</td>
                    <? $row9 = $mysqli->query("SELECT * FROM z_ruteros WHERE id='$idrutero' ");
                        $row10 = $row9->fetch_assoc();
                    ?>
                    <td class="h6"><b><? $totalpago=$subtotal*$row10['ppp']; echo $sumatotal3;?></b></td>
                    </tr>
                    <tr>
                    <td></td>
                    <td class="text-right h6"><b>SUMA TOTAL DE PAQUETES <spam class="text-danger"><? echo $subtotal;?></spam></b></td>
                    <? $row9 = $mysqli->query("SELECT * FROM z_ruteros WHERE id='$idrutero' ");
                        $row10 = $row9->fetch_assoc();
                    ?>
                    <td class="h6"><b><? $totalpago=$subtotal*$row10['ppp']; echo $totalpago;?></b></td>
                    </tr>
                    <tr>
                    <td></td>
                    <td class="text-right h6"><b>SUMA TOTAL DE DIAS <spam class="text-danger"><? echo $days;?></spam></b></td>
                    <td class="h6"><b><? echo $days*$row10['ppr'];?></b></td>
                    </tr>
                </tfoot>
            </table>
          </div>
        </div>
        <a class="btn btn-primary inline-block" href="print-report.php">Imprimir Reporte</a>
      </div>
    </div>
  <?php include "foot.php"; ?>
</body>
</html>    
    
 <?*/



// Instanciation of inherited class
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('P',array(140,78));  
$pdf->SetMargins(2, 0, 2); 
#Establecemos el margen inferior: 
$pdf->SetAutoPageBreak(true,2); 
//logo
$pdf->Image('practipasta-pos.jpg',30,0,20,11.6,'JPG');
$pdf->Cell(15,2,"",0,1,"C");
//encabezado factura
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(75,3,"DISTRIBUIDORA PRACTIPASTA JJ",'0',1,"C");
$pdf->SetFont("Arial", "",8);
$pdf->Cell(75,3,"NIT 13760821-8",'0',1,"C");
$pdf->Cell(75,3,utf8_decode("RÉGIMEN COMÚN"),'0',1,"C");
$pdf->Cell(75,3,utf8_decode("Carrera 87 bis # 49-34 Sur Barrio Betania - Bogotá"),'0',1,"C");
$pdf->Cell(75,3,utf8_decode("Tel.: + 571 493 0730  -  Cel.: + 57 311 533 5623"),'0',1,"C");
$pdf->Cell(75,1,"",0,1,"C");
$pdf->Cell(75,1,"$linea",0,1,"");
$rutero=srute($idrutero); $nomrutero=$rutero['nombre'];
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(75,3,utf8_decode("$nomrutero"),0,1,"C");
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(75,3,"ULTIMO PAGO - ".substr("$fechai",0,10),0,1,"C");
$pdf->Cell(75,3,"FECHA ACTUAL - ".substr("$fecha4",0,10),0,1,"C");
$pdf->Cell(75,1,"",0,1,"C");
//ENCABEZADO FACTURA
$pdf->Cell(75,3,"$linea",0,1,"");
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(58,3,"NOMBRE PRODUCTO",0,0,"");
$pdf->Cell(15,3,"UNDS",0,1,"R");
$pdf->Cell(75,1,"$linea",0,1,"");
$pdf->Cell(75,1,"",0,1,"");

                  foreach($endproductos as $pkey => $pitem){ 
                    foreach($arrayidproductos as $key => $item){
                            if($pitem==$item){$suma=$arraycantidadproductos[$key];$bono=$arraybono[$key];$precio=$arrayprecioproducto[$key];$cantidadtotal=$suma+$bono;$sumatotal2=$cantidadtotal*$precio;$sumatotal3=$sumatotal3+$sumatotal2;$sumatotal=$sumatotal+$suma+$bono;}
                        }
                        $subtotal=$subtotal+$sumatotal; $produc=sprod($pitem); $codprod=$produc['codigo'];$nombresub=$produc['producto']; $totaldes=$sumatotal; 
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(48,3,utf8_decode($nombresub),'0','0',"");
    $pdf->Cell(7,3,$cantidad,'0','0',"C");
    $pdf->Cell(18,3,number_format($totaldes),'0',1,"R");
    if($bono>0){
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(11,3,utf8_decode(substr("$codprod", 0, 5)),'0','0',"");
    $pdf->Cell(37,3,utf8_decode($nombresub),'0','0',"");
    $pdf->Cell(7,3,$bono,'0','0',"C");
    $pdf->Cell(18,3,'BONO','0',1,"R");}
    $sumatotal=0;
    /*
    
    echo substr("$nomprod", 0, 20);
    echo $cantidad;
    echo $precio;*/
    
} $pdf->SetFont("Arial", "B",8);
    $pdf->Cell(48,3,"Total Unidades ",'0','0',"R");
    $pdf->Cell(25,3,number_format($subtotal),'0',1,"R");
$pdf->Cell(75,3,"$linea",0,1,"");
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(40,3,"DESCIPCION",0,0,"");
$pdf->Cell(5,3,"#",0,0,"");
$pdf->Cell(10,3,"PRECIO",0,0,"");
$pdf->Cell(20,3,"TOTAL",0,1,"R");
$row9 = $mysqli->query("SELECT * FROM z_ruteros WHERE id='$idrutero' ");
$row10 = $row9->fetch_assoc();
$ppp=$row10['ppp'];
$ppr=$row10['ppr'];
$totalpago=$subtotal*$row10['ppp'];
$tdays=$days*$row10['ppr'];
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(60,3,"VENTA TOTAL",'0','0',"");
    $pdf->Cell(15,3,'$ '.number_format($sumatotal3),'0',1,"R");
    $pdf->Cell(40,3,"UNDS POR PAGAR",'0','0',"");
    $pdf->Cell(5,3,"$subtotal",'0','0',"R");
    $pdf->Cell(10,3,"$ppp",'0','0',"R");
    $pdf->Cell(20,3,'$ '.number_format($totalpago),'0',1,"R");
    $pdf->Cell(40,3,"DIAS POR PAGAR",'0','0',"");
    $pdf->Cell(5,3,"$days",'0','0',"R");
    $pdf->Cell(10,3,"$ppr",'0','0',"R");
    $pdf->Cell(20,3,'$ '.number_format($tdays),'0',1,"R");
    $subtotal=$totalpago+$tdays;
    
$pdf->SetFont("Arial", "B",8);
    $pdf->Cell(55,3,"SUBTOTAL",'0','0',"R");
    $pdf->Cell(20,3,'$ '.number_format($subtotal),'0',1,"R");
    $totalapagar=$sumatotal3-$totalpago-$tdays;
    $pdf->Cell(75,3,"$linea",0,1,"");
    $pdf->Cell(55,3,"TOTAL",'0','0',"R");
    $pdf->Cell(20,3,'$ '.number_format($totalapagar),'0',1,"R");
    

    $pdf->SetFont("Arial", "",8);
$pdf->Cell(75,1,"$linea",0,1,"");
$pdf->Cell(75,1,"",0,1,""); 
$pdf->SetFont("Arial", "",7);   
$pdf->Cell(75,3,"Favor verificar su cambio",0,1,"C");
$pdf->Cell(75,3,"Gracias por su pedido",0,1,"C");
$pdf->Cell(75,3,utf8_decode("No se admiten reclamaciones después de"),0,1,"C");
$pdf->Cell(75,3,"entregado el producto",0,1,"C");
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(75,3,"conservar este recibo para reclamos",'0',1,"C");
$pdf->SetFont("Arial", "",8);
$pdf->Cell(75,1,"$linea",0,1,"");
/*
Favor verificar su cambio
Gracias por su pedido
No se admiten reclamaciones después de 
entregado el producto.

[id] => 1
[idproducto] => 1
[cantidad] => 15
[precio] => 2300
[iva] => 5
[descuento] => 0
[bono] => 0
[codigopedido] => 000001
[iduser] => 2
[idcliente] => 583
[idrutero] => 1
[fechaentrega] => 2018-08-06
[estado] => 2
[pago] => 2
[fecha] => 2018-08-06 11:52:18

while($row2 = $row1->fetch_assoc()) {
    $pdf->Cell(75,3,utf8_decode(print_r($row2)),'0',1,"C");
}*/
$pdf->Output();
?>