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

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

 




################## Invoice Start ####################################



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

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



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

try
{
	
	///////////// retrieve patients ////////////////////////////////////////
	
	$retrievePatientOb = new classPatient;
	$retrievePatientOb->setLocationID($_SESSION['security']['location_id']);
	$patients_array = $retrievePatientOb->retrievePatientByLocationID();
	
	
	///////////// retrieve patient single data ///////////////////////////////
	
	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 classSaleInvoice;
		$retrieveInvoiceOb->setSaleInvoiceID($invoice_id);
		$invoice_array = $retrieveInvoiceOb->retrieveInvoiceDetail();
		$invoice_items_array = $retrieveInvoiceOb->retrieveInvoiceItemsDetail();
	}
	
	///////////// retrieve product data ///////////////////////////////
	
	$retrieveProdOb = new classProduct;
	$product_array = $retrieveProdOb->retrieveActiveProducts();
	
}
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($patients_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>
@page {
  margin-top: 0cm;
  margin-bottom: 0cm;
  padding-left: 1px;
}
table.bottomBorder {
font-size:8px; }
table.bottomBorder td, table.bottomBorder th { border-bottom:1px dotted black; font-size:8px;}

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

@media print {
  body {
    width: 50%;
	margin: 1px;
	padding-left: 0px;
	font-size:8px;
  }
}
<!--
@page { size: landscape; }
-->
</style>
<body style="font-size:8px;">
<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"><p><img alt="" src="../images/org_print_logos/<?php echo $_SESSION['security']['org_id']?>.png" style="width:100%;"></br><?php echo $loc_address;?>, Ph: <?php echo $loc_phone;?></p></div><br />



      <!-- page start-->
       <div class="row invoice_print">
            
                    <div class="panel-body invoice">
                        <div class="invoice-header">
                            <div class="">
                              <h2>SALE RECEIPT</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']!='')
						{
							?>
                        <br />
                        <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
                          <tr>
                            <td><table width="100%" border="0" align="left" cellspacing="0" cellpadding="0">
                              <tbody>
                                <tr>
                                  <td width="36%">Patient Name :</td>
                                  <td width="64%"><strong>
                                    <?php if(isset($patient_single_detail[0]['patient_fname'])&&$patient_single_detail[0]['patient_lname']){echo $patient_single_detail[0]['patient_fname'].' '.$patient_single_detail[0]['patient_lname'];}else{echo 'Outdoor';}?>
                                  </strong></td>
                                </tr>
                                <tr>
                                  <td>Phone :</td>
                                  <td><strong>
                                    <?php if(isset($patient_single_detail[0]['phone'])){echo $patient_single_detail[0]['phone'];}?>
                                  </strong></td>
                                </tr>
                                <tr>
                                  <td>Patient ID :</td>
                                  <td><?php  if(isset($patient_single_detail[0]['patient_id'])){echo $patient_single_detail[0]['patient_id'];}?></td>
                                </tr>
                                <tr>
                                  <td>Invoice # :</td>
                                  <td><?php echo $invoice_id;?></td>
                                </tr>
                              </tbody>
                            </table></td>
                          </tr>
                          <tr>
                            <td><table class="bottomBorder" cellpadding="0" cellspacing="0" width="100%">
                              <thead>
                                <tr style="background:#CCC;">
                                  <th width="5%" align="left">ID</th>
                                  <th width="20%" align="left">Product</th>
                                  <th width="15%" align="left" class="text-center">Packing</th>
                                  <th width="15%" align="right" class="text-center">Retail Price per Unit</th>
                                  <th width="10%" align="right" class="text-center">Qty</th>
                                  <th width="10%" align="right" class="text-center">Total</th>
                                  <th width="10%" align="right"class="text-center">Discount</th>
                                  <th width="15%" align="right" class="text-center">Net Total</th>
                                </tr>
                              </thead>
                              <tbody>
                                <?php
								if(isset($invoice_items_array[0]['saleitems_id'])&&$invoice_items_array[0]['saleitems_id']!='')
								{
									for($ri=0;$ri<count($invoice_items_array);$ri++)
									{
										$singleProdOb = new classProduct;
										$singleProdOb->setProductID($invoice_items_array[$ri]['product_id']);
										$single_prod_detail = $singleProdOb->retrieveProductByProductID();
										
										?>
                                <tr>
                                  <td><?php echo $invoice_items_array[$ri]['product_id'];?></td>
                                  <td><?php echo $single_prod_detail[0]['product_name'];?> <?php echo $single_prod_detail[0]['medicine_type'];?></td>
                                  <td class="text-center"><?php echo $invoice_items_array[$ri]['packing_type'];?></td>
                                  <td align="right" class="text-right">Rs. <?php echo number_format($invoice_items_array[$ri]['retail_price'],2);?></td>
                                  <td align="right" class="text-center"><?php echo $invoice_items_array[$ri]['qty'];?></td>
                                  <td align="right" class="text-right"><?php 
                                                    if ($invoice_items_array[$ri]['packing_type']=='Unit/Tablets') 
                                                    { 
                                                        $total = $invoice_items_array[$ri]['retail_price']*$invoice_items_array[$ri]['qty'];
                                                    } 
                                                    elseif ($invoice_items_array[$ri]['packing_type']=='Leaf') 
                                                    {  
                                                        $total = $invoice_items_array[$ri]['retail_price']*$invoice_items_array[$ri]['qty'];
                                                    } 
                                                    elseif ($invoice_items_array[$ri]['packing_type']=='Box/Pack') 
                                                    { 
                                                        $total = $invoice_items_array[$ri]['retail_price']*$invoice_items_array[$ri]['qty'];
                                                    }
                                                    else
                                                    {
                                                        $total = 0;
                                                    }
                                                    
                                                   ?>
                                    <?php echo "Rs. ".number_format($total,2);?></td>
                                  <td align="right" class="text-center"><?php echo $invoice_items_array[$ri]['discount']." %";?></td>
                                  <td align="right" class="text-right">Rs. <span id="net_total_<?php echo $ri;?>"><?php echo number_format($invoice_items_array[$ri]['net_total'],2);?></span></td>
                                </tr>
                              </tbody>
                              <input name="product_id[]" class="product_id" id="product_id_<?php echo $ri;?>" value="<?php echo $single_prod_detail[0]['product_id'];?>" type="hidden" size="5"/>
                              <input name="saleitems_id[]" class="saleitems_id" id="saleitems_id_<?php echo $ri;?>" value="<?php echo $invoice_items_array[$ri]['saleitems_id'];?>" type="hidden" size="5"/>
                              <?php
									}
								}
								?>
                              <tr>
                                <td></tbody></td>
                              </tr>
                            </table></td>
                          </tr>
                          <tr>
                            <td><table width="23%" border="0" cellpadding="0" cellspacing="0" align="right">
                              <tr>
                                <td width="78%" align="right">Total: Rs.</td>
                                <td width="22%" align="right"><?php echo number_format($invoice_array[0]['invoice_amount'],2)?></td>
                              </tr>
                              <tr>
                                <td align="right">Discount: </td>
                                <td align="right"><?php echo $invoice_array[0]['discount']." %";?></td>
                              </tr>
                              <tr>
                                <td align="right">Grand Total: Rs.</td>
                                <td align="right"><?php echo number_format($invoice_array[0]['amount_payable'],2);?></td>
                              </tr>
                            </table></td>
                          </tr>
                        </table>

					  
                            
                             <div>
									<h4 style="color:#000;">Terms &amp; Conditions</h4>
									<p style="color:#000;">1. Check Cash and ensure quality & quantity before leaving.</p>
									<p style="color:#000;">2. Loose tablets, without packing are not refundable.</p>
									<p style="color:#000;">3. Fridge items, inhalers, test strips and opened syrups are not refundable.</p>
									<h3 class="inv-label itatic" style="color:#000;">Thank you for your business</h3>
                                 <div style="color:#999; text-align:center;">All Rights Reserved © Software designed by Softusion Inc. 2013<br />
        For details visit us at www.softusion.com</div>
								</div>
							
							<?php
						}
						else
						{
							?>
                            
                            <?php
						}
						?>
                    </div>
               
        </div>
        
      <!-- page end-->


</body>
</html>