<?php
	
	$fontsize = 9;
	$x = 280;
	$y = ($fontsize/2);
	
	$col_test =       (16/100)*$x;
	$col_result =     (10/100)*$x;
	$col_units =      ( 7/100)*$x;
	$col_method =     (31/100)*$x;
	$col_accredited = ( 6/100)*$x;
	$col_auth_user =  (11/100)*$x;
	$col_inhouse =    ( 8/100)*$x;
	$col_modreason =  (11/100)*$x;
	//                100
	
	$filelocation = "resultreport.php";
	
	if(isset($_GET['c'])){
		require "inc/connect.php"; //also includes settings
		require("inc/texttonumber.php"); // function for converting text result to number
		require("inc/numbertotext.php"); // function for converting number result to text
		
		$pdfoutput=true; //Use true for pdf output; false for html output
		
		if($pdfoutput){
			require("inc/pdf/fpdf.php");
			class PDF extends FPDF{
				function Header(){
					global $link;
				}
				function Footer(){
					global $link;
				}
			}
			$pdf=new PDF();
			$pdf->AliasNbPages(); //this allows PHP to see the total number of pages in the PDF, just before output. Used in the page footer
			$pdf->SetFillColor(255,255,255); //white
			$pdf->SetDisplayMode('fullpage');
			$pdf->SetAuthor($_SESSION['userFirst'].' '.$_SESSION['userLast'],1);
			$pdf->SetCreator('DairyWindow',1);
			$pdf->SetSubject('Result report',1);
			$pdf->SetTitle(  'Result report',1);
			
			$pdf->SetFont('Arial','',$fontsize);
			$pdf->SetY(10); //mm from top
			$pdf->SetTextColor(0,0,0); //black
			
		}
		
		//get units
		$sql="
			Select
				dw_unit.unitID,
				dw_unit.unitText
			From
				dw_unit
			Where
				dw_unit.unitCypher = ".clean($_GET['c'])." And
				dw_unit.unitActive = 1
			Order By
				dw_unit.unitActive Desc,
				Case dw_unit.unitText
					When 'F' Then 1
					When 'M' Then 2
					When 'L' Then 3
					Else 4
				End,
				Field(dw_unit.unitText, 'F', 'M', 'L', 'Comp', 'Comp1', 'Comp 1', 'Comp2', 'Comp 2'),
				LPad(dw_unit.unitText,20,0),
				dw_location.locationOrder,
				dw_location.locationText
		";
		if(!$qryunit = mysqli_query($link,$sql)) reporterror(mysqli_error($link), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
		while($unitarray = mysqli_fetch_array($qryunit)){
			$pdf->AddPage('L');
			$pdf->Cell($x,$y,$unitarray['unitText'],1,1);
			
			$pdf->Cell($col_test,$y,'Test',1,0,'L',1);
			$pdf->Cell($col_result,$y,'Result',1,0,'L',1);
			$pdf->Cell($col_units,$y,'Units',1,0,'L',1);
			$pdf->Cell($col_method,$y,'Method',1,0,'L',1);
			$pdf->Cell($col_accredited,$y,'Accredited',1,0,'C',1);
			$pdf->Cell($col_auth_user,$y,'Authorised',1,0,'L',1);
			$pdf->Cell($col_inhouse,$y,'In house',1,0,'L',1);
			$pdf->Cell($col_modreason,$y,'Changed',1,1,'L',1);
			
			$sql="
				Select
					dw_result.resultUnit,
					dw_result.resultFriendly,
					dw_result.resultModReason,
					dw_test.testText,
					dw_test.testCode,
					dw_test.testUnits,
					dw_test.testMethod,
					dw_test.testAccredited,
					dw_test.testLabName,
					dw_user.userFirst,
					dw_user.userLast,
					dw_resultrelease.releaseID
				From
					dw_result Left Join
					dw_test
						On dw_result.resultTest = dw_test.testID Inner Join
					dw_resultrelease
						On dw_test.testID = dw_resultrelease.releaseTest Left Join
					dw_user
						On dw_resultrelease.releaseUser = dw_user.userID Left Join
					dw_group
						On dw_test.testGroup = dw_group.groupID
				Where
					dw_result.resultUnit = ".$unitarray['unitID']." And
					dw_test.testActive = 1 And
					dw_resultrelease.releaseCypher = ".clean($_GET['c'])." And
					dw_group.groupActive = 1
				Order By
					dw_group.groupOrder,
					dw_test.testOrder
			";
			if(!$qryresult = mysqli_query($link,$sql)) reporterror(mysqli_error($link), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			$pdf->MultiCell($x,$y,$sql,1);
			while($resultarray = mysqli_fetch_array($qryresult)){
				$pdf->Cell($col_test,$y,$resultarray['testText'],1,0,'L',1);
				$pdf->Cell($col_result,$y,$resultarray['resultFriendly'],1,0,'L',1);
				$pdf->Cell($col_units,$y,$resultarray['testUnits'],1,0,'L',1);
				$pdf->Cell($col_method,$y,$resultarray['testMethod'],1,0,'L',1);
				if($resultarray['testAccredited']==1){
					$pdf->SetFont('ZapfDingbats');
					$pdf->Cell($col_accredited,$y,'4',1,0,'C',1);
					$pdf->SetFont('Arial');
				}else{
					$pdf->Cell($col_accredited,$y,'',1,0,'C',1);
				}
				$pdf->Cell($col_auth_user,$y,$resultarray['userFirst']." ".$resultarray['userLast'],1,0,'L',1);
				if($resultarray['testLabName']==$_SESSION['comp_name']){
					$pdf->SetFont('ZapfDingbats');
					$pdf->Cell($col_inhouse,$y,'4',1,0,'L',1);
					$pdf->SetFont('Arial');
				}else{
					$pdf->Cell($col_inhouse,$y,$resultarray['testLabName'],1,0,'L',1);
				}
				$pdf->Cell($col_modreason,$y,$resultarray['resultModReason'],1,1,'L',1);
			}
			mysqli_free_result($qryresult);
		}
		mysqli_free_result($qryunit);
		
		if($pdfoutput){
			$pdf->Output('file.pdf','D',1);
		}else{
			echo "</table>";
			echo "Total tests: ".$testcount++."<br/>";
		}
		die();
		
	}else{
		echo "No cypher specified";
	}
?>