<?php
$r = "../";
require($r."vendor/fpdf181/fpdf.php");
$code=$_GET['code'];
require_once($r."inc/funciones.php");
validNotSession($r);
$mysqli = connectDB($r);
$spe=$_SESSION['perfil'];
if($spe!=$pad && $spe!=$psu && $spe!=$pbo){ exit(header("Location:".$r."dashboard.php?error=NO TIENES PERMISO PARA ESTA SECCION"));}
$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!='0'");
while($row2 = $row1->fetch_assoc()) {
    
    $codigo=$row2['codigopedido'];
    
    $abono=$row2['abono'];
    $ica=$row2['fuente'];
    $fuente=$row2['ica'];
    $fecha=$row2['fecha'];
    $idcliente=$row2['idcliente'];
    $adicional=$row2['adicional'];
    $rutero=$row2['idrutero'];
    $pago=$row2['pago'];
    
}
$linea="- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -";

$rute=srute($rutero);
$cliente=sclien($idcliente);
$nomcliente=$cliente['nombre'];
$facturacion=$cliente['facturacion'];
$nitcliente=$cliente['cedula'];
$dircliente=$cliente['direccion'];
$telcliente=$cliente['celular1'];
if($cliente['tipocliente']==1){$tipocliente="S";}
elseif($cliente['tipocliente']==2){$tipocliente="D";}
else{$tipocliente="O";}
$city=scity($cliente['municipio']);

// Instanciation of inherited class
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('P',array(180,78));  
$pdf->SetMargins(2, 0, 2); 
#Establecemos el margen inferior: 
$pdf->SetAutoPageBreak(true,2); 
//logo
//$pdf->Image('logo.jpg',30,0,20,11.6,'JPG');
$pdf->Cell(15,2,"",0,1,"C");
//encabezado factura
$pdf->SetFont("Arial", "B",14);
$pdf->Cell(75,3,"QUIMIFOREN S.A.S",'0',1,"C");

$pdf->SetFont("Arial", "I",8);
$pdf->Cell(75,3,"La Esencia de tu producto",'0',1,"C");

$pdf->SetFont("Arial", "",8);
$pdf->Cell(75,3,"NIT 900.839.489-4",'0',1,"C");
$pdf->Cell(75,3,utf8_decode("IVA RÉGIMEN COMÚN"),'0',1,"C");
$pdf->Cell(75,3,utf8_decode("actividad econimica 4664"),'0',1,"C");
$pdf->Cell(75,3,utf8_decode("SOMOS GRANDES CONTRIBUYENTES"),'0',1,"C");
$pdf->Cell(75,3,utf8_decode("Bodega calle 55 # 77j-14 Sur, Barrio Catalina II - Bogotá"),'0',1,"C");
$pdf->Cell(75,3,utf8_decode("Tel.: + 571 604 22 14"),'0',1,"C");
$pdf->Cell(75,3,utf8_decode("correo.: gerencia@quimiforen.com"),'0',1,"C");
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(75,3,utf8_decode("www.quimiforen.com"),'0',1,"C");
$pdf->Cell(75,3,"",0,1,"C");
// decripcion del cliente y factura
$pdf->Cell(28,3,utf8_decode("Factra de Venta N°.: "),'0',0,"");
$pdf->SetFont("Arial", "B",8);
$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");
$pdf->Cell(28,3,utf8_decode("Cliente: "),'0',0,"");
/*if($facturacion==0){
    $pdf->Cell(45,3,'VENTAS DE MOSTRADOR','0',1,"R");
}else{*/
    $sunmobre=substr("$nomcliente", 0, 45);
    $sunmobre2=substr("$nomcliente", 45, 30);
    $pdf->Cell(45,3,utf8_decode($sunmobre),'0',1,"R");
    if($sunmobre2!=""){
    $pdf->Cell(11,3,utf8_decode(""),'0',0,"");
    $pdf->Cell(45,3,utf8_decode($sunmobre2),'0',1,"L");}
/*}*/
$pdf->Cell(28,3,utf8_decode("Dirección: "),'0',0,"");
$pdf->Cell(45,3,utf8_decode(substr("$dircliente",0,43)),'0',1,"L");
$sub2=substr("$dircliente",43,43);
if($sub2!=""){
$pdf->Cell(28,3,utf8_decode(""),'0',0,"");
$pdf->Cell(45,3,utf8_decode("$sub2"),'0',1,"L");}
$pdf->Cell(28,3,utf8_decode("Ciudad: "),'0',0,"");
$pdf->Cell(45,3,utf8_decode($city['municipio']),'0',1,"R");
$pdf->Cell(28,3,utf8_decode("Teléfono: "),'0',0,"");
$pdf->Cell(45,3,utf8_decode($telcliente),'0',1,"R");
$pdf->Cell(28,3,utf8_decode("NIT y/o Cédula: "),'0',0,"");
$pdf->Cell(45,3,utf8_decode($nitcliente.' '.$tipocliente),'0',1,"R");
$pdf->Cell(28,3,utf8_decode("Rutero: "),'0',0,"");
$pdf->Cell(45,3,utf8_decode($rute['nombre']),'0',1,"R");
$pdf->Cell(75,1,"",0,1,"");
//ENCABEZADO FACTURA
$pdf->Cell(75,5,"$linea",0,1,"");
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(42,3,"NOMBRE PRODUCTO",0,0,"");
$pdf->Cell(12,3,"UNDS",0,0,"C");
$pdf->Cell(15,3,"VALOR",0,1,"R");
$pdf->Cell(75,5,"$linea",0,1,"");
$pdf->Cell(75,1,"",0,1,"");
$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!='0'");
while($row2 = $row1->fetch_assoc()) {
    $idproducto=$row2['idproducto'];
    $cantidad=$row2['cantidad'];
    $bono=$row2['bono'];
    $bonos=$bonos+$bono;
    $precio=$row2['precio'];
    $producto=sprod($idproducto);
    $nomprod=$producto['producto'];
    $codprod=$producto['codigo'];
    $total=$precio*$cantidad;
    $totalitems=$totalitems+$cantidad;
    $subtotal=$subtotal+$total;
    $des=$row2['descuento'];
    $iva=$row2['iva'];
    $iva=$iva*0.01;
    $descu=($precio*$des)/100;
    $iva=$total*$iva;
    $tivas=$tivas+$iva;
    $preciodes=$precio-$descu;
    $totaldes=$preciodes*$cantidad;
    $subtotaldes=$subtotaldes+$totaldes;
    $desc=$total-$totaldes;
    $descuen=$desc+$descuen;
    $nombresub=substr("$nomprod", 0, 20);
    // imprecion
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(42,3,utf8_decode($nombresub),'0','0',"");
    $pdf->Cell(13,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");}
    
    /*
    
    echo substr("$nomprod", 0, 20);
    echo $cantidad;
    echo $precio;*/
    
}

$pdf->Cell(75,1,"",0,1,"");
$pdf->Cell(75,5,"$linea",0,1,"");
$pdf->Cell(75,1,"",0,1,"");
    $pdf->Cell(75,2,"",0,1,"");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(48,4,"BASE IVA",'0','0',"R");
    $pdf->SetFont("Arial", "",10);
    $pdf->Cell(25,4,'$ '.number_format($subtotaldes),'0',1,"R");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(48,4,"IVA",'0','0',"R");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(25,4,'$ '.number_format($tivas),'0',1,"R");
    $pdf->Cell(48,4,"OTROS",'0','0',"R");
    $pdf->Cell(25,4,'$ '.number_format($adicional),'0',1,"R");
    $pdf->Cell(48,4,"RTE FUENTE",'0','0',"R");
    $pdf->Cell(25,4,'$ '.number_format($fuente),'0',1,"R");
    $pdf->Cell(48,4,"RTE ICA",'0','0',"R");
    $pdf->Cell(25,4,'$ '.number_format($ica),'0',1,"R");
    $pdf->Cell(48,4,"TOTAL",'0','0',"R");
     $pdf->Cell(25,4,'$ '.number_format($subtotaldes+$tivas-$ica-$fuente),'0',1,"R");
    $pdf->SetFont("Arial", "",8);
$pdf->Cell(75,5,"$linea",0,1,"");
$pdf->Cell(75,1,"",0,1,"");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(4,4,' ','LRBT',0,"");
$pdf->SetFont("Arial", "",8);
$pdf->Cell(28,4,"Empaque devolutivo",'0','1',"L");
// ---
$pdf->Cell(75,1,"",0,1,"");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(4,4,' ','LRBT',0,"L");
$pdf->SetFont("Arial", "",8);
$pdf->Cell(28,4,utf8_decode("Certificado de Análisis"),'0','1',"L");
//----
$pdf->Cell(75,1,"",0,1,"");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(4,4,' ','LRBT',0,"L");
$pdf->SetFont("Arial", "",8);
$pdf->Cell(28,4,"Cargue y Descargue",'0','1',"L");
$pdf->Cell(75,5,"$linea",0,1,"");


$pdf->Cell(75,1,"",0,1,""); 
$pdf->SetFont("Arial", "",7);   
$pdf->Cell(75,3,"Favor verificar su pedido al momento de recibirlo",0,1,"C");
$pdf->Cell(75,3,"Gracias por su compra",0,1,"C");
$pdf->Cell(75,3,utf8_decode("No se admiten reclamaciones después de"),0,1,"C");
$pdf->Cell(75,3,"8 Dias de entregado el producto",0,1,"C");
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(75,3,"conservar este recibo para reclamos",'0',1,"C");
/*
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();
?>