<?php 
require('../db/db.php');
if(isset($_GET['patientid'])&&$_GET['patientid']!='')
{
	$patientid=$_GET['patientid'];
}
if(isset($_GET['supplierid'])&&$_GET['supplierid']!='')
{
	$supplierid=$_GET['supplierid']; 
}
?>
<!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" -->

<!--PATIENT DIV-->
<?php 
if (isset($patientid)&&$patientid!='') 
{ 
	?>
	<div class="content">
   <?php 
   
   function age_from_dob($dob) 
   {

		list($y,$m,$d) = explode('-', $dob);
	   
		if (($m = (date('m') - $m)) < 0) 
		{
			$y++;
		} 
		elseif ($m == 0 && date('d') - $d < 0)
		{
			$y++;
    	}
   
    	return date('Y') - $y;
 	}
		   $raw_results = mysqli_query($con,"SELECT * FROM patients where patient_id='$patientid'");
		while($row = mysqli_fetch_array($raw_results))
  		{ 
		
		  ?>
   <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">
      <tr>
        <td valign="bottom">
          <table width="40%" align="left" border="0" cellspacing="0" cellpadding="0">
            
            <tr>
              <td width="49%"><table width="100%" border="0" cellspacing="0" cellpadding="0" class="bottomBorder">
                <tr>
                <h2>PAYMENT HISTORY</h2>
                  <td><h2><?php echo $row['patient_fname']; echo " "; echo $row['patient_lname']; ?></h2>
			    <?php echo $row['address'];?></br><?php echo "Ph.  ";echo $row['phone'];?></td>
                  <td></td>
                </tr>
                <tr>
                  <td width="49%">Age: <?php echo age_from_dob($row['dob']).' years';?></td>
                  <td width="51%">Blood Group: <?php echo $row['blood_group'];?></td>
                </tr>
               
                <tr>
                <td>Total Payment Made:</td>
                <td><?php 
				$result = mysqli_query($con,"SELECT SUM(payment) FROM product_saleinvoice where patient_id='$patientid'");
				$row = mysqli_fetch_array($result);
				echo $row['SUM(payment)'];?></td>
                </tr>
                <tr>
                <td>Total Payment Due:</td>
                <td><h2><?php 
				$result = mysqli_query($con,"SELECT SUM(balance) FROM product_saleinvoice where patient_id='$patientid'");
				$row = mysqli_fetch_array($result);
				echo $row['SUM(balance)'];?></h2></td>
                </tr>
              </table></td>
            </tr>
          </table>
          </td>

      </tr>
      </table></td>
  </tr>
  <tr>
    <td width="511"><table width="100%">
      <tr>
        <td><table width="100%"  class="bottomBorder">
              <tr>
                <th width="10%" align="center" valign="top"><h3>Date</h3></th>
                <th width="15%" align="center" valign="top"><h3>Sale Invoice ID</h3></th>
               	<th width="15%" align="right" valign="top"><h3>Amount Payable</h3></th>
                <th width="15%" align="right" valign="top"><h3>Paid Amount</h3></th>
                <th width="15%" align="right" valign="top"><h3>Balance</h3></th>
                </tr>
              	<?php
				$result11 = mysqli_query($con,"SELECT * FROM  product_saleinvoice where patient_id='$patientid' order by saleinvoice_date desc");
				while($row11 = mysqli_fetch_array($result11))
				{
					?>
					<tr>
						<td align="center"><?php echo $row11['saleinvoice_date'];?></p></td>
						<td align="center"><?php echo $row11['saleinvoice_id'];?></p></td>
						<td align="right"><?php echo $row11['amount_payable'];?></p></td>
						<td align="right"><?php echo $row11['payment'];?></p></td>
						<td align="right"><?php echo $row11['balance'];?></p></td>
						
					</tr>
					<?php 
				}
				?>
              </table></td>
      </tr>
     
      <?php } ?>
      <tr>
        <td></form></td>
      </tr>
    </table></td>
  </tr>
  </table>
   </table> </div>
	<?php 
}
elseif(isset ($supplierid) && $supplierid!='')
{
	?>  
	<!--Supplier DIV-->
	<div class="content">
   <?php 
$raw_results = mysqli_query($con,"SELECT * FROM suppliers where supplier_id='$supplierid'");
while($row = mysqli_fetch_array($raw_results))
	{ 

		  ?>
   <table width="100%" border="0" align="left" cellpadding="1" cellspacing="10">
  
  
  <tr>
    <td align="right"><table width="100%" border="0" cellspacing="1" cellpadding="1" class="bottomBorder">
      <tr>
        <td valign="bottom"><h1>PAYMENT HISTORY</h1>
          <table width="40%" align="left" border="0" cellspacing="0" cellpadding="0" class="rounded_corners">
            <tr>
              <td><h2><?php echo $row['company_name']; ?></h2>
			    <?php echo $row['company_address'];?></br><?php echo "Ph.  ";echo $row['company_phone'];?></td>
            </tr>
            <tr>
              <td width="49%"><table width="100%" border="0" cellspacing="0" cellpadding="0">
               
                </tr>
                <tr>
                  <td width="49%">Contact Person:</td><td> <?php echo $row['supplier_name'];;?></td>
                  
                </tr>
                <tr><td width="51%">Phone: </td><td><?php echo $row['supplier_phone'];?></td></tr>
               
                <tr>
                <td>Total Payment Made:</td>
                <td><?php 
				$result = mysqli_query($con,"SELECT SUM(payment) FROM product_recinvoice where supplier_id='$supplierid'");
				$row = mysqli_fetch_array($result);
				echo $row['SUM(payment)'];?></td>
                </tr>
                <tr>
                <td>Total Payment Due:</td>
                <td><h2><?php 
				$result = mysqli_query($con,"SELECT SUM(balance) FROM product_recinvoice where supplier_id='$supplierid'");
				$row = mysqli_fetch_array($result);
				echo $row['SUM(balance)'];?></h2></td>
                </tr>
              </table></td>
            </tr>
          </table>
         </td>
        
      </tr>
      </table></td>
  </tr>
  <tr>
    <td width="511"><table width="100%">
      <tr>
        <td><table width="100%"  class="bottomBorder">
              <tr>
                <th width="10%" align="center" valign="top"><h3>Date</h3></th>
                <th width="15%" align="center" valign="top"><h3>Receive Invoice ID</h3></th>
              	<th width="15%" align="right" valign="top"><h3>Amount Payable</h3></th>
                <th width="15%" align="right" valign="top"><h3>Paid Amount</h3></th>
                <th width="10%" align="right" valign="top"><h3>Balance</h3></th>
                <th width="10%" align="right" valign="top"><h3>&nbsp;</h3></th>
                </tr>
				<?php
                $result11 = mysqli_query($con,"SELECT * FROM product_recinvoice where supplier_id='$supplierid'");
                while($row11 = mysqli_fetch_array($result11))
                {
                
                ?>
                <tr>
                    <td align="left"><?php echo $row11['recinvoice_date'];?></p></td>
                    <td align="center"><?php echo $row11['recinvoice_id'];?></p></td>
                    <td align="right"><?php echo $row11['amount_payable'];?></p></td>
                    <td align="right"><?php echo $row11['payment'];?></p></td>
                    <td align="right"><?php echo $row11['balance'];?></p></td>
                    <td align="center">
                    <?php 
                    if ($row11['balance']>0) 
                    { 
						$recinvoice_id = $row11['recinvoice_id'];
						$amount_payable = $row11['balance'];
						
						echo "Make Payment"; 

                    } 
                    ?>
                    </p></td>
                </tr>
                <?php 
				}
				?>
              </table></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <?php } ?>
      <tr>
        <td></form></td>
      </tr>
    </table></td>
  </tr>
  </table>
   </table> </div>
	<?php 
}
?>
  <!-- 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>
