<?php
header('Content-Type: text/html; charset=UTF-8');
include ('bd.php');
require ('fpdf/fpdf.php');
date_default_timezone_set("America/La_Paz");
include "core/autoload.php";
include "core/app/model/SellData.php";
include "core/app/model/ChanceData.php";
include "core/app/model/ChanceTrackData.php";
include "core/app/model/ProductData.php";
include "core/app/model/PersonData.php";
include "core/app/model/ProposalData.php";
include "core/app/model/UserData.php";
include "core/app/model/SpendBoxData.php";
include "core/app/model/OperationData.php";
include "core/app/model/OperationTypeData.php";


class PDF extends FPDF
{

	function Header()
	{

		//Logo
		$this->SetFillColor(0,0,0); 
		//$this->Image('logomod.jpg',0,2,60);
		$this->Cell(189 ,10,'',0,1);//end of line
		//Arial bold 15
		$this->SetFont('Arial','B',8);
		//Movernos a la derecha
		$this->Cell(30);
		//T�tulo
		$this->Cell(21,5,'',0,0,'C');
		$this->Cell(100);
		$this->SetFont('Arial','B',15);
		$this->Cell(21,5,'Nro.'.$GLOBALS['bill'],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,'COTIZACION',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,'',0,1);
		$this->Cell(31,4,'',0,0);
		$this->Cell(100);
		$this->Cell(31,4,'',0,1);
		$this->Cell(31,4,'',0,1);
		//Salto de l�nea
		$this->Ln(5);
		$this->SetAlpha(0.1);
		//$this->Image('fondo9.png', 16, 60, $fpdf->w, $fpdf->h);
		$this->SetAlpha(1);
	}

	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 = ProposalData::getById($_GET["id"]);

$us = UserData::getById($sell->user_id);
$sdate = $sell->created_at;
$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(25);
	if($sell->chancetrack_id == 0){
		$pdf->Cell(87 ,5,'Nombre: ',0,0);
	}else{
		$client = $sell->getChance();
		$pdf->Cell(87 ,5,'Nombre: '.$client->getChance()->getPerson()->name,0,0);
	}


	$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);

$pdf->Cell(20 ,5,'Cantidad',0,0);
$pdf->Cell(110 ,5,'Detalle',0,0, 'C');
$pdf->Cell(25 ,5,'P/Unit.',0,0);
$pdf->Cell(20 ,5,'TOTAL',0,1);//end of line

$pdf->SetFont('Arial','',10);

foreach($operations as $o){
	$total = $o->q*$o->disc;
	$mo=$total*($sell->porc_wf/100);
	$u = ($total+$mo)*$sell->porc_u;
}
	$ic = $i - $c;
	foreach($operations as $o){

		$a=$o->q*$o->disc*($sell->porc_wf/100);
		$b=$o->q*$o->disc;
		$c=$a+$b;
		$d=$c*$sell->porc_u;

		$product  = $o->getProduct();
		$pdf->Cell(20	,5,$o->q,0,0,'C');
		$pdf->SetFont('Arial','',8);
		$pdf->Cell(110 ,5,strtoupper($product->name),0,0);
		$pdf->SetFont('Arial','',10);
		$pdf->Cell(25 ,5,number_format($d/$o->q,2,'.',','),0,0);
		$subtotal += $d;
		$pdf->Cell(20 ,5,number_format($d,2,'.',','),0,1,'R');//end of line

	}

	while($ic > 1){
		$pdf->Cell(20 ,5,"",0,0);
		$pdf->Cell(20 ,5,"",0,0);
		$pdf->Cell(110 ,5,"",0,0);
		//$pdf->Cell(25 ,5,"",1,0);
		$pdf->Cell(25 ,5,"",0,0);
		$pdf->Cell(20 ,5,"",0,1,'R');//end of line
		$ic = $ic - 1;
	}
		$pdf->Cell(155 ,5,'SUBTOTAL BS.',0,0);
		$pdf->Cell(20 ,5,number_format($subtotal,2,'.',','),0,1,'R');//end of line
		$pdf->Cell(155 ,5,'TOTAL BS.',0,0);
		$pdf->Cell(20 ,5,number_format($u,2,'.',','),0,1,'R');//end of line

		//$pdf->SetY(-40);
		$pdf->SetFont('Arial','I',8);

		$pdf->Cell(40 ,5,'Usuario:'.$us->name.' '.$us->lastname,0,0,'C');
		$pdf->Cell(70 ,5,'Hora:'.$hora,0,0,'C');
		$pdf->Cell(70 ,5,'',0,1,'C');

$pdf->Output();