<?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/AccountData.php";
include "core/app/model/ProductData.php";
include "core/app/model/UserData.php";
include "core/app/model/NoteLocalData.php";
include "core/app/model/OperationData.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.jpg', 0, 2, 45);
		//Arial bold 15
		$this->SetFont('Arial', 'B', 8);
		//Movernos a la derecha
		$this->Cell(30);
		//T�tulo
		$this->Cell(21, 5, 'EMPRESAS FLORES IRIARTE', 0, 0, 'C');
		$this->Cell(100);
		$this->SetFont('Arial', 'B', 9);
		$this->Cell(21, 5, 'LA PAZ', 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, 'REPORTE INVENTARIO GENERAL', 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, 'Telf: 2', 0, 1);
		$this->Cell(31, 4, 'Email: ', 0, 1);
		$this->Cell(31, 4, 'www.fliartecompany.com', 0, 1);
		//Salto de l�nea
		$this->Ln(5);
	}
	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();
if ($_GET["station_id"] == "" && $_GET["sd"] == "" && $_GET["ed"] == "") {
	$operations = NoteLocalData::getAll();
} elseif ($_GET["station_id"]  == "") {
	$operations = NoteLocalData::getAllByDate($_GET["sd"], $_GET["ed"]);
} elseif ($_GET["sd"] == "" && $_GET["ed"] == "") {
	switch ($_GET["station_id"]) {
		case 1:
			$operations = NoteLocalData::getAllCaniax();
			break;
		case 2:
			$operations = NoteLocalData::getAllSaye();
			break;
		case 3:
			$operations = NoteLocalData::getAllDes();
			break;
		case 4:
			$operations = NoteLocalData::getAllIsuc();
			break;
		case 5:
			$operations = NoteLocalData::getAllMoto();
			break;
		case 6:
			$operations = NoteLocalData::getAllBibosis();
			break;
	}
} else {
	switch ($_GET["station_id"]) {
		case 1:
			$operations = NoteLocalData::getAllByDateCaniax($_GET["sd"], $_GET["ed"]);
			break;
		case 2:
			$operations = NoteLocalData::getAllByDateSaye($_GET["sd"], $_GET["ed"]);
			break;
		case 3:
			$operations = NoteLocalData::getAllByDateDes($_GET["sd"], $_GET["ed"]);
			break;
		case 4:
			$operations = NoteLocalData::getAllByDateIsuc($_GET["sd"], $_GET["ed"]);
			break;
		case 5:
			$operations = NoteLocalData::getAllByDateMoto($_GET["sd"], $_GET["ed"]);
			break;
		case 6:
			$operations = NoteLocalData::getAllByDateBibosis($_GET["sd"], $_GET["ed"]);
			break;
	}
}


$sdate = date('Y-m-d');
$dia = date('d', strtotime($sdate));
$anio = date('Y', strtotime($sdate));
$mes = date('m', strtotime($sdate));

$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(0, 5, 'DEL: ' . $fi, 1, 1);

// $pdf->Cell(0, 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(24, 5, 'Codigo', 1, 0);
$pdf->Cell(50, 5, 'Producto', 1, 0, 'C');
$pdf->Cell(25, 5, 'Cantidad', 1, 0);
$pdf->Cell(26, 5, 'Operacion', 1, 0);
$pdf->Cell(30, 5, 'Fecha Inicio', 1, 0);
$pdf->Cell(30, 5, 'Fecha Fin', 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 = SellData::getById($o->sell_id);
	//$user = $u->getUser()->name;
	if ((strpos($o->place, 'SORATA')) !== false) {
		$place = 'CANIAX';
	} elseif ((strpos($o->place, 'PATACAMAYA')) !== false) {
		$place = 'ISSUCADELA';
	} else {
		$place = 'DESAGUADERO';
	}
	$pdf->Cell(24, 5, $o->code, 1, 0);
	$pdf->Cell(50, 5, $o->product, 1, 0);
	$pdf->Cell(25, 5, $o->q, 1, 0);
	$pdf->Cell(26, 5, $place, 1, 0);
	$pdf->Cell(30, 5, $o->date_in, 1, 0, 'C');
	$pdf->Cell(30, 5, $o->date_out, 1, 1, 'C');
}

//summary
// $pdf->Cell(25, 5, 'TOTAL BS.', 0, 0);



$pdf->Output();
