<?php
$reqnum = $_GET['req'];
$reqnum = str_pad($reqnum, 10, 0, STR_PAD_LEFT);
$sql = "select p.PATNUMBER,
		[Name] = case 
				when p.TITLEID is not null then ISNULL(p.FIRSTNAME,'') + ' ' + ISNULL(p.NAME,'') + ', ' + tx.SHORTTEXT
				else ISNULL(p.FIRSTNAME,'') + ' ' + ISNULL(p.NAME,'')
			end,
		format(p.BIRTHDATE,'dd/MMM/yyyy') as dob,
		age = case
			when year(spr.COLLECTIONDATE) - year(p.BIRTHDATE) > 0 then (
				case 
				when format(p.BIRTHDATE,'MMdd')=format(spr.COLLECTIONDATE,'MMdd') then cast(DATEDIFF(YEAR,p.BIRTHDATE, spr.COLLECTIONDATE) as varchar) + 'Y'
				else cast( DATEDIFF(hour,p.BIRTHDATE, spr.COLLECTIONDATE)/8766 as varchar) + 'Y' end 
			)
			when month(spr.COLLECTIONDATE) - month(p.BIRTHDATE) > 0 then cast( DATEDIFF(MM,p.BIRTHDATE,spr.COLLECTIONDATE) as varchar) + 'M'
			else cast ( floor ( ( day(spr.COLLECTIONDATE) - day(p.BIRTHDATE) ) / 7) as varchar ) + 'W'
		end,
		[Gender] = case
			when p.SEX = 1 then 'M'
			when p.SEX = 2 then 'F'
			else ''
			end,
		spr.HOSTORDERNUMBER
		from SP_REQUESTS spr
		left join PATIENTS p on spr.PATID=p.PATID
		left join DICT_TEXTS tx on tx.TEXTID=p.TITLEID
		where spr.SP_ACCESSNUMBER='$reqnum'";
$stmt = sqlsrv_query( $conn1, $sql );
if( $stmt == false) { die( print_r( sqlsrv_errors(), true) ); }
$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ;
$patnum = $row[0];
$patnum = substr($patnum,14);
//$patnum = str_pad(substr($row[0],5),17," ");
$patname = $row[1];
$dob = $row[2];
$age = $row[3];
$sex = $row[4];
$hospnum = $row[5];
$date = date("d/M/Y H:i");
$bar = "[
N
OD
q400
Q224,24+0
I8,A,001
D10
A10,3,0,3,1,1,N,\"$patname\"
A10,27,0,2,1,1,N,\"$sex $dob $age\"
A225,27,0,3,1,1,N,\"$reqnum\"
B120,50,0,1,2,8,90,N,\"$reqnum\"
A80,150,0,2,2,1,N,\"$hospnum\"
A10,195,0,1,1,1,N,\"HIS : $hospnum\"
A190,190,0,2,1,1,N,\"$date\"
P1\n]";

$sql = "select SAMPCODE, SHORTTEXT, TESTS from v_sp_reqtube where SP_ACCESSNUMBER='$reqnum'";
$stmt = sqlsrv_query( $conn2, $sql );
if( $stmt == false) { die( print_r( sqlsrv_errors(), true) ); }
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC)) {
$sampcode = $row[0];
$tubename = $row[1];
$tests = $row[2];
$tubeid = $sampcode.substr("$reqnum",5,5);
/*$bar .= "[
N
OD
q400
Q224,24+0
I8,A,001
D10
A10,3,0,3,1,1,N,\"$patname \"
A10,27,0,2,1,1,N,\"$sex $age\"
B160,50,0,1,2,8,90,N,\"$tubeid\"
A380,27,5,3,1,1,N,\"$tubeid\"
A10,80,0,2,1,2,R,\"$tubename\"
A10,150,0,2,1,1,N,\"$tests\"
A10,180,0,1,1,1,N,\"LIS : $reqnum\"
A10,195,0,1,1,1,N,\"HIS : $hospnum\"
A190,190,0,2,1,1,N,\"$date\"
P1
]";
*/
$bar .= "[
N
OD
q400
Q224,24+0
I8,A,001
D10
A10,3,0,3,1,1,N,\"$patname \"
A10,27,0,2,1,1,N,\"$sex $age\"
B130,50,0,1,2,8,90,N,\"$tubeid\"
A380,27,5,3,1,1,N,\"$tubeid\"
A10,80,0,2,1,2,R,\"$tubename\"
A10,150,0,2,1,1,N,\"$tests\"
A10,180,0,1,1,1,N,\"LIS : $reqnum\"
A10,195,0,1,1,1,N,\"HIS : $hospnum\"
A190,190,0,2,1,1,N,\"$date\"
P1
]";
}

$handle = fopen("./file.txt","w+");
fwrite($handle,$bar);
fclose($handle);
exec($command);
?>
Barcode Printed
<script>
window.close();
</script>