<?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/BoardData.php";
include "core/app/model/NoteData.php";
include "core/app/model/ProductData.php";
include "core/app/model/UserData.php";
include "core/app/model/SpendBoxData.php";
include "core/app/model/PersonData.php";
include "core/app/model/OperationTypeData.php";
include "core/app/model/VehicleData.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, 'CANIAX S.RL.', 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, 'SORATA', 0, 0, 'C');
		$this->Cell(25);
		$this->SetFont('Arial', 'B', 12);
		$this->Cell(31, 5, 'NOTA DE ABASTECIMIENTO', 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, 'Ingreso a Sorata', 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(20, 5, 'RECIBI', 0, 0, 'C');
		$this->Cell(10, 5, '', 0, 0, 'C');
		$this->Cell(10, 5, 'ENTREGUE.', 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");
	}
}

$sell = NoteData::getById($_GET["id"]);
$sdate = $sell->created_at;
$dia = date('d', strtotime($sdate));
$anio = date('Y', strtotime($sdate));
$mes = date('m', strtotime($sdate));
$total = 0;


$pdf = new FPDF($orientation = 'P', $unit = 'mm', array(80, 120));
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 8);    //Letra Arial, negrita (Bold), tam. 20
$textypos = 5;
$pdf->setY(2);
$pdf->setX(2);
$pdf->Cell(5, $textypos, "CANIAX SRL");
$pdf->SetFont('Arial', '', 5);    //Letra Arial, negrita (Bold), tam. 20
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, '-----------------------------------');
$textypos += 6;
$pdf->setX(2);
$di = date_create($sell->created_at);
$df = date_format($di, 'd/m/Y');
$pdf->Cell(5, $textypos, 'FECHA: ' . $df);
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, 'USUARIO: ' . $sell->getUser()->name . ' ' . $sell->getUser()->lastname);
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, "CHOFER: " . $sell->getDriver()->name . ' ' . $sell->getDriver()->lastname);
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, "CISTERNA: " . $sell->getVehicle()->plate);
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, "PRODUCTO: " . $sell->getProduct()->name);
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, "CANTIDAD: " . $sell->q_f);
$textypos += 6;
$pdf->setX(2);
if ($sell->product_id == 1) {
	$pdf->Cell(5, $textypos, "MEDIDA INICIAL: " . $sell->m_ig1 . ' / ' . BoardData::getByMmProd($sell->m_ig1, 1)->lt);
	$textypos += 6;
	$pdf->setX(2);
	$pdf->Cell(5, $textypos, "MEDIDA FINAL: " . $sell->m_fg1 . ' / ' . BoardData::getByMmProd($sell->m_fg1, 1)->lt);
	$textypos += 6;
	$pdf->setX(2);
	$pdf->Cell(5, $textypos, "MERMA: " . (BoardData::getByMmProd($sell->m_fg1, 1)->lt - BoardData::getByMmProd($sell->m_ig1, 1)->lt - $sell->q_f));
} else {
	$pdf->Cell(5, $textypos, "MEDIDA INICIAL: " . $sell->m_id . ' / ' . BoardData::getByMmProd($sell->m_id, 2)->lt);
	$textypos += 6;
	$pdf->setX(2);
	$pdf->Cell(5, $textypos, "MEDIDA FINAL: " . $sell->m_fd . ' / ' . BoardData::getByMmProd($sell->m_fd, 2)->lt);
	$textypos += 6;
	$pdf->setX(2);
	$pdf->Cell(5, $textypos, "MERMA: " . (BoardData::getByMmProd($sell->m_fd, 1)->lt - BoardData::getByMmProd($sell->m_id, 1)->lt - $sell->q_f));
}


$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, '-----------------------------------');


$pdf->setX(2);
$pdf->Cell(5, $textypos + 26, 'FIRMA A CONFORMIDAD');

$pdf->output();
