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

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

if(isset($_SESSION['temp']['diagshiftid']))
{
	$shiftid=$_SESSION['temp']['diagshiftid'];
}




if(isset($_GET['diaginvoice_id'])&&isset($_GET['patient_id'])!='')
{
	$_SESSION['temp']['diaginvoice_id'] = base64_decode($_GET['diaginvoice_id']);
	$_SESSION['temp']['patient_id'] = base64_decode($_GET['patient_id']);
	header("Location:printdiaginv.php");
}

if(isset($_SESSION['temp']['patient_id'])&&$_SESSION['temp']['patient_id']!='')
{
	$patient_id = $_SESSION['temp']['patient_id'];
}
if(isset($_SESSION['temp']['diaginvoice_id'])&&$_SESSION['temp']['diaginvoice_id']!='')
{
	$invoice_id = $_SESSION['temp']['diaginvoice_id'];
}



################## Retrieve Data Start ####################################

try
{
	
	///////////// retrieve patients ////////////////////////////////////////
	
	$retrievePatientOb = new classPatient;
	if(isset($patient_id)&&($patient_id!=''&&$patient_id!=0))
	{
		$retrievePatientOb->setPatientAutoID($patient_id);
		$patient_single_detail = $retrievePatientOb->retrievePatientByPatientID();
		
	}
	
	///////////// retrieve invoice data ///////////////////////////////
	
	if(isset($invoice_id)&&$invoice_id!='')
	{
		$retrieveInvoiceOb = new classDiagnostic;
		$retrieveInvoiceOb->setDiagInvoiceID($invoice_id);
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetail();
		$invoice_items_array = $retrieveInvoiceOb->retrieveInvoiceItemsDetail();
	}
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}	
		

try
{
	$retrieveLocOB = new classUser;
	$retrieveLocOB->setOrganizationID($_SESSION['security']['org_id']);
	$location_array = $retrieveLocOB->retrieveLocationsOfOrganization();
	
	$org_name = ucwords($retrieveLocOB->retrieveOrgNameByOrgID($_SESSION['security']['org_id']));
	$loc_address = $location_array[0]['address'];
	$loc_phone = $location_array[0]['phone'];
	$contact_person  = $location_array[0]['contact_person'];
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}	

/*echo '<pre>';
print_r($invoice_items_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">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <link href="../css/print.css" rel="stylesheet">
</head>
<style>
table.bottomBorder { border-collapse:collapse; }
table.bottomBorder td, table.bottomBorder th { border-bottom:1px dotted black;}

@media screen {
  body {
    width: 95%;
  }
}

@media print {
  body {
    width: 50%;
	margin: 5px;
	padding: 5px;
  }
}
<!--
@page { size: landscape; }
-->
</style>
<body style="font-size:9px;">

       <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>
        
       <div class="invoice_print" align="center" style="font-size:14px;"><p><strong><?php echo $org_name;?></strong></br><?php echo $loc_address;?></br>Phone: <?php echo $loc_phone;?>: <?php echo $contact_person;?></p></div>



      <!-- page start-->
      
       <div class="row invoice_print" id="pageHeader">
            
                    <div class="panel-body invoice">
                    
                        <div class="invoice-header">
                            <div class="">
                              <h2>INVOICE</h2>
                            </div>
                            
                        </div>
                      	<?php
						if(isset($error_message)&&$error_message!='')
						{
							?>
                            <table>
							<tr>
								<td><span class="message_red"><?php echo $error_message;?></span></td>
							</tr>
                           
                            </table>
							<?php
						}
						?>
                        
                        <?php
						if(isset($_SESSION['temp']['patient_id'])&&$_SESSION['temp']['patient_id']!='')
						{
							?>
							<div id="result"></div>
                            
                            <table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-size:12px;">
                            
                                <tr>
                                    <td width="24%">Patient ID:</td>
                                    <td width="33%"><?php  if(isset($patient_single_detail[0]['patient_auto_id'])){echo $patient_single_detail[0]['patient_auto_id'];}?></td>
                                    <td width="19%">Date:</td>
                                    <td width="19%"><?php echo date('d M Y h:i:s A',$invoice_array[0]['diaginvoice_date']);?></td>
                                </tr>
                                
                                <tr>
                                    <td>Patient Name:</td>
                                    <td><?php if(isset($patient_single_detail[0]['patient_fname'])){echo strtoupper($patient_single_detail[0]['patient_fname'].' '.$patient_single_detail[0]['patient_lname']);}else{echo 'OUTDOOR';}?></td>
                                    <td>Invoice #:</td>
                                    <td><?php echo $invoice_id;?></td>
                                </tr>
                                
                                <tr>
                                    <td>s/d/w of:</td>
                                    <td>
                                <?php if(isset($patient_single_detail[0]['relationship_name'])){echo strtoupper($patient_single_detail[0]['relationship_name']);}?>
                               </td>
                                    <td>Referred by:</td>
                                    <td><?php echo strtoupper($invoice_array[0]['referred_by']);?></td>
                                </tr>
                                
                                <tr>
                                    <td>Phone:</td>
                                    <td>
                                <?php if(isset($patient_single_detail[0]['phone'])){echo $patient_single_detail[0]['phone'];}?>
                                </td>
                                    <td>Address:</td>
                                    <td>
                                <?php if(isset($patient_single_detail[0]['address'])){echo strtoupper($patient_single_detail[0]['address']);}?>
                               </td>
                                </tr>
                                
                                <tr>
                                    <td>Gender:</td>
                                    <td>
                                <?php if(isset($patient_single_detail[0]['gender'])){echo strtoupper($patient_single_detail[0]['gender']);}?>
                               </td>
                                    <td>Age:</td>
                                    <td><?php if(isset($patient_single_detail[0]['age'])){echo $patient_single_detail[0]['age'].' years';}?></td>
                                </tr>
                                
                                <tr>
                                    <td>Referred To:</td>
                                    <td><?php echo strtoupper($invoice_array[0]['referred_to']);?></td>
                                
                                    <td></td>
                                    <td></td>
                                </tr>
                            
                            </table>
								
							<br />

							<table class="bottomBorder" cellpadding="0" cellspacing="0" width="100%" style="font-size:12px;">
                            
								<thead>
									<tr style="background:#CCC;">
										<th width="2%" align="left">T-Code</th>
										<th width="20%" align="left">Description</th>
										<th width="15%" align="right">Amount</th>
									</tr>
								</thead>
                                
								<tbody>
                               	<?php
								
								if(isset($invoice_items_array[0]['diagitems_id'])&&$invoice_items_array[0]['diagitems_id']!='')
								{
									$group = '';
									$old = 0;
									
									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;"></td>
                                                    <td><h4 style="font-size:14px; margin-bottom: 0; margin-top: 0;">Group: <?php echo ucwords($diagnosis_name);?></h4></td>
                                                    <td align="right">Rs. <?php echo number_format($price,2);?></td>
                                                </tr>
                                                <?php
												$group = 'yes';
												
												
											}
											else
											{
												?>
												<tr>
                                            
												<td style="border-bottom:none;"><?php echo $id;?></td>
                                                
												<td style="border-bottom:none;"><?php echo $diagnosis_name;?></td>
                                                
												<td align="right" style="border-bottom:none;">
                                                <?php
                                                if($group_id==0)
												{
													echo 'Rs. '.number_format($invoice_items_array[$ri]['net_total'],2);
												}
												?>
                                                </td>
                                               
                                                
											</tr>
                                            	<?php
											}
									}
								}
								?>  
                                </tbody>
                                
							</table>

                            <table width="40%" border="0" cellpadding="0" cellspacing="0" align="right" style="font-size:12px; border-top:1px dotted black;">
                            	<tr>
                            <td width="64%">Total:</td>
                            <td width="36%" align="right">Rs. <?php echo number_format($invoice_array[0]['invoice_amount'],2);?></td>
                            </tr>
                            	<tr>
                            <td>Discount: </td>
                            <td align="right"><?php echo $invoice_array[0]['discount'].'%';?></td>
                            </tr>
                            	<tr>
                            <td>Grand Total:</td>
                            <td align="right">Rs. <?php echo number_format($invoice_array[0]['amount_payable'],2);?></td>
                            </tr>
                            	<tr>
                            <td>Payment Received:</td>
                            <td align="right">Rs. <?php echo number_format($invoice_array[0]['payment'],2);?></td>
                            </tr>
                            </table>
                            
                            <br />

							<h4>Terms &amp; Conditions</h4>
                            <p>1. Check Cash before leaving.</p>
                            <p>2. No Legal responsibility and Financial Liability is accepted for any error or ommission.</p>
                            <p>3. Fee is non refundable, once the tests are done.</p>
                            <p>4. NOT VALID FOR ANY COURT CASES.</p>
                            <h3 class="inv-label itatic">Thank you for your business</h3>
							
                            <div style="color:#737373; text-align:center;">All Rights Reserved © Software designed by Softusion Inc. 2013<br />For details visit us at www.softusion.com</div>
							
							<?php
						}
						else
						{
							?>
                            
                            <?php
						}
						?>
                        
                    </div>
               
        </div>
        
      <!-- page end-->


</body>
</html>