<?php
header('Content-Type: text/html; charset=UTF-8');
include ('bd.php');
require ('fpdf/fpdf.php');
require ('alphapdf.php');
date_default_timezone_set("America/La_Paz");

class PDF extends FPDF
{

	function Header()
	{
		//Logo
		$this->SetFillColor(0,0,0); 
		$this->Image('logo.jpg',0,2,45);
		//Arial bold 15
		$this->SetFont('Arial','B',8);
		//Movernos a la derecha
		$this->Cell(30);
		//T�tulo
		$this->Cell(21,5,'PLASTICOS BAUTISTA',0,0,'C');
		$this->Cell(98);
		$this->SetFont('Arial','B',9);
		$this->Cell(21,5,'COCHABAMBA',0,1,'C');
		$this->Cell(20);
		$this->Cell(31,5,'A & B',0,0,'C');
		$this->Cell(20);
		$this->SetFont('Arial','B',12);
		$this->Cell(31,5,'SOLICITUD A PROVEEDOR',0,0,'C');
		$this->Cell(35);
		$this->SetFont('Arial','B',9);
		$this->Cell(10,5,'DIA',0,0,'C');
		$this->Cell(5);
		$this->Cell(10,5,'MES',0,0,'C');
		$this->Cell(5);
		$this->Cell(10,5,utf8_decode('AÑO'),0,1,'C');
		
		$this->SetFont('Arial','',8);
		$this->Cell(31,4,'Km. 7.5 carretera Sacaba',0,1);
		$this->Cell(31,4,'Esmeralda Norte - Zona Puntiti',0,1);
		$this->Cell(31,4,'Telf: 4245622 - 76418600 - 76490462',0,1);
		$this->Cell(31,4,'Email: ventas@plasticosbautista-ayb.com',0,1);
		$this->Cell(31,4,'www.plasticosbautista-ayb.com',0,1);
		//Salto de l�nea
		$this->Ln(5);
	}
	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');

		$this->Cell(70 ,5,'Recibido por',0,0,'C');
		$this->Cell(70 ,5,'Entregado por',0,0,'C');
		$this->Cell(70 ,5,'V.B.',0,0,'C');
	}
	function addNumTVA($tva)
	{
		$this->SetFont( "Arial", "B", 10);
		$r1  = $this->w - 80;
		$r2  = $r1 + 70;
		$y1  = 80;
		$y2  = $y1+10;
		$mid = $y1 + (($y2-$y1) / 2);
		$this->RoundedRect($r1, $y1, ($r2 - $r1), ($y2-$y1), 2.5, 'D');
		$this->Line( $r1, $mid, $r2, $mid);
		$this->SetXY( $r1 + 16 , $y1+1 );
		$this->Cell(40, 4, "TVA Intracommunautaire", '', '', "C");
		$this->SetFont( "Arial", "", 10);
		$this->SetXY( $r1 + 16 , $y1+5 );
		$this->Cell(40, 5, $tva, '', '', "C");
	}
}

$con = connect();
$query = "SELECT * FROM operation o, sell s, product p, person pe WHERE o.sell_id = (SELECT MAX(id) FROM sell ) and p.id = o.product_id and s.person_id = pe.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['address1'];
$nit_nombre = $row['name'];
$nit_nombre2 = $row['lastname'];
$nit = $nit_nombre." ".$nit_nombre2;
$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->SetAlpha(0.1);
$pdf->Image('fondo.png', 16, 100, $fpdf->w, $fpdf->h);
$pdf->SetAlpha(1);
$pdf->SetFont('Arial','',10);
	

//set font to arial, bold, 14pt
$pdf->SetFont('Arial','B',10);

//$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

$pdf->Cell(0 ,5,'Proveedor: '.$nit,1,1);

$pdf->Cell(0 ,5,'Direccion: '.$nit_cliente,1,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',10);

$pdf->Cell(24 ,5,'CANTIDAD',1,0);
$pdf->Cell(100 ,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,'TOTAL',1,1);//end of line

$pdf->SetFont('Arial','',10);

//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(24	,5,$item['q'],1,0);
	$pdf->Cell(100 ,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;
}

$query2 = "SELECT * FROM sell WHERE id = $sell_id";
$_query2 = mysql_query($query2, $con) or die(mysql_error());
$cons = mysql_fetch_assoc($_query2);

//summary
if($cons['discount'] > 0) {
	$pdf->Cell(121 ,5,'Descuento:',0,0);
	$pdf->Cell(28 ,5,$cons['discount'],0,0);
} else {
	$pdf->Cell(121 ,5,'',0,0);
	$pdf->Cell(28 ,5,'',0,0);
}
$pdf->Cell(25 ,5,'TOTAL BS.',0,0);
$pdf->Cell(20 ,5,$total - $cons['discount'],1,1,'R');//end of line

$pdf->Cell(189 ,30,'',0,1);//end of line

$pdf->Output();