<?php
header('Content-Type: text/html;');
require ('fpdf/fpdf.php');
date_default_timezone_set("America/La_Paz");
include "core/autoload.php";
include "core/app/model/EventData.php";
include "core/app/model/UserData.php";
include "core/app/model/CategoryData.php";
include "core/app/model/ProjectData.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_implelab.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, 'IMP-REG-DES-001', 1, 1, 'C');
		$this->Cell(30);
		$this->Cell(100, 5, 'PLANILLA DE SEGUIMIENTO A DESPACHOS', 1, 0, 'C');
		$this->Cell(50, 5, 'REVISION 2', 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");
}
}


$operations = array();
$pid = UserData::getById($_GET["uid"]);
$operations = EventData::getAllByDateP($pid->id,$_GET["sd"],$_GET["ed"]);

$sdate = date("");
$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($sell->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';

	$pdf  = new PDF('P', 'mm', 'letter');
	$pdf->AliasNbPages();
	$pdf->AddPage();

	$pdf->SetFont('Arial','',10);
		

	//set font to arial, bold, 14pt
	$pdf->SetFont('Arial','B',10);


	$pdf->Cell(189 ,10,'',0,1);//end of line

$pdf->Cell(189 ,5,'',0,1);//end of line

//Numbers are right-aligned so we give 'R' after new line parameter
//display the items
//$i = 11;
//$c = count($operations);

$pdf->SetFont('Arial','B',8);

$i = 1;
$si = 0;
$no = 0;
$pdf->SetWidths(array(7,48,25,30,20,20,20,23));
$pdf->Row(array('Nro','Descripcion de producto( No. Nota de remision o transferencia/Cantidad)','Sucursal de Destino','Fecha de Despacho','Medio de Transporte','Nro de Guia','Conformidad','Observaciones'));
$pdf->SetFont('Arial', '', 8);
$pdf->SetWidths(array(7,48,25,30,20,20,10,10,23));
foreach ($operations as $o) {
	$txt = iconv('utf-8', 'cp1252', $o->description);

	if ($o->is_active == 9) {
		$pdf->Row(array($i,$txt,$o->getProject()->name,$o->created_at,$o->getCategory()->name,$o->title,'SI','',''));
		$si++;
	} else {
		$pdf->Row(array($i,$txt,$o->getProject()->name,$o->created_at,$o->getCategory()->name,$o->title,'','NO',''));
		$no++;
	}
	$i++;
}
$pdf->Cell(150);
$pdf->Cell(10 ,5,$si,1,0);
$pdf->Cell(10 ,5,$no,1,1);
$pdf->Cell(50);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(100 ,5,'Indicador de "Despachos Conformes"',1,0);
$pdf->Cell(20 ,5,number_format(($si/count($operations)*100),2,'.',','),1,0,'C');

//$pdf->Output('seguimiento_despacho.pdf','D');
$pdf->Output();