<?php $r="../";
require_once($r."inc/funciones.php");
validNotSession($r);
$mysqli = connectDB($r);
$inicio=$_GET['inicial'];if($inicio==""){$inicio="2019-01-01";}
$final=$_GET['final'];if($final==""){$final= date("Y-m-d");}
$row3=$mysqli->query("SELECT * FROM z_pedidos WHERE fecha BETWEEN '$inicio 00:00:00' AND '$final 23:59:59' AND estado>=1");
?>

<!DOCTYPE html>
<html lang="es">
  <?php include $r."inc/head.php"; ?>s<?php
$r="../";
require_once($r."vendor/fpdf181/fpdf.php");
require_once($r."inc/funciones.php");
$mysqli=connectDB($r);
$linea="- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -";


$fecha=date("Y-m-d");
// Instanciation of inherited class
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('P',array(144,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->Cell(75,3,"JOHN JAIRO MORENO GARAVITO",'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,3,"",0,1,"C");
// decripcion del cliente y factura
$pdf->Cell(28,3,utf8_decode("AZ VENTAS N°.: "),'0',0,"");
$pdf->SetFont("Arial", "B",8);
$codes = $mysqli->query("SELECT * FROM z_codigoventa WHERE estado=0 ORDER BY id ASC");
$code = $codes->fetch_assoc();
$idcode=$code[id];
$codigo=$code[codigo];
$pdf->Cell(45,3,utf8_decode($codigo),'0',1,"R");
$pdf->SetFont("Arial", "",8);
$pdf->Cell(28,3,utf8_decode("Fecha: "),'0',0,"");
$pdf->Cell(45,3,utf8_decode($fecha),'0',1,"R");

$arrayivas= array();
$arraytotal= array();
$productos = $mysqli->query("SELECT * FROM z_productos");
while($producto = $productos->fetch_assoc()) {
    $item=$producto['iva'];
    if (in_array($item, $arrayivas)) {}else{array_push($arrayivas,$item);array_push($arraytotal,'0');}
}
$s=0;
$row1 = $mysqli->query("SELECT * FROM z_pedido INNER JOIN z_pedidos ON z_pedido.codigopedido=z_pedidos.codigopedido WHERE z_pedidos.fecha BETWEEN '$fecha 00:00:00' AND '$fecha 23:59:59'");
while($row2 = $row1->fetch_assoc()) {
    $cantidad=$row2['cantidad'];
    $precio=$row2['precio'];
    $iva=$row2['iva'];
    if($s==0){$icode=$row2[codigopedido];$s=1;}else{$fcode=$row2[codigopedido];}
    $total1=$cantidad*$precio;
    foreach($arrayivas as $key => $item){
        if($item==$iva){
            $total2=$arraytotal[$key];
            $arraytotal[$key]=$total2+$total1;
        }
    }
    
    
}

$pdf->SetFont("Arial", "",8);
$pdf->Cell(28,3,utf8_decode("Facturado De: "),'0',0,"");
$pdf->Cell(45,3,utf8_decode($icode.'-'.$fcode),'0',1,"R");

$pdf->SetFont("Arial", "",8);
$pdf->Cell(75,1,"$linea",0,1,"");
foreach($arrayivas as $key => $item){
            $pdf->Cell(75,1,"",0,1,"");
            $pdf->SetFont("Arial", "",8);
            $pdf->Cell(48,3,"IVA BASE $item%",'0','0',"R");
            $total=$arraytotal[$key];
            $pdf->Cell(25,3,'$ '.number_format($total),'0',1,"R");
    }
    
$pdf->SetFont("Arial", "",8);
$pdf->Cell(75,1,"$linea",0,1,"");
foreach($arrayivas as $key => $item){
            $pdf->Cell(75,1,"",0,1,"");
            $pdf->SetFont("Arial", "",8);
            $pdf->Cell(48,3,"IVA $item%",'0','0',"R");
            $total=$arraytotal[$key];
            $iva=$arrayivas[$key];
            $iva=$iva/100;
            $total=$total*$iva;
            $pdf->Cell(25,3,'$ '.number_format($total),'0',1,"R");
    }
$pdf->SetFont("Arial", "",8);
$pdf->Cell(75,1,"$linea",0,1,"");
$ttotal=array_sum($arraytotal);
$pdf->Cell(75,1,"",0,1,"");
$pdf->SetFont("Arial", "",8);
$pdf->Cell(48,3,"TOTAL VENTAS",'0','0',"R");
$pdf->Cell(25,3,'$ '.number_format($ttotal),'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,"");
if($ttotal=="0") {
}
else{
    $mysqli->query("UPDATE z_codigoventa SET estado=1 WHERE id=$idcode");
    $pdf->Output('F','resivos/'.$fecha.'.pdf');
}
?>

<body class="fixed-nav sticky-footer bg-dark" id="page-top">
  <div class="content-wrapper">
    <div class="container-fluid">
      <?php include $r."inc/nav.php"; ?>
      
      <div class="card mb-3">
        <div class="h3 card-header">
          <i class="fa fa-table"></i> PEDIDOS POR ENTREGAR</div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>CLIENTE</th>
                  <th>N째 PEDIDO</th>
                  <th>TOTAL</th>
                  <th>IVA</th>
                  <th>RTE FUNTE</th>
                  <th>RTE ICA</th>
                </tr>
              </thead>
              <tbody>
              <? while($row4 = $row3->fetch_assoc()) {?>
                    <tr>
                        <td><? echo $row4[idcliente]; ?></td>
                        <td><? echo $row4[codigopedido]; $codigopedido=$row4[codigopedido]?></td>
                        
                        <td><? echo $row4[total]; ?></td>
                        <? $row5=$mysqli->query("SELECT * FROM z_pedido WHERE codigopedido=$codigopedido"); 
                            while($row6 = $row5->fetch_assoc()) {
                                $total=$row6[cantidad]*$row6[precio];
                                $totaliva=$total*$row6[iva]/100;
                                $supertotal=$supertotal+$totaliva;
                                $supertotal2=$supertotal2+$totaliva;
                                $sumatotaliva=$sumatotaliva+$supertotal;
                            }
                        ?>
                        <td><? echo $supertotal; $supertotal=0; ?></td>
                        <td><? echo $row4[fuente]; $sumatotalfuente=$sumatotalfuente+$row4[fuente]; ?></td>
                        <td><? echo $row4[ica]; $sumatotalica=$sumatotalica+$row4[ica];?></td>
                    </tr>
                <? } ?>
              </tbody>
              <tfoot>
                <tr>
                  <th>CLIENTE</th>
                  <th>N째 PEDIDO</th>
                  <th>TOTAL</th>
                  <th><? echo $supertotal2; ?></th>
                  <th><? echo $sumatotalfuente; ?></th>
                  <th><? echo $sumatotalica;  ?></th>
                </tr>
              </tfoot>
            </table>
          </div>
        </div>
      </div>
      
      <?php include $r."inc/foot.php"; ?>
      <script>
      $('#dataTable').DataTable( {
        "lengthMenu": [[-1,25, 50, 100, 500], ["All",25, 50, 100, 500 ]]
    } );
  </script>
    </div>
  </div>
</body>
</html>
