<?php
session_start();                                     /* Here session is start */
require_once("../codelibrary/inc/variables.php"); /* This file is required for database connection.  */
require_once("../codelibrary/inc/functions.php"); /* This file contain all function.  */
@extract($_REQUEST);                                 /* This is used for get the request. */
validate_user();                                     /* Validate user for open/access this page with login. */

/* log4php start here */
include('../log4php/Logger.php');
Logger::configure('../logger-config.xml');
$logger = Logger::getLogger("-OPD Payment Print-");
/* log4php end here */

if($_POST['printHalf']==1)
{
	$printHalf=1; 
}

if($_POST['testIds'])
{
	 
	$count=count($testIds);
	for($i=0;$i<$count;$i++)
		{
			$testBlock= mysql_query_db("select * from health_pathology where groupId='".$testIds[$i]."' and isGroup=1");
			$num=mysql_num_db($testBlock);
			if($num==0){
			//normal test then need its group
			$noHeadGp=mysql_query_db("select * from health_pathology where testId='".$testIds[$i]."' and groupId!=0");
			$numHeadGp=mysql_num_db($noHeadGp);
			if($numHeadGp==0){
				$alltest[]=$testIds[$i];
			}
			else{
				$fnoHeadGp=mysql_fetch_db($noHeadGp);
				$alltest[]=$fnoHeadGp['groupId'];
			}
			
			}
			else{
			while($fetchall=mysql_fetch_db($testBlock)){
					$subtestBlock=mysql_query_db("select * from health_pathology where testId='".$fetchall['testId']."'");
					$fsubtestBlock=mysql_fetch_db($subtestBlock);
					$alltest[]=$fsubtestBlock['testId'];
				}	
			}	
		}
}
//print_r($alltest);  
$count=count($alltest);
if($count!=0){
$alltest=implode(",",$alltest);
//print_r($alltest);
}
else{
$alltest[]=0;	
$alltest=implode(",",$alltest);	
}

if($reportId){
$report = mysql_query_db("select * from health_pathology_report_add where reportId='$reportId'");
$freport=mysql_fetch_db($report);
$source=$freport['source'];	
if($source=="OPD"){
$pay=mysql_query_db("select * from health_investigation_payment where receiptNo='".$freport['refId']."'");
$fpay=mysql_fetch_db($pay);
$regDate=$fpay['paymentDate'];
$sqldeptt=mysql_query_db("select depttName from  health_department_diagnosis where id='".$fpay['depttId']."'");
$depttline=mysql_fetch_db($sqldeptt);
$sqldoctor=mysql_query_db("select * from health_reffered_doctor_path where doctorId='".$fpay['doctorId']."'");
$doctorline=mysql_fetch_db($sqldoctor);
$qual= mysql_query_db("select * from health_qualification where qualificationId='".$doctorline['qualificationId']."'");
$fqual= mysql_fetch_db($qual);

$sql=mysql_query_db("select * from health_patient where patientId='".$fpay['patientId']."'");
$line=mysql_fetch_db($sql);
$user= mysql_query_db("select * from health_user where userId='".$fpay['updateBy']."'");
$fuser= mysql_fetch_db($user);
}
else{
$pay=mysql_query_db("select * from health_diagnosis_ipd where id='".$freport['refId']."'");
$fpay=mysql_fetch_db($pay);
$regDate=$fpay['postDate'];
$ipd= mysql_query_db("select * from health_ipd where ipdId='".$fpay['ipdId']."'");
$fipd= mysql_fetch_db($ipd);
$sqldeptt=mysql_query_db("select depttName from  health_department_diagnosis where id='".$fpay['departmentId']."'");
$depttline=mysql_fetch_db($sqldeptt);
$sqldoctor=mysql_query_db("select doctorName from health_doctors where id='".$fipd['doctorId']."'");
$doctorline=mysql_fetch_db($sqldoctor);
$sql=mysql_query_db("select * from health_patient where patientId='".$fipd['patientId']."'");
$line=mysql_fetch_db($sql);
$user= mysql_query_db("select * from health_user where userId='".$fpay['userId']."'");
$fuser= mysql_fetch_db($user);	
}

}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title><?php echo ucfirst($line['patientInitial']." ".$line['patientName']);?></title> <!-- Title-->
        <link href="../css/style.css" rel="stylesheet" type="text/css"> 
        <link href="../css/print-setup-lab.css" rel="stylesheet" type="text/css"> 
		<link href="../awesome/css/font-awesome.css" rel="stylesheet" type="text/css">     <!-- Font awesome link for use font awesome icon-->
        <link href="../awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"> <!-- Font awesome link for use font awesome icon-->
		<script src="../ajax.js"></script>
        <!-- validation css and js start here -->
        <link rel="stylesheet" href="../validation/css/vstyle.css">
        <!-- validation css and js end -->
        <script>
				function _isNumberKey(evt){
                var charCode = (evt.which) ? evt.which : event.keyCode
						if (charCode > 31 && (charCode < 48 || charCode > 57))
                        return false;
						return true;
				}
        </script>
<style media="print">
	  @page {
	  size: auto;
	  margin:0 0 0 0;
		   }
	table { page-break-inside:auto; }
    tr    { page-break-inside:avoid; page-break-after:auto }
    thead { display:table-header-group }
    tfoot { display:table-footer-group }	   	   
</style>
    </head>
    <body onload="myconfig()">
        <div class="full95r">
            <div class="clearfix10"></div>
                <div class="mauto90">
				      <div class="page-header" <?php if($printHalf==1){echo "style='height:170px';";}?>>
                            <div class="full">
                                    <div class="full50 printlogofont36" style="display:table; height:<?php if($printHalf==1){ echo '100px';} else{echo '150px';}?>">
                                        <span style="display:table-cell; vertical-align: middle;">
                                            <?php //_printLogo();?>
                                        </span>
                                    </div>
                                    <div class="full50r txt_right printgrey16" style="display:table; height:<?php if($printHalf==1){ echo '100px';} else{echo '150px';}?>">
                                        <span style="display:table-cell; vertical-align: middle;">
                                            <?php //_hospitalAddress();?>
                                        </span>
                                    </div>
                                </div>
								       
                                        <div class="full95 border_all">
                                            <div class="full40">
                                                <div class="full" style="padding:5px">

												<div class="full printgrey16"><strong>Report Date: </strong> <?php echo date("d-M-Y",strtotime($freport['result_date']));?></div>
                                                <div class="clearfix7"></div>
												<div class="full printgrey16"><strong>Name: </strong> <?php echo ucfirst($line['patientInitial']." ".$line['patientName']);?></div>
                                                <div class="clearfix7"></div>
												<div class="full printgrey16"> <strong> Age / Gen: </strong><?php echo $line['age'].$line['age_type'];?> / <?php echo $line['gender'];?> </div>
                                                <div class="clearfix7"></div>
												
												<!--<div class="full printgrey16"><strong>Reference No:</strong><?php echo $freport['source'];echo"/";echo $freport['refId'];?></div>
                                                <div class="clearfix7"></div>-->
												</div>
                                            </div>
                                            <div class="full40">
                                                <div class="full" style="padding:5px">
												<!--<div class="full printgrey16"><strong>Specimen Date: </strong> <?php echo date("d-M-Y",strtotime($freport['date']));?> &nbsp;<?php echo date("h:i:s A",strtotime($freport['time']));?></div>
                                                <div class="clearfix7"></div>-->
									            <div class="full printgrey16"><strong>Report Time: </strong> <?php echo date("h:i:s A",strtotime($freport['result_time']));?></div>
                                                <div class="clearfix7"></div>
												<div class="full printgrey16"><b>UHID</b>:<?php echo $line['patientId']?> <strong>Report ID:</strong><?php echo date("Y",strtotime($today));?>/<?php echo $reportId; ?></div>
                                                <div class="clearfix7"></div>
												<div class="full printgrey16"><strong>Ref. By: </strong> Dr.<?php if($doctorline['doctorName']!=''){echo $doctorline['doctorName'];} else{echo "NA";}?></div> <!--echo " &nbsp; "; echo "(".$fqual['qualificationName'].")";-->
                                                <div class="clearfix7"></div>
												<!--<div class="full printgrey16"><strong>Print By: </strong> <?php echo $fuser['userName'];?></div>
                                                <div class="clearfix7"></div>
												<div class="full printgrey16">(Identity of the patient can't be verified)</div>
                                                <div class="clearfix7"></div>-->
											 </div>
                                            </div>
											
											<div class="full20 txt_left">
											<?php $path="barcode/images/".$line['patientId'].".png";
											$actual="../opd/".$path;
											?>
											<?php if(file_exists($actual)) {?>
											<img src="../opd/<?php echo $path;?>" style="width:auto;margin-top:10px">
											<?php }?>
											</div>
											</div>
											<div class="clearfix30"></div>
										</div>
										   
                                             <!---common till here--->
											   <table class="full">
												<thead>
												  <tr>
													<td>
													  <!--place holder for the fixed-position header-->
													  <div class="page-header-space" <?php if($printHalf==1){echo "style='height:190px';";}?>></div>
													</td>
												  </tr>
                                            
												</thead>
												<tbody>
											   <tr>
												<td>
												
                                                <div class="full95 page">
												<div class="full"></div>
												<div class="clearfix10"></div>
													
												<table class="full">
												
												 <!--for_general--   >
												 <?php
												 $findgp=mysql_query_db("select distinct groupId from health_pathology_test_results where (groupId in ($alltest) or testId in ($alltest) ) and reportId='$reportId' order by groupId ASC");
												 $master=array();
												 $masterHead=array();
												 $nonGroupHead=array();
												 //$m=0;
												 while($findgroup=mysql_fetch_db($findgp)){
													//group Name
													//echo $findgroup['groupId'];
													$m=0;$h=0;
													$noGroupPrint=0;
													//head for non-group ID
													if($findgroup['groupId']==0){
														$nonGroup=mysql_query_db("select * from health_pathology_test_results where groupId=0");
														while($fnonGroup=mysql_fetch_db($nonGroup)){
															  $nonGroupTest=mysql_query_db("select * from health_pathology where testId='".$fnonGroup['testId']."'");
														      $nonGrouphead=mysql_fetch_db($nonGroupTest);
															  $headId=$nonGrouphead['headId'];
														}
														$interp_mode="single"; //$interp_id=$fnonGroup['testId'];
													}
													else{
													//find actual count all of a gp
													$findCount=mysql_query_db("SELECT count(testId) as allCount from `health_pathology` where `groupId`='".$findgroup['groupId']."' and isGroup='0' and status=1");
													$ffindCount=mysql_fetch_db($findCount);
													 $ffindCount['allCount'];
													//find exist count all of a gp
													$existCount=mysql_query_db("SELECT count(testId) as existCount from `health_pathology_test_results` where `groupId`='".$findgroup['groupId']."' and reportId='$reportId'");
													$fexistCount=mysql_fetch_db($existCount);
													$fexistCount['existCount'];
													if($ffindCount['allCount']>$fexistCount['existCount']){
														$noGroupPrint=1;
													}
													
													$findgpName=mysql_query_db("select * from health_pathology where testId='".$findgroup['groupId']."'");
                                                    $fgroupName=mysql_fetch_db($findgpName);
													$subGroupId=$fgroupName['groupId'];
													$headId=$fgroupName['headId'];
													$interp_mode="group"; $interp_id2=$fgroupName['testId'];
												 }
													
													$findhead=mysql_query_db("select * from health_pathology_head where headId='$headId'");
                                                    $findheadName=mysql_fetch_db($findhead);
													//code for head ID
													if(!(in_array($headId,$masterHead))){
															array_push($masterHead,$headId);
															$h=1;
															//print_r($master);
														}
														else{
															$h=0;
														}?>
														<!--code for head Print-->
														
														<tr class="printgrey18 bold line_hgt10" style="display:<?php if($h!=1){echo "none";}?>">
														<td colspan="4" style="text-decoration:;text-align:center"><?php echo $findheadName['name'];?></td>
														</tr>
													<?php
													
													
													//print main group if it is not exist in table as urine exam.
													$mainHeader=mysql_query_db("select * from health_pathology_test_results where reportId='$reportId' and groupId='$subGroupId'");
                                                    $fnumHeader=mysql_num_db($mainHeader);
													if($fnumHeader==0)
													{   
												        $masterName=mysql_query_db("select * from health_pathology where testId='$subGroupId'");
                                                        $fmasterName=mysql_fetch_db($masterName);
														$m++;
														//echo $subGroupId;
														if(!(in_array($subGroupId,$master))){
															array_push($master,$subGroupId);
															$m=1;
															//print_r($master);
														}
														else{
															$m=0;
														}
													
													}
													?>
													<!--master group name-->
													
													<tr class="printgrey18 bold line_hgt10" style="display:<?php if($m!=1){echo "none";}?>">
													<td colspan="4" style="text-decoration:;text-align:center;display:<?php if($noGroupPrint==1){echo "none";}?>"><?php echo $fmasterName['name'];?></td>
													</tr>
													<!--sub group name-->
													<tr class="printgrey16 bold line_hgt10 black_bck" style="display:<?php if($noGroupPrint==1){echo "none";}?>">
													<td colspan="4" style="text-decoration:;text-align:<?php if($subGroupId==0){echo "center";}else{echo "left";}?>"><?php echo $fgroupName['name'];?></td>
													</tr>
												    <?php 
													$test=mysql_query_db("select * from health_pathology_test_results where reportId='$reportId' and groupId='".$findgroup['groupId']."' and result!=''");
                                                    $num=mysql_num_db($test);
													if($num!=0){
														$c=1;
													while($ftest=mysql_fetch_db($test))
													{
														
														$findName=mysql_query_db("select * from health_pathology where testId='".$ftest['testId']."'");
                                                        $ftestname=mysql_fetch_db($findName);
														
														//method
														$findMethod=mysql_query_db("select * from health_pathology_method where testId='".$ftest['testId']."'");
                                                        $numMethod=mysql_num_db($findMethod);
														$ffindMethod=mysql_fetch_db($findMethod);
														//echo $ftestname['name'];
														//get min-max 
														$testRange=mysql_query_db("select * from health_pathology_range where testId='".$ftest['testId']."'");
													    $findRange=mysql_fetch_db($testRange);
														$out=0;
														$r=floatval($ftest['result']);$min=floatval($findRange['min']);$max=floatval($findRange['max']);
														if($r<$min || $r>$max)
														{
															$out=1;
														}
														if(($findRange['min']=="") || (!is_numeric($findRange['min']))){$out=0;}
														$length=strlen($ftest['result']);
														if($length>30){
														?>
														 <tr class="printgrey16 <?php if($printHalf==1){echo 'line_hgt30';}else{echo 'line_hgt20';}?>">
                                                         <td style="width:30%;"><?php echo $ftestname['name'];?></td>
														 <td colspan="3" style="width:70%;font-weight:<?php if($out==1){echo 'bold';}?>"><?php echo $ftest['result'];?></td>
														 
													    </tr>	
														
														<?php	
														}
														else{
													    ?>
													 <tr class="printgrey16 <?php if($printHalf==1){echo 'line_hgt30';}else{echo 'line_hgt20';}?>">
                                                         <td style="width:30%;"><?php echo $ftestname['name'];?></td>
														 <td style="width:30%;font-weight:<?php if($out==1){echo 'bold';}?>"> : &nbsp;&nbsp;&nbsp;<?php echo $ftest['result'];?></td>
														 <td style="width:10%;"><?php echo $ftestname['unit'];?></td>
														 <td style="width:30%;" class="txt_right"><?php echo $findRange['min'];?><?php if($findRange['max']){echo "-";echo $findRange['max']; };?> <?php //echo $ftestname['unit'];?></td>
														 
													</tr>
													
													<?php
														}
													if($numMethod!=0){?>
													<tr class="printgrey12 line_hgt10 ">
                                                         <td style="width:100%;text-decoration:italic" colspan="4">(<?php echo $ffindMethod['method'];?>)</td>
														 
													</tr>
													<?php 
													 $interpIndiv=mysql_query_db("select * from health_pathology_interp where testId='".$ftest['testId']."' and status=1");
												     $numInterpIndiv=mysql_num_db($interpIndiv);
													 $finterpIndiv=mysql_fetch_db($interpIndiv);
													 ?>
													<tr class="printgrey14" style="display:<?php if($numInterpIndiv==0){echo "none";}?>">
													<td colspan="4"><?php echo $finterpIndiv['comment'];?>
													
													</td>
												    </tr>
													
													<?php } ?>
													
													
												 <?php 
												 if($findgroup['groupId']==0)
												 {
													 $interp_id=$ftest['testId'];
													 $interp=mysql_query_db("select * from health_pathology_interp where testId='$interp_id' and status=1");
												     $numInterp=mysql_num_db($interp);
													 $finterp=mysql_fetch_db($interp);
													 ?>
													<tr class="printgrey14" style="display:<?php if($numInterp==0){echo "none";}?>">
													<td colspan="4"><strong>Interpretation:</strong><?php echo $finterp['comment'];?>
													
													</td>
												    </tr>
													<tr>
													<td colspan="4" class="txt_center">--------------------------------------------------------------------------------</td>
													</tr>
													<?php
												 }
												 
												 }
												 }//end of if
												 
												 $interp=mysql_query_db("select * from health_pathology_interp where testId='$interp_id2' and status=1");
												 $numInterp2=mysql_num_db($interp);
												 $finterp=mysql_fetch_db($interp);
												 ?>
												 <tr class="printgrey14" style="display:<?php if($numInterp2==0 || $noGroupPrint==1){echo "none";}?>">
													<td colspan="4"><strong>Interpretation:</strong><?php echo $finterp['comment'];?>
													
													</td>
												 </tr>
												<tr class="printgrey14">
													<td colspan="4">
													<span style="boreder:1px dotted #000">&nbsp;</span>
													</td>
												 </tr>
												 <?php }
												 $notes=mysql_query_db("select * from health_pathology_notes where reportId='$reportId'");
												 $numNotes=mysql_num_db($notes);
												 $findNotes=mysql_fetch_db($notes);
												 ?>
												 <tr class="printgrey14" style="display:<?php if(trim($findNotes['notes'])==""){echo "none";}?>">
													<td colspan="4"><strong>Note:</strong>
													<?php echo $findNotes['notes'];?>
													</td>
												 </tr>
												 </table>
												 <div class="clearfix5 printgrey16 txt_center">---------------------------------------------End of Report---------------------------------------------</div>
												 <div class="clearfix5"></div>	
													</div>
                                                    </td>
													</tr>
												</tbody>
												<!--gen ends-->
												
												<tfoot>
												
												  <tr>
													<td>
													  
													  <div class="page-footer-space" style="text-align:">
													 
													  <div class="full">
														<div class="full80 printgrey16">&nbsp;</div>
														<div class="full20r printgrey16 txt_right">&nbsp;</div>
													  </div>
													  </div>
													</td>
												  </tr>
												 
												</tfoot>
												  <tr>
													<td>
													  <?php
													      $sett= mysql_query_db("select * from health_path_info");
														  $fsett= mysql_fetch_db($sett);
													   ?>
													    <div class="full">
														<div class="full80 printgrey16"><strong></strong></div>
														<div class="full20r printgrey16 txt_right">
														<?php
														if($fsett['sign']){ 
															   echo '<img src="data:image;base64,'.$fsett['sign'].'" style="width:max-width:100px;max-height:40px;">'; 
															
													    echo "<br>";
														echo $fsett['name']; echo "<br>";
														echo $fsett['qualification']; echo "<br>";
														}
														?>
														</div>
													  </div>
													  
													</td>
												  </tr>
											</table>
                        </div>
		            </div>
			 <script>
			function myconfig(){
					window.print();
					window.onafterprint = function(){
					<?php 
					if($history==1){ ?>
					window.close()	
					<?php }
					else { ?>
					document.location.href = "all-reports.php";
					<?php }?>
					}
				}
            </script>
    </body>
</html>