<?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/FormData.php";
include "core/app/model/ProductData.php";
include "core/app/model/UserData.php";
include "core/app/model/MiterData.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, '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, 'VALE DE COMBUSTIBLE', 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(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");
	}
}

$sell = FormData::getById($_GET["id"]);
$sdate = $sell->created_at;
$dia = date('d', strtotime($sdate));
$anio = date('Y', strtotime($sdate));
$mes = date('m', strtotime($sdate));


$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, "PRODUCTO: " . $sell->getMiter()->getProduct()->name);
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, "CLIENTE: " . $sell->name);
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, "PLACA: " . $sell->plate);
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, '-----------------------------------');
$textypos += 6;
$pdf->setX(2);
$pdf->Cell(5, $textypos, 'CANT.       PRECIO               TOTAL');

$total = 0;
$off = $textypos + 6;

$pdf->setX(2);
$pdf->Cell(5, $off, $sell->q);
$pdf->setX(10);
if ($sell->dif == 1) {
	$pdf->Cell(11, $off,  "Bs. " . number_format($sell->getMiter()->getProduct()->price_out, 2, ".", ","), 0, 0, "R");
	$pdf->setX(32);
	$pdf->Cell(11, $off,  "Bs. " . number_format($sell->q * $sell->getMiter()->getProduct()->price_out, 2, ".", ","), 0, 0, "R");
	$off += 6;
	$textypos = $off + 6;
	$pdf->setX(2);
	$pdf->Cell(5, $textypos, "TOTAL: ");
	$pdf->setX(38);
	$pdf->Cell(5, $textypos, "Bs. " . number_format($sell->q * $sell->getMiter()->getProduct()->price_out, 2, ".", ","), 0, 0, "R");
} else {
	$pdf->Cell(11, $off,  "Bs. " . number_format($sell->dif_v, 2, ".", ","), 0, 0, "R");
	$pdf->setX(32);
	$pdf->Cell(11, $off,  "Bs. " . number_format($sell->q * $sell->dif_v, 2, ".", ","), 0, 0, "R");
	$off += 6;
	$textypos = $off + 6;
	$pdf->setX(2);
	$pdf->Cell(5, $textypos, "TOTAL: ");
	$pdf->setX(38);
	$pdf->Cell(5, $textypos, "Bs. " . number_format($sell->q * $sell->dif_v, 2, ".", ","), 0, 0, "R");
}

$pdf->setX(2);
$pdf->Cell(5, $textypos + 26, 'FIRMA A CONFORMIDAD');

$pdf->output();
