<?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/MedicData.php";
include "core/app/model/UserData.php";


class PDF extends FPDF
{

	function Header()
	{
		$dia=date('d');
		$anio=date('Y');	
		$mes=date('m');
		//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',9);
		$this->Cell(21,5,'',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,'RANKING',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,$dia,0,0,'C');
		$this->Cell(15,4,$mes,0,0,'C');
		$this->Cell(15,4,$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 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,'',0,0,'C');
		$this->Cell(70 ,5,'',0,0,'C');
		$this->Cell(70 ,5,'',0,1,'C');
		$this->Cell(70 ,5,'Generado por '.$GLOBALS['user_id'],0,0,'C');
		$this->Cell(70 ,5,'',0,0,'C');
		$this->Cell(70 ,5,'Hora '.date('g:ia'),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");
}
}

//$user_id = UserData::getById($_POST["user_id"])->name;

$operations = array();

	$operations = MedicData::getAll();


$pdf  = new PDF();
$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(97 ,5,'DEL: '.$fi,1,0);
//$pdf->Cell(97 ,5,'AL: '.$ff,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(14 ,5,'Codigo',1,0);
$pdf->Cell(105 ,5,'Nombre',1,0, 'C');
$pdf->Cell(28 ,5,'Especialidad',1,0);
$pdf->Cell(18 ,5,'Cantidad',1,0);
//$pdf->Cell(20 ,5,'Operacion',1,0);
//$pdf->Cell(20 ,5,'Usuario',1,0);
$pdf->Cell(20 ,5,'Total',1,1);//end of line

$pdf->SetFont('Arial','',8);

//Numbers are right-aligned so we give 'R' after new line parameter
//display the items

foreach($operations as $o){
	//$u = $o->getSell()->user_id;
	//$user = UserData::getById($u);
	if($o->person_id==""){
		$pdf->Cell(14,5,$o->id,1,0);
		$pdf->Cell(105,5,$o->name.' '.$o->lastname,1,0);
		$pdf->Cell(28,5,$o->nit,1,0);
		$pdf->Cell(18,5,$o->getSumById($o->id)->cant,1,0);
		//$pdf->Cell(20,5,$o->getOperationType()->name,1,0);
		//$pdf->Cell(20,5,$user->name,1,0);
		$pdf->Cell(20,5,number_format($o->getSumById($o->id)->total,2,'.',','),1,1,'R');
	}else{
		$pdf->Cell(14,5,$o->id,1,0);
		$pdf->Cell(105,5,$o->getPerson()->name." ".$o->getPerson()->lastname,1,0);
		$pdf->Cell(28,5,$o->getPerson()->ci,1,0);
		$pdf->Cell(18,5,number_format($o->total,2,'.',','),1,0);
		//$pdf->Cell(20,5,$o->getOperationType()->name,1,0);
		//$pdf->Cell(20,5,$user->name,1,0);
		$pdf->Cell(20,5,$o->created_at	,1,1,'R');
	}
		
}

//summary
//$pdf->Cell(25 ,5,'TOTAL BS.',0,0);



$pdf->Output();