<?php
header('Content-Type: text/html; charset=UTF-8');
require ('fpdf/fpdf.php');
require ('alphapdf.php');
date_default_timezone_set("America/La_Paz");
include "core/autoload.php";
include "core/app/model/StatusData.php";
include "core/app/model/MedicData.php";
include "core/app/model/PacientData.php";
include "core/app/model/ReservationData.php";
include "core/app/model/ReservationCategoryData.php";
include "core/app/model/UserData.php";
include "core/app/model/PaymentData.php";
include "core/app/model/SubCategoryData.php";
//include "core/app/model/OperationTypeData.php";


class PDF extends FPDF
{

	function Header()
	{
		$dia=date('d');
		$anio=date('Y');	
		$mes=date('m');

		//Logo
		$this->SetFillColor(0,0,0); 
		$this->Image('logo_labogen.jpg',15,10,15);
		//Arial bold 15
		$this->SetFont('Arial','B',8);
		//Movernos a la derecha
		$this->Cell(30);
		//T�tulo

		$this->SetFont('Arial','B',9);
		$this->Cell(100,5,'REGISTRO',1,0,'C');
		$this->Cell(50,5,'LAB-REG-VEN-010',1,1,'C');
		$this->Cell(30);
		$this->Cell(100,5,'FORMULARIO DE TOMA DE MUESTRAS GENETICAS',1,0,'C');
		$this->Cell(50,5,'REVISION 1',1,1,'C');
		
		//Salto de l�nea
		$this->Ln(5);

		$this->SetAlpha(0.1);
		//$this->Image('fondo2.png', 22, 80,150, 30);
		$this->SetAlpha(1);
	}
	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,'Generado por',0,0,'C');
		$this->Cell(70 ,5,'',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");
}
}
$reservation = ReservationData::getById($_GET["id"]);
$operations = ReservationCategoryData::getAllByReservationId($reservation->id);
$pacients = PacientData::getAll();
$medics = MedicData::getAll();
$statuses = StatusData::getAll();
$payments = PaymentData::getAll();
//$sell = SellData::getById($_GET["id"]);
$sdate = date('m-d-Y');
$now = date_create($reservation->date_at);
$birth = date_create($reservation->getPacient()->day_of_birth);
$edad = date_diff($birth,$now);
$dia=date('d',strtotime($sdate));
$anio=date('Y',strtotime($sdate));	
$mes=date('m',strtotime($sdate));
//$operations = OperationData::getAllProductsBySellId($_GET["id"]);
$total = 0;

if(isset($sell->person_id) ) {
	$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(146 ,5,'Nombre: '.$client->name.' '.$client->lastname,1,0);
	$pdf->Cell(50 ,5,'Nro de Nota: '.$sell->bill,1,1);
	$pdf->Cell(50 ,5,'Nro de Nota: '.$sell->bill,1,1);
	$pdf->Cell(146 ,5,'Direccion: '.$client->address1,1,0);
	$pdf->Cell(50 ,5,'Metodo de pago: '.$sell->pay,1,1);

}else{

	$pdf  = new PDF();
	$pdf->AliasNbPages();
	$pdf->AddPage();
	$pdf->SetAlpha(0.1);
	//$pdf->Image('fondo.png', 16, 60, $fpdf->w, $fpdf->h);
	$pdf->SetAlpha(1);
	$pdf->SetFont('Arial','',10);
		

	//set font to arial, bold, 14pt
	$pdf->SetFont('Arial','B',9);

	//$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(90 ,5,'LUGAR Y FECHA DE TOMA DE MUESTRA:'.$reservation->date_at,1,0);
	$pdf->Cell(90 ,5,'MEDICO O LABORATORIO: '.$reservation->getMedic()->name.' '.$reservation->getMedic()->lastname,1,1);
	$pdf->Cell(90 ,5,'NOMBRE PACIENTE '.$reservation->getPacient()->name.' '.$reservation->getPacient()->lastname,1,0);
	$pdf->Cell(90 ,5,'EDAD DEL PACIENTE: '.$edad->format('%y'),1,1);
	$pdf->Cell(90 ,5,'',1,0);
	$pdf->Cell(90 ,5,'CI: '.$reservation->getPacient()->ci,1,1);
	$pdf->Cell(90 ,5,'TIPO DE MUESTRA',1,0);
	$pdf->Cell(90 ,5,'FECHA DE RECEPCION: '.$reservation->date_at,1,1);
	$pdf->Cell(90 ,5,'FECHA DE NACIMIENTO '.$reservation->getPacient()->day_of_birth,1,0);
	$pdf->Cell(90 ,5,'FECHA DE ENTREGA: '.$sell->bill,1,1);
}

//$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
$pdf->SetFont('Arial','B',10);

$pdf->Cell(35 ,5,'CODIGO',1,0);
$pdf->Cell(140 ,5,'DESCRIPCION',1,0, 'C');
$pdf->Cell(20 ,5,'PRECIO',1,1);//end of line

$pdf->SetFont('Arial','',10);

	$ic = $i - $c;
	foreach($operations as $o){
		$product  = $o->getCategory();
			
		$pdf->Cell(35,5,$product->code,1,0);
		$pdf->Cell(140,5,substr($product->name,0,70),1,0);
		$subtotal = $product->price;
		$pdf->Cell(20 ,5,number_format($subtotal,2,'.',','),1,1,'R');//end of line
		
		$total += $subtotal;
	}
	while($ic > 1){
		$pdf->Cell(20 ,5,"",1,0);
		$pdf->Cell(105 ,5,"",1,0);
		$pdf->Cell(25 ,5,"",1,0);
		//$pdf->Cell(25 ,5,"",1,0);
		$pdf->Cell(25 ,5,"",1,0);
		$pdf->Cell(20 ,5,"",1,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
	
	//} 
	$pdf->Cell(175 ,5,'TOTAL BS.',0,0);
	$pdf->Cell(20 ,5,number_format($total,2,'.',','),1,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(70 ,5,'Recibido',0,0,'C');
		$pdf->Cell(70 ,5,'Entregado',0,0,'C');
		$pdf->Cell(70 ,5,'V.B.',0,1,'C');

$pdf->Output();