<?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/SatisfactionData.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);
		$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, 'IMP-REG-DES-001', 1, 1, 'C');
		$this->Cell(50, 5, 'LABOGEN', 1, 1, 'C');
		$this->Cell(30);
		$this->Cell(100, 5, 'PLANILLA DE SATISFACCION DE CLIENTE', 1, 0, 'C');
		//$this->Cell(50, 5, 'REVISION 2', 1, 1, 'C');
		$this->Cell(50, 5, 'SANTA CRUZ', 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 = array();
if ($_GET["p_id"] == "0" && $_GET["id"] == "0") {
	$operations = SatisfactionData::getAllByDateOp($_GET["sd"], $_GET["ed"]);
} elseif ($_GET["p_id"] == "1" && $_GET["id"] == 0) {
	$operations = SatisfactionData::getAllByDateOpAC($_GET["sd"], $_GET["ed"]);
} elseif ($_GET["p_id"] == "2" && $_GET["id"] == 0) {
	$operations = SatisfactionData::getAllByDateOpTm($_GET["sd"], $_GET["ed"]);
} elseif ($_GET["p_id"] == "1" && $_GET["id"] > 0) {
	$operations = SatisfactionData::getAllByDateOpAcR($_GET["sd"], $_GET["ed"], $_GET["id"]);
} elseif ($_GET["p_id"] == "2" && $_GET["id"] > 0) {
	$operations = SatisfactionData::getAllByDateOpTmR($_GET["sd"], $_GET["ed"], $_GET["id"]);
} elseif ($_GET["p_id"] == "0" && $_GET["id"] > 0) {
	$operations = SatisfactionData::getAllByDateOpR($_GET["sd"], $_GET["ed"], $_GET["id"]);
} elseif ($_GET["p_id"] == "1" && $_GET["id"] ="") {
	$operations = SatisfactionData::getAllByDateOpAcRN($_GET["sd"], $_GET["ed"]);
} elseif ($_GET["p_id"] == "2" && $_GET["id"] ="") {
	$operations = SatisfactionData::getAllByDateOpTmRN($_GET["sd"], $_GET["ed"]);
} elseif ($_GET["p_id"] == "0" && $_GET["id"] ="") {
	$operations = SatisfactionData::getAllByDateOpRN($_GET["sd"], $_GET["ed"]);
} else {
	$operations = SatisfactionData::getAllByDateOPRS($_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', 10);

$i = 0;
$a1 = 0;
$a2 = 0;
$a3 = 0;
$a4 = 0;
$t1 = 0;
$t2 = 0;
$t3 = 0;
$t4 = 0;
$pdf->Cell(15);
$pdf->Cell(15, 5, 'Nro', 1, 0);
$pdf->Cell(40, 5, 'Fecha', 1, 0, 'C');
$pdf->Cell(50, 5, 'Concepto', 1, 0,'C');
$pdf->Cell(50, 5, 'Calificacion', 1, 1,'C'); //end of line
foreach ($operations as $o) {
	$pdf->SetFont('Arial', '', 10);
	$i++;
	$pdf->Cell(15);
	$pdf->Cell(15, 5, $i, 1, 0);
	$pdf->Cell(40, 5, $o->created_at, 1, 0);
	if ($o->ac > 0) {
		$pdf->Cell(50, 5, 'ATENCION AL CLIENTE', 1, 0,'C');
		switch ($o->ac) {
			case 1:
				$pdf->Cell(50, 5, 'MUY BUENO', 1, 1,'C'); $a1++;
				break;
			case 2:
				$pdf->Cell(50, 5, 'BUENO', 1, 1,'C'); $a2++;
				break;
			case 3:
				$pdf->Cell(50, 5, 'REGULAR', 1, 1,'C'); $a3++;
				break;
			case 4:
				$pdf->Cell(50, 5, 'MALO', 1, 1,'C'); $a4++;
				break;
		}
	} else {
		$pdf->Cell(50, 5, 'TOMA DE MUESTRAS', 1, 0,'C');
		switch ($o->tm) {
			case 1:
				$pdf->Cell(50, 5, 'MUY BUENO', 1, 1,'C'); $t1++;
				break;
			case 2:
				$pdf->Cell(50, 5, 'BUENO', 1, 1,'C'); $t2++;
				break;
			case 3:
				$pdf->Cell(50, 5, 'REGULAR', 1, 1,'C'); $t3++;
				break;
			case 4:
				$pdf->Cell(50, 5, 'MALO', 1, 1,'C'); $t4++;
				break;
		}
	}
	//$supertotal += ($o->total - $o->discount);
}

$pdf->Cell(189 ,5,'',0,1);//end of line

$pdf->Cell(50);
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(30, 5, 'MUY BUENO', 1, 0,'C');
$pdf->Cell(30, 5, 'BUENO', 1, 0,'C');
$pdf->Cell(30, 5, 'REGULAR', 1, 0,'C');
$pdf->Cell(30, 5, 'MALO', 1, 1,'C');
$pdf->Cell(15);
$pdf->Cell(35, 5, 'ATENCION AL CLIENTE', 1, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(30, 5, $a1, 1, 0,'C');
$pdf->Cell(30, 5, $a2, 1, 0,'C');
$pdf->Cell(30, 5, $a3, 1, 0,'C');
$pdf->Cell(30, 5, $a4, 1, 1,'C');
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(15);
$pdf->Cell(35, 5, 'TOMA DE MUESTRAS', 1, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(30, 5, $t1, 1, 0,'C');
$pdf->Cell(30, 5, $t2, 1, 0,'C');
$pdf->Cell(30, 5, $t3, 1, 0,'C');
$pdf->Cell(30, 5, $t4, 1, 1,'C');
$pdf->Cell(70);
//$pdf->SetFont('Arial', 'B', 10);
//$pdf->Cell(80, 5, 'Indicador de "Despachos Conformes"', 1, 0);
//$pdf->Cell(20, 5, number_format(($a1 / count($operations) * 100), 2, '.', ','), 1, 0, 'C');

//$pdf->Output('seguimiento_despacho.pdf','D');
$pdf->Output();
