<?php
include('../config/config.php');
include('../classes/classProduct.php');
include('../classes/classReceiveInvoice.php');
include('../classes/classSaleInvoice.php');
include('../classes/classPatient.php');
include('../classes/classTransaction.php');
include('../classes/classProductClosingReconcilation.php');

/*echo '<pre>';
print_r($_POST);
echo '</pre>';
exit;
*/
if(isset($_POST['payment_type'])&&$_POST['payment_type']=='incoming')
{
	$_SESSION['payment_type'] = $_POST['payment_type'];
}
elseif(isset($_POST['payment_type'])&&$_POST['payment_type']=='sales')
{
	$_SESSION['payment_type'] = $_POST['payment_type'];
}

################## Retrieve Invoices Start ####################################

if(isset($_SESSION['payment_type'])&&$_SESSION['payment_type']=='sales')
{
	$retrieveInvoiceOb = new classSaleInvoice;
	if(isset($_POST['search_invoice_by_date'])&&$_POST['search_invoice_by_date']!='')
	{
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailHavingBalanceByDate($_POST['search_invoice_by_date'],$_SESSION['security']['location_id']);
	}
	else
	{
		$retrieveInvoiceOb->setLocationID($_SESSION['security']['location_id']);
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailHavingBalanceByLocationID();
	}
	
	
	$total_invoices = 0;
	$total_payables = 0;
	$total_payments = 0;
	if(isset($invoice_array[0]['saleinvoice_id'])&&$invoice_array[0]['saleinvoice_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(3,3);	
	}
	catch (MyException $e)
	{
		$error_message = $e->getUserMsg();
	}
}
else
{
	$retrieveInvoiceOb = new classReceiveInvoice;
	if(isset($_POST['search_invoice_by_date'])&&$_POST['search_invoice_by_date']!='')
	{
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailHavingBalanceByDate($_POST['search_invoice_by_date'],$_SESSION['security']['location_id']);
	}
	else
	{
		$retrieveInvoiceOb->setLocationID($_SESSION['security']['location_id']);
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetailHavingBalanceByLocationID();	
	}
	
	$total_invoices = 0;
	$total_payables = 0;
	$total_payments = 0;
	  
	if(isset($invoice_array[0]['recinvoice_id'])&&$invoice_array[0]['recinvoice_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(3,4);	
	}
	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" -->SALE INVOICES<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
  <div class="row">
        <div class="col-sm-12">
          <section class="panel">
            <header class="panel-heading">
              <h2>PATIENT BALANCE PAYMENTS</h2>
            </header>
            <table width="100%" border="0" cellspacing="5" cellpadding="5">
              <tr>
                <td width="10%" valign="top"></td>
                <td width="40%" valign="top"></td>
                <td width="70%" align="right"></td>
              </tr>
            </table>
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="10%" align="right"></td>
                <td width="40%">
                        
                  </td>
                  <td width="40%" align="right">
            <p><?php if(isset($_SESSION['payment_type'])&&$_SESSION['payment_type']=='sales'){echo "selected";}?>Patient Balance</p>
            
            <p><?php if(isset($_SESSION['payment_type'])&&$_SESSION['payment_type']=='incoming'){echo "selected";}?>Supplier Balance</p></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" style="padding-top: 0px; margin-top: -34px;">
              <div class="adv-table">
                <table class="bottomBorder" id="dynamic-table" width="100%" cellpadding="0" cellspacing="0">
                <?php
				if(isset($_SESSION['payment_type'])&&$_SESSION['payment_type']=='sales')
				{
                	  ?>
                      <thead>
                        <tr>
                          <th width="3%" align="center">ID</th>
                          <th width="11%" align="center">Date</th>
                          <th width="16%">Patient Name</th>
                          <th width="13%">Amount Payable</th>
                          <th width="10%">Payment</th>
                          <th width="10%">Balance</th>
                          <th width="16%" align="center" class="hidden-phone">Payment Status</th>
                        </tr>
                      </thead>
                      <tbody>
                      <?php
                      if(isset($invoice_array[0]['saleinvoice_id'])&&$invoice_array[0]['saleinvoice_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]['saleinvoice_id'];?></td>
                              
                              <td align="center"><?php echo date('d M Y',$invoice_array[$inv]['saleinvoice_date']);?></td>
                              
                              <td><?php echo $patient_name;?></td>
                              
                              <td align="right">Rs. <?php echo $invoice_array[$inv]['amount_payable'];?></td>
                              
                              <td align="right">Rs. <?php echo $invoice_array[$inv]['payment'];?></td>
                              
                              <td align="right">Rs. <?php echo $invoice_array[$inv]['balance'];?></td>
                              
                              <td align="center">
                                <p id="<?php echo $inv;?>"> <span>Receive Payment</span></p>
                              </td>
                              
                              
                              
                            </tr>
                            
                             <input type="hidden" name="saleinvoice_id" class="set_to_pop" id="saleinvoice_id_<?php echo $inv;?>" value="<?php echo $invoice_array[$inv]['saleinvoice_id'];?>">
                            <input type="hidden" name="patient_id" class="set_to_pop" id="patient_id_<?php echo $inv;?>" value="<?php echo $invoice_array[$inv]['patient_id'];?>">
                            <input type="hidden" name="amount_payable" class="set_to_pop" id="amount_payable_income_<?php echo $inv;?>" value="<?php echo $invoice_array[$inv]['balance'];?>">
                            
                            <?php
                          }
                      }
                      else
                      {
                        ?>
                        <tr>
                            <table>
                                <tr>
                                    <td>No entry found.</td>
                                </tr>
                            </table>
                        </tr>
                        <?php
    
                      }
                      ?>
                      </tbody>
                      <?php
				}
				else
				{
					 ?>
                      <thead>
                        <tr>
                          <th width="3%" align="center">ID</th>
                          <th width="11%" align="center">Date</th>
                          <th width="16%">Supplier Name</th>
                          <th width="13%">Amount Payable</th>
                          <th width="10%">Payment</th>
                          <th width="10%">Balance</th>
                          <th width="16%" align="center" class="hidden-phone">Payment Status</th>
                        </tr>
                      </thead>
                      <tbody>
                      <?php
					  if(isset($invoice_array[0]['recinvoice_id'])&&$invoice_array[0]['recinvoice_id']!='')
				  	  {
						  for($inv=0;$inv<count($invoice_array);$inv++)
						  {
							  if($inv%2==0){ $class = 'gradeX';}else{$class = 'gradeC';}
							  
							  $suppOb = new classProduct;
							  $supp_info = $suppOb->retrieveSupplierBySupplierID($invoice_array[$inv]['supplier_id']);
							  $supplier_name = $supp_info[0]['company_name'];
							 
							?>
							<tr class="<?php echo $class;?>">
							
							  
							  
							  <td align="center"><?php echo $invoice_array[$inv]['recinvoice_id'];?></td>
							  
							  <td align="center"><?php echo date('d M Y',$invoice_array[$inv]['recinvoice_date']);?></td>
							  
							  <td><?php echo $supplier_name;?></td>
							  
							  <td align="right">Rs. <?php echo $invoice_array[$inv]['amount_payable'];?></td>
							  
							  <td align="right">Rs. <?php echo $invoice_array[$inv]['payment'];?></td>
							  
							  <td align="right">Rs. <?php if($invoice_array[$inv]['balance']!=0){echo $invoice_array[$inv]['balance'];}else{echo 0;}?></td>
							  
							  <td align="center">
							  <p id="<?php echo $inv;?>"><span>Make Payment</span></p>
							   </td>
							  
							  
							</tr>
							
							<input type="hidden" name="recinvoice_id" class="set_to_pop" id="recinvoice_id_<?php echo $inv;?>" value="<?php echo $invoice_array[$inv]['recinvoice_id'];?>">
							<input type="hidden" name="supplier_id" class="set_to_pop" id="supplier_id_<?php echo $inv;?>" value="<?php echo $invoice_array[$inv]['supplier_id'];?>">
							<input type="hidden" name="amount_payable" class="set_to_pop" id="amount_payable_expense_<?php echo $inv;?>" value="<?php echo $invoice_array[$inv]['balance'];?>">
							
							<?php
						  }
				  	  }
					  else
                      {
                        ?>
                        <tr>
                            <table>
                                <tr>
                                    <td>No entry found.</td>
                                </tr>
                            </table>
                        </tr>
                        <?php
    
                      }
                      ?>
                      </tbody>
                      <?php
				}
			    ?>
			    <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>