<?php
$r = "../";
require($r."vendor/fpdf181/fpdf.php");
require_once($r."inc/funciones.php");
$codigopedido=$_GET['code'];
$mysqli = connectDB($r);
$infopedido = sinfo('z_orden_compra',$codigopedido);
$ica= $infopedido['ica'];
$fuente = $infopedido['retefuente'];

// Instanciation of inherited class
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('P','Letter');  
$pdf->SetMargins(15, 0, 1); 
#Establecemos el margen inferior: 
$pdf->SetAutoPageBreak(true,2); 
//logo
$pdf->Image('imgs/logo.png',15,5,80,15,'png');

$pdf->SetFont("Arial", "B",18);
$pdf->Text(135,20,utf8_decode('ORDEN DE COMPRA'));

$pdf->Cell(185,20,'',0,1,"");
$pdf->SetFont("Arial", "B",11);
$pdf->Cell(25,6,utf8_decode('NIT: '),0,0,"");
$pdf->SetFont("Arial", "",11);
$pdf->Cell(70,6,utf8_decode('900.839.489-4'),0,0,"");
$pdf->SetFont("Arial", "B",11);
$pdf->Cell(90,6,'FECHA: '.$infopedido['fecha_entrega'],0,1,"L");

$pdf->SetFont("Arial", "B",11);
$pdf->Cell(25,6,utf8_decode('DIRECCION:'),0,0,"");
$pdf->SetFont("Arial", "",11);
$pdf->Cell(70,6,utf8_decode('Carrera 66a #3-26'),0,0,"");
$pdf->Cell(90,6,'CQMF#: '.$infopedido['id'],0,1,"L");
$pdf->SetFont("Arial", "B",11);
$pdf->Cell(25,6,utf8_decode('TELEFONO: '),0,0,"");
$pdf->SetFont("Arial", "",11);
$pdf->Cell(70,6,utf8_decode('6018050046'),0,1,"");
$pdf->SetFont("Arial", "B",11);
$pdf->Cell(25,6,utf8_decode('CELULAR: '),0,0,"");
$pdf->SetFont("Arial", "",11);
$pdf->Cell(70,6,utf8_decode('3138294092'),0,1,"");
$pdf->SetFont("Arial", "B",11);
$pdf->Cell(95,6,utf8_decode('www.quimiforen.com'),0,1,"");
$pdf->Cell(95,6,utf8_decode('Bogota-Colombia'),0,1,"");
$pdf->Cell(185,6,'',0,1,"C");

$pdf->SetFont("Arial", "",10);
$pdf->setFillColor(0,80,160); 
$pdf->SetFont("Arial", "B",10);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(96,6,utf8_decode('DATOS DEL PROVEEDOR'),0,0,"",1);
$pdf->Cell(90,6,'REQUISITOS',0,1,"L",1);
$pdf->Cell(186,3,'',0,1,"L");

$pdf->SetTextColor(0,0,0);
$pdf->SetFont("Arial", "",10);
$pdf->Cell(24,6,utf8_decode('PROVEEDOR: '),0,0,"");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(72,6,utf8_decode($infopedido['proveedor']),0,0,"");

$medio = sinfo('z_pagos_medios',$infopedido['idpago']);
$pdf->SetFont("Arial", "B",10);
$pdf->Cell(13,6,'PAGO: ',0,0,"L");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(77,6,utf8_decode($medio['medio']),0,1,"L");

$pdf->SetFont("Arial", "B",10);
$pdf->Cell(24,6,utf8_decode('CONTACTO: '),0,0,"");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(72,6,utf8_decode($infopedido['asesor']),0,0,"");

$pdf->SetFont("Arial", "b",10);
$pdf->Cell(27,6,'DOCUMENTOS: ',0,0,"L");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(63,6,utf8_decode(substr($infopedido['documentos'],0,30)),0,1,"L");

$pdf->SetFont("Arial", "B",10);
$pdf->Cell(24,6,utf8_decode('TELEFONO: '),0,0,"");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(72,6,utf8_decode($infopedido['celular']),0,0,"");

$pdf->Cell(90,6,utf8_decode(substr($infopedido['documentos'],30,70)),0,1,"L");

$pdf->SetFont("Arial", "B",10);
$pdf->Cell(24,6,utf8_decode('CIUDAD: '),0,0,"");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(72,6,utf8_decode(sinfo('z_municipios',$infopedido['ciudad'])['municipio']),0,0,"");
$pdf->Cell(90,6,utf8_decode(substr($infopedido['documentos'],100,70)),0,1,"L");

$pdf->SetFont("Arial", "B",10);
$pdf->Cell(24,6,utf8_decode('CORREO: '),0,0,"");
$pdf->SetFont("Arial", "",10);
$pdf->Cell(72,6,utf8_decode($infopedido['correo']),0,0,"");




//ENCABEZADO FACTURA
$pdf->Cell(200,13,"",'0',1,"C");
$row1 = $mysqli->query("SELECT * FROM `z_orden_compra_pedido` WHERE idordencompra=$codigopedido AND eliminado=0");


$pdf->SetTextColor(255,255,255);
$pdf->SetFont("Arial", "B",10);
$pdf->Cell(96,6,utf8_decode('PRODUCTO'),0,0,"",1);
$pdf->Cell(17,6,'UND',0,0,"C",1);
$pdf->Cell(30,6,'PRECIO',0,0,"R",1);
$pdf->Cell(42,6,'TOTAL',0,1,"R",1);


$pdf->SetTextColor(0,0,0);
$pdf->SetFont("Arial", "",9);
$ivas = 0;
while($row2 = $row1->fetch_assoc()) {
    $producto   = $row2['producto'];
    $cantidad   = $row2['cantidad'];
    $presentacion   = $row2['presentacion'];
    $iva        = $row2['iva'];
    $precio     = $row2['precio'];
    $total=$precio*$cantidad;
    // imprecion
    $pdf->Cell(96,6,utf8_decode($producto.' - ('.$presentacion.')'),1,0,"");
    $pdf->Cell(17,6,number_format($cantidad),1,0,"C");
    $pdf->Cell(30,6,number_format($precio),1,0,"R");
    $pdf->Cell(42,6,number_format($total),1,1,"R");
}
$pdf->Cell(96,6,utf8_decode(''),0,0,"");
$pdf->Cell(17,6,'',0,0,"C");
$pdf->SetFont("Arial", "B",11);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(30,6,'SUBTOTAL',0,0,"R",1);
$pdf->Cell(42,6,number_format($infopedido['subtotal']),0,1,"R",1);
$pdf->SetFont("Arial", "",10);
$pdf->SetTextColor(0,0,0);

$pdf->Cell(96,6,utf8_decode(''),0,0,"");
$pdf->Cell(17,6,'',0,0,"C");
$pdf->Cell(30,6,'',0,0,"R");
$pdf->Cell(42,6,'',0,1,"R");

$pdf->SetFont("Arial", "B",10);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(113,6,'COMENTARIOS O INSTRUCCIONES',0,0,"C",1);
$pdf->Cell(30,6,'IVA',0,0,"R",1);
$pdf->SetFont("Arial", "",10);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(42,6,number_format($infopedido['tiva']),1,1,"R");

$pdf->Cell(113,6,utf8_decode(substr($infopedido['comentarios'],0,74)),0,0,"L");
$pdf->SetFont("Arial", "B",10);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(30,6,'RETEFUENTE',0,0,"R",1);
$pdf->SetFont("Arial", "",10);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(42,6,number_format($fuente),1,1,"R");

$pdf->Cell(113,6,utf8_decode(substr($infopedido['comentarios'],74,74)),0,0,"C");
$pdf->SetFont("Arial", "B",10);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(30,6,'ICA',0,0,"R",1);
$pdf->SetFont("Arial", "",10);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(42,6,number_format($ica),1,1,"R");

$pdf->Cell(113,6,utf8_decode(substr($infopedido['comentarios'],148,74)),0,0,"C");
$pdf->SetFont("Arial", "B",10);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(30,6,'OTROS',0,0,"R",1);
$pdf->SetFont("Arial", "",10);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(42,6,number_format($infopedido['adicional']),1,1,"R");


$pdf->Cell(113,6,utf8_decode(substr($infopedido['comentarios'],233,74)),0,0,"C");
$pdf->SetFont("Arial", "B",10);
$pdf->SetTextColor(255,255,255);
$pdf->Cell(30,6,'TOTAL A PAGAR',0,0,"R",1);
$pdf->Cell(42,6,number_format(($infopedido['subtotal']+$infopedido['tiva']+$infopedido['adicional'])-($infopedido['ica']+$infopedido['retefuente'])),0,1,"R",1);
$pdf->SetFont("Arial", "",11);
$pdf->SetTextColor(0,0,0);
$pdf->Cell(185,20,'',0,1,"L");
$pdf->SetFont("Arial", "",11);
$pdf->Cell(185,4,utf8_decode('María Isabel Forero'),0,1,"L");
$pdf->SetFont("Arial", "B",9);
$pdf->Cell(35,4,'DPTO COMERCIAL',0,1,"C");


$pdf_output = $pdf->Output('S');

//$pdf->Output('S');

// Enviar el PDF al navegador y inhabilitar el almacenamiento en caché
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename=OCQM-'.$codigopedido.'.pdf"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
echo $pdf_output;
?>