<?php
function getHost($conn,$ACCESSNUMBER) {
	$sql = "select EXTERNALORDERNUMBER from REQUESTS where ACCESSNUMBER='$ACCESSNUMBER'";
	$stmt = sqlsrv_query( $conn, $sql );
	if( $stmt == false) { die( print_r( sqlsrv_errors(), true) ); }
	$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC);
	$HOSTNUMBER = $row[0];
	return $HOSTNUMBER;	
}
include("config.php");
//include("_function.php");
$ACCESSNUMBER = $_GET['acc'];
$HOSTNUMBER = getHost($conn, $ACCESSNUMBER);
echo "$HOSTNUMBER";
/*
$result = getResult($conn, $ACCESSNUMBER,$eng);
$info = getData2($conn,$ACCESSNUMBER);
$notes = getNotes($conn, $ACCESSNUMBER);
$collData = getCollData($conn, $ACCESSNUMBER);
$recvData = getRecvData($conn, $ACCESSNUMBER);
$noSample = getNoSample($conn,$ACCESSNUMBER);
if( $noSample == '' ) {
	$status = getStatus($conn, $ACCESSNUMBER);
} else {
	$status = "PENDING";
}
$valBy = getValBy($conn, $ACCESSNUMBER);

if(!isset($_GET['date'])) { $date = date('d-m-Y H:i'); }
else { $date = $_GET['date']; } 
$npage = count($result);
$i=1;
$raw ='';
$pdf ='';
$tmp = "<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<link rel='stylesheet' href='normalize.min.css' />";
$raw .= $tmp; $pdf .= $tmp; $tmp = '';
$raw .= "\r\n<link rel='stylesheet' href='style.css' />"; 
$pdf .= "\r\n<link rel='stylesheet' href='pdf.css' />";
$tmp = "</head>
<body style='-webkit-print-color-adjust:exact;'>";
$raw .= $tmp; $pdf .= $tmp; $tmp = '';

foreach($result as $page) {
	$tmp .= "<div id='page'>
	<div id='pagetop' style='height:0.01cm'> </div>";
	if($preview==1) { $tmp.= "<div style='font-size:30px'>preview only do not print</div>" ; }
	$raw .= $tmp; $pdf .= $tmp; $tmp = '';
	$pdf .= "<img src='http://glenlis/spooler_db/gleneagleshdr.png' class='img'/>";
	$tmp .= "<div id='dinfo'>
	$info
	</div>
	<div id='dresult'>
	<table class='result'>
		<colgroup>
			<col style='width:26%;'></col> 
			<col style='width:10%;'></col> 
			<col style='width:15%;'></col> 
			<col style='width:12%;'></col> 
			<col style='width:10%;'></col> 
			<col style='width:15%;'></col> 
			<col style='width:12%;'></col> 
		</colgroup>
		<tr> <th rowspan='2' style='border-right:solid 1px black; border-bottom:solid 1px black;'>TEST</th>  
			<th colspan='3' style='border-right:solid 1px black;'>CONVENTIONAL</th> <th colspan='3'>INTERNATIONAL</th> </tr>
		<tr style='border-bottom:solid 1px black;'> <th>RESULT</th> <th>REF. RANGES</th> <th style='border-right:solid 1px black;'>UNIT</th> <th>RESULT</th> <th>REF. RANGES</th> <th>UNIT</th> </tr>
		$page
	";
	// lastpage show note
	if($i != $npage) {
		$tmp .="</table>";
	} else { 
		$tmp .= "$noSample</table>
		<table> <tr><td>Note :</td> <td><pre>$notes</pre></td></tr>
		</table>";
	}
	$tmp .= "</div>";

	$tmp .= "<div id='footer'>
		<table class='footer'>
			<tr> <td>";
	if($i == $npage) { $tmp .= "Status : $status"; }
	$tmp .= "<pre class='small'>Collected on $collData\r\nReceived on $recvData</pre>
	Page $i/$npage Printed By : $valBy $date </td>";
	$raw .= $tmp; $pdf .= $tmp; $tmp = '';
	
	$tmp .="
	<td class='right'><pre>

	(__________________)
	Authorised Signature
	</pre></td>";
	$raw .= $tmp; $tmp = '';
	$pdf .= "<td class='right'><pre><b>&rdquo;This result is valid without signature.&rdquo;</b></pre></td>";

	$tmp .="
			</tr>
		</table>
	</div>
	";
	$raw .= $tmp; $pdf .= $tmp; $tmp = '';

	$pdf .="<img src='http://glenlis/spooler_db/gleneaglesftr.png' class='img'/>"; 
	$tmp .= "</div>";
	$raw .= $tmp; $pdf .= $tmp; $tmp = '';
	$i+=1;
}

$tmp .="</body>";
$raw .= $tmp; $pdf .= $tmp; $tmp = '';
echo $raw;

if($preview != 1) {
	//pdf
	$file = fopen("process_pdf/$HOSTNUMBER.html","w+");
	fwrite($file, $pdf);
	fclose($file);
	//archive

	$folder = date('Ym');
	$date = date('YmdHi');
	//$file = fopen("archive/$date"."_$HOSTNUMBER.html","w+");
	$filename = "archive/$folder/$date"."_$HOSTNUMBER.html";
	$dirname = dirname($filename);
	if (!is_dir($dirname)) { mkdir($dirname, 0777, true); }
	$file = fopen("archive/$folder/$date"."_$HOSTNUMBER.html","w+");
	fwrite($file, $pdf);
	fclose($file);
	//oru
	$file = fopen("process_oru/$ACCESSNUMBER.oru","w+");
	$date = date('Y-m-d H:i');
	fwrite($file, "$ACCESSNUMBER\r\n$HOSTNUMBER\r\n$date\r\n$status");
	fclose($file);
}
*/
?>