<?php 
include('../config/config.php');
include("../classes/classShift.php");
include("../classes/classDoctor.php");
include("../classes/classVisits.php");
include('../classes/classPatient.php');
include("../classes/classImages.php");
include('../classes/classTransaction.php');
include('../classes/classSaleInvoice.php');
include('../classes/classProductClosingReconcilation.php');

/*echo '<pre>';
print_r($_SESSION);
echo '</pre>';
*/
################################################################################
#########################Pagination Starts here. {########################
################################################################################
$array['item_count']=0;
$retrieveVisitOb = new classVisits;

if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==9)
{
	$retrieveVisitOb->setLocationID($_SESSION['security']['location_id']);
	$array['item_count'] = $retrieveVisitOb->retrieveCountAllVisits($_SESSION['print']['from'],$_SESSION['print']['to']);
}
elseif($_SESSION['security']['userrole_id']==4)
{
	$retrieveDocOB = new classDoctor;
	$retrieveDocOB->setLocationID($_SESSION['security']['location_id']);
	$retrieveDocOB->setCreatedBy($_SESSION['security']['user_id']);
	$doc_array = $retrieveDocOB->retrieveSpecificDoctors();
	
	$retrieveVisitOb->setDoctorID($doc_array[0]['doctor_id']);
	$array['item_count'] = $retrieveVisitOb->countVisitsByDoctorID($_SESSION['print']['from'],$_SESSION['print']['to']);
}
elseif($_SESSION['security']['userrole_id']==5)
{
	$retrieveDocOB = new classDoctor;
	$retrieveDocOB->setLocationID($_SESSION['security']['location_id']);
	$retrieveDocOB->setAssistantID($_SESSION['security']['user_id']);
	$doc_array = $retrieveDocOB->retrieveAssistantDoctors();
	
	if(isset($doc_array[0]['doctor_id']))
	{
		$array['item_count'] = 0;
		for($d=0;$d<count($doc_array);$d++)
		{
			$retrieveVisitOb->setDoctorID($doc_array[$d]['doctor_id']);
			$array['item_count'] += count($retrieveVisitOb->retrieveVisitsByDoctorID($_SESSION['print']['from'],$_SESSION['print']['to']));
		}
		
	}
	/*echo '<pre>';
	print_r($doc_array);
	echo '</pre>';*/
	
}

if(isset($_GET['page']) && isset($_GET['items']))
{
	if($array['item_count'] > $_GET['items'])
	{
		$array['pages'] = ceil($array['item_count']/$_GET['items']);
		if($_GET['page'] >$array['pages'])
		{
			$_GET['page'] = 1;
		}
		$array['current_page'] = $_GET['page'];
		if($_GET['page'] > 1)
		{
			$starting_record = $_GET['items']*($_GET['page']-1);
			$ending_record = $_GET['items'];
			$array['limit'] = $starting_record . "," . $ending_record;
			$limit = $array['limit'];
		}
		else
		{
			$limit = $_GET['items'];
		}
	}
	else
	{
		$array['current_page'] = $_GET['current_page'] = 1;
		$array['pages'] = 1;
		$array['limit'] = $_GET['items'];
		$limit = $array['limit'];
	}
}
else
{
	if($array['item_count'] > 20)
	{
		$array['pages'] = ceil($array['item_count']/20);
	}
	else
	{
		$array['pages'] = 1;
	}
	$array['current_page'] = 1;
	$array['limit'] = 20;
	$limit = $array['limit'];
}

$_SESSION['pagination']['item_count']=$array['item_count'];
$_SESSION['pagination']['pages']=$array['pages'];
$_SESSION['pagination']['current_page']=$array['current_page'];
$_SESSION['pagination']['limit']=$limit;

################################################################################
######################### Pagination Ends here. }##########################
################################################################################

################## Search Patients Start ####################################
try
{
	if(@$_SESSION['print']['by_id']!=''||@$_SESSION['print']['by_name']!=''||@$_SESSION['print']['by_phone']!='')
	{
		$retrievePatientOb = new classVisits;
		$visits_array = $retrievePatientOb->retrieveVisitByPatient($_SESSION['print']['by_id'],$_SESSION['print']['by_name'],$_SESSION['print']['by_phone'],$_SESSION['security']['location_id'],$_SESSION['print']['from'],$_SESSION['print']['to']);
	}
	else
	{
		$retrieveVisitOb = new classVisits;
		
		if($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==9)
		{
			$retrieveVisitOb->setLocationID($_SESSION['security']['location_id']);
			$visits_array = $retrieveVisitOb->retrieveAllVisits($_SESSION['print']['from'],$_SESSION['print']['to'],$limit);
		}
		elseif($_SESSION['security']['userrole_id']==4)
		{
			$retrieveDocOB = new classDoctor;
			$retrieveDocOB->setCreatedBy($_SESSION['security']['user_id']);
			$retrieveDocOB->setLocationID($_SESSION['security']['location_id']);
			$doc_array = $retrieveDocOB->retrieveSpecificDoctors();
			
			$retrieveVisitOb->setDoctorID($doc_array[0]['doctor_id']);
			$visits_array = $retrieveVisitOb->retrieveVisitsByDoctorID($_SESSION['print']['from'],$_SESSION['print']['to'],$limit);
		}
		elseif($_SESSION['security']['userrole_id']==5)
		{
			$retrieveDocOB = new classDoctor;
			$retrieveDocOB->setAssistantID($_SESSION['security']['user_id']);
			$retrieveDocOB->setLocationID($_SESSION['security']['location_id']);
			$doc_array = $retrieveDocOB->retrieveAssistantDoctors();
			
			if(isset($doc_array[0]['doctor_id']))
			{
				for($v=0;$v<count($doc_array);$v++)
				{
					$retrieveVisitOb->setDoctorID($doc_array[$v]['doctor_id']);
					$temp_visit_array = $retrieveVisitOb->retrieveVisitsByDoctorID($_SESSION['print']['from'],$_SESSION['print']['to']);
					
					for($vis=0;$vis<count($temp_visit_array);$vis++)
					{
						$visits_array[] = $temp_visit_array[$vis];
						
					}
					
					
				}
				
			}
			
			
		}
		
	}
	
	$total_patients = 0;
	$total_payables = 0;
	$total_payments = 0;
	if(isset($visits_array[0]['visit_id'])&&$visits_array[0]['visit_id']!='')
	{
		for($int=0;$int<count($visits_array);$int++)
		{
			$total_patients ++;
			$total_payables += $visits_array[$int]['payable'];
			$total_payables += $visits_array[$int]['payment'];
		}
	}

}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}

/*echo '<pre>';
print_r($visits_array);
echo '</pre>';*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/print.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link rel="shortcut icon" href="../images/favicon.png">
    <title>Med2TEC Hospital Management Information System</title> 
    <link href="../css/print.css" rel="stylesheet">
    <link href="../datepicker/jquery-ui.css" rel="stylesheet" >  
    <script type="text/javascript" src="../datepicker/jquery-1.9.1.js"></script>
	<script type="text/javascript" src="../datepicker/jquery-ui.js"></script> 
</head>

<body> 
<div align="right"> 
 <SCRIPT LANGUAGE="JavaScript">  
if (window.print) {
document.write('<form class="no-print"><input type=button name=print value="Print" onClick="window.print()"></form>');
}
</script></div>

<section class="wrapper">
<div style="background:#FFF; padding:0px 0px 0; height:auto;">
  <table cellspacing="0" cellpadding="0" width="100%" border="0" style="font-size:11px;">
    <tbody>
      <tr>
        <td align="left" style="background:#fff;"><img alt="" src="../images/org_print_logos/<?php echo $_SESSION['security']['org_id']?>.png" style="width:100%;"></td>
      </tr>
    </tbody>
  </table>
  
</br>

</div>

<div class="row">
          <section class="panel">
            <header class="panel-heading">
              <h2><!-- InstanceBeginEditable name="title" -->OPD PRESCRIPTIONS<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
  
	<table class="bottomBorder" cellpadding="0" cellspacing="0" width="100%">
                  <thead>
                    <tr>
           			 <th width="10%" align="center">Visit Date</th>
            		 <th width="5%" align="center">ID</th>
            		 <th width="15%" align="left">Patient's Name</th>
           		 	 <th width="10%" align="left">Phone</th>
            		 <th width="15%" align="left">Doctor's Name</th>
            		 <th width="20%" align="center">Medicines</th>
            		 <th width="20%" align="center">Tests & Procedures</th>
                    </tr>
                  </thead>
                  <tbody>
                   <?php
                    if(isset($visits_array[0]['shift_id'])&&$visits_array[0]['shift_id']!='')
                    {
                    	for($v=0;$v<count($visits_array);$v++)
						{
							if($v%2==0){$class = 'gradeX';}{$class = 'gradeC';}
							
							$visit_id = $visits_array[$v]['visit_id'];
							$visit_id_encoded = base64_encode($visit_id);
							$retrieveVisitPrecOb = new classVisits;
							if($retrieveVisitPrecOb->checkMedicineLabTestProcedureInPrescription($visit_id,'medicine'))
							{
							$patient_id = $visits_array[$v]['patient_id'];
							$patient_id_encoded = base64_encode($patient_id);
							
							$doctor_id = $visits_array[$v]['doctor_id'];
							$doctor_id_encoded = base64_encode($visits_array[$v]['doctor_id']);
							
							$patienthistory_link = "<a href='patienthistory.php?patientid=$patient_id_encoded&visitid=$visit_id_encoded'>Prescription</a>";
							
							
							$patientOb = new classPatient;
							$patientOb->setPatientAutoID($patient_id);
							$patient_info = $patientOb->retrievePatientByPatientID();
							
							$doctorOb = new classDoctor;
							$doctorOb->setDoctorID($visits_array[$v]['doctor_id']);
							$doctor_detail = $doctorOb->retrieveDoctorByDoctorID();
							
							$doctor_name = $doctor_detail[0]['title'].'. '.$doctor_detail[0]['doc_fname'].' '.$doctor_detail[0]['doc_lname'];
							
							$retrieveVisitPrecOb = new classVisits;
							$retrieveVisitPrecOb->setVisitID($visit_id);
							$prec_array = $retrieveVisitPrecOb->retrievePrescription();
							
							$saleInvOb = new classSaleInvoice;
							$inv_data = $saleInvOb->retrieveInvoiceDetailByVisitID($visit_id);
							
				
							
							?>
							<tr class="<?php echo $class;?>">
                                <td align="center"><?php echo date('d M Y',$visits_array[$v]['visit_date']);?></td>
                                <td align="center"><?php echo $patient_info[0]['patient_id'];?></td>
                                <td align="left"><?php echo $patient_info[0]['patient_fname'].' '.$patient_info[0]['patient_lname'];?></td>
                                <td align="left"><?php echo $patient_info[0]['phone'];?></td>
                                <td align="left"><?php echo $doctor_name;?></td>
                                <td align="left">
                                <?php
								for($prm=0;$prm<count($prec_array);$prm++)
								{
									if($prec_array[$prm]['product_id']!=0)
									{	
										$retreiveMedOb = new classVisits;
										$retreiveMedOb->setMedicineID($prec_array[$prm]['product_id']);
										$med_info = $retreiveMedOb->retrieveMedicineByMedicineID();
										
										echo ucwords($med_info[0]['product_name'])." - ".$prec_array[$prm]['dosage'].'<br>';
									}
								}
								?>
                                </td>
                                <td align="left">
                                <?php
								for($prm=0;$prm<count($prec_array);$prm++)
								{
									 if($prec_array[$prm]['labtest_id']!=0)
									 {	
										 $retreiveMedOb = new classVisits;
										 $retreiveMedOb->setLabTestID($prec_array[$prm]['labtest_id']);
										 $labtest_info = $retreiveMedOb->retrieveLabTestsByLabTestID();
															
										 echo ucwords($labtest_info[0]['labtest_name']).'<br>';
									}
								}
								for($prm=0;$prm<count($prec_array);$prm++)
								{ 
									if($prec_array[$prm]['procedure_id']!=0)
									{	
										$retreiveMedOb = new classVisits;
										$retreiveMedOb->setProcedureID($prec_array[$prm]['procedure_id']);
										$procedure_info = $retreiveMedOb->retrieveProcedureByProcedureID();
											
										echo ucwords($procedure_info[0]['procedure_name']).'<br>';
									}
								}
								?>
                                </td>
							</tr>
                            
                           
							<?php
							}
						}
                    }
					else
					{
						?>
							<tr>
								<table>
									<tr>
										<td>No entries found!</td>
									</tr>
								</table>
							</tr>
						<?php
					}
                    ?>
                  </tbody>
                  <tfoot>
                  </tfoot>
                </table>
  <!-- page end-->
  
<!-- InstanceEndEditable -->
</section>
</div>

<!--<p align="center" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; margin-top:260px;"><strong><!--317 A-1, Nursery Stop, Township, Lahore. 
Ph: 042-35151666</strong><br />
For Appointment call at 042-35151666 (bring your Patient ID Card) </p>-->

   <div class="sidepop" style="float:right;">
    <div class="sidepopbutton stemp"><div style="color: rgb(0, 0, 0); text-align: right; font-size: 10px; margin-top: 4px; position: absolute; right: 180px;">© Software by Softusion Inc. 0322 6155 219</div>   
    </div>
</div>

<!--<div class="sidepop" style="float:right;">
    <div class="sidepopbutton stemp"><div style="color:#000; text-align:right; font-size:9px;">© Software designed by Softusion Inc. Contact us at 0322 6155 219</div>   
    </div>
</div>-->


          
</body>
<!-- InstanceEnd --></html>