<?php 
require('../db/db.php');
require('../classes/classFilter.php');
require('../classes/classInvoice.php');

if(isset($_GET['patientid'])&&$_GET['patientid']!='')
{
	$patient_id=$_GET['patientid'];
	$_SESSION['patientid']=$patient_id;
}
else
{
	$patient_id=$_SESSION['patientid'];
}
	
	
if(isset($_GET['editsaleinvoiceid'])&&$_GET['editsaleinvoiceid']!='')
{
	$editsaleinvoiceid=$_GET['editsaleinvoiceid'];
	$_SESSION['invoiceidsale']=$editsaleinvoiceid;
}

$saleinvoiceidsale=$_SESSION['invoiceidsale'];

/////////////////////////////Refund Medicine////////////////////////

/*echo '<pre>';
print_r($_POST);
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" -->SALE INVOICES<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
 

  
  
<div class="content">
   <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0">
     <p 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></p>

  <tr>
    <td align="right"><table width="100%" border="0" cellspacing="0" cellpadding="0" class="bottomBorder">
      <tr>
        <td valign="bottom"><h1>SALE REFUNDS</h1>
          <table width="40%" align="left" border="0" cellspacing="0" cellpadding="0" class="rounded_corners">
          
            <tr>
              <td width="25%">Patient Name:</td>
                <td width="75%"><strong>
                <?php
                $raw_results2 = mysqli_query($con,"SELECT * FROM patients WHERE patient_id='$patient_id'");
                $row2 = mysqli_fetch_array($raw_results2);
                echo $row2['patient_fname'];
                echo " ";
                echo $row2['patient_lname']; ?>  <?PHP if ($patient_id=='' && $patient_id==0) {echo "OUT DOOR PATIENT" ; echo $patient_id;  echo $patient_id; }?> 
                </strong></td>
            </tr>
           <?php if  ($patient_id!='' && $patient_id!=0) {?> <tr>
              <td>Phone no:</td>
              <td><?php echo $row2['phone'];?></td> 
            </tr>
            <tr>
              <td>Patient ID:</td>
              <td><strong><?php echo $row2['reg_id'];?></strong></td> 
            </tr>
            <tr> <?php }?>
              <td>&nbsp;</td>
              <td>&nbsp;</td> 
            </tr>
            <tr>
              <td>Invoice ID:</td>
              <td><strong><?php  echo $saleinvoiceidsale; ?></strong></td>
            </tr>
          </table></td>
        </tr>
      </table></td>
  </tr>
  <tr>
  <td align="right"></td>
  </tr>
  
  <tr>
    <td width="511"><table width="100%">
    
	<?php
    if(isset($errorMessage)&&$errorMessage!="")
    {
		?>
		<tr>
			<td style="color:red;"><?php echo $errorMessage;?></td>
		</tr>
		<?php
    }
    
    if(isset($_GET['error'])&&$_GET['error']!="")
    {
    	?>
		<tr>
			<span style="float:left; color:red;"><?php echo $_GET['error'];?></span>
		</tr>
		<?php
    }
    ?>
      
      <tr>
        <td>
        <div id="container-5">
            <table width="100%" class="bottomBorder">
              <tr>
                <th width="5%" align="center" valign="top"><h3>ID</h3></th>
                <th width="20%" align="left" valign="top"><h3>Product</h3></th>
                <th width="15%" align="center" valign="top"><h3>Packing</h3></th>
                <th width="10%" align="center" valign="top"><h3>Price</h3></th>
                <th width="6%" align="center" valign="top"><h3>Qty</h3></th>
                 <th width="10%" align="center" valign="top"><h3>Qty Refund</h3></th>
                <th width="13%" align="center" valign="top"><h3>Total</h3></th>
                <th width="12%" align="center" valign="top"><h3>Discount</h3></th>
                <th width="15%" align="center" valign="top"><h3>Net Total</h3></th>
                </tr>
                <?php
			   $result4 = mysqli_query($con,"SELECT * FROM product_saleitems where saleinvoice_id='$saleinvoiceidsale'");
				while($row4 = mysqli_fetch_assoc($result4))
				{
					$saleitemsid = $row4["saleitems_id"]; 
					$productid = $row4["product_id"];
					
					$raw_results5 = mysqli_query($con,"SELECT * FROM products WHERE product_id='$productid'");
					while($row5 = mysqli_fetch_assoc($raw_results5))
					{ 
						if(($row4['qty']-$row4['refund_qty'])==0)
						{
							
							?>
                            <tr style="text-decoration:line-through;">
                        
                            <td align="center" valign="middle"><?php echo $row5['product_id'];?></p></td>
                            
                            <td align="left" valign="middle"><?php echo $row5['product_name']. " - " . $row5['medicine_type'];?></p></td>
                            
                            <td align="center" valign="middle"><?php echo $row4['packing_type'];?></td>
                            
                            <td align="right" valign="middle">
                                <?php
                                    if ($row4['packing_type']=='Unit/Tablets') {  echo $row5['unit_retailprice'];} 
                                    if ($row4['packing_type']=='Leaf') {  echo $row5['leaf_retailprice'];} 
                                    if ($row4['packing_type']=='Box/Pack') {  echo $row5['box_retailprice'];}
                                ?>
                               
                            </td>
                            
                            <td align="center" valign="middle"><?php echo $row4['qty']-$row4['refund_qty']; ?></td> 
                            
                            <td align="center" valign="middle"><?php echo $row4['refund_qty'];?> </td> 
                            
                            <td align="right" valign="middle">
                                <?php  
                                
                                if ($row4['packing_type']=='Unit/Tablets') 
                                { 
                                    $total = $row5['unit_retailprice']*$row4['qty'];
                                } 
                                elseif($row4['packing_type']=='Leaf') 
                                {   
                                    $total = $row5['leaf_retailprice']*$row4['qty'];
                                } 
                                elseif($row4['packing_type']=='Box/Pack') 
                                {   
                                 $total = $row5['box_retailprice']*$row4['qty'];
                                } 
                                else
                                {
                                    $total = 0;
                                }
                                echo "Rs. ".number_format($total,2); 
                                ?>
                            </td>
                            
                            <td align="center" valign="middle"><?php echo $row4['discount']; ?></td>
                            
                            <td align="right" valign="middle"><?php echo "Rs. ".number_format($row4['net_total'],2); ?></td>
                            
                            </tr>
                            <?php
						}
						else
						{
							?>
							<input name="productid[]" value="<?php echo $row5['product_id'];?>" type="hidden" size="5"/>
							<input name="saleitemsid[]" value="<?php echo $row4['saleitems_id'];?>" type="hidden" size="5"/>
							<input name="unitqty[]" type="hidden" id="qty" size="5" value="<?php if(isset($row4['unitsaleqty'])&&$row4['unitsaleqty']!='0'){ echo $row4['unitsaleqty']; }?>"  /> 
							<input name="unitqtyold[]" type="hidden" id="qty" size="5" value="<?php if(isset($row4['unitsaleqty'])&&$row4['unitsaleqty']!='0'){ echo $row4['unitsaleqty']; }?>" />
							
							<tr>
							
							<td align="center" valign="middle"><?php echo $row5['product_id'];?></p></td>
							
							<td align="left" valign="middle"><?php echo $row5['product_name']. " - " . $row5['medicine_type'];?></p></td>
							
							<td align="center" valign="middle">
							
								<?php echo $row4['packing_type']; ?>
							</td>
							
							<td align="right" valign="middle">
								<?php
									if ($row4['packing_type']=='Unit/Tablets') {  echo $row5['unit_retailprice'];} 
									if ($row4['packing_type']=='Leaf') {  echo $row5['leaf_retailprice'];} 
									if ($row4['packing_type']=='Box/Pack') {  echo $row5['box_retailprice'];}
								?>
								<input name="unitprice[]" type="hidden" size="5" value="<?php 
									if ($row4['packing_type']=='Unit/Tablets') {  echo $row5['unit_retailprice'];} 
									if ($row4['packing_type']=='Leaf') {  echo $row5['leaf_retailprice'];} 
									if ($row4['packing_type']=='Box/Pack') {  echo $row5['box_retailprice'];}
								?>"/>
							</td>
							
							<td align="center" valign="middle"><?php echo $row4['qty']-$row4['refund_qty']; ?></td> 
							
							<td align="center" valign="middle">	 </td> 
							
							<td align="right" valign="middle">
								<?php  
								
								if ($row4['packing_type']=='Unit/Tablets') 
								{ 
									$total = $row5['unit_retailprice']*$row4['qty'];
								} 
								elseif($row4['packing_type']=='Leaf') 
								{   
									$total = $row5['leaf_retailprice']*$row4['qty'];
								} 
								elseif($row4['packing_type']=='Box/Pack') 
								{   
								 $total = $row5['box_retailprice']*$row4['qty'];
								} 
								else
								{
									$total = 0;
								}
								echo "Rs. ".number_format($total,2); 
								?>
							</td>
							
							<td align="center" valign="middle"><?php echo $row4['discount']; ?></td>
							
							<td align="right" valign="middle"><?php echo "Rs. ".number_format($row4['net_total'],2); ?></td>
							
							</tr>
                            <?php
						}
					}
				}
				?>
              </table>
              
            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="bottomBorder">
            <?php 
		  		$raw_result5 = mysql_query("SELECT sum(refund_amount) FROM product_saleitems where saleinvoice_id='$saleinvoiceidsale'") or die(mysql_error()); 
	 			while($row5 = mysql_fetch_array($raw_result5))
				{
					
					$invoiceamount=$row5['sum(refund_amount)'];
					
					?>
					<tr>
                        <td width="75%" align="right"><h4>Total Refund Amount:</h4></td>
                        <td width="25%" align="right"><h4><?php echo "Rs. ".number_format($invoiceamount,2); ?>
                        <input name="invoiceamount" type="hidden" value="<?php echo $invoiceamount; ?>" />
                        </h4></td>
					</tr>
					
					<?php 
				}
					?>
                 
            </table>
        </div>
        </td>
      </tr>
     
    </table></td>
  </tr> 
  </table>
  </div></form>
   </table>
  <!-- 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> 