<?php
header('Content-Type: text/html; charset=UTF-8');
include ('bd.php');
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/PersonData.php";
include "core/app/model/SellData.php";
include "core/app/model/UserData.php";
include "core/app/model/ProductData.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('ico.jpg',15,2,15);
		//Arial bold 15
		$this->SetFont('Arial','B',8);
		//Movernos a la derecha
		$this->Cell(30);
		//T�tulo
		$this->Cell(21,5,'NOGALCONS S.R.L.',0,0,'C');
		$this->Cell(100);
		$this->SetFont('Arial','B',9);
		$this->Cell(21,5,'COCHABAMBA',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 CLIENTE',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,'Av. Circunvalacion entre ',0,0);
		$this->Cell(108);
		$this->Cell(15,4,$dia,0,0,'C');
		$this->Cell(15,4,$mes,0,0,'C');
		$this->Cell(15,4,$anio,0,1,'C');
		$this->Cell(31,4,'Calle Vaticano y Calle Manuela Gutierrez',0,1);
		$this->Cell(31,4,'Telf: 4245622 - 76418600 - 76490462',0,1);
		$this->Cell(31,4,'Email: ventas@nogalcons.com',0,1);
		$this->Cell(31,4,'www.nogalcons.com',0,1);
		//Salto de l�nea
		$this->Ln(5);

		$this->SetAlpha(0.1);
		$this->Image('fondo2.png', 22, 110,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");
}
}

$sell = SellData::getByPersonId($_GET["id"]);
$client = PersonData::getById($_GET["id"]);
//$user = $sell->getUser();

$operations = array();


//$pdf  = new PDF('P', 'mm', array(279, 216));
$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,'CLIENTE: '.$client->name.' '.$client->lastname,1,1);

$pdf->Cell(0 ,5,'DIRECCION: '.$client->address1,1,1);

//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line

//invoice contents


//Numbers are right-aligned so we give 'R' after new line parameter
//display the items
foreach($sell as $s){
	$pdf->SetFont('Arial','B',10);
$pdf->Cell(20 ,5,'Fecha',1,0);
$pdf->Cell(12 ,5,'Nro',1,0, 'C');
$pdf->Cell(70 ,5,'Nombre',1,0);
$pdf->Cell(20 ,5,'Pago',1,0);
$pdf->Cell(20 ,5,'Cantidad',1,0, 'C');
$pdf->Cell(20 ,5,'P/Unidad',1,0, 'C');
$pdf->Cell(20 ,5,'Precio',1,1, 'C');
//$pdf->Cell(20 ,5,'Total',1,1);//end of line

$pdf->SetFont('Arial','',10);
	$operations = OperationData::getAllProductsBySellId($s->id);
		$total = 0;
		foreach($operations as $o){
			$product  = ProductData::getById($o->product_id);//$operation->getProduct();

			$dt1 = date_create($o->created_at);
			
			$df1 = date_format($dt1,'d/m/Y');
			$pdf->Cell(20,5,$df1,1,0);
			$pdf->Cell(12,5,$s->bill,1,0);
			$pdf->Cell(70,5,$product->name,1,0);
			$pdf->Cell(20,5,$s->pay,1,0);
			$pdf->Cell(20,5,$o->q,1,0);
			if($o->disc>0){
				$pdf->Cell(20,5,$o->disc,1,0);
				$pdf->Cell(20,5,"".number_format($o->q*$o->disc,2,'.',','),1,1,'R');
				$subtotal = $o->q*$o->disc;
				$total+=$o->q*$o->disc;
			}else{
				$pdf->Cell(20,5,$product->price_out,1,0);
				$pdf->Cell(20,5,"".number_format($o->q*$product->price_out,2,'.',','),1,1,'R');
				$subtotal = $o->q*$product->price_out;
				$total+=$o->q*$product->price_out;
			}
			//$pdf->Cell(20,5,$total,1,1,'R');
			$supertotal+= $total;
		}
		if($s->pay == "CREDITO")
		{
			$d+= $total;
		}

		$pdf->Cell(162 ,5,'Total: ',1,0);
		$pdf->Cell(20,5,"".number_format($total,2,'.',','),1,1,'R');

		$pdf->Cell(189 ,10,'',0,1);

}
$pdf->Cell(189 ,10,'',0,1);//end of line
$pay = AccountData::getByPersonId($_GET["id"]);
if(count($pay>0)){
	$pdf->SetFont('Arial','B',10);
	$pdf->Cell(30 ,5,'Codigo',1,0);
	$pdf->Cell(80 ,5,'Recibo',1,0);
	$pdf->Cell(20 ,5,'Pago',1,0);
	$pdf->Cell(40 ,5,'Fecha',1,1);//end of line
	$pdf->SetFont('Arial','',10);

	foreach($pay as $p){
		$a+= $p->amount;

		$pdf->Cell(30,5,$p->id,1,0);
		$pdf->Cell(80,5,$p->code,1,0);
		$pdf->Cell(20,5,"".number_format($p->amount,2,'.',','),1,0);
		$dt = date_create($p->created_at);
		$df = date_format($dt,'d/m/Y');
		$pdf->Cell(40,5,$df,1,1,'R');

	}
}

//summary
$pdf->SetFont('Arial','',20);
$pdf->Cell(25 ,5,'',0,1);
$pdf->Cell(25 ,10,'TOTAL CREDITO BS.'.number_format($d,2,'.',','),0,1);
$pdf->Cell(25 ,10,'TOTAL PAGOS BS.'.number_format($a,2,'.',','),0,1);
$pdf->Cell(25 ,10,'SALDO BS.'.number_format($d-$a,2,'.',','),0,1);
$pdf->SetFont('Arial','',10);


$pdf->Output();