<?php

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

class PDF_MC_Table extends FPDF
//class PDF extends FPDF 

{
	var $widths;
	var $aligns;

	function __construct()
    {
      parent::FPDF();
    }
	
	function SetWidths($w)
	{
		//Set the array of column widths
		$this->widths=$w;
	}

	function SetAligns($a)
	{
		//Set the array of column alignments
		$this->aligns=$a;
	}
	
	function Row1($data)
	{
		//Calculate the height of the row
		$nb=0;
		$data_c = count($data);
		for($i=0;$i<$data_c;$i++)
		{	
			$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
			$h=5*$nb;
			//Issue a page break first if needed
			$this->CheckPageBreak($h);
			//Draw the cells of the row
			$this->SetLineWidth(.07);		
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'C';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			//$this->Rect($x,$y,$w,$h);
			//Print the text
			$this->MultiCell($w,5,$data[$i],0,$a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w,$y);
		}		
		//Go to the next line
		$this->Ln($h);
	}

	function CheckPageBreak($h)
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger)
			$this->AddPage($this->CurOrientation);
	}

	function NbLines($w,$txt)
	{
		//Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",'',$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$nl=1;
		while($i<$nb)
		{
			$c=$s[$i];
			if($c=="\n")
			{
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
				continue;
			}
			if($c==' ')
				$sep=$i;
			$l+=$cw[$c];
			if($l>$wmax)
			{
				if($sep==-1)
				{
					if($i==$j)
						$i++;
				}
				else
					$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
			}
			else
				$i++;
		}
		return $nl;
	}

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

	// Definirea Header-ului
	function Header()
	{		
		$wan2=$_POST["an"]+1;
		$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);
		
		$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,'REZUMAT INCASARI',0,0,'C');
		$this->Ln(5);	
		$this->SetX(10);
		$this->Cell(0,6,'LUNA: '.$wdenluna_sel.'       ANUL SCOLAR: '.$_POST['an'].' - '.$wan2,0,0,'C');	
		$this->Ln(10);
		
		date_default_timezone_set('Europe/Bucharest');
		$date = date('d-m-Y H:i', time());
		$this->SetFont('Times','I',8);
		$this->SetX(54);
		$this->Cell(15,6,$date,0,0,'L');
		
		$this->SetFont('Times','I',8);	
		$this->SetX(129);
		$this->Cell(30,8,'Pagina '.$this->PageNo().'/nr_pgn',0,0,'R');		
		$this->Ln(8);
		$this->SetX(55);
		
		$x = $this->GetX();
		$y = $this->GetY();
		$this->SetFont('Arial','B',10);
		
		$this->MultiCell(15, 12,' Nr.crt. ', 1,'C');
		$this->SetXY($x + 15, $y);
		$this->MultiCell(25, 12,'  Data  ', 1,'C');		
		$this->SetXY($x + 40, $y);
		$this->MultiCell(15, 12,' Nr. CH ', 1,'C');
		$this->SetXY($x + 55, $y);
		$this->MultiCell(16, 6,'Din care anulate', 1,'C');
		$this->SetXY($x + 71, $y);
		$this->MultiCell(25, 12,' Suma ', 1,'C');		

		$this->Ln(7);		// Adauga rand nou		
	}
		
	// Definirea Footer-ului
	function Footer() 
	{
/*	
		$this->SetY(-30);		// 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($data1)
	{
		//Colors, line width and bold font

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

        //Header
		$w = (array(15,25,15,16,25));
		$this->SetWidths(array(15,25,15,16,25));

        //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($data1 as $row)
        {								
			$this->SetFont('','',10);
			
			$this->SetX(55);
			$this->Row1(array($row[0],$row[1],$row[2],$row[3],$row[4]));
			//$this->Ln();
			
			// flips from true to false and vise versa

			$fill =! $fill;
        }
		$this->Ln(4);
		$this->SetX(55);
        $this->Cell(array_sum($w),0,'','T');
    }

}

//connect to database
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'");
	
	$wdenluna_sel=substr($_POST["luna_sel"],3,strlen($_POST["luna_sel"])-3);
	$wdengrp_sel=substr($_POST["grp_sel"],3,strlen($_POST["grp_sel"])-3);	
		
	$query="SELECT * FROM reg_man order by id";
	$result=mysql_query($query) or die("Interogare esuata: ".mysql_error());

	$i=1;
	$suma_tot_luna=0;
			
	while($row=mysql_fetch_array($result))
	{	
		$suma_tot_luna = $suma_tot_luna + $row["val"];	
		
		$data1[$i] = array($i, $row['ziua'], $row['nr_ch'], $row['nr_ch_a'], $row['val']);
		++$i;	
	}


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

$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);

$x = $pdf->GetX();
$y = $pdf->GetY();
$pdf->SetY($y-5);

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

$data = date('d-m-Y');

$pdf->SetFont('Times','B',10);
$pdf->Ln(3);
$pdf->SetX(121);
$pdf->Cell(9,5,'TOTAL:',0,'','C');
$pdf->SetX(133);
$pdf->Cell(10,5,$suma_tot_luna,0,'','C');


ob_end_clean();

$pdf->Output('Lista registru lunar_'.$wdengrp_sel.'_'.$wdenluna_sel.'.pdf','I');

mysql_close($connection);
?>