<?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 DE DEBITE SI PLATI',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(15,12,'ID',1,0,'C');	// Adauga un text
		$this->Cell(70,12,'Nume Prenume prescolar',1,0,'C');
		$this->Cell(26,6,'Precedent',1,0,'C');
		$this->Cell(40,6,'Luna '.$wdenluna_sel.'',1,0,'C');
		$this->Cell(45,6,'Cumulat',1,0,'C');
		$this->Ln();
		$this->SetX(95);
		$this->Cell(13,6,'Debite',1,0,'C');
		$this->Cell(13,6,'Plati',1,0,'C');
		$this->Cell(20,6,'Debite',1,0,'C');
		$this->Cell(20,6,'Plati',1,0,'C');
		$this->Cell(15,6,'Debite',1,0,'C');
		$this->Cell(15,6,'Plati',1,0,'C');
		$this->Cell(15,6,'Rest_PL',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(15,70,13,13,20,20,15,15,15);
		
        $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],6,$row[0],0,'','C',$fill);
			$this->Cell($w[1],6,$row[1],0,'','L',$fill);
			$this->Cell($w[2],6,$row[2],0,'','C',$fill);
			$this->Cell($w[3],6,$row[3],0,'','C',$fill);
			$this->Cell($w[4],6,$row[4],0,'','C',$fill);
			$this->Cell($w[5],6,$row[5],0,'','C',$fill);
			$this->Cell($w[6],6,$row[6],0,'','C',$fill);
			$this->Cell($w[7],6,$row[7],0,'','C',$fill);
			$this->Cell($w[8],6,$row[8],0,'','C',$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 * FROM copii WHERE codgrp=\"".substr($_POST["grp_sel"],0,2)."\"  order by nume,prenume,codcopil";
	$result=mysql_query($query) or die("Interogare esuata: ".mysql_error());
	$total = mysql_num_rows($result);
	
// build the data array from the database records.

	$wlu=substr($_POST["luna_sel"],0,2);
	$wdebit=0;
	$wplati=0;
	$wrest=0;
	$wdebit_tot=0;
	$wplati_tot=0;
	$wrest_tot=0;
	$wdebit_prec=0;
	$wplati_prec=0;
	
	While($row = mysql_fetch_array($result)) 
	{
		$nume_pren_copil = $row["nume"]." ".$row["prenume"];
	
				if($wlu=="01")
				{			
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"]+$row["d_val12"]+$row["d_val01"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"]+$row["p_val12"]+$row["p_val01"];
					$rest_ln= $debit_ln - $plati_ln;
							
					$debit_prec=$debit_ln - $row["d_val01"];
					$plati_prec=$plati_ln - $row["p_val01"];
							
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
							
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
							
					$wdebit += $row["d_val01"];
					$wplati += $row["p_val01"];
					$wrest += $row["r_val01"];
							
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;				
					
					//$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row['d_val01'], $row['p_val01'], $debit_ln, $plati_ln, $rest_ln);
					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val01"], $row["p_val01"], $debit_ln, $plati_ln,$rest_ln);
				}
				if($wlu=="02")
				{				
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"]+$row["d_val12"]+$row["d_val01"]+$row["d_val02"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"]+$row["p_val12"]+$row["p_val01"]+$row["p_val02"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val02"];
					$plati_prec=$plati_ln - $row["p_val02"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit += $row["d_val02"];
					$wplati += $row["p_val02"];
					$wrest += $row["r_val02"];
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;
					
					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val02"], $row["p_val02"], $debit_ln, $plati_ln,$rest_ln);		
				}
				if($wlu=="03")
				{				
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"]+$row["d_val12"]+$row["d_val01"]+$row["d_val02"]+$row["d_val03"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"]+$row["p_val12"]+$row["p_val01"]+$row["p_val02"]+$row["p_val03"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val03"];
					$plati_prec=$plati_ln - $row["p_val03"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit += $row["d_val03"];
					$wplati += $row["p_val03"];
					$wrest += $row["r_val03"];
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;				

					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val03"], $row["p_val03"], $debit_ln, $plati_ln,$rest_ln);	
				}
				if($wlu=="04")
				{
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"]+$row["d_val12"]+$row["d_val01"]+$row["d_val02"]+$row["d_val03"]+$row["d_val04"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"]+$row["p_val12"]+$row["p_val01"]+$row["p_val02"]+$row["p_val03"]+$row["p_val04"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val04"];
					$plati_prec=$plati_ln - $row["p_val04"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit += $row["d_val04"];
					$wplati += $row["p_val04"];
					$wrest += $row["r_val04"];
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;		

					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val04"], $row["p_val04"], $debit_ln, $plati_ln,$rest_ln);	
				}
				if($wlu=="05")
				{
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"]+$row["d_val12"]+$row["d_val01"]+$row["d_val02"]+$row["d_val03"]+$row["d_val04"]+$row["d_val05"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"]+$row["p_val12"]+$row["p_val01"]+$row["p_val02"]+$row["p_val03"]+$row["p_val04"]+$row["p_val05"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val05"];
					$plati_prec=$plati_ln - $row["p_val05"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit += $row["d_val05"];
					$wplati += $row["p_val05"];
					$wrest += $row["r_val05"];
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;

					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val05"], $row["p_val05"], $debit_ln, $plati_ln,$rest_ln);	
				}
				if($wlu=="06")
				{
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"]+$row["d_val12"]+$row["d_val01"]+$row["d_val02"]+$row["d_val03"]+$row["d_val04"]+$row["d_val05"]+$row["d_val06"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"]+$row["p_val12"]+$row["p_val01"]+$row["p_val02"]+$row["p_val03"]+$row["p_val04"]+$row["p_val05"]+$row["p_val06"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val06"];
					$plati_prec=$plati_ln - $row["p_val06"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit += $row["d_val06"];
					$wplati += $row["p_val06"];
					$wrest += $row["r_val06"];
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;				

					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val06"], $row["p_val06"], $debit_ln, $plati_ln,$rest_ln);	
				}
				if($wlu=="07")
				{
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"]+$row["d_val12"]+$row["d_val01"]+$row["d_val02"]+$row["d_val03"]+$row["d_val04"]+$row["d_val05"]+$row["d_val06"]+$row["d_val07"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"]+$row["p_val12"]+$row["p_val01"]+$row["p_val02"]+$row["p_val03"]+$row["p_val04"]+$row["p_val05"]+$row["p_val06"]+$row["p_val07"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val07"];
					$plati_prec=$plati_ln - $row["p_val07"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit += $row["d_val07"];
					$wplati += $row["p_val07"];
					$wrest += $row["r_val07"];
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;			

					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val07"], $row["p_val07"], $debit_ln, $plati_ln,$rest_ln);	
				}
				if($wlu=="08")
				{
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"]+$row["d_val12"]+$row["d_val01"]+$row["d_val02"]+$row["d_val03"]+$row["d_val04"]+$row["d_val05"]+$row["d_val06"]+$row["d_val07"]+$row["d_val08"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"]+$row["p_val12"]+$row["p_val01"]+$row["p_val02"]+$row["p_val03"]+$row["p_val04"]+$row["p_val05"]+$row["p_val06"]+$row["p_val07"]+$row["p_val08"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val08"];
					$plati_prec=$plati_ln - $row["p_val08"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit += $row["d_val08"];
					$wplati += $row["p_val08"];
					$wrest += $row["r_val08"];
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;				

					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val08"], $row["p_val08"], $debit_ln, $plati_ln,$rest_ln);	
				}
				if($wlu=="09")
				{
					$debit_ln=$row["d_val00"]+$row["d_val09"];
					$plati_ln=$row["p_val09"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val09"];
					$plati_prec=$plati_ln - $row["p_val09"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit += $row["d_val09"];
					$wplati += $row["p_val09"];
					$wrest += $row["r_val09"];
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;					

					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val09"], $row["p_val09"], $debit_ln, $plati_ln,$rest_ln);	
				}
				if($wlu=="10")
				{
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"];
					$plati_ln=$row["p_val09"]+$row["p_val10"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val10"];
					$plati_prec=$plati_ln - $row["p_val10"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit += $row["d_val10"];
					$wplati += $row["p_val10"];
					$wrest += $row["r_val10"];
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;						

					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val10"], $row["p_val10"], $debit_ln, $plati_ln,$rest_ln);	
				}
				if($wlu=="11")
				{
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val11"];
					$plati_prec=$plati_ln - $row["p_val11"];

					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}					
					
					$wdebit += $row["d_val11"];
					$wplati += $row["p_val11"];
					$wrest += $row["r_val11"];
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;
						
					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val11"], $row["p_val11"], $debit_ln, $plati_ln,$rest_ln);	
				}
				if($wlu=="12")
				{
					$debit_ln=$row["d_val00"]+$row["d_val09"]+$row["d_val10"]+$row["d_val11"]+$row["d_val12"];
					$plati_ln=$row["p_val09"]+$row["p_val10"]+$row["p_val11"]+$row["p_val12"];
					$rest_ln= $debit_ln - $plati_ln;
					
					$debit_prec=$debit_ln - $row["d_val12"];
					$plati_prec=$plati_ln - $row["p_val12"];
					
					if ($debit_prec == 0)
					{
						$debit_prec = ' ';
					}
					if ($plati_prec == 0 )
					{
						$plati_prec = ' ';
					}
					
					$wdebit += $row["d_val12"];
					$wplati += $row["p_val12"];
					$wrest += $row["r_val12"];
					
					$wdebit_prec += $debit_prec;
					$wplati_prec += $plati_prec;
					
					$wdebit_tot += $debit_ln;
					$wplati_tot += $plati_ln;
					$wrest_tot += $rest_ln;					
						
					$data[] = array($row['codcopil'], $nume_pren_copil, $debit_prec, $plati_prec, $row["d_val12"], $row["p_val12"], $debit_ln, $plati_ln,$rest_ln);	
				}		
	}

// start and build the PDF document
//$str = utf8_decode($str);
//$str = iconv('UTF-8', 'windows-1252', $str);

$pdf = new PDF();

//Column titles

//$header=array('Com. prod.','Denumire prod.','Varianta','Cant.','Com. aprov.','Data init.');

$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);
//for($i=1; $i<=35; $i++) $pdf->Cell(0,10,'Linia de text numarul '.$i,0,1);


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

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

$pdf->SetFont('Times','B',10);
$pdf->Ln(1);
$pdf->Cell(30,5,'Total prescolari: '.$total,0,'','C');
$pdf->Ln(3);
$pdf->SetX(82);
$pdf->Cell(10,5,'TOTAL:',0,'','C');
$pdf->SetX(95);
$pdf->Cell(13,5,$wdebit_prec,0,'','C');
$pdf->SetX(108);
$pdf->Cell(13,5,$wplati_prec,0,'','C');
$pdf->SetX(121);
$pdf->Cell(20,5,$wdebit,0,'','C');
$pdf->SetX(141);
$pdf->Cell(20,5,$wplati,0,'','C');
$pdf->SetX(161);
$pdf->Cell(15,5,$wdebit_tot,0,'','C');
$pdf->SetX(176);
$pdf->Cell(15,5,$wplati_tot,0,'','C');
$pdf->SetX(191);
$pdf->Cell(15,5,$wrest_tot,0,'','C');

ob_end_clean();

$pdf->Output('Borderou debite plati_'.$wdengrp_sel.'_'.$data.'.pdf','I');

?>