<?php
include('../config/config.php');
include('../classes/classDiagnostic.php');
include('../classes/classPatient.php');

$retrieveInvoiceOb = new classDiagnostic;

$to = date('Y-m-d',time());
$from = date('Y-m-d',time());
$from_to_style = 'none';
$invoice_id_style = 'none';
$patient_id_style = 'none';
$referrer_name_style = 'none';

if(isset($_POST['search_type'])&&$_POST['search_type']=='by_date')
{
	$to = $_POST['to'];
	$from = $_POST['from'];
	$invoice_array = $retrieveInvoiceOb->retrieveAllInvoiceDetailByDate($from,$to,$_SESSION['security']['location_id']);
	$from_to_style = 'table-cell';
}
elseif(isset($_POST['search_type'])&&$_POST['search_type']=='by_invoice_id')
{
	$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailByInvoiceIDRanges($_POST['invoice_id'],$_SESSION['security']['location_id']);
	$invoice_id_style = 'inline';	
	
}
elseif(isset($_POST['search_type'])&&$_POST['search_type']=='by_patient_id')
{
	$to = $_POST['to'];
	$from = $_POST['from'];
	
	$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailByPatientName($from,$to,$_POST['patient_id'],$_SESSION['security']['location_id']);
	$patient_id_style = 'inline';	
	$from_to_style = 'table-cell';
}
elseif(isset($_POST['search_type'])&&$_POST['search_type']=='by_referrer_name')
{
	$to = $_POST['to'];
	$from = $_POST['from'];
	
	$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailByReferrerName($from,$to,$_POST['referrer_name'],$_SESSION['security']['location_id']);
	$referrer_name_style = 'inline';	
	$from_to_style = 'table-cell';
}
else
{
	$invoice_array = $retrieveInvoiceOb->retrieveAllInvoiceDetailByDate($from,$to,$_SESSION['security']['location_id']);
	$from_to_style = 'table-cell';	
}




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


?>
<!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" -->DIAGNOSTIC INVOICES<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->

            <table width="100%" border="0" cellspacing="5" cellpadding="5">
            
              
              <tr>
              <form action="" method="post">
        <table width="100%" border="0" cellpadding="1" cellspacing="0">
        
        <tr>
        	<td width="1%" align="right"></td>
            <td width="15%">Search Type: 
            <select name="search_type" id="search_type">
            <option value="by_date" <?php if(isset($_POST['search_type'])&&$_POST['search_type']=='by_date'){echo 'selected';}?>>By Date</option>
            <option value="by_invoice_id" <?php if(isset($_POST['search_type'])&&$_POST['search_type']=='by_invoice_id'){echo 'selected';}?>>By Invoice ID</option>
            <option value="by_patient_id" <?php if(isset($_POST['search_type'])&&$_POST['search_type']=='by_patient_id'){echo 'selected';}?>>By Patient Name</option>
             <option value="by_referrer_name" <?php if(isset($_POST['search_type'])&&$_POST['search_type']=='by_referrer_name'){echo 'selected';}?>>By Referrer Name</option>
            </select>
            </td>
               
                <td width="15%" id="from" style="display: <?php echo $from_to_style;?>;">From: <input type="text" name="from" value="<?php echo $from;?>" id="datepicker" class="form-control" readonly style="display:inline; width:65%;"></td>
                
                <td width="15%" id="to" style="display:  <?php echo $from_to_style;?>;">To: <input type="text" name="to" value="<?php echo $to;?>" id="datepicker1" class="form-control" readonly style="display:inline; width:65%;"> 
                </td>
                
                <td width="15%" id="invoice_id" style="display:  <?php echo $invoice_id_style;?>;">Invoice ID: <input type="text" name="invoice_id" value="<?php echo @$_POST['invoice_id'];?>" class="form-control"  style="width:31%;"> 
               </td>
                
                <td width="15%"  id="patient_id" style="display:  <?php echo $patient_id_style;?>;">Patient Name: <input type="text" name="patient_id" value="<?php echo @$_POST['patient_id'];?>" class="form-control" style="width:31%;"> 
               </td>
                
              	<td width="15%"  id="referrer_name" style="display:  <?php echo $referrer_name_style;?>;">Referrer Name: <input type="text" name="referrer_name" value="<?php echo @$_POST['referrer_name'];?>" class="form-control" style="width:31%;"> 
              </td>
                
                <td width="25%"><input type="submit" class="btn btn-success btn-sm" name="search_by_date" value="Search"></td>
                
        </tr>
        
        </table>
        </form>
              </tr>
              
            </table>
            
            <br>
            <br>
            
            <?php
			if(isset($error_message)&&$error_message!='')
			{
				?>
				<table>
				<tr>
					<td><span class="message_red"><?php echo $error_message;?></span></td>
				</tr>
				<tr><td>&nbsp;</td></tr>
				</table>
				<?php
			}
			?>
            
            <div class="panel-body">
              <div class="adv-table">
                <table class="bottomBorder" cellpadding="0" cellspacing="0" width="100%">
                  <thead>
                    <tr>
					  <th width="5%" align="center">Inv. ID</th>
                      <th width="10%" align="center">Date</th>
                      <th width="20%" align="left">Patient Name</th>
                      <th width="10%" align="right">Invoice Total</th>
                      <th width="10%" align="right">Discount</th>
                      <th width="10%" align="right">Amount Payable</th>
                      <th width="10%" align="right">Payment</th>
                      <th width="10%" align="right">Balance</th>
                      <th width="15%" align="center" class="hidden-phone">Status</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php
				  
					$total_payables = 0;
					$total_payments = 0;

				  if(isset($invoice_array[0]['diaginvoice_id'])&&$invoice_array[0]['diaginvoice_id']!='')
				  {
					  for($inv=0;$inv<count($invoice_array);$inv++)
					  {
						  if($inv%2==0){ $class = 'gradeX';}else{$class = 'gradeC';}
						  
						  if($invoice_array[$inv]['patient_id']!=0)
						  {
							  $retrievePatientOb =  new classPatient;
							  $retrievePatientOb->setPatientAutoID($invoice_array[$inv]['patient_id']);
							  $patient_single_detail = $retrievePatientOb->retrievePatientByPatientID();
							  
							  $patient_name = $patient_single_detail[0]['patient_fname'].' '.$patient_single_detail[0]['patient_lname'];
						  }
						  else
						  {
							 $patient_name = 'Out Door';
						  }
						 
						 
						?>
						<tr class="<?php echo $class;?>">
                        
						  
                          
						  <td align="center"><?php echo $invoice_array[$inv]['diaginvoice_id'];?></td>
                          
						  <td align="center"><?php echo date('d M Y',$invoice_array[$inv]['diaginvoice_date']);?></td>
                          
						  <td align="left"><?php echo $patient_name;?></td>
                          
                          <td align="right">Rs. <?php echo number_format($invoice_array[$inv]['invoice_amount'],2);?></td>
                          
                          <td align="right"><?php echo $invoice_array[$inv]['discount'];?>%</td>
                          
						  <td align="right">Rs. <?php echo number_format($invoice_array[$inv]['amount_payable'],2);?></td>
                          
						  <td align="right">Rs. <?php echo number_format($invoice_array[$inv]['payment'],2);?></td>
                          
						  <td align="right">Rs. <?php echo number_format($invoice_array[$inv]['balance'],2);?></td>
                          
						  <td align="center">
							<?php
                            if($invoice_array[$inv]['payment_status']=="paid") 
                            {
								?>
                                <span>Paid</span>
                                <?php 
                            }
							else
                            {
								?>
                               <span id="<?php echo $inv;?>">Not paid</span>
                                <?php 
                            }
                            ?>
                          
                          </td>
                          
						  
						</tr>
                        <?php
						
						 	$total_payables += $invoice_array[$inv]['amount_payable'];
	  						$total_payments += $invoice_array[$inv]['payment'];
					  }
					  
					  ?>
                      <tr>
                          <td align="center"></td>
                          <td align="center"></td>
                          <td align="center"></td>
                          <td align="right"></td>
                          <td align="right"><strong>Total: </strong></td>
                          <td align="right"><strong>Rs. <?php echo number_format($total_payables,2);?></strong></td>
                          <td align="right"><strong>Rs. <?php echo number_format($total_payments,2);?></strong></td>
                          <td align="right"></td>
                          <td align="center"></td>
                      </tr>
                      <?php
				  }
				  else
				  {
					?>
					<tr>
                        <table>
                            <tr>
                                <td>No entries found!</td>
                            </tr>
                        </table>
					</tr>
					<?php

				  }
				  ?>
			
                    
                  </tbody>
                  <tfoot>
                  </tfoot>
                </table>
              </div>
            </div>
            
  <!-- 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>

<script>
	$(function() {
		
		$("#datepicker").datepicker({
		showOn: "button",
		buttonImage: "../images/calendar.gif",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd"
		});
		
		$("#datepicker1").datepicker({
		showOn: "button",
		buttonImage: "../images/calendar.gif",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		dateFormat: "yy-mm-dd"
		
		});
	
	});
	$("#search_type").change(function() {
	
		var search_type = $("#search_type").val();
		
		if(search_type=='by_date')
		{
			 $("#from").css('display','table-cell');
			 $("#to").css('display','table-cell');
			 $("#invoice_id").css('display','none');
			 $("#patient_id").css('display','none');
			 $("#referrer_name").css('display','none');
		}
		if(search_type=='by_invoice_id')
		{
			 $("#from").css('display','none');
			 $("#to").css('display','none');
			 $("#invoice_id").css('display','inline');
			 $("#patient_id").css('display','none');
			 $("#referrer_name").css('display','none');
		}
		if(search_type=='by_patient_id')
		{
			 $("#from").css('display','table-cell');
			 $("#to").css('display','table-cell');
			 $("#invoice_id").css('display','none');
			 $("#patient_id").css('display','inline');
			  $("#referrer_name").css('display','none');
		}
		if(search_type=='by_referrer_name')
		{
			 $("#from").css('display','table-cell');
			 $("#to").css('display','table-cell');
			 $("#invoice_id").css('display','none');
			 $("#patient_id").css('display','none');
			 $("#referrer_name").css('display','inline');
		}
	});
</script>