<?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");
include "core/autoload.php";
include "core/app/model/SellData.php";
include "core/app/model/ProductData.php";
include "core/app/model/PersonData.php";
include "core/app/model/PriceData.php";
include "core/app/model/BillData.php";
include "core/app/model/UserData.php";
include "core/app/model/SpendBoxData.php";
include "core/app/model/OperationData.php";
include "core/app/model/OperationTypeData.php";

class PDF extends FPDF

{
	var $widths;
	var $aligns;
	function SetWidths($w)
	{
		//Set the array of column widths
		$this->widths=$w;
	}
	function SetAligns($a)
	{
		//Set the array of column alignments
		$this->aligns=$a;
	}
	function fill($f)
	{
		//juego de arreglos de relleno
		$this->fill=$f;
	}
	function Row($data)
	{
		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
		$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
		$h=5*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++)
		{
		$w=$this->widths[$i];
		$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
		//Save the current position
		$x=$this->GetX();
		$y=$this->GetY();
		//Draw the border
		//$this->Rect($x,$y,$w,$h,$style);
		//Print the text
		$this->MultiCell($w,5,$data[$i],0,$a,$fill);
		//Put the position to the right of the cell
		$this->SetXY($x+$w,$y);
		}
		//Go to the next line
		$this->Ln($h);
	}
	function CheckPageBreak($h)
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger)
		$this->AddPage($this->CurOrientation);
	}
	function NbLines($w,$txt)
	{
		//Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont[‘cw’];
		if($w==0)
		$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",”,$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb–;
			$sep=-1;
			$i=0;
			$j=0;
			$l=0;
			$nl=1;
			while($i<$nb)
			{
				$c=$s[$i];
				if($c=="\n")
				{
					$i++;
					$sep=-1;
					$j=$i;
					$l=0;
					$nl++;
				continue;
			}
		if($c==' ')
		$sep=$i;
		$l+=$cw[$c];
		if($l>$wmax)
		{
			if($sep==-1)
			{
				if($i==$j)
				$i++;
			}
			else
				$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
		}
		else
			$i++;
		}
		return $nl;
	}
	function Header()
	{

		//Logo
		$this->SetFillColor(0,0,0); 
		$this->Image('logomod.jpg',0,2,60);
		$this->Cell(189 ,10,'',0,1);//end of line
		//Arial bold 15
		$this->SetFont('Arial','B',8);
		//Movernos a la derecha
		$this->Cell(30);
		//T�tulo
		$this->Cell(21,5,'',0,0,'C');
		$this->Cell(100);
		$this->SetFont('Arial','B',15);
		$this->Cell(21,5,'Nro.'.$GLOBALS['bill'],0,1,'C');
		$this->Cell(20);
		$this->Cell(31,5,'',0,0,'C');
		$this->Cell(25);
		$this->SetFont('Arial','B',12);
		$this->Cell(31,5,'NOTA DE COMPRA DE MERCADERIA',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,'',0,0);
		$this->Cell(108);
		$this->Cell(15,4,$GLOBALS['dia'],0,0,'C');
		$this->Cell(15,4,$GLOBALS['mes'],0,0,'C');
		$this->Cell(15,4,$GLOBALS['anio'],0,1,'C');
		$this->Cell(31,4,'',0,1);
		$this->Cell(31,4,'',0,1);
		$this->Cell(31,4,'',0,0);
		$this->Cell(100);
		$this->Cell(31,4,'',0,1);
		$this->Cell(31,4,'',0,1);
		//Salto de l�nea
		$this->Ln(5);
		$this->SetAlpha(0.1);
		//$this->Image('fondo9.png', 16, 60, $fpdf->w, $fpdf->h);
		$this->SetAlpha(1);
	}

	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");
}
}


$sell = SellData::getById($_GET["id"]);
$bill = $sell->bill;
$us = UserData::getById($_GET["uid"]);
$sdate = $sell->created_at;
$hora=date('h:m:s',strtotime($sdate));
$dia=date('d',strtotime($sdate));
$anio=date('Y',strtotime($sdate));	
$mes=date('m',strtotime($sdate));
$operations = OperationData::getAllProductsBySellId($_GET["id"]);
$total = 0;

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';

if($sell->nit>0 ) {



	//$client = $sell->getPerson();
	
	$pdf  = new PDF('P', 'mm', array(216, 178));
	$pdf->AliasNbPages();
	$pdf->AddPage();

	$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(25);
	//$pdf->Cell(96 ,5,'Cochabamba ',1,0);
	//$pdf->Cell(50 ,5,'CI: '.$sell->nit,1,1);
	$pdf->Cell(87 ,5,'Nombre: '.$sell->name,1,0);
	//$pdf->Cell(25);
	$pdf->Cell(67 ,5,'CI: '.$sell->nit,1,0);
	//$pdf->Cell(50 ,5,'Metodo de pago: '.$sell->pay,1,1);
	$pdf->Cell(189 ,10,'',0,1);//end of line

}else{

	$pdf  = new PDF('P', 'mm', array(216, 178));
	$pdf->AliasNbPages();
	$pdf->AddPage();

	$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(25);
	//$pdf->Cell(96 ,5,'Cochabamba, '.$dia.' de '.$letra.' de '.$anio,1,0);
	$client = $sell->getPerson();
	//$pdf->Cell(50 ,5,'CI: '.$client->ci,1,1);
	$pdf->Cell(87 ,5,'PROVEEDOR: '.$client->name.' '.$client->lastname,1,0);
	//$pdf->Cell(25);
	$pdf->Cell(67 ,5,'Empresa/Institucion: '.$client->company,1,0);
	//$pdf->Cell(50 ,5,'Metodo de pago: '.$sell->pay,1,1);
	$pdf->Cell(189 ,10,'',0,1);//end of line

}
if($sell->pay === 'CREDITO'){
	$pdf->Cell(25);
	//$pdf->Cell(96 ,5,'Cochabamba, '.$dia.' de '.$letra.' de '.$anio,1,0);
	//$pdf->Cell(50 ,5,'CI: '.$client->ci,1,1);
	$pdf->Cell(87 ,5,'Tipo: CREDITO',1,0);
	//$pdf->Cell(25);
	$di = date_create($sell->date_pay);
	$df = date_format($di,'d/m/Y');
	$pdf->Cell(67 ,5,'Fecha de Pago: '.$df,1,0);
	//$pdf->Cell(50 ,5,'Metodo de pago: '.$sell->pay,1,1);
	$pdf->Cell(189 ,10,'',0,1);//end of line
}
//$pdf->Cell(0 ,5,'AL: '.$ff,1,1);

//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,5,'',0,1);//end of line

//invoice contents

//Numbers are right-aligned so we give 'R' after new line parameter
//display the items
$i = 11;
$c = count($operations);
if($c>0){
	$pdf->SetFont('Arial','B',10);

$pdf->Cell(20 ,5,'Codigo',0,0);
$pdf->Cell(20 ,5,'Cantidad',0,0);
$pdf->Cell(110 ,5,'Detalle',0,0, 'C');
$pdf->Cell(25 ,5,'P/Unit.',0,0);
$pdf->Cell(20 ,5,'TOTAL',0,1);//end of line

$pdf->SetFont('Arial','',10);

	$ic = $i - $c;
	foreach($operations as $o){
		$product  = $o->getProduct();
		$price = $o->getPrice();
		$subtotal = $price->price_in*$o->q;
		$pdf->SetWidths(array(20,20,110,25,20));
		$pdf->SetAligns(array('C','C','L','C','C'));
		$pdf->Row(array(utf8_decode($product->code),utf8_decode($o->q),utf8_decode($product->description),utf8_decode($price->price_in),utf8_decode($subtotal)));

		//$pdf->Cell(20	,5,$product->code,0,0,'C');
		//$pdf->Cell(20	,5,$o->q,0,0,'C');
		//$pdf->MultiCell(70 ,5,$product->description,0,0);
		//$pdf->Cell(25 ,5,$item['presentation'],1,0);
		//if($o->disc > 0){
		//	$pdf->Cell(25 ,5,number_format($product->price_out - $o->disc,2,'.',','),0,0);
		//	$subtotal = $o->q*($product->price_out - $o->disc);
		//	$pdf->Cell(20 ,5,number_format($subtotal,2,'.',','),0,1,'R');//end of line
		//}else{
		//	$pdf->Cell(25 ,5,number_format($product->price_out,2,'.',','),0,0);
		//	$subtotal = $product->price_out*$o->q;
		//	$pdf->Cell(20 ,5,number_format($subtotal,2,'.',','),0,1,'R');//end of line
		//}
		
		$total += $subtotal;
	}
	while($ic > 1){
		$pdf->Cell(20 ,5,"",0,0);
		$pdf->Cell(20 ,5,"",0,0);
		$pdf->Cell(110 ,5,"",0,0);
		//$pdf->Cell(25 ,5,"",1,0);
		$pdf->Cell(25 ,5,"",0,0);
		$pdf->Cell(20 ,5,"",0,1,'R');//end of line
		$ic = $ic - 1;
	}
	//summary
	//if($sell->discount > 0) {
	//	$pdf->Cell(175 ,5,'Descuento:',0,0);
	//	$pdf->Cell(20 ,5,$sell->discount,1,1,'R');//end of line
	
	//} 
	if($sell->discountp > 0){
		$pdf->Cell(175 ,5,'SUBTOTAL BS.',0,0);
		$pdf->Cell(20 ,5,number_format($total,2,'.',','),0,1,'R');//end of line

		$pdf->Cell(175 ,5,'TOTAL BS.',0,0);
		$pdf->Cell(20 ,5,number_format($total - ($total*$sell->discountp/100),2,'.',','),0,1,'R');//end of line
	}else{
		$pdf->Cell(175 ,5,'SUBTOTAL BS.',0,0);
		$pdf->Cell(20 ,5,number_format($total,2,'.',','),0,1,'R');//end of line

		$pdf->Cell(175 ,5,'TOTAL BS.',0,0);
		$pdf->Cell(20 ,5,number_format($total - $sell->discount,2,'.',','),0,1,'R');//end of line
	}
	
	
}else{
	$pdf->SetFont('Arial','',30);
	$pdf->Cell(189 ,30,'',0,1);//end of line
	$pdf->Cell(0 ,5,'NOTA ANULADA',0,0,'C');
	$pdf->Cell(20 ,5,'',0,1,'R');//end of line
}

		//$pdf->SetY(-40);
		$pdf->SetFont('Arial','I',8);


		//$pdf->Cell(70 ,5,'NOMBRE:................................',0,0,'C');
		//$pdf->Cell(70 ,5,'NOMBRE:................................',0,0,'C');
		//$pdf->Cell(70 ,5,'',0,1,'C');
		//$pdf->Cell(70 ,5,'CI:...........',0,0,'C');
		//$pdf->Cell(70 ,5,'CI:...........',0,0,'C');
		//$pdf->Cell(70 ,5,'',0,1,'C');
		$pdf->Cell(30 ,5,'Usuario:'.$us->name.' '.$us->lastname,0,0,'C');
		$pdf->Cell(70 ,5,'Hora:'.$hora,0,0,'C');
		$pdf->Cell(70 ,5,'Una vez retirada la mercaderia No se aceptan Cambios ni Devoluciones',0,1,'C');

$pdf->Output();