<?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');


/*echo '<pre>';
print_r($_SESSION);
echo '</pre>';*/

if(isset($_GET['shiftid'])&&isset($_GET['doctorid']))
{
	$_SESSION['temp']['shiftid'] = base64_decode($_GET['shiftid']);
	$_SESSION['temp']['doctorid'] = base64_decode($_GET['doctorid']);
	header("Location:mgmtvisits.php");
}

if(isset($_SESSION['temp']['shiftid'])&&$_SESSION['temp']['shiftid']!='')
{
	$shift_id = $_SESSION['temp']['shiftid'];
}
if(isset($_SESSION['temp']['doctorid'])&&$_SESSION['temp']['doctorid']!='')
{
	$doctor_id = $_SESSION['temp']['doctorid'];
}




/////////////////////////////Add New Visit ////////////////////////

if(isset($_POST['patient_type'])&&$_POST['patient_type']!='')
{	
	if($_POST['patient_type']=='out_patient')
	{
		$patient_id = "0";
	}
	else
	{
		$patient_id = $_POST['patient_type'];
	}
	
	try
	{
		$visitCreateOb = new classVisits;
		$visitCreateOb->setDoctorID($doctor_id);
		$visitCreateOb->setShiftID($shift_id);
		$visitCreateOb->setPatientID($patient_id);
		$visitCreateOb->setLocationID($_SESSION['security']['location_id']);
		$visitCreateOb->execute();
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}	
}

/////////////////////////////Delete shift////////////////////////

if(isset($_GET['del_visit']))
{
	$del_visit = base64_decode($_GET['del_visit']);
		
	try
	{	
		$delVisitOb = new classVisits;
		$delVisitOb->setVisitID($del_visit);
		$delVisitOb->deleteVisit();
		
		header("Location:mgmtvisits.php");
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}	
}


///////////// retrieve Visits ////////////////////////////////////////

try
{
	$retrieveVisitOb = new classVisits;
	$retrieveVisitOb->setShiftID($shift_id);
	$retrieveVisitOb->setDoctorID($doctor_id);
	$visits_array = $retrieveVisitOb->retrieveVisits();
	
	$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_payments += $visits_array[$int]['payment'];
		}
	}

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

///////////// retrieve shift Detail ////////////////////////////////////////

try
{
	$retrieveShiftOb = new classShift;
	$retrieveShiftOb->setShiftID($shift_id);
	$shift_array = $retrieveShiftOb->retrieveShiftByShiftID();
	
	
	$shift_created_date = date('d M Y , l',$shift_array[0]['date']);
	$shift_created_time = date('h:i A',$shift_array[0]['start_time']);
	
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}	


///////////// retrieve doctor Detail ////////////////////////////////////////

try
{
	$doctorOb = new classDoctor;
	$doctorOb->setDoctorID($doctor_id);
	$doctor_detail = $doctorOb->retrieveDoctorByDoctorID();
	
	$doctor_name = $doctor_detail[0]['title'].'. '.$doctor_detail[0]['doc_fname'].' '.$doctor_detail[0]['doc_lname'];
	
	$doctor_acc_id = $doctor_detail[0]['account_id'];
	
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}	

///////////// retrieve patients ////////////////////////////////////////

/*try
{
	$retrievePatientOb = new classPatient;
	$retrievePatientOb->setLocationID($_SESSION['security']['location_id']);
	$patients_array = $retrievePatientOb->retrievePatientByLocationID();
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}*/
///////////// retrieve Accounts ////////////////////////////////////////
try
{
	$tranOb = new classTransaction;
	$account_data = $tranOb->retrieveAccountHeadAndAccountByID(1,$doctor_acc_id);	
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}	

///////////// retrieve medical issues ////////////////////////////////////////
try
{
	$medIssueOb = new classVisits;
	$medIssueOb->setOrganizationID($_SESSION['security']['org_id']);
	$med_issue_array = $medIssueOb->retrieveMedicalIssues();
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}
?>
<!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" -->DAILY PATIENTS' VISITS<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
            
<table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
              
                <td width="20%" valign="bottom"></td>
                <td width="80%" align="right">
                <div class="col-md-5" style="float: right;">
            <div class="profile-nav alt">
              <section class="panel">
                <div class="user-heading alt clock-row terques-bg">
                  <h1><?php echo $doctor_name;?></h1>
                  <p class="text-left"><?php echo $shift_created_date?></p>
                  <p class="text-left">Started at: <?php echo $shift_created_time;?></p></br>
                  <p class="text-left"></p>
                  <span>Total Patients: <div style="float:right;"><?php echo $total_patients;?></div></span></br>
                  <span>Total Payables: <div style="float:right;">Rs. <?php echo number_format($total_payables,2);?></div></span></br>
                  <span>Total Payments: <div style="float:right;">Rs. <?php echo number_format($total_payments,2);?></div></span>
                </div>
              </section>
            </div>
          </div>
          		</td>
                
              </tr>
            </table>

<br>
 <?php
			if(isset($error_message)&&$error_message!='')
			{
				?>
              <span class="message_red"><?php echo $error_message;?></span>
              <?php
			}
			?>
				<table  class="bottomBorder" cellpadding="0" cellspacing="0" width="100%">
                  <thead>
                    <tr>
            			<th width="5%" align="center">Token</th>
            			<th width="10%" align="center">Time</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="10%" align="left">Issue</th>
            			<th width="10%" align="center">Amount Payable</th>
            			<th width="10%" align="center">Payment</th>
            			<th width="10%" align="center">Balance</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);
							
							$patient_id = $visits_array[$v]['patient_id'];
							$patient_id_encoded = base64_encode($patient_id);
							
							$patientOb = new classPatient;
							$patientOb->setPatientAutoID($patient_id);
							$patient_info = $patientOb->retrievePatientByPatientID();
							
							$retrieveVisitPrecOb = new classVisits;
							$retrieveVisitPrecOb->setVisitID($visit_id);
							$prec_array = $retrieveVisitPrecOb->retrievePrescription();
							
							$retrieveVisitPrecOb->setMedicalIssueID($visits_array[$v]['med_issue_id']);
							$medical_issue_array = $retrieveVisitPrecOb->retrieveMedicalIssueByID();
							
							if ($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==4||$_SESSION['security']['userrole_id']==9)
							{
								$patienthistory_link = "<a href='opdhistory.php?patientid=$patient_id_encoded&visitid=$visit_id_encoded'>Clinical History</a>";
							}
							else
							{
								$href = 'javascript:Popup("printopdhistory.php?patientid='.$patient_id_encoded.'&visitid='.$visit_id_encoded.'")';	 
								$patienthistory_link = "<a href='$href'>Patient History</a>";
								
							} 
							
							if(isset($visits_array[$v]['zakat'])&&$visits_array[$v]['zakat']=='zakat')
							{
								$payable = $visits_array[$v]['zakat'];
							}
							else
							{
								$payable = 'Rs. '.$visits_array[$v]['payable'];
							}
							
							if($visits_array[$v]['pay_status']!= "paid" && $visits_array[$v]['zakat']!='zakat')
							{
								
                               $payment = "<span id='$v'>Receive Payment</span>";
                                
							  //$payment =  "<a href='transactopd.php?patientid=$patient_id_encoded&visitid=$visit_id_encoded'>Receive Payment</a>";
							} 
							else 
							{ 
								$payment = "Rs. ".$visits_array[$v]["payment"]; 
							}
							
							$balance = 'Rs. '.$visits_array[$v]['balance'];
							
							
							if($visits_array[$v]['pay_status']=="" && $visits_array[$v]['temp']=="" && $visits_array[$v]['bp']=="" && $visits_array[$v]['bsr']=="" && $visits_array[$v]['complaint']=="" && $visits_array[$v]['diagnosis']=="") 
							{
								 $delete = "<a href='?del_visit=$visit_id_encoded'><img src='../images/delete.png'></a>";
							}
							else
							{
								$href = 'javascript:Popup("printopdtoken.php?patientid='.$patient_id_encoded.'&visitid='.$visit_id_encoded.'")';
								$delete = "<a href='$href'>Print</a>";
							}
							
							?>
							
							<?php 
							if(($_SESSION['security']['userrole_id']==1||$_SESSION['security']['userrole_id']==2||$_SESSION['security']['userrole_id']==3||$_SESSION['security']['userrole_id']==9)&&($prec_array[0]['product_id']=="")) 
							{
								
								 ?><tr><?php
							}
							elseif(($_SESSION['security']['userrole_id']!=1||$_SESSION['security']['userrole_id']!=2||$_SESSION['security']['userrole_id']!=3||$_SESSION['security']['userrole_id']!=9)&&$visits_array[$v]['pay_status']=="")
							{
								
								 ?><tr><?php
							}
							else
							{
								?><tr class="<?php echo $class;?>"><?php
							}
							?>
							
                                <td align="center"><?php echo $visits_array[$v]['token_no'];?></td>
                                <td align="center"><?php echo date('h:i A',$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="center"><?php echo $patient_info[0]['phone'];?></td>
                                <td align="left"><?php echo @ucwords($medical_issue_array[0]['med_issue']);?></td>
                                <td align="right"><?php echo $payable;?></td>
                                <td align="right"><?php echo $payment;?></td>
                                <td align="right" ><?php echo $balance;?></td>
							</tr>
                            
							<?php
						}
                    }
					else
					{
						?>
							
						<?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>