<?php

	# Incluyendo librerias necesarias #
	require "../../../pdf/fpdf.php";

	class PDF extends FPDF
{


}
require('../../../Clases/db.php');
  if(empty($_REQUEST['cl']) || empty($_REQUEST['f']))
  {
    echo "No es posible generar la factura.";
  }else{
    $codCliente = $_REQUEST['cl'];
    $noFactura = $_REQUEST['f'];

$mostrar = new conn ();
$conex = $mostrar->crearconn();
$sql = "SELECT f.N_Factura,DATE_FORMAT(f.Fecha, '%d/%m/%Y') as fecha,DATE_FORMAT(f.Fecha,'%H:%i:%s') as  hora, f.Total, f.ID_Cliente, us.Username as User_Vendedor, d.Nombre as Nombre_cliente, d.Cedula as Cedula_cliente, d.Telefono as telefono, d.Direccion as direccion   from factura f
INNER join cliente cl  on f.ID_Cliente = cl.ID 
INNER join usuario us  on f.ID_Usuario = us.ID 
INNER JOIN datos_personales d on cl.CI_Datos = d.Cedula where f.N_Factura = $noFactura and d.Cedula = $codCliente";
$result = $conex->query($sql);

$pdf = new PDF();

while($row = $result->fetch_assoc()){
    $pdf->SetX(4);

$pdf->SetMargins(17,17,17);
	$pdf->AddPage();
$pdf->Image('../../../Imagenes/logo.png',165,12,35,35,'PNG');
	$pdf->SetFont('Arial','B',16);
	$pdf->SetTextColor(32,100,210);
	$pdf->Cell(150,10,iconv("UTF-8", "ISO-8859-1",strtoupper("Emperadora Internet y Mas")),0,0,'L');

	$pdf->Ln(9);

	$pdf->SetFont('Arial','',10);
	$pdf->SetTextColor(39,39,51);
	$pdf->Cell(150,9,iconv("UTF-8", "ISO-8859-1","RIF: J-29966140-6"),0,0,'L');

	$pdf->Ln(5);

	$pdf->Cell(150,9,iconv("UTF-8", "ISO-8859-1","Valencia Barrio Monumental III Av principal"),0,0,'L');

	$pdf->Ln(5);

	$pdf->Cell(150,9,iconv("UTF-8", "ISO-8859-1","Teléfono: 0241-8480889"),0,0,'L');

	$pdf->Ln(5);

	$pdf->Cell(150,9,iconv("UTF-8", "ISO-8859-1","Email: laemperadorainternetymas@gmail.com"),0,0,'L');

	$pdf->Ln(10);

	$pdf->SetFont('Arial','',10);
	$pdf->Cell(30,7,iconv("UTF-8", "ISO-8859-1","Fecha de emisión:"),0,0);
	$pdf->SetTextColor(97,97,97);
	$pdf->Cell(116,7,iconv ("UTF-8", "ISO-8859-1",date("d/m/Y", strtotime($row['fecha']))." ".date($row['hora'])),0,0,'L');
	$pdf->SetFont('Arial','B',10);
	$pdf->SetTextColor(39,39,51);
	$pdf->Cell(35,7,iconv("UTF-8", "ISO-8859-1",strtoupper("Factura Nro.")),0,0,'C');

	$pdf->Ln(7);

	$pdf->SetFont('Arial','',10);
	$pdf->Cell(12,7,iconv("UTF-8", "ISO-8859-1","Cajero:"),0,0,'L');
	$pdf->SetTextColor(97,97,97);
	$pdf->Cell(134,7,iconv("UTF-8", "ISO-8859-1",$row['User_Vendedor']),0,0,'L');
	$pdf->SetFont('Arial','B',10);
	$pdf->SetTextColor(97,97,97);
	$pdf->Cell(35,7,iconv("UTF-8", "ISO-8859-1",strtoupper($row['N_Factura'])),0,0,'C');

	$pdf->Ln(10);

	$pdf->SetFont('Arial','',10);
	$pdf->SetTextColor(39,39,51);
	$pdf->Cell(13,7,iconv("UTF-8", "ISO-8859-1","Cliente:"),0,0);
	$pdf->SetTextColor(97,97,97);
	$pdf->Cell(60,7,iconv("UTF-8", "ISO-8859-1",$row['Nombre_cliente']),0,0,'L');
	$pdf->SetTextColor(39,39,51);
	$pdf->Cell(13,7,iconv("UTF-8", "ISO-8859-1","Cedula: "),0,0,'L');
	$pdf->SetTextColor(97,97,97);
	$pdf->Cell(60,7,iconv("UTF-8", "ISO-8859-1",$row['Cedula_cliente']),0,0,'L');
	$pdf->SetTextColor(39,39,51);
	$pdf->Cell(7,7,iconv("UTF-8", "ISO-8859-1","Tel:"),0,0,'L');
	$pdf->SetTextColor(97,97,97);
	$pdf->Cell(35,7,iconv("UTF-8", "ISO-8859-1",$row['telefono']),0,0);
	$pdf->SetTextColor(39,39,51);

	$pdf->Ln(7);

	$pdf->SetTextColor(39,39,51);
	$pdf->Cell(6,7,iconv("UTF-8", "ISO-8859-1","Dir:"),0,0);
	$pdf->SetTextColor(97,97,97);
	$pdf->Cell(109,7,iconv("UTF-8", "ISO-8859-1",$row['direccion']),0,0);

	$pdf->Ln(9);
	# Tabla de productos #
	$pdf->SetFont('Arial','',8);
	$pdf->SetFillColor(23,83,201);
	$pdf->SetDrawColor(23,83,201);
	$pdf->SetTextColor(255,255,255);
	$pdf->Cell(32,8,iconv("UTF-8", "ISO-8859-1","Cod Producto"),1,0,'C',true);
	$pdf->Cell(90,8,iconv("UTF-8", "ISO-8859-1","Producto"),1,0,'C',true);
	$pdf->Cell(15,8,iconv("UTF-8", "ISO-8859-1","Cant."),1,0,'C',true);
	$pdf->Cell(25,8,iconv("UTF-8", "ISO-8859-1","Precio $"),1,0,'C',true);
	$pdf->Cell(25,8,iconv("UTF-8", "ISO-8859-1","Subtotal $"),1,0,'C',true);

	$pdf->Ln(8);

	
	$pdf->SetTextColor(39,39,51);
     /*----------  Detalles de la tabla  ----------*/
     $sql2 = "SELECT d.Cantidad, d.Precio_Unt, d.subtotal, d.ID_Factura, d.Cod_Productos, p.Nombre as Nombre_Producto from detalles_factura d 
			inner join productos p on d.Cod_Productos = p.Cod 
			where ID_Factura = '$noFactura'";
$resultado = $conex->query($sql2);

while($rows = $resultado->fetch_assoc()){


    $pdf->Cell(32,7,iconv("UTF-8", "ISO-8859-1",$rows['Cod_Productos']),'LR',0,'C');
    $pdf->Cell(90,7,iconv("UTF-8", "ISO-8859-1",$rows['Nombre_Producto']),'L',0,'C');
	$pdf->Cell(15,7,iconv("UTF-8", "ISO-8859-1",$rows['Cantidad']),'L',0,'C');
	$pdf->Cell(25,7,iconv("UTF-8", "ISO-8859-1",$rows['Precio_Unt']),'L',0,'C');
	$pdf->Cell(25,7,iconv("UTF-8", "ISO-8859-1",$rows['subtotal']),'LR',0,'C');
	$pdf->Ln(7);
	/*----------  Fin Detalles de la tabla  ----------*/

}

$pdf->SetFont('Arial','B',9);
$pdf->Cell(100,7,iconv("UTF-8", "ISO-8859-1",''),'T',0,'C');
	$pdf->Cell(15,7,iconv("UTF-8", "ISO-8859-1",''),'T',0,'C');
	$pdf->Cell(32,7,iconv("UTF-8", "ISO-8859-1","TOTAL FACTURA $"),'T',0,'C');
	$pdf->Cell(40,7,iconv("UTF-8", "ISO-8859-1",$row['Total']),'T',0,'C');

	$pdf->Ln(7);
$pdf->Output("I",$row['N_Factura'],true);
}
}
