<?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/CreditData.php";
include "core/app/model/PersonData.php";
include "core/app/model/Sell2Data.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('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,'PLASTICOS BAUTISTA | A & B',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,'A & B',0,0,'C');
		$this->Cell(25);
		$this->SetFont('Arial','B',12);
		$this->Cell(31,5,'REPORTE SALDO PROVEEDOR',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,'Km. 7.5 carretera Sacaba',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,'Esmeralda Norte - Zona Puntiti',0,1);
		$this->Cell(31,4,'Telf: 4245622 - 76418600 - 76490462',0,1);
		$this->Cell(31,4,'Email: ventas@plasticosbautista-ayb.com',0,1);
		$this->Cell(31,4,'www.plasticosbautista-ayb.com',0,1);
		//Salto de l�nea
		$this->Ln(5);

		$this->SetAlpha(0.1);
		$this->Image('fondo.png', 48, 70, $fpdf->w, $fpdf->h);
		$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");
}
}

$users = PersonData::getProviders();

$operations = array();

$pdf  = new PDF('L', 'mm', array(279, 216));
$pdf->AliasNbPages();
$pdf->AddPage();
	
//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


//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
$pdf->SetFont('Arial','B',10);
$pdf->Cell(100 ,5,'Nombre',1,0, 'C');
$pdf->Cell(30 ,5,'Credito',1,0);
$pdf->Cell(30 ,5,'Debito',1,0);
$pdf->Cell(30 ,5,'Saldo',1,1, 'C');


//$pdf->Cell(20 ,5,'Total',1,1);//end of line

	$pdf->SetFont('Arial','',10);

	foreach($users as $user){
		$q = 0;
		$qa = 0;
		$ctotal = 0;
		$cu = $user->id;
		if(count($cs = Sell2Data::getByPersonID($user->id))>0){
			foreach($cs as $c)
			{
				if($c->pay == "CREDITO")
				{
					$q+= $c->total;
				}

			}
		}	
		if(count($credits = CreditData::getByPersonId($user->id))>0){
			foreach($credits as $c)
			{ 
				$ctotal+=$c->amount; $csubtotal += $ctotal;
			}
		}
	$pdf->Cell(100,5,$user->name.' | '.$user->lastname,1,0);
	$pdf->Cell(30,5,"".number_format($q,2,'.',','),1,0);
	$pdf->Cell(30,5,"".number_format($ctotal,2,'.',','),1,0);
	$pdf->Cell(30,5,"".number_format($q-$ctotal,2,'.',','),1,1,'R');
	}


$pdf->Output();