<?php
include ('bd.php');
require ('fpdf/fpdf.php');
require_once('phpqrcode/qrlib.php');
require_once('CodigoControlV7.php');
date_default_timezone_set("America/La_Paz");


class PDF extends FPDF
{

	function Header()
	{
		//Logo

		$this->Image('logo.jpg',10,10,-300);
		//Arial bold 15
		$this->SetFont('Arial','B',12);
		//Movernos a la derecha
		$this->Cell(80);
		//T�tulo
		$this->Cell(30,10,'PLASTICOS BAUTISTA',0,1,'C');
		$this->Cell(30,10,'A & B',0,0,'C');
		//Salto de l�nea
		$this->Ln(20);
	}
	function Footer()
	{
		//Posici�n: a 1,5 cm del final
		$this->SetY(-15);
		//Arial italic 8
		$this->SetFont('Arial','I',8);
		//N�mero de p�gina
		$this->Cell(0,10,'',0,0,'C');
	}
}

$con = connect();
$query = "SELECT * FROM operation o, sell s, product p WHERE o.sell_id = (SELECT MAX(id) FROM sell ) and p.id = o.product_id and o.created_at = s.created_at";
$_query = mysql_query($query, $con) or die(mysql_error());
$row = mysql_fetch_assoc($_query);

$product_id = $row['product_id'];
$q = $row['q'];
$sell_id = $row['sell_id'];
$name = $row['name'];
$description = $row['description'];
$price_out = $row['price_out'];
$presentation = $row['presentation'];
$numero_autorizacion = '2904002737366';
$nit_cliente = $row['nit'];
$nit_nombre = $row['nit_name'];
$fecha = '28042007';//date('d') + date('m') + date('Y');
$clave = 'x�_PiJrs%9b_yf=Be}xkJ(=HZR7_I]FH9fP(S_BiV}nFQMZa7j[d5%Q=8$G*D([7n';
	
//DATOS A MOSTRAR

$dia=date('d');
$anio=date('Y');	
$mes=date('m');

			if($mes == '01')
				$letra = 'Enero';
			if($mes == '02')
				$letra = 'Febrero';
			if($mes == '03')
				$letra = 'Marzo';
			if($mes == '04')
				$letra = 'Abril';
			if($mes == '05')
				$letra = 'Mayo';
			if($mes == '06')
				$letra = 'Junio';
			if($mes == '07')
				$letra = 'Julio';
			if($mes == '08')
				$letra = 'Agosto';
			if($mes == '09')
				$letra = 'Septiembre';
			if($mes == '10')
				$letra = 'Octubre';
			if($mes == '11')
				$letra = 'Noviembre';
			if($mes == '12')
				$letra = 'Diciembre';
								
$fecha_l = $dia.' de '.$letra.' de '.$anio;


$pdf  = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',10);
	
$pdf->Image('logo.jpg', 10, 170, 40, 40, 'jpg');//set font to arial, bold, 14pt
$pdf->SetFont('Arial','B',10);

//Cell(width , height , text , border , end line , [align] )
$pdf->Image('logo.jpg',0, 0, 40, 40, 'jpg');
$pdf->Cell(100 ,5,'PLASTICOS BAUTISTA',0,1);

$pdf->Cell(50 ,5,'A & B',0,0);
$pdf->Cell(70 ,0,'NOTA DE ENTREGA',0,1,'C');//end of line

//set font to arial, regular, 12pt
$pdf->SetFont('Arial','',12);

$pdf->Cell(110 ,5,'',0,0);
$pdf->Cell(59 ,5,'',0,1);//end of line

$pdf->Image('logo.jpg',10,10,-300);

$pdf->Cell(110 ,5,'Km. 7 1/2 carretera Sacaba',0,0);
$pdf->Cell(45 ,5,'Cochabamba',0,0);
$pdf->Cell(34 ,5,'',0,1);//end of line

$pdf->Cell(110 ,5,'Esmeralda norte - Zona Puntiti',0,0);
$pdf->Cell(45 ,5,'NRO FACTURA',0,0);
$pdf->Cell(34 ,5,''.$sell_id,0,1);//end of line

$pdf->Cell(110 ,5,'',0,0);
$pdf->Cell(45 ,5,' DIA | MES | AÑO',0,0);
$pdf->Cell(34 ,5,'',0,1);//end of line

//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line

//billing address
$pdf->Cell(100 ,5,'Lugar y Fecha : La Paz, '.$fecha_l,0,1);//end of line
$pdf->Cell(100 ,5,'Hora : '.date('H:i'),0,1);//end of line

//add dummy cell at beginning of each line for indentation

$pdf->Cell(90 ,5,'',0,1);
$pdf->Cell(90 ,5,'Nombre: '.$nit_nombre,0,1);
$pdf->Cell(90 ,5,'Direccion: '.$nit_cliente,0,1);

$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'',0,1);

$pdf->Cell(10 ,5,'',0,0);

$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'',0,1);

$pdf->SetFont('Arial','B',20);

//$pdf->Cell(155 ,5,'',0,1,'C');

$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'',0,1);



//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line

//invoice contents
$pdf->SetFont('Arial','B',12);

$pdf->Cell(22 ,5,'Cantidad',1,0);
$pdf->Cell(92 ,5,'Concepto',1,0, 'C');
$pdf->Cell(25 ,5,'Medida',1,0);
$pdf->Cell(25 ,5,'P.U./Kilo',1,0);
$pdf->Cell(20 ,5,'Subtotal',1,1);//end of line

$pdf->SetFont('Arial','',12);

//Numbers are right-aligned so we give 'R' after new line parameter
$query1 = "SELECT * FROM operation o, sell s, product p WHERE o.sell_id = (SELECT MAX(id) FROM sell ) and p.id = o.product_id and o.created_at = s.created_at";
$_query1 = mysql_query($query1, $con) or die(mysql_error());
//display the items
while($item = mysql_fetch_array($_query1)){
	$pdf->Cell(22	,5,$item['q'],1,0);
	$pdf->Cell(92 ,5,$item['name']." ".$item['description'],1,0);
	$pdf->Cell(25 ,5,$item['price_out'],1,0);
	$pdf->Cell(25 ,5,$item['price_out'],1,0);
	$subtotal = $item['price_out']*$item['q'];
	$pdf->Cell(20 ,5,$subtotal,1,1,'R');//end of line
	$total += $subtotal;
}

$codigocontrol = CodigoControlV7::generar($numero_autorizacion, $sell_id, $nit_cliente, $fecha, $total, $clave);
$qr_ = '13281324014|'.$sell_id.'|'.$numero_autorizacion.'|'.date('d/m/y').'|'.$total.'|'.$total.'|'.$nit_cliente.'|'.$codigocontrol.'|0|0|0|0';

QRcode::png($qr_,"qr.png");


//summary
$pdf->Cell(89 ,5,'TOTAL BS.',0,0);
$pdf->Cell(25 ,5,'',0,0);
$pdf->Cell(25 ,5,'',1,0);
$pdf->Cell(25 ,5,'',1,0);
$pdf->Cell(20 ,5,$total,1,1,'R');//end of line


//$pdf->Cell(0,30,'CODIGO DE CONTROL '.$codigocontrol,0,1);

//$pdf->Image('qr.png', 10, 170, 40, 40, 'png');

//$pdf->Cell(119 ,5,'"ESTA FACTURA CONTRIBUYE AL DESARROLLO DEL PAIS,',0,1);
//$pdf->Cell(119 ,5,'EL USO ILICITO DE ESTA SERA SANCIONADO DE ACUERDO A LA LEY, "',0,0);

$pdf->Cell(700,85,$pdf->Image('logo.jpg',30,12,160),0,0,'C');
$pdf->Cell(100,12,"Presupuesto: ". $campodb['nropresuputao']);
$pdf->Cell(100,12,"Fecha: ". date('d/m/Y'));
$pdf->Line(35,40,190,40);
$pdf->Cell(90,12,"Nif: ".$rowcli['nif']);
$pdf->Line(35,48,190,48);
$pdf->Ln(7);
$pdf->Cell(100,12,"Domicilio: ". $campodb['direccion']);
$pdf->Line(35,56,190,56);
$pdf->Ln(7);
$pdf->Cell(90,12,"Teléfono: ".$campodb['telefono']);
$pdf->Line(35,62,190,62);
$pdf->Ln(7);
$pdf->Cell(100,12,"Equipo: ".$campodb['ordenador']);
$pdf->Line(35,68,190,68);
$pdf->Ln(9);
$pdf->SetFont('Arial','B',10);
 
$pdf->Cell(60,12,'PRESUPUESTO');
 
$pdf->Ln(2);
 
$pdf->SetFont('Arial','',8);

$pdf->Output();