<?php

//require("&#46;&#46;/&#46;&#46;/fpdf/fpdf.php");
define('FPDF_FONTPATH', 'font/');
require('fpdf.php');

class PDF extends FPDF 
{

	function setDate($dat)
	{
        $this->header_date = $dat;
    }
	
    function getDate()
	{
        return $this->header_date;
    }	

	// Definirea Header-ului
	function Header()
	{		
		$denunit=$_POST["denunit"];
		$wdenluna_sel=substr($_POST["luna_sel"],3,strlen($_POST["luna_sel"])-3);
		//$wdengrp_sel=substr($_POST["grp_sel"],3,strlen($_POST["grp_sel"])-3);
		$wan2=$_POST["an"]+1;	
		
		$this->SetY(5);

		$this->SetFont('Times','B',12);
		$this->Cell(0,6,$denunit,0,0,'L');	// Adauga un text
		$this->Ln(7);		
		$this->Cell(0,6,'BORDEROU INCASARI',0,0,'C');
		//$this->Ln(5);
		//$this->SetX(100);
		//$this->Cell(20,6,'GRUPA: '.$wdengrp_sel.'',0,0,'C');
		$this->Ln(5);
		$this->SetX(70);
		$this->Cell(20,6,'LUNA: '.$wdenluna_sel.'',0,0,'C');
		$this->SetX(130);
		$this->Cell(20,6,'ANUL SCOLAR: '.$_POST["an"].' - '.$wan2.'',0,0,'C');
		$this->Ln(10);
		
		$this->SetFont('Times','B',10);	// Seteaza Fontul Bold 12
		$this->Cell(0,10,'Pagina '.$this->PageNo().'/nr_pgn',0,0,'R');		
		$this->Ln(8);
		
		$this->Cell(10,6,'Nr.crt.',1,0,'C');
		$this->Cell(8,6,'ID',1,0,'C');	// Adauga un text
		$this->Cell(65,6,'Nume Prenume prescolar',1,0,'C');
		$this->Cell(13,6,'Tip doc',1,0,'C');
		$this->Cell(18,6,'Data',1,0,'C');
		$this->Cell(20,6,'Nr. chitanta',1,0,'C');
		$this->Cell(12,6,'Suma',1,0,'C');
		$this->Cell(11,6,'Anulat',1,0,'C');
		$this->Cell(40,6,'Grupa',1,0,'C');

		$this->Ln(7);		// Adauga rand nou		
	}
		
	// Definirea Footer-ului
	function Footer() 
	{
		$this->SetY(-15);		// Seteaza pozitia la 15mm pe verticala fata de subsolul paginii		
		$this->SetFont('Times','I',8);
		
		// Seteaza data si ora
		date_default_timezone_set('Europe/Bucharest');
		$date = date('d-m-Y H:i:s', time());
		$this->Cell(15,6,'Data/Ora: '.$date);
		
		$this->SetFont('Arial','I',10);	// Seteaza Fontul Italic 8	
	}

    function BuildTable($data)
	{
		//Colors, line width and bold font

        $this->SetFillColor(255, 255, 255);
        $this->SetTextColor(0);
        $this->SetDrawColor(0);
        $this->SetLineWidth(.3);
        $this->SetFont('','B');

        //Header

        $w=array(10,8,65,13,18,20,12,11,40);
		
        $this->SetTextColor(0);
        $this->SetFont('');
		
        //now spool out the data from the $data array
 
// now you can output the converted city name to pdf

        $fill=true; // used to alternate row color backgrounds
        foreach($data as $row)
        {		
			$this->Cell($w[0],4,$row[0],0,'','C',$fill);
			$this->Cell($w[1],4,$row[1],0,'','C',$fill);
			$this->Cell($w[2],4,$row[2],0,'','L',$fill);
			$this->Cell($w[3],4,$row[3],0,'','C',$fill);
			$this->Cell($w[4],4,$row[4],0,'','C',$fill);
			$this->Cell($w[5],4,$row[5],0,'','C',$fill);
			$this->Cell($w[6],4,$row[6],0,'','C',$fill);
			$this->Cell($w[7],4,$row[7],0,'','C',$fill);
			$this->Cell($w[8],4,$row[8],0,'','L',$fill);
			$this->Ln();
			
			// flips from true to false and vise versa

			$fill =! $fill;
        }

        $this->Cell(array_sum($w),0,'','T');
    }

}

//connect to database
include 'javascripts.php';
include 'conectare.php';

	$connection=mysql_connect("$host","$username","$password") or die("Nu aveti acces la server");
	$baza="apponlin_".$_POST["aplic"]."_".$_POST["unit"];
	$db=mysql_select_db($baza,$connection) or die("Nu puteti selecta Baza de date ".$baza);
	
//	mysql_query ("set character_set_results='latin2'");

//	$wunit_sel = substr($_POST["unitate"],0,3)."_".$_POST["unit_sel"];

	$query = "SELECT id, codcopil, tip_doc, serie_doc, nr_doc, ln, data_doc, val_doc, anulat FROM chitante WHERE ln = \"".substr($_POST["luna_sel"],0,2)."\" order by data_doc,id";
	$result = mysql_query($query) or die("Interogare esuata: ".mysql_error());
	//$total = mysql_num_rows($result);
		
	$i=0;
	$suma_tot=0;
		
	While($row = mysql_fetch_array($result)) 
	{
		$wcodcopil = $row["codcopil"];

		$query1 = "SELECT codgrp, codcopil, nume, prenume FROM copii WHERE codcopil=\"".$wcodcopil."\" LIMIT 1";
		$result1 = mysql_query($query1) or die("Interogare esuata: ".mysql_error());	
		$row1 = mysql_fetch_array($result1);	
	
		$nume_pren_copil = $row1["nume"]." ".$row1["prenume"];
		
		$query1_1="SELECT codgrp,dengrp from grupe WHERE codgrp=\"".$row1["codgrp"]."\"";
		$result1_1=mysql_query($query1_1) or die("Interogare esuata: ".mysql_error());
		$row1_1=mysql_fetch_array($result1_1);
		
		IF (strpos($row1_1['dengrp'],'-') == TRUE)
		{
			$den_grupa = substr($row1_1["dengrp"], 0, strpos($row1_1["dengrp"], '-'));			
		}
		ELSE
		{
			$den_grupa = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $row1_1['dengrp']))); // scoate spatiile albe multiple dintre cuvinte si lasa doar 1
		}
	
		if ($row["anulat"] <> 'X')
		{
			$suma_tot += $row["val_doc"];
		}	
		
		if ($row["tip_doc"] == "CHA")
		{
			$nr_chitanta = $row["serie_doc"]."_".$row["nr_doc"];
		}
		else
		{				
			$nr_chitanta = $row["nr_doc"];
		}
		
		if ($row["anulat"] == 'X')
		{
			$suma = "0(".$row["val_doc"].")";
		}
		else
		{				
			$suma = $row["val_doc"];
		}	
		
			$data_doc = date("d-m-Y", strtotime($row["data_doc"]));		
			$i=$i+1;							
			$data[] = array($i, $row['codcopil'], $nume_pren_copil, $row["tip_doc"], $data_doc, $nr_chitanta, $suma, $row["anulat"], $den_grupa);	
	}

// start and build the PDF document
$pdf = new PDF();

$pdf->AliasNbPages('nr_pgn');		// Seteaza un alias care va prelua nr. total de pagini
$pdf->AddPage();				// Initializeaza adaugarea paginilor

// Seteaza un font si adauga mai multe linii de text pt. a depasi o pagina
$pdf->SetFont('Times','',10);

// call the table creation method
$pdf->BuildTable($data);

$data = date('d-m-Y');
$wdenluna_sel=substr($_POST["luna_sel"],3,strlen($_POST["luna_sel"])-3);

$pdf->SetFont('Times','B',10);
$pdf->Ln(1);
//$pdf->Cell(30,5,'Total prescolari: '.$total,0,'','C');
$pdf->Ln(3);
$pdf->SetX(130);
$pdf->Cell(10,5,'TOTAL:',0,'','C');
$pdf->SetX(135);
$pdf->Cell(30,5,$suma_tot,0,'','C');


ob_end_clean();

$pdf->Output('Borderou incasari_'.$wdenluna_sel.'_'.$data.'.pdf','I');

?>