<?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'];
$row1 = $mysqli->query("SELECT * FROM z_pedido INNER JOIN z_pedidos ON z_pedido.codigopedido=z_pedidos.codigopedido WHERE z_pedidos.codigopedido='$code'");
while($row2 = $row1->fetch_assoc()) {
    if($row2['estado']===0){
        echo 'FALTA APROBAR PEDIDOD';
        exit();
    }
    $codigo=$row2['codigopedido'];
    $ica=$row2['ica'];
    $fuente=$row2['fuente'];
    $abono=$row2['abono'];
    $fecha=$row2['fecha'];
    $idcliente=$row2['idcliente'];
    $adicional=$row2['adicional'];
    $rutero=$row2['idrutero'];
    $estado=$row2['estado'];
    $pago=$row2['pago'];
    $nota=$row2['nota'];
    
}
$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(300,78));  
$pdf->SetMargins(2, 0, 2); 
#Establecemos el margen inferior: 
$pdf->SetAutoPageBreak(true,2); 
//logo

if($pago==2){
    if($estado==5){
        $pdf->Image('anulada.jpg',10,80,60,32,'JPG');
    }else{
        $pdf->Image('cancelado.png',10,80,60,32,'PNG');
    }
}
$pdf->Cell(15,2,"",0,1,"C");
//encabezado factura
$pdf->SetFont("Arial", "B",8);
$pdf->Cell(75,3,"TIQUETE DE SALIDA",'0',1,"C");
$pdf->Cell(75,3,"BOGOTA",'0',1,"C");
$pdf->Cell(75,10,"",0,1,"");
// decripcion del cliente y factura
$pdf->Cell(28,3,utf8_decode("TIQUETE DE SALIDA 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,"");
    $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(25,3,utf8_decode("Dirección: "),'0',0,"");
$pdf->Cell(48,3,utf8_decode(substr("$dircliente",0,43)),'0',1,"R");
$sub2=substr("$dircliente",43,43);
if($sub2!=""){
$pdf->Cell(25,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,"");
if($facturacion==0){
    $pdf->Cell(45,3,'1111111111','0',1,"R");
}else{
    $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(33,3,"NOMBRE PRODUCTO",0,0,"");
$pdf->Cell(15,3,"CANT",0,0,"L");
$pdf->Cell(10,3,"PRECIO",0,0,"R");
$pdf->Cell(15,3,"TOTAL",0,1,"R");
$pdf->Cell(75,5,"$linea",0,1,"");
$row1 = $mysqli->query("SELECT * FROM z_pedido INNER JOIN z_pedidos ON z_pedido.codigopedido=z_pedidos.codigopedido WHERE z_pedidos.codigopedido='$code'");
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'];
    $infopresentacion = sinfo('z_presentaciones_productos',$row2['idpresentacion']);
    $total=$precio*$cantidad;
    $totalitems=$totalitems+$cantidad;
    $subtotal=$subtotal+$total;
    $des=$row2['descuento'];
    $descu=($precio*$des)/100;
    $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(35,3,utf8_decode($nombresub),'0','0',"");
    $pdf->Cell(10,3,numodec($cantidad),'0','0',"C");
    $pdf->Cell(10,3,'$'.number_format($precio),'0','0',"C");
    $pdf->Cell(18,3,'$'.number_format($totaldes),'0',1,"R");
    $pdf->Cell(35,3,'('.utf8_decode($infopresentacion['presentacion']).')','0','1',"");
    
}

$pdf->Cell(75,1,"",0,1,"");
$pdf->Cell(75,5,"$linea",0,1,"");
$pdf->Cell(75,1,"",0,1,"");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(38,4,"SUB TOTAL",'0','0',"R");
    $pdf->SetFont("Arial", "B",10);
    $pdf->Cell(35,4,moneda($subtotaldes),'0',1,"R");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(38,4,"ADICIONALES",'0','0',"R");
    $pdf->SetFont("Arial", "B",10);
    $pdf->Cell(35,4,moneda($adicional),'0',1,"R");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(38,4,"TOTAL",'0','0',"R");
    $pdf->SetFont("Arial", "B",10);
    $ttotal = $subtotaldes+$adicional;
    $pdf->Cell(35,4,moneda($ttotal),'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,3,"OBSERVACIONES: ".utf8_decode(substr($nota, 0, 35)),0,1,"L");
$pdf->Cell(75,3,utf8_decode(substr($nota, 35, 57)),0,1,"L");
$pdf->Cell(75,3,utf8_decode(substr($nota, 92, 57)),0,1,"L");

$pdf->Cell(75,16,"",0,1,"");
$pdf->Cell(75,1,"____________________________________",0,1,"C");
$pdf->Cell(75,2,"",0,1,"");
$pdf->Cell(75,1,"Firma y Sello de Recibido por",0,1,"C");
$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");

$pdf->Output();
?>