<?php
if(isset($_GET['preview'])) { $preview = $_GET['preview']; } else { $preview=0; }
if(isset($_GET['eng'])) { $eng = $_GET['eng']; $lang='eng'; } else { $eng = 0; $lang = 'ind'; }
if(isset($_GET['acc'])) { $ACCESSNUMBER = $_GET['acc']; } 

include("config.php");
include("_function.php");
function cutData($text) {
	//$text .= strlen($text);
	if( strlen($text) > 95 ) { 
		$split_text = explode(" ", $text);
		$cut_length = 11;
		$split_text_cut1 = array_slice($split_text, 0, $cut_length);
		$split_text_cut2 = array_slice($split_text, $cut_length, count($split_text));
		$text1 = implode(" ", $split_text_cut1);
		$text2 = implode(" ", $split_text_cut2); 
		$text = $text1."\r\n".$text2;
	}
	return($text);
}
$HOSTNUMBER = getHost($conn, $ACCESSNUMBER);
$result = getResult($conn, $ACCESSNUMBER,$eng);
$info = getData2($conn,$ACCESSNUMBER);
$notes = getNotes($conn, $ACCESSNUMBER);
$others = getOthers($conn,$ACCESSNUMBER, $eng);
$collData = getCollData($conn, $ACCESSNUMBER);
$recvData = getRecvData($conn, $ACCESSNUMBER);
$collData = cutData($collData);
$recvData = cutData($recvData);
$noSample = getNoSample($conn,$ACCESSNUMBER);
if( $noSample == '' ) {
	$status = getStatus($conn, $ACCESSNUMBER);
} else {
	$status = "PENDING";
}
//if($ACCESSNUMBER != '3121849766') {$status = "FINAL";}
if($preview == 0) { 
	$sql = "INSERT INTO GDC_CMOD.dbo.AUDIT_REQUESTS(ACCESSNUMBER, STEPDATE, STEPTYPE, STEPSTATUS) 
		VALUES('$ACCESSNUMBER', GETDATE(), 'PRINT', '$status')";
	$stmt = sqlsrv_query($conn,$sql);
	if( $stmt == false) { die( print_r( sqlsrv_errors(), true) ); }	
}

$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='assets/normalize.min.css' />";
$raw .= $tmp; $pdf .= $tmp; $tmp = '';
$raw .= "\r\n<link rel='stylesheet' href='assets/style.css' />"; 
$pdf .= "\r\n<link rel='stylesheet' href='assets/pdf.css' />";
$tmp = "</head>
<body style='-webkit-print-color-adjust:exact;'>";
$raw .= $tmp; $pdf .= $tmp; $tmp = '';


if($eng==1) { $othertitle = "Non Laboratory Test"; }
else { $othertitle = "Pemeriksaan Non Laboratorium"; }
$countpage = substr_count($result[$npage],"\r");
$countothers = substr_count("$others","\r");
$countline = $countpage + $countothers;
$pageadd = 0;
if($countline > 39) {
	$npage += 1;
	$pageadd = 1;
}

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='assets/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 nosample, others and note
	if($pageadd !=1) {
		if( $i != $npage ) {
			$tmp .="</table>";
		} else { 
			$tmp .= "$noSample </table>";
			$tmp .= "<table> <tr><td>Note :</td> <td><pre>$notes</pre></td></tr>
					</table> <br/>\r\n";
			
			if($others != '' && $countline < 38) {
				$tmp .= "<table><tr><td><b>$othertitle :</b><br/>\r\n";
				$tmp .= "$others</td></tr></table>";
				$others = '';
			}
		}
	} else { // page tambahan = 1
		if( $i != $npage-1 ) {
			$tmp .="</table>";
		} else { 
			$tmp .= "$noSample </table>";
			$tmp .= "<table> <tr><td>Note :</td> <td><pre>$notes</pre></td></tr>
					</table> <br/>\r\n";
		}
	}
	$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='assets/gleneaglesftr.png' class='img img-footer'/>"; 
	$tmp .= "</div>";
	$raw .= $tmp; $pdf .= $tmp; $tmp = '';
	$i+=1;
}

if($others != '') {
	$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='assets/gleneagleshdr.png' class='img'/>";
	$tmp .= "<div id='dinfo'>
	$info
	</div>
	<div id='dresult'>
	<table class='others' style='width:15cm'>
	<tr><td><b>$othertitle : </b><br/>\r\n
	$others</td></tr></table>
	</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 .="<br/><img src='assets/gleneaglesftr.png' class='img img-footer'/>"; 
	$tmp .= "</div>";
}

$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
	/*
	$date = date('YmdHi');
	
	*/
	$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\r\n-");
	fclose($file);
}
?>