<?php
$r = "../";
require($r."vendor/fpdf181/fpdf.php");
$code=$_GET['code'];
$codigopedido = intval($code);
require_once($r."inc/funciones.php");
$mysqli = connectDB($r);
$spe=$_SESSION['perfil'];
$row1 = $mysqli->query("SELECT * FROM `z_pedidos` WHERE codigopedido=$codigopedido");
$infopedido = $row1->fetch_assoc();



$infocliente = info('nombre, cedula, direccion, celular1, municipio','z_clientes',$infopedido['idcliente']);

$nomcliente =$infocliente['nombre'];
$facturacion =$infocliente['facturacion'];
$nitcliente =$infocliente['cedula'];
$dircliente =$infocliente['direccion'];
$telcliente =$infocliente['celular1'];
$city=scity($infocliente['municipio']);

// 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('logo.jpg',30,0,20,11.6,'JPG');
$pdf->SetFont("Arial", "",11);
$pdf->Cell(196,37,"",'0',"1","C");
$pdf->Cell(215,3,utf8_decode($nomcliente),'0',"1","L");
$pdf->Cell(195,5,"",'0',1,"C");
$pdf->Cell(58,3,substr("$dircliente", 0, 35),'0',0,"");
$pdf->Cell(40,3,"$nitcliente",'0',0,"");
$pdf->Cell(25,3,"$fechai",'0',0,"");
$pdf->Cell(25,3,"$fechaf",'0',"0","");
$pdf->Cell(30,3,"",'0',"1","C");
$pdf->Cell(196,5,"",'0',1,"C");
$pdf->Cell(58,3,utf8_decode("$city[municipio]"),'0',0,"");
$pdf->Cell(40,3,"$telcliente",'0',0,"");
$pdf->Cell(25,3,"",'0',0,"");
$pdf->Cell(25,3,"",'0',"0","");
$pdf->Cell(30,3,"",'0',"1","C");

//ENCABEZADO FACTURA
$pdf->Cell(200,13,"",'0',1,"C");
$row1 = $mysqli->query("SELECT * FROM `z_pedido` WHERE codigopedido=$codigopedido");
while($row2 = $row1->fetch_assoc()) {
    
    $cantidad   = $row2['cantidad'];
    $iva        = $row2['iva'];
    $precio     = $row2['precio'];
    $bono       = $row2['bono'];
    
    $bonos = $bonos+$bono;
    $infoproducto= info('codigo, producto','z_productos',$row2['idproducto']);
    $infopresentacion = sinfo('z_presentaciones_productos',$row2['idpresentacion']);
    $nomprod=$infoproducto['producto'];
    $codprod=$infoproducto['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($infoproducto['producto'], 0, 30);
    // imprecion
    $pdf->Cell(17,5,numodec($cantidad),0,'0',"C");
    $pdf->Cell(96,5,utf8_decode('('.$infopresentacion['presentacion'].') '.$infoproducto['codigo'].' - '.$infoproducto['producto']),0,0,"");
    $pdf->Cell(30,5,number_format($precio),0,0,"R");
    $pdf->Cell(42,5,number_format($totaldes),0,1,"R");
  
    
}
$numero=round($subtotaldes+$tivas-$fuente-$ica,0,PHP_ROUND_HALF_UP);
$resultado = convertir($numero);
$subtraer1=substr("$resultado", 0, 54);
$subtraer2=substr("$resultado", 54, 54);
$pdf->Text(13, 150, $subtraer1);
$pdf->Text(13, 154, $subtraer2);
$pdf->Text(170, 146,number_format($subtotal));
$pdf->Text(170, 152,number_format($tivas));
$pdf->Text(170, 158,number_format($fuente));
$pdf->Text(170, 164,number_format($ica));
$pdf->Text(170, 170,number_format($subtotaldes+$tivas-$fuente-$ica));

$pdf->Output();
?>