<?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'];
    $bono=$row2['bono'];
    $ica=$row2['ica'];
    $fuente=$row2['fuente'];
    $abono=$row2['abono'];
    $fechai=$row2['fecha'];
    $fecha = $fechai;
    $fechaComoEntero = strtotime($fecha);
    $fechai = date("d    m   y", $fechaComoEntero);
    $fechaf=$row2['fechaentrega'];
    $fecha = $fechaf;
    $fechaComoEntero = strtotime($fecha);
    $fechaf = date("d   m   y", $fechaComoEntero);
    $idcliente=$row2['idcliente'];
    $adicional=$row2['adicional'];
    $rutero=$row2['idrutero'];
    $pago=$row2['pago'];
    
}
$cliente=sclien($idcliente);
$nomcliente=$cliente['nombre'];
$facturacion=$cliente['facturacion'];
$nitcliente=$cliente['cedula'];
$dircliente=$cliente['direccion'];
$telcliente=$cliente['celular1'];
$city=scity($cliente['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,"$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");
/*
$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($dircliente),'0',1,"R");
$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");*/

//ENCABEZADO FACTURA
$pdf->Cell(200,13,"",'0',1,"C");
$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'];
    $funte=$row2['fuente'];
    $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->Cell(17,5,$cantidad,0,'0',"C");
    $pdf->Cell(11,5,utf8_decode(substr("$codprod", 0, 5)),0,0,"");
    $pdf->Cell(85,5,' - '.utf8_decode($nombresub),0,0,"");
    $pdf->Cell(30,5,'$ '.number_format($precio),0,0,"R");
    $pdf->Cell(42,5,'$ '.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;*/
    
}
$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->Cell(75,1,"",0,1,"");
$pdf->Cell(75,1,"$linea",0,1,"");
$pdf->Cell(75,1,"",0,1,"");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(48,3,"SUB TOTAL ",'0','0',"R");
    $pdf->Cell(25,3,'$ '.number_format($subtotal),'0',1,"R");
    $pdf->Cell(48,3,"ADICIONALES ",'0','0',"R");
    $pdf->Cell(25,3,'$ '.number_format($adicional),'0',1,"R");
    $pdf->Cell(48,3,"IVA ",'0','0',"R");
    $pdf->Cell(25,3,'$ '.number_format($tivas),'0',1,"R");
    $pdf->Cell(48,3,"RTE ICA ",'0','0',"R");
    $pdf->Cell(25,3,'$ '.number_format($ica),'0',1,"R");
    $pdf->Cell(48,3,"RTE FUENTE ",'0','0',"R");
    $pdf->Cell(25,3,'$ '.number_format($fuente),'0',1,"R");
    
    
    $pdf->Cell(75,2,"",0,1,"");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(48,4,"TOTAL A PAGAR",'0','0',"R");
    $pdf->SetFont("Arial", "B",10);
    $pdf->Cell(25,4,'$ '.number_format($subtotaldes+$adicional),'0',1,"R");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(48,4,"TOTAL UNDS",'0','0',"R");
    $pdf->SetFont("Arial", "B",10);
    $pdf->Cell(25,4,number_format($totalitems+$bonos),'0',1,"R");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(48,4,"ABONOS",'0','0',"R");
    $pdf->SetFont("Arial", "B",10);
    $pdf->Cell(25,4,$abono,'0',1,"R");
    $pdf->SetFont("Arial", "",8);
    $pdf->Cell(48,4,"SALDO RESTANTE",'0','0',"R");
    $pdf->SetFont("Arial", "B",10);
    if($pago==1){
    $pdf->Cell(25,4,'BONO','0',1,"R");}
    else{
    $pdf->Cell(25,4,'$ '.number_format($subtotaldes+$adicional-$abono),'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,"");*/
$pdf->Output();
?>