<?php
checkLogin('adm');
checkEnteService('men');
include("functions.inc.php");

if(!preg_match('/^[0-1][0-9]/',$_GET['mm']) || !preg_match('/^[1-9][0-9]{3}/',$_GET['yyyy']))
	die();

class PDF extends FPDF
{
	var $headerText;
	var $hideFooter = false;

	// Simple table
	function BasicTable($header, $data){
		
		// Header
		$this->SetFont('Arial','B',11);
		foreach($header as $col){
			$align = $not_fisrt_col ? 'C' : 'L';
			$this->Cell($col['width'],6,$col['val'],1,0,$align);
			$not_fisrt_col = true;
		}
		$this->Ln();
		
		$this->SetFont('Arial','',8);

		// Data
		$fill = false;
		$this->SetFillColor(224,235,255);
		
		foreach($data as $row){
			$not_fisrt_col = false;
			foreach($row as $col){
				
				$align = $not_fisrt_col ? 'C' : 'L';

				switch($col['val']){
					case 'P': $val = 'X';
						break;	
					case 'G': $val = 'AG';
						break;	
					case 'I': $img = true;
						$val = $this->Image(ROOT.'/includes/modules/servizi/images/print_alert.png', $this->getX()+3.3, $this->getY()+0.8, 3.3, 3.3, 'PNG');
						break;	
					case 'S': $img = true;
						$val = $this->Image(ROOT.'/includes/modules/servizi/images/print_circle.png', $this->getX()+2.5, $this->getY()+0.8, 3.3, 3.3, 'PNG');
						break;	
					default:
						$val = $col['val'];
				} 
				if($img){
					$this->Cell($col['width'],5,$val,1,0);
					$img = false;
				}else{
					$this->Cell($col['width'],5,$val,1,0,$align,$fill);
				}
				
				$not_fisrt_col = true;
			}
			$fill = !$fill;
			$this->Ln();
		}
	}
	
	
	function Header(){
		$this->SetFont('Arial','B',11);
		$this->MultiCell(0,4,$this->headerText,0,'C');
		$this->Ln(5);
	}

	function Footer(){
		
		if($this->hideFooter)
			return;
			
		$this->SetFont('Arial','B',10);
		$this->SetY(-15);
		$this->Cell(40,10,'LEGENDA:');
		$this->Image(ROOT.'/includes/modules/servizi/images/print_circle.png', $this->getX()-5, $this->getY()+3, 3.3, 3.3, 'PNG');
		$this->Cell(40,10,'SENZA BUONO');
		
		$this->SetFont('Arial','B');
		$this->Cell(6,10,'X');
		$this->SetFont('Arial','');
		$this->Cell(35,10,'PRESENTE');

		$this->SetFont('Arial','B');
		$this->Cell(6,10,'A');
		$this->SetFont('Arial','');
		$this->Cell(35,10,'ASSENTE');

		$this->SetFont('Arial','B');
		$this->Cell(10,10,'AG');
		$this->SetFont('Arial','');
		$this->Cell(40,10,'ASSENTE GIUSTIFICATO');
	}
}


//estraggo alunni iscritti al servizio mensa
$result = $mysqli->query("
SELECT 
CONCAT_WS(' ',anagrafiche.anagr_cognome,anagrafiche.anagr_nome) AS anagrafica,
anagrafiche.anagr_id,
grp.grp_nome AS gruppo,
subgrp.grp_nome AS sottogruppo,
attachments.att_id,
serv_men.servmen_note

FROM serv_men

LEFT JOIN anagrafiche ON serv_men.anagr_id = anagrafiche.anagr_id
LEFT JOIN gruppi grp ON serv_men.scuola_id = grp.grp_id 
LEFT JOIN gruppi subgrp ON serv_men.classe_id = subgrp.grp_id 
LEFT JOIN attachments ON anagrafiche.anagr_id = attachments.anagr_id 

WHERE 

anagrafiche.ente_id = ".$_SESSION['user']['ente_id']." AND 
anagrafiche.anno = ".intval($_GET['yyyy'])." AND
grp.grp_id ". (!empty($_GET['grp']) ? "=".intval($_GET['grp']) : "IS NOT NULL") ." AND 
subgrp.grp_id ". (!empty($_GET['grp_sub']) ? "=".intval($_GET['grp_sub']) : "IS NOT NULL") ."

GROUP BY anagrafiche.anagr_id
ORDER BY grp.grp_nome, subgrp.grp_nome, anagrafiche.anagr_cognome, anagrafiche.anagr_nome
");

if($result->num_rows > 0){
	while($row=$result->fetch_assoc()){
		$anagrafiche[$row['gruppo']][$row['sottogruppo']][] = array(
			'anagr'=>entities_pdf(welltxt($row['anagrafica'])),
			'id'=>$row['anagr_id'],
			'alert'=>$row['att_id'] <> NULL || $row['servmen_note'] <> NULL ? 'I' : '',
			'intolleranze'=>$row['att_id'] <> NULL ? true : false,
			'note'=>entities_pdf(mb_strtoupper($row['servmen_note'],'UTF-8'))
			);
	}
}else{
	header("Location: ../adm/?noprintresult");	
	die();
}

//presenze
$presenze = get_presenze_all($_GET['grp'],$_GET['grp_sub'],$_GET['yyyy'],$_GET['mm']);

$mese = intval($_GET['mm']);
$anno = $_GET['yyyy'];
$time = mktime(0,0,0,$mese,1,$anno);
$n_giorni_tot = date("t",$time);
$first_col_width = 60;
$col_width = 8.5;

//array con i giorni utili da scrivere sul calendario (no sabato e domenica)
for($i=1;$i<=$n_giorni_tot;$i++){
	$day_of_week = date('w',mktime(0,0,0,$mese,$i,$anno));
	if($day_of_week <> 0 && $day_of_week <> 6)
		$days[] = $i;
}
$n_giorni = count($days);

	
$pdf = new PDF('L','mm','A4');
$pdf->SetMargins(15,10);

//header
$header[0] = array('width'=>$first_col_width,'val'=>'MESE DI '.strtoupper(mese_ita($mese)));
$header[1] = array('width'=>10,'val'=>'INT');
for($i=0;$i<$n_giorni;$i++)
	$header[] = array('width'=>$col_width,'val'=>$days[$i]);



foreach($anagrafiche as $grp_name => $v){
	foreach($v as $subgrp_name => $alunni){

		$pdf->headerText = entities_pdf(strtoupper($grp_name)).chr(10).'Presenze in mensa degli alunni della classe '.entities_pdf(strtoupper($subgrp_name));
		
		$pdf->AddPage();
		$pdf->SetFont('Arial','',11);
		$n_alunni = count($alunni);
		
		//alunni
		for($i=0;$i<$n_alunni;$i++){
			
			for($x=0;$x<$n_giorni;$x++){
				$data[$i][$x] = array('width'=>$col_width,'val'=>$presenze[$alunni[$i]['id']][$days[$x]]);
			}
			
			array_unshift($data[$i],array('width'=>10,'val'=>$alunni[$i]['alert']));
			array_unshift($data[$i],array('width'=>$first_col_width,'val'=>$alunni[$i]['anagr']));
			
			if($alunni[$i]['alert'] == 'I'){
				$intolleranze[$alunni[$i]['id']] = array (
					'anagr' => $alunni[$i]['anagr'],
					'intolleranze' => $alunni[$i]['intolleranze'],
					'note' => $alunni[$i]['note']
				);
			}
		}
		
		$pdf->BasicTable($header,$data);
		$classe[$grp_name][$subgrp_name] = $n_alunni;

		unset($data);
	}
}

$pdf->headerText = 'RIEPILOGO NUMERO ALUNNI IN MENSA';
$pdf->AddPage();
$pdf->hideFooter = true;
$pdf->SetFont('Arial','',10);

$pdf->SetDrawColor(167,202,255);

foreach($classe as $grp_name => $v){
	$tot = array_sum($v);
	$tot_global += $tot;
	$pdf->SetFillColor(224,235,255);
	$pdf->SetFont('Arial','B',10);
	$pdf->Cell(215,6,$grp_name,'',0,'L',1);
	$pdf->SetFont('Arial','',10);
	$pdf->Cell(30,6,' ','',0,'R',1);
	$pdf->SetFont('Arial','B',10);
	$pdf->Cell(15,6,$tot,'',1,'R',1);
	$pdf->Ln(1);
	
	$pdf->SetFillColor(224,235,255);
	$pdf->SetFont('Arial','',10);

	foreach($v as $subgrp_name => $n){
		$pdf->Cell(130,6,$subgrp_name,'B',0);
		$pdf->Cell(130,6,$n,'B',1,'R');
	}
	
	$pdf->Ln(10);
	
}

$pdf->SetFillColor(224,235,255);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(260,5,'Totale:       '.$tot_global,'',0,'R',1);



if($intolleranze){
	ksort($intolleranze);
	//intolleranze
	$pdf->SetFont('Arial','B',11);
	$pdf->headerText = 'INTOLLERANZE ALIMENTARI';
	$pdf->AddPage();
	$pdf->SetDrawColor(167,202,255);

	$pdf->SetFont('Arial','B',10);
	$pdf->Cell(55,6,'Alunno','',0,'',1);
	$pdf->Cell(30,6,'Intolleranze','',0,'',1);
	$pdf->Cell(175,6,'Note','',1,'',1);

	$pdf->Ln(5);


	
	foreach($intolleranze as $alunno){
		$pdf->SetFont('Arial','',10);
		$pdf->Cell(63,4,$alunno['anagr']);
		$pdf->SetFont('Arial','',8);
		$pdf->Cell(22,4,($alunno['intolleranze'] ? 'SI' : ''));
		$pdf->MultiCell(165,4,$alunno['note']);
		$pdf->Ln(1);
		$pdf->Line($pdf->getX(),$pdf->getY(),$pdf->getX()+260,$pdf->getY());
		$pdf->Ln(2);
	}		
}


$pdf->Output('mensa_presenze_'.strtolower(mese_ita($mese)).'-'.$anno.'.pdf','D');
?>