<?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 - DETAILED BY TESTS<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
	<?php
    if(isset($error_message)&&$error_message!='')
    {
        ?>
      	<span class="message_red"><?php echo $error_message;?></span>
      	<?php
    }
    ?>

	<section class="wrapper">
		<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>
      <!-- page start-->
      <div class="row">
        <div class="col-sm-12">
          <section class="panel">
            <div class="panel-body">
              
				<table width="100%"  class="bottomBorder"  cellspacing="0" cellpadding="0">
                <?php
                if(isset($invoice_array[0]['diaginvoice_id'])&&$invoice_array[0]['diaginvoice_id']!='')
				{
					for($inv=0;$inv<count($invoice_array);$inv++)
					{
						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';
						}
						
						$retrieveInvoiceOb = new classDiagnostic;
						$retrieveInvoiceOb->setDiagInvoiceID($invoice_array[$inv]['diaginvoice_id']);
						$invoice_items_array = $retrieveInvoiceOb->retrieveInvoiceItemsDetail();
						
						?> 
                        <tr>
							<td style="border-bottom:0px;">&nbsp;</td>
						</tr>
						<tr>
							<td><table width="100%" cellspacing="0" cellpadding="0" >
							<tr>
							<td width="10%" style="border-bottom:0px; font-size:14px;"><strong>Inv. ID:</strong> <?php echo $invoice_array[$inv]['diaginvoice_id'];?></td>
							<td width="25%" style="border-bottom:0px; font-size:14px;"><strong>Invoice Date:</strong> <?php echo date('d-M-Y',$invoice_array[$inv]['diaginvoice_date']);?></td>
							<td width="65%" style="border-bottom:0px; font-size:14px;"><strong>Patient Name:</strong> <?php echo $patient_name?></td>
							</tr>
							</table></td>
						</tr>
						
						<tr>
						<td><table width="100%" cellspacing="0" cellpadding="0" style="border-bottom:0px;">
						<thead>
                        
							<tr>
                            	<th width="10%" align="center" style="border-bottom:0px;">T-Code</th>
								<th width="75%" align="left" style="border-bottom:0px;">Test Name</th>
								<th width="15%" align="right" style="border-bottom:0px;">Price</th>
							</tr>
                            <?php
                            if(isset($invoice_items_array[0]['diagitems_id']))
                            {
								$old = 0;
								$group = '';
								for($ri=0;$ri<count($invoice_items_array);$ri++)
								{
									$type = $invoice_items_array[$ri]['diagnosis_type'];
									$id = $invoice_items_array[$ri]['diagnosis_id'];
									$diagitems_id = $invoice_items_array[$ri]['diagitems_id'];
									$price = $invoice_items_array[$ri]['price'];
									$specimen = $invoice_items_array[$ri]['specimen'];
									$group_id = $invoice_items_array[$ri]['group_id'];
									
									
									$diagOb = new classDiagnostic;
									if($type=='group')
									{
										$diagnosis_name = $diagOb->retrieveTestGroupNameByID($id);
										$group_cost = $diagOb->retrieveTestGroupCostByID($id);
									}
									if($type=='labtest')
									{
										$diagOb->setLabTestID($id);
										$labtest_array = $diagOb->retrieveLabTestsByLabTestID();
										$diagnosis_name = $labtest_array[0]['labtest_name'];
									}
									if($type=='procedure')
									{
										$diagOb->setProcedureID($id);
										$proc_array = $diagOb->retrieveProcedureByProcedureID();
										$diagnosis_name = $proc_array[0]['procedure_name'];
									}
									
									
									
									
									if($group_id==0&&$group=='yes')
									{
										?>
										<tr>
											<td style="background-color:white;"></td>
											<td style="background-color:white;">&nbsp;</td>
                                            <td style="background-color:white;">&nbsp;</td>
										</tr>
										<?php
										$group = '';
										
									}
									
									if($type=='group')
									{
										?>
										<tr>
                                            <td style="background-color:white; border-bottom:0px;"></td>
                                            <td><strong>Group: <?php echo strtoupper($diagnosis_name);?></strong></td>
                                            <td align="right" style="border-bottom:0px;"><?php echo number_format($price,2);?></td>
										</tr>
										<?php
										$group = 'yes';
												
									}
									else
									{		
										?>
										<tr>
											<td align="center" style="border-bottom:0px;"><?php echo $id;?></td>
											<td align="left" style="border-bottom:0px;"><?php echo $diagnosis_name;?></td>
											<td align="right" style="border-bottom:0px;"><?php echo number_format($price,2);?></td>
										</tr>
										<?php
									}
								}
							}
							?>
							
							
							<tr>
								<th align="center" style="border-bottom:0px;"></th>
								<th align="right" style="border-bottom:0px;">Total</th>
								<th align="right" style="border-bottom:0px;"><?php echo number_format($invoice_array[$inv]['invoice_amount'],2)?></th>
							</tr>
							<tr>
								<th align="center" style="border-bottom:0px;"></th>
								<th align="right" style="border-bottom:0px;">Discount</th>
								<th align="right" style="border-bottom:0px;"><?php 
								$discount = $invoice_array[$inv]['invoice_amount']*$invoice_array[$inv]['discount']/100;?></th>
							</tr>
							<tr>
								<th align="center" style="border-bottom:0px;"></th>
								<th align="right" style="border-bottom:0px;">Amount Paid</th>
								<th align="right" style="border-bottom:0px;"><?php echo number_format($invoice_array[$inv]['payment'],2)?></th>
							</tr>
						</thead>
						</table></td>
						</tr>
						
						<?php
					}
				}
					?>
                    
				</table>
                
				

              </div>
            </div>
          </section>
        </div>
      </div>
      <!-- page end-->
    </section>
  <!-- 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>