<?php
checkLogin('adm');
checkEnteService('bus');

class PDF extends FPDF
{
	var $widths;
	var $aligns;
	var $line_h = 3.5;
	var $cell_padding = 1;
	var $fill = false;
	
	function Header(){
		$this->SetFont('Arial','B',11);
		$this->MultiCell(0,6,entities_pdf(strtoupper($GLOBALS['grp_name'])).' - SERVIZIO BUS - Stampa del '.date("d/m/Y"),0,'C');
		$this->Ln(5);
	}
	
	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 Row($data)
	{
		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
			$nb=max($nb, $this->NbLines($this->widths[$i], $data[$i]));
		$h=$this->line_h*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);
		
		$fill_color = $this->fill ? 'DF' : '';
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++)
		{
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			$this->Rect($x - $this->cell_padding, $y - $this->cell_padding, $w + $this->cell_padding, $h + $this->cell_padding*2,$fill_color);
			//Print the text
			$this->MultiCell($w, $this->line_h, $data[$i], 0, $a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w + $this->cell_padding, $y);
		}
		//Go to the next line
		$this->fill = !$this->fill;
		$this->Ln($h+ $this->cell_padding*2);
	}
	
	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;
	}
}

//delegati al recupero
$result = $mysqli->query("SELECT anagr_id, CONCAT_WS(' ',deleg_cognome,deleg_nome) AS anagrafica FROM servizi_delegati WHERE serv_cod = 'bus'");
while($row=$result->fetch_assoc()){
	$delegati[$row['anagr_id']][] = entities_pdf(welltxt($row['anagrafica']));
}

//bus di appartenenza (o dirottato)
if(empty($_GET['grp']))
	$grp_id = "IS NOT NULL";
else if(intval($_GET['grp']) > 0)
	$grp_id = "=".intval($_GET['grp'])." AND servbus_dirotta = 0";
else
	$grp_id = "IS NOT NULL AND servbus_dirotta = 1";


//estraggo alunni iscritti al servizio bus
$result = $mysqli->query("
SELECT 
CONCAT_WS(' ',anagrafiche.anagr_cognome,anagrafiche.anagr_nome) AS anagrafica,
CONCAT_WS(' ',serv_bus.servbus_ferm_and_ind,serv_bus.servbus_ferm_and_n) AS fermata_and,
CONCAT_WS(' ',serv_bus.servbus_ferm_rit_ind,serv_bus.servbus_ferm_rit_n) AS fermata_rit,
serv_bus.servbus_congedo,
serv_bus.servbus_dirotta,
comuni.comune,
users.user_cell,
grp.grp_nome AS gruppo,
subgrp.grp_nome AS sottogruppo,
anagrafiche.anagr_id


FROM serv_bus

LEFT JOIN anagrafiche ON serv_bus.anagr_id = anagrafiche.anagr_id
LEFT JOIN users ON anagrafiche.user_id = users.user_id
LEFT JOIN comuni ON anagrafiche.comune_id = comuni.comune_id
LEFT JOIN gruppi grp ON serv_bus.scuola_id = grp.grp_id 
LEFT JOIN gruppi subgrp ON serv_bus.classe_id = subgrp.grp_id 

WHERE 

anagrafiche.ente_id = ".$_SESSION['user']['ente_id']." AND grp.grp_id ".$grp_id." AND anagrafiche.anno = ".$_SESSION['user']['ente_anno']."

ORDER BY serv_bus.servbus_dirotta, grp.grp_nome, anagrafiche.anagr_cognome, anagrafiche.anagr_nome
");
if($result->num_rows > 0){
	while($row=$result->fetch_assoc()){
		
		$grp = $row['servbus_dirotta'] == 0 ? $row['gruppo'] : 'Altro BUS';
		
		$deleg = $delegati[$row['anagr_id']] ? implode(chr(10),$delegati[$row['anagr_id']]) : '';
		
		$anagrafiche[$grp][$row['anagr_id']] = array(
			entities_pdf($row['anagrafica']),
			entities_pdf(welltxt($row['fermata_and'])),
			entities_pdf(welltxt($row['fermata_rit'])),
			$deleg,
			($row['servbus_congedo'] == 1 ? 'SI' : ''),
			entities_pdf($row['comune']),
			entities_pdf($row['user_cell']),
			entities_pdf($row['sottogruppo'])
		);
	}

	
		
}else{
	header("Location: ../adm/?noprintresult");	
	die();
}


$pdf = new PDF('L','mm','A4');
$pdf->SetMargins(15,10);
$pdf->SetWidths(array(40, 45, 45, 40, 20, 25, 30, 15));
$pdf->SetFillColor(224,235,255);

foreach($anagrafiche as $grp_name => $v){
	
	//header
	$pdf->AddPage();
	$pdf->SetFont('Arial','B',8);
	$pdf->SetAligns(array('C', 'C', 'C', 'C', 'C', 'C', 'C', 'C'));
	$pdf->Row(array('Utente','Fermata ANDATA','Fermata RITORNO','Accompagnatori','Discesa autonoma','Residenza','Telefono','Classe'));
	$pdf->SetFont('Arial','',8);
	$pdf->SetAligns(array('L', 'L', 'L', 'C', 'C', 'C', 'C', 'C'));
	
	foreach($v as $id => $alunno){

		$pdf->Row($alunno);
	
	}
}

$pdf->Output('bus_elenco.pdf','D');
?>