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

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

$retrieveInvoiceOb = new classDiagnostic;
if(isset($_SESSION['print']['from'])&&isset($_SESSION['print']['to']))
{
	$from = $_SESSION['print']['from'];
	$to = $_SESSION['print']['to'];
	
	$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailPaidByDate($from,$to,$_SESSION['security']['location_id']);
}
elseif(isset($_SESSION['print']['view'])&&$_SESSION['print']['view']=='all')
{
	$from = '';
	$to = '';

	$retrieveInvoiceOb->setLocationID($_SESSION['security']['location_id']);
	$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailPaidByLocationID();
}
else
{
	$from = date('Y-m-d',time());
	$to = date('Y-m-d',time());
	$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailPaidByDate($from,$to,$_SESSION['security']['location_id']);
	
}


$total_invoices = 0;
$total_payables = 0;
$total_payments = 0;
if(isset($invoice_array[0]['diaginvoice_id'])&&$invoice_array[0]['diaginvoice_id']!='')
{
  
  for($int=0;$int<count($invoice_array);$int++)
  {
	  $total_invoices ++;
	  $total_payables += $invoice_array[$int]['amount_payable'];
	  $total_payments += $invoice_array[$int]['payment'];
  }
}

try
{
	$tranOb = new classTransaction;
	$account_data = $tranOb->retrieveAccountHeadAndAccountByID(11,9);	
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}



/*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" -->INVOICES & RESULTS<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
  <div class="row">
        <div class="col-sm-12">
          <section class="panel">
            
            <table width="100%" border="0" cellspacing="5" cellpadding="5">
              <tr>
              
                
                
                
                
                <td width="50%" align="left" valign="bottom">
                
                
                </td>
                
                <td width="50%" align="right">
                <div class="col-md-4" style="float: right; width:60%;">
                <div class="profile-nav alt">
                  <section class="panel">
                    <div class="user-heading alt clock-row terques-bg">
                      <h1>Total Sales</h1>
                      
                      <p class="text-left"></p>
                      <span>Total Invoices: <div style="float:right;"><?php echo $total_invoices;?></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>
            <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="center">Patient Name</th>
                      <th width="13%" align="right">Amount Payable</th>
                      <th width="13%" align="right">Payment</th>
                      <th width="13%" align="right">Balance</th>
                      <th width="10%" align="right" class="hidden-phone">Status</th>
                    </tr>
                  </thead>
                  <tbody>
                  	<?php
					function is_array_empty($arr)
					{
						if(is_array($arr))
						{
							for($i=0;$i<count($arr);$i++)
							{
								if($arr[$i]['diagnosis_result']!='')
								{
									return true;
									break;
								}
							}
							return false;
						}
						
					}
							
					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';}
							
							$diaginvoice_id_encoded = base64_encode($invoice_array[$inv]['diaginvoice_id']);
							$patient_id_encoded = base64_encode($invoice_array[$inv]['patient_id']);
							
							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';
							}
							
							
							
							$diagInvOb = new classDiagnostic;
							$diagInvOb->setDiagInvoiceID($invoice_array[$inv]['diaginvoice_id']);
							$invoice_items_array = $diagInvOb->retrieveInvoiceItemsDetail();
							if(is_array_empty($invoice_items_array))
							{
								$href = 'javascript:Popup("printtestresults.php?diaginvoice_id='.$diaginvoice_id_encoded.'&patientid='.$patient_id_encoded.'")';
								$results = "<a href='$href'>Results</a>";
							}
							else
							{
								$results = '';
							}
							
							?>
							<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="center"><?php echo $patient_name;?></td>
                                <?php /*?><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><?php */?>
                                <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="right">Paid</td>
							</tr>
							<?php
						}
					}
					else
					{
						?>
						<tr>
                            <table>
                                <tr>
                                    <td>No entries found!</td>
                                </tr>
                            </table>
						</tr>
						<?php
					
					}
				  	?>
			
                    
                  </tbody>
                  <tfoot>
                  </tfoot>
                </table>
              </div>
            </div>
          </section>
        </div>
      </div>
  <!-- page end-->

<!--<p align="center" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; margin-top:260px;"><strong>For Appointment Call at 042-35151666 (bring your Patient ID Card)</strong>317 A-1, Nursery Stop, Township, Lahore 042-35151666 (Not Valid for Court or any Legal Purpose)</p>
--><!-- 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>