<?php
include("../sessionreserved.php");
include("../../db.php");

function setMarkup($pdf,$arr){
	if(is_array($arr)){
		foreach($arr as $dati){
			$testo = $dati[0];
			if($testo <> ''){
				$x = $dati[1];
				$y = $dati[2]-8;
				$fontsize = $dati[3];
				$fonttype = $dati[4];
				$pdf->SetFont('arial',$fonttype,$fontsize);
				$pdf->SetXY($x,$y);
				if($testo == 'V')
					$pdf->Image('../images/modulocheck.gif',$x+1,$y-2,4);
				else					
					$pdf->Cell(20,1,$testo);
			}
		}
	}
}

//estraggo i dettagli della visita
$result = mysql_query("
	SELECT *, 
	DATE_FORMAT(datavisita,'%d/%m/%Y') AS datavisitaOK,
	DATE_FORMAT(data_inizlav,'%d/%m/%Y') AS data_inizlavOK,
	DATE_FORMAT(data_finelav,'%d/%m/%Y') AS data_finelavOK,
	DATE_FORMAT(data_prossvisita,'%d/%m/%Y') AS data_prossvisitaOK
	
	FROM visite 
	LEFT JOIN users ON visite.userid = users.userid 
	LEFT JOIN visite_imp ON visite.idvisita = visite_imp.idvisita 
	WHERE visite.idvisita = '".$_GET['id']."'");

	
require('../classes/pdf/fpdf.php');

$pdf=new FPDF();
$pdf->SetAutoPageBreak(false);
$pdf->SetMargins(0,0,0);
$pdf->AddPage();
$pdf->SetFont('arial','',10);


if(mysql_num_rows($result) > 0){
	$campi = true;
	$dir = mysql_result($result,0,'dir');
}

if($dir == 'cpt')
	$pdf->Image('../images/modulo_p1_cpt.gif',0,0,210);
else
	$pdf->Image('../images/modulo_p1.gif',0,0,210);
	

	
if($campi){
	

$zonaRLST = mysql_result($result,0,'zona');
$anagrRLST = mysql_result($result,0,'anagrafica');


$pdf->SetXY(28,30);
$pdf->Cell(20,1,$anagrRLST);

//zona
//$pntxy[] = array($zonaRLST,7,28,10,'B');


$codice = mysql_result($result,0,'codice'); 
if(!empty($codice))
	$pntxy[] = array("CODICE ".$codice,158,28,10,'B');

$idvisita = mysql_result($result,0,'idvisita');
$userid = mysql_result($result,0,'userid'); 

$datavisita = mysql_result($result,0,'datavisitaOK');

$pntxy[] = array($datavisita,170,38,10,'');

$tipovisita = mysql_result($result,0,'tipovisita'); 
if($tipovisita == 'aziendale') $pntxy[] = array("V",7,46,12,'B');
if($tipovisita == 'richiesta') $pntxy[] = array("V",43,46,13,'B');
if($tipovisita == 'pos') $pntxy[] = array("V",77,46,12,'B');
if($tipovisita == 'altro') $pntxy[] = array("V",110,46,12,'B');

$tipoaltro = mysql_result($result,0,'tipoaltro'); 
$pntxy[] = array($tipoaltro,124,45,10,'');

$nvisita = mysql_result($result,0,'nvisita');
if($nvisita == '1') $pntxy[] = array("V",21,53,12,'B');
if($nvisita == '2') $pntxy[] = array("V",38,53,12,'B');
if($nvisita == '3') $pntxy[] = array("V",54,53,12,'B');

if(mysql_result($result,0,'congiunta') == 1) $pntxy[] = array("V",70,53,12,'B');
$congiuntadesc = mysql_result($result,0,'congiuntadesc');
$pntxy[] = array($congiuntadesc,103,53,10,'');

if(mysql_result($result,0,'custom_comune') == ''){
	$comune = mysql_result($result,0,'cant_comune');
	$strada = mysql_result($result,0,'cant_strada');
	$numero = mysql_result($result,0,'id_numero');
}else{
	$comune = mysql_result($result,0,'custom_comune');
	$strada = mysql_result($result,0,'custom_strada');
	$numero = mysql_result($result,0,'custom_numero');
}

$pntxy[] = array($comune,28,75,10,'');
$pntxy[] = array($strada,90,75,10,'');
$pntxy[] = array($numero,182,75,10,'');

mysql_result($result,0,'lavpubblico') == 1 ?  $pntxy[] = array("V",24,83,12,'B') :  $pntxy[] = array("V",45,83,12,'B');;

$commit_anagra = mysql_result($result,0,'commit_anagra'); 
$pntxy[] = array($commit_anagra,31,90,10,'');
$commit_piva = mysql_result($result,0,'commit_piva'); 
$pntxy[] = array($commit_piva,18,98,10,'');
$commit_codfis = mysql_result($result,0,'commit_codfis'); 
$pntxy[] = array($commit_codfis,115,98,10,'');
$commit_via = mysql_result($result,0,'commit_via'); 
$pntxy[] = array($commit_via,14,105,10,'');
$commit_n = mysql_result($result,0,'commit_n'); 
$pntxy[] = array($commit_n,97,105,10,'');
$commit_comune = mysql_result($result,0,'commit_comune'); 
$pntxy[] = array($commit_comune,130,105,10,'');
$commit_prov = mysql_result($result,0,'commit_prov'); 
$pntxy[] = array($commit_prov,18,113,10,'');
$commit_tel = mysql_result($result,0,'commit_tel'); 
$pntxy[] = array($commit_tel,41,113,10,'');

$codnat = mysql_result($result,0,'codnat'); 
$codtiponat = mysql_result($result,0,'codtiponat'); 

$result2 = mysql_query("SELECT * FROM natopera WHERE codnat = ".intval($codnat)." OR codtiponat = ".intval($codtiponat));
while($row = mysql_fetch_array($result2)){
	if($row['codnat'] == $codnat)
		$natura = utf8_encode($row['nat']);
	if($row['codtiponat'] == $codtiponat)
		$dett = utf8_encode($row['tiponat']);
}
$pntxy[] = array($natura,40,121,10,'');
$pntxy[] = array($dett,27,128,10,'');


//impresa
$imp_anagrafica = mysql_result($result,0,'imp_anagrafica'); 
$pntxy[] = array($imp_anagrafica,50,150,10,'');
$imp_comune = mysql_result($result,0,'imp_comune'); 
$pntxy[] = array($imp_comune,43,158,10,'');
$imp_prov = mysql_result($result,0,'imp_prov'); 
$pntxy[] = array($imp_prov,180,158,10,'');
$imp_indirizzo = mysql_result($result,0,'imp_indirizzo'); 
$pntxy[] = array($imp_indirizzo,15,165,10,'');
$imp_numero = mysql_result($result,0,'imp_numero'); 
$pntxy[] = array($imp_numero,183,165,10,'');
$imp_piva = mysql_result($result,0,'imp_piva'); 
$pntxy[] = array($imp_piva,19,173,10,'');
$imp_codfisc = mysql_result($result,0,'imp_codfisc'); 
$pntxy[] = array($imp_codfisc,115,173,10,'');

$imp_esterna = mysql_result($result,0,'imp_esterna');
if($imp_esterna == 1) $pntxy[] = array("V",20,180,12,'B');
if($imp_esterna == 2) $pntxy[] = array("V",42,180,12,'B');

$imp_assoc = mysql_result($result,0,'imp_assoc');
if($imp_assoc == 'COL') $pntxy[] = array("V",107,180,12,'B');
if($imp_assoc == 'CNA') $pntxy[] = array("V",148,180,12,'B');
if($imp_assoc == 'CON') $pntxy[] = array("V",164,180,12,'B');

$imp_tel = mysql_result($result,0,'imp_tel'); 
$pntxy[] = array($imp_tel,15,188,10,'');
$imp_fax = mysql_result($result,0,'imp_fax'); 
$pntxy[] = array($imp_fax,68,188,10,'');
$imp_email = mysql_result($result,0,'imp_email');  
$pntxy[] = array($imp_email,125,188,10,'');

$imp_attivita = mysql_result($result,0,'imp_attivita'); 
$pntxy[] = array($imp_attivita,33,195,10,'');

$ccnl = mysql_result($result,0,'ccnl'); 
if($ccnl == 'ind') $pntxy[] = array("V",35,203,12,'B');
if($ccnl == 'art') $pntxy[] = array("V",50,203,12,'B');
if($ccnl == 'altro') $pntxy[] = array("V",64,203,12,'B');
$ccnl_altro = mysql_result($result,0,'ccnl_altro'); 
$pntxy[] = array($ccnl_altro,80,203,10,'');

$tipologiacod = mysql_result($result,0,'tipologiacod');  
$lavorazionecod = mysql_result($result,0,'lavorazionecod'); 
$attivitacod = mysql_result($result,0,'attivitacod'); 

$result2 = mysql_query("SELECT * FROM attivitaspec");
while($row = mysql_fetch_array($result2)){
	if($row['tipologiacod'] == $tipologiacod)
		$spec = utf8_encode($row['tipologia']);
	if($row['lavorazionecod'] == $lavorazionecod)
		$lav = utf8_encode($row['lavorazione']);
	if($row['attivitacod'] == $attivitacod)
		$att = utf8_encode($row['attivita']);
}
$pntxy[] = array($spec,33,219,10,'');
$pntxy[] = array($lav,36,226,10,'');
$pntxy[] = array($att,30,234,10,'');
$rls = mysql_result($result,0,'rls'); 
$pntxy[] = array($rls,33,242,10,'');

$n_addetti = mysql_result($result,0,'n_addetti'); 
$pntxy[] = array($n_addetti,36,256,10,'');
$n_operai = mysql_result($result,0,'n_operai'); 
$pntxy[] = array($n_operai,62,256,10,'');
$n_impiegati = mysql_result($result,0,'n_impiegati'); 
$pntxy[] = array($n_impiegati,86,256,10,'');
$n_apprendisti = mysql_result($result,0,'n_apprendisti'); 
$pntxy[] = array($n_apprendisti,112,256,10,'');
$n_interinali = mysql_result($result,0,'n_interinali'); 
$pntxy[] = array($n_interinali,138,256,10,'');
$n_parttime = mysql_result($result,0,'n_parttime'); 
$pntxy[] = array($n_parttime,162,256,10,'');
$n_autonomi = mysql_result($result,0,'n_autonomi'); 
$pntxy[] = array($n_autonomi,187,256,10,'');

$n_addettipres = mysql_result($result,0,'n_addettipres'); 
$data_inizlav = mysql_result($result,0,'data_inizlavOK'); 
$data_finelav = mysql_result($result,0,'data_finelavOK');
if($data_inizlav == '00/00/0000')
	$data_inizlav = '';
if($data_finelav == '00/00/0000')
	$data_finelav = '';
$pntxy[] = array($n_addettipres,40,264,10,'');
$pntxy[] = array($data_inizlav,93,264,10,'');
$pntxy[] = array($data_finelav,153,264,10,'');

$partecip1 = mysql_result($result,0,'partecip1'); 
$pntxy[] = array($partecip1,10,277,10,'');
$partecip2 = mysql_result($result,0,'partecip2'); 
$pntxy[] = array($partecip2,10,283,10,'');
$partecip3 = mysql_result($result,0,'partecip3'); 
$pntxy[] = array($partecip3,10,289,10,'');
$partecip4 = mysql_result($result,0,'partecip4'); 
$pntxy[] = array($partecip4,10,294,10,'');
$partecip1_funz = mysql_result($result,0,'partecip1_funz'); 
$pntxy[] = array($partecip1_funz,85,277,10,'');
$partecip2_funz = mysql_result($result,0,'partecip2_funz');
$pntxy[] = array($partecip2_funz,85,283,10,'');
$partecip3_funz = mysql_result($result,0,'partecip3_funz'); 
$pntxy[] = array($partecip3_funz,85,289,10,'');
$partecip4_funz = mysql_result($result,0,'partecip4_funz'); 
$pntxy[] = array($partecip4_funz,85,294,10,'');

setMarkup($pdf,$pntxy);
unset($pntxy);
}
########################### SECONDA PAGINA ######################
$pdf->AddPage();
if($dir == 'cpt')
	$pdf->Image('../images/modulo_p2_cpt.gif',0,0,210);
else
	$pdf->Image('../images/modulo_p2.gif',0,0,210);

if($campi){

$note = mysql_result($result,0,'note');
if($note <> ""){
	$pdf->SetXY(10,95);
	$pdf->MultiCell(180,5,$note);
}
	
mysql_result($result,0,'psc_vis') == 1 ? $pntxy[] = array("V",10,27,12,'B') : NULL;
mysql_result($result,0,'psc_vis') == 2 ? $pntxy[] = array("V",19,27,12,'B') : NULL;
mysql_result($result,0,'psc_ido') == 1 ? $pntxy[] = array("V",81,27,12,'B') : NULL;
mysql_result($result,0,'psc_ido') == 2 ? $pntxy[] = array("V",90,27,12,'B') : NULL;

mysql_result($result,0,'pos_vis') == 1 ? $pntxy[] = array("V",10,32,12,'B') : NULL;
mysql_result($result,0,'pos_vis') == 2 ? $pntxy[] = array("V",19,32,12,'B') : NULL;
mysql_result($result,0,'pos_ido') == 1 ? $pntxy[] = array("V",81,32,12,'B') : NULL;
mysql_result($result,0,'pos_ido') == 2 ? $pntxy[] = array("V",90,32,12,'B') : NULL;

mysql_result($result,0,'pss_vis') == 1 ? $pntxy[] = array("V",10,38,12,'B') : NULL;
mysql_result($result,0,'pss_vis') == 2 ? $pntxy[] = array("V",19,38,12,'B') : NULL;
mysql_result($result,0,'pss_ido') == 1 ? $pntxy[] = array("V",81,38,12,'B') : NULL;
mysql_result($result,0,'pss_ido') == 2 ? $pntxy[] = array("V",90,38,12,'B') : NULL;

mysql_result($result,0,'docinfo_vis') == 1 ? $pntxy[] = array("V",10,46,12,'B') : NULL;
mysql_result($result,0,'docinfo_vis') == 2 ? $pntxy[] = array("V",19,46,12,'B') : NULL;
mysql_result($result,0,'docinfo_ido') == 1 ? $pntxy[] = array("V",81,46,12,'B') : NULL;
mysql_result($result,0,'docinfo_ido') == 2 ? $pntxy[] = array("V",90,46,12,'B') : NULL;

mysql_result($result,0,'libretti_vis') == 1 ? $pntxy[] = array("V",10,53,12,'B') : NULL;
mysql_result($result,0,'libretti_vis') == 2 ? $pntxy[] = array("V",19,53,12,'B') : NULL;
mysql_result($result,0,'libretti_ido') == 1 ? $pntxy[] = array("V",81,53,12,'B') : NULL;
mysql_result($result,0,'libretti_ido') == 2 ? $pntxy[] = array("V",90,53,12,'B') : NULL;

mysql_result($result,0,'reginf_vis') == 1 ? $pntxy[] = array("V",10,58,12,'B') : NULL;
mysql_result($result,0,'reginf_vis') == 2 ? $pntxy[] = array("V",19,58,12,'B') : NULL;
mysql_result($result,0,'reginf_ido') == 1 ? $pntxy[] = array("V",81,58,12,'B') : NULL;
mysql_result($result,0,'reginf_ido') == 2 ? $pntxy[] = array("V",90,58,12,'B') : NULL;
 
mysql_result($result,0,'duvri_vis') == 1 ? $pntxy[] = array("V",10,64,12,'B') : NULL;
mysql_result($result,0,'duvri_vis') == 2 ? $pntxy[] = array("V",19,64,12,'B') : NULL;
mysql_result($result,0,'duvri_ido') == 1 ? $pntxy[] = array("V",81,64,12,'B') : NULL;
mysql_result($result,0,'duvri_ido') == 2 ? $pntxy[] = array("V",90,64,12,'B') : NULL;

mysql_result($result,0,'dpi1_dist') == 1 ? $pntxy[] = array("V",10,75,12,'B') : NULL;
mysql_result($result,0,'dpi1_dist') == 2 ? $pntxy[] = array("V",19,75,12,'B') : NULL;
mysql_result($result,0,'dpi1_form') == 1 ? $pntxy[] = array("V",81,75,12,'B') : NULL;
mysql_result($result,0,'dpi1_form') == 2 ? $pntxy[] = array("V",90,75,12,'B') : NULL;

mysql_result($result,0,'dpi2_dist') == 1 ? $pntxy[] = array("V",10,81,12,'B') : NULL;
mysql_result($result,0,'dpi2_dist') == 2 ? $pntxy[] = array("V",19,81,12,'B') : NULL;
mysql_result($result,0,'dpi2_form') == 1 ? $pntxy[] = array("V",81,81,12,'B') : NULL;
mysql_result($result,0,'dpi2_form') == 2 ? $pntxy[] = array("V",90,81,12,'B') : NULL;

mysql_result($result,0,'dpi3_dist') == 1 ? $pntxy[] = array("V",10,87,12,'B') : NULL;
mysql_result($result,0,'dpi3_dist') == 2 ? $pntxy[] = array("V",19,87,12,'B') : NULL;
mysql_result($result,0,'dpi3_form') == 1 ? $pntxy[] = array("V",81,87,12,'B') : NULL;
mysql_result($result,0,'dpi3_form') == 2 ? $pntxy[] = array("V",90,87,12,'B') : NULL;

###################
mysql_result($result,0,'rchimico_vis') == 1 ? $pntxy[] = array("V",111,27,12,'B') : NULL;
mysql_result($result,0,'rchimico_vis') == 2 ? $pntxy[] = array("V",120,27,12,'B') : NULL;
mysql_result($result,0,'rchimico_ido') == 1 ? $pntxy[] = array("V",182,27,12,'B') : NULL;
mysql_result($result,0,'rchimico_ido') == 2 ? $pntxy[] = array("V",191,27,12,'B') : NULL;

mysql_result($result,0,'rvibraz_vis') == 1 ? $pntxy[] = array("V",111,32,12,'B') : NULL;
mysql_result($result,0,'rvibraz_vis') == 2 ? $pntxy[] = array("V",120,32,12,'B') : NULL;
mysql_result($result,0,'rvibraz_ido') == 1 ? $pntxy[] = array("V",182,32,12,'B') : NULL;
mysql_result($result,0,'rvibraz_ido') == 2 ? $pntxy[] = array("V",191,32,12,'B') : NULL;

mysql_result($result,0,'rrumore_vis') == 1 ? $pntxy[] = array("V",111,38,12,'B') : NULL;
mysql_result($result,0,'rrumore_vis') == 2 ? $pntxy[] = array("V",120,38,12,'B') : NULL;
mysql_result($result,0,'rrumore_ido') == 1 ? $pntxy[] = array("V",182,38,12,'B') : NULL;
mysql_result($result,0,'rrumore_ido') == 2 ? $pntxy[] = array("V",191,38,12,'B') : NULL;

mysql_result($result,0,'pimus_vis') == 1 ? $pntxy[] = array("V",111,44,12,'B') : NULL;
mysql_result($result,0,'pimus_vis') == 2 ? $pntxy[] = array("V",120,44,12,'B') : NULL;
mysql_result($result,0,'pimus_ido') == 1 ? $pntxy[] = array("V",182,44,12,'B') : NULL;
mysql_result($result,0,'pimus_ido') == 2 ? $pntxy[] = array("V",191,44,12,'B') : NULL;

mysql_result($result,0,'orario_vis') == 1 ? $pntxy[] = array("V",111,49,12,'B') : NULL;
mysql_result($result,0,'orario_vis') == 2 ? $pntxy[] = array("V",120,49,12,'B') : NULL;
mysql_result($result,0,'orario_ido') == 1 ? $pntxy[] = array("V",182,49,12,'B') : NULL;
mysql_result($result,0,'orario_ido') == 2 ? $pntxy[] = array("V",191,49,12,'B') : NULL; 

mysql_result($result,0,'servizi_pres') == 1 ? $pntxy[] = array("V",111,60,12,'B') : NULL;
mysql_result($result,0,'servizi_pres') == 2 ? $pntxy[] = array("V",120,60,12,'B') : NULL;
mysql_result($result,0,'servizi_norma') == 1 ? $pntxy[] = array("V",182,60,12,'B') : NULL;
mysql_result($result,0,'servizi_norma') == 2 ? $pntxy[] = array("V",191,60,12,'B') : NULL;

mysql_result($result,0,'spogliat_pres') == 1 ? $pntxy[] = array("V",111,66,12,'B') : NULL;
mysql_result($result,0,'spogliat_pres') == 2 ? $pntxy[] = array("V",120,66,12,'B') : NULL;
mysql_result($result,0,'spogliat_norma') == 1 ? $pntxy[] = array("V",182,66,12,'B') : NULL;
mysql_result($result,0,'spogliat_norma') == 2 ? $pntxy[] = array("V",191,66,12,'B') : NULL;

mysql_result($result,0,'mensa_pres') == 1 ? $pntxy[] = array("V",111,72,12,'B') : NULL;
mysql_result($result,0,'mensa_pres') == 2 ? $pntxy[] = array("V",120,72,12,'B') : NULL;
mysql_result($result,0,'mensa_norma') == 1 ? $pntxy[] = array("V",182,72,12,'B') : NULL;
mysql_result($result,0,'mensa_norma') == 2 ? $pntxy[] = array("V",191,72,12,'B') : NULL;

mysql_result($result,0,'cassetta_pres') == 1 ? $pntxy[] = array("V",111,77,12,'B') : NULL;
mysql_result($result,0,'cassetta_pres') == 2 ? $pntxy[] = array("V",120,77,12,'B') : NULL;
mysql_result($result,0,'cassetta_norma') == 1 ? $pntxy[] = array("V",182,77,12,'B') : NULL;
mysql_result($result,0,'cassetta_norma') == 2 ? $pntxy[] = array("V",191,77,12,'B') : NULL;

mysql_result($result,0,'numeri_pres') == 1 ? $pntxy[] = array("V",111,83,12,'B') : NULL;
mysql_result($result,0,'numeri_pres') == 2 ? $pntxy[] = array("V",120,83,12,'B') : NULL;
mysql_result($result,0,'numeri_norma') == 1 ? $pntxy[] = array("V",182,83,12,'B') : NULL;
mysql_result($result,0,'numeri_norma') == 2 ? $pntxy[] = array("V",191,83,12,'B') : NULL;

$data_prossvisita = mysql_result($result,0,'data_prossvisitaOK'); 
$ora_prossvisita = substr(mysql_result($result,0,'ora_prossvisita'),0,5); 
if($data_prossvisita == '00/00/0000')
	$data_prossvisita = '';
if($ora_prossvisita == '00:00')
	$ora_prossvisita = '';

$pntxy[] = array($data_prossvisita,68,150,10,'');
$pntxy[] = array($ora_prossvisita,110,150,10,'');

mysql_result($result,0,'chiuso') == 1 ? $pntxy[] = array("V",8,158,12,'B') : NULL;
mysql_result($result,0,'completata') == 1 ? $pntxy[] = array("V",8,165,12,'B') : NULL;

setMarkup($pdf,$pntxy);
}else{
	$codice = "stampa";	
}


$pdf->Output($codice.".pdf",'I');

?>