<?php
checkLogin('adm');
checkEnteService('pps');
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
{
	function Header(){
		$this->SetFont('Arial','B',11);
		$this->MultiCell(0,4,entities_pdf(strtoupper($GLOBALS['grp_name'])).' - Stampa del '.date('d/m/Y').chr(10).'PRE / POST SCUOLA DEGLI ALUNNI DELLA CLASSE "'.entities_pdf(strtoupper($GLOBALS['subgrp_name'])).'"',0,'C');
		$this->Ln(5);
	}

	// Simple table
	function BasicTable($header, $data){
		
		$this->SetFillColor(224,235,255);
		
		// Header
		$this->SetFont('Arial','B',11);
		$ncol = 1;

		foreach($header as $col){		
			if($ncol == 1){
				$this->SetFont('Arial','B',10);
				$this->Cell($col['width'],12,$col['val'],1,0,'L');
				$this->SetFont('Arial','B',11);
			}else if($ncol == 2){
				$this->Cell($col['width'],6,'',1,0,'C');
			}else{
				$this->Cell($col['width']*2,6,$col['val'],1,0,'C');
			}
			$ncol++;
		}
		
		$this->Ln();
		$this->SetFont('Arial','',7);
		$ncol = 1;
		foreach($header as $col){
			if($ncol==1){
				$this->Cell($col['width'],6);
			}else if($ncol == 2){
				$this->SetFont('Arial','B',11);
					$this->SetFillColor(200,200,200);
				$this->Cell($col['width'],6,$col['val'],1,0,'C',1);
				$this->SetFont('Arial','',7);
					$this->SetFillColor(224,235,255);
			}else{
				$this->Cell($col['width'],6,'PR',1,0,'C',1);
				$this->Cell($col['width'],6,'PO',1,0,'C');
			}
			$ncol++;
		}
		
		
		$this->Ln();
		$this->SetFont('Arial','',7);

		// Data
		$fill = true;
		
		foreach($data as $row){
			$ncol = 1;	
			foreach($row as $col){
				if($ncol==1){		
					$this->Cell($col['width'],5,$col['val'],'LTB',0,'L',$fill);
				}else if($ncol == 2){
					$this->SetFillColor(200,200,200);
					$this->Cell($col['width'],5,$col['val'],1,0,'C',1);
					$this->SetFillColor(224,235,255);
				}else{
					$c = !$c;
					if(!$fill && $c)
						$this->SetFillColor(240,240,240);
					$this->Cell($col['width'],5,$col['val'],1,0,'C',$c);
				}
				$ncol++;
			}
			$this->SetFillColor(224,235,255);
			$fill = !$fill;
			$this->Ln();
		}
	}
		
}


//estraggo alunni iscritti al servizio pre / post scuola
$result = $mysqli->query($q="
SELECT 
CONCAT_WS(' ',anagrafiche.anagr_cognome,anagrafiche.anagr_nome) AS anagrafica,
anagrafiche.anagr_id,
grp.grp_nome AS gruppo,
subgrp.grp_nome AS sottogruppo,
serv_pps.servpps_pre,
serv_pps.servpps_post,
serv_pps.servpps_congedo

FROM serv_pps

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

WHERE 

anagrafiche.ente_id = ".$_SESSION['user']['ente_id']." 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") ." AND
(serv_pps.servpps_pre = 1 OR serv_pps.servpps_post = 1)


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(
			'id'=>$row['anagr_id'],
			'anagr'=>entities_pdf(welltxt($row['anagrafica'])),
			'pre'=>$row['servpps_pre'],
			'post'=>$row['servpps_post'],
			'cong'=>$row['servpps_congedo']
		);
	}
}else{
	header("Location: ../adm/?noprintresult");	
	die();
}

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


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

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

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

//header
$header[0] = array('width'=>$first_col_width,'val'=>'MESE DI '.strtoupper(mese_ita($mese)));
$header[1] = array('width'=>6,'val'=>'C');
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->AddPage();
		$pdf->SetFont('Arial','',11);
		$n_alunni = count($alunni);
		
		//alunni
		for($i=0;$i<$n_alunni;$i++){
			for($x=0;$x<$n_giorni;$x++){
				$val = str_replace('P','X',$presenze[$alunni[$i]['id']][$days[$x]]['pre']);
				$data[$i][] = array('width'=>$col_width,'val'=>$val);
				$val = str_replace('P','X',$presenze[$alunni[$i]['id']][$days[$x]]['post']);
				$data[$i][] = array('width'=>$col_width,'val'=>$val);
				
			}
			
			array_unshift($data[$i],array('width'=>6,'val'=>($alunni[$i]['cong'] == 1 ? 'SI' : 'NO')));
			array_unshift($data[$i],array('width'=>$first_col_width,'val'=>$alunni[$i]['anagr']));			
		}
		
		$pdf->BasicTable($header,$data);

		unset($data);
	}
}

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