<?php require('../db/db.php');

///////////////////////////////Setting the salea View //////////////////
if (isset($_POST["month"]) && $_POST["month"] != "0")
{
	$_SESSION['transviewmonth']=$_POST['month'];
}
else
{
	$_SESSION['transviewmonth']=date('d M Y');
}
$day=date('d',strtotime($_SESSION['transviewmonth']));
$month=date('m',strtotime($_SESSION['transviewmonth']));
$year=date('Y',strtotime($_SESSION['transviewmonth']));

################################################################################
#########################Pagination Starts here. {########################
################################################################################
				
				$raw_results = mysql_query("SELECT count(patient_id) FROM patients") or die(mysql_error()); 
				
				$row = mysql_fetch_assoc($raw_results);
				
				$array['item_count'] = $row['count(patient_id)'];
				if(isset($_GET['page'])AND isset($_GET['items']))
				{
					if($array['item_count'] > $_GET['items'])
					{
						$array['pages'] = ceil($array['item_count']/$_GET['items']);
						if($_GET['page'] >$array['pages'])
						{
							$_GET['page'] = 1;
						}
						$array['current_page'] = $_GET['page'];
						if($_GET['page'] > 1)
						{
							$starting_record = $_GET['items']*($_GET['page']-1);
							$ending_record = $_GET['items'];
							$array['limit'] = $starting_record . "," . $ending_record;
							$limit = $array['limit'];
						}
						else
						{
							$limit = $_GET['items'];
						}
					}
					else
					{
						$array['current_page'] = $_GET['current_page'] = 1;
						$array['pages'] = 1;
						$array['limit'] = $_GET['items'];
						$limit = $array['limit'];
					}
				}
				else
				{
					if($array['item_count'] > 20)
					{
						$array['pages'] = ceil($array['item_count']/20);
					}
					else
					{
						$array['pages'] = 1;
					}
					$array['current_page'] = 1;
					$array['limit'] = 20;
					$limit = $array['limit'];
				}
				
				$_SESSION['pagination']['item_count']=$array['item_count'];
				$_SESSION['pagination']['pages']=$array['pages'];
				$_SESSION['pagination']['current_page']=$array['current_page'];
				$_SESSION['pagination']['limit']=$array['limit'];

################################################################################
######################### Pagination Ends here. }##########################
################################################################################
?>

<!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="1" cellpadding="0" class="bottomBorder">
      <tr>
        <td valign="bottom"><h1>PHARMACY TRANSACTIONS</h1>
        <span class="message_blue"><strong><?php if(isset($month)&& $month) { echo date('d M Y',strtotime($_SESSION['transviewmonth'])) ;}
			 else { echo date('d M Y'); }?></strong></span>
        </td>
        <td align="right" valign="top">&nbsp;</td>
      </tr>
      </table></td>
  </tr>
  <tr>
    <td width="511"><table width="100%">
      <form action="" method="post" id="form">
      <tr>
        <td><table width="100%" border="0" cellspacing="1" cellpadding="1">
         
          <tr>
              <td width="30%">
              <table width="30%" border="0" cellpadding="0" cellspacing="0" class="bottomBorder" align="right">
              <?php
            		
							$result_income = mysql_query("SELECT SUM(payment) FROM  `transactions` WHERE  `transact_type`='Income' AND  `acchead_id` =3 AND DAY(transact_time)=$day AND MONTH(transact_time)=$month AND YEAR(transact_time)=$year") or die(mysql_error()); 
	 					while($income = mysql_fetch_array($result_income))
							{
								$result_expense = mysql_query("SELECT SUM(payment) FROM transactions WHERE  transact_type='Expense' AND acchead_id='3' AND DAY(transact_time)=$day AND MONTH(transact_time)=$month AND YEAR(transact_time)=$year") or die(mysql_error()); 
	 					while($expense = mysql_fetch_array($result_expense))
							{
								$result_sum = mysql_query("SELECT sum(balance) FROM transactions WHERE  acchead_id='3' AND DAY(transact_time)=$day AND MONTH(transact_time)=$month AND YEAR(transact_time)=$year") or die(mysql_error()); 
	 					while($sum = mysql_fetch_array($result_sum))
							{
								
								$count_row = mysql_query("SELECT *  FROM transactions WHERE  acchead_id='3' AND DAY(transact_time)=$day AND MONTH(transact_time)=$month AND YEAR(transact_time)=$year");

									$num_rows = mysql_num_rows($count_row);
									$totalincome=$income['SUM(payment)'];
									$totalexpense=$expense['SUM(payment)'];
									$totalbalance=$sum['sum(balance)'];
			?>
              <tr>
                <td align="right">Total Transactions:</td>
                <td align="right"><h4><?php echo "$num_rows \n"; ?></h4>
                  </p></td>
              </tr>
              <tr>
                <td align="right">Total Income:</td>
                <td align="right"><h4>Rs. <?php echo $totalincome; ?></h4>
                  </p></td>
              </tr>
              <tr>
                <td align="right">Total Expense:</td>
                <td align="right"><h4>Rs. <?php echo $totalexpense; ?></h4>
                  </p></td>
              </tr>
              <tr>
                <td align="right">Balance:</td>
                <td align="right"><h4>Rs. <?php echo $totalbalance; ?></h4>
                  </p></td>
              </tr>
              <?php } } }?>
            </table></td>
              
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table width="100%" class="bottomBorder">
          <tr>
          	 <th width="20%" align="center" valign="top"><h3>Date</h3></th>
            <th width="10%" align="center" valign="top"><h3>Trans ID</h3></th>
            <th width="20%" align="center" valign="top"><h3>Account Name</h3></th>
            <th width="20%" align="center" valign="top"><h3>Description</h3></th>
            <th width="10%" align="center" valign="top"><h3>Expense</h3></th>
            <th width="10%" align="center" valign="top"><h3>Income</h3></th>
          </tr>
			<?php
				$raw_results = mysql_query("SELECT * FROM transactions WHERE  acchead_id='3' AND DAY(transact_time)=$day AND MONTH(transact_time)=$month AND YEAR(transact_time)=$year order by transact_time desc") or die(mysql_error()); 
				while($row = mysql_fetch_array($raw_results))
				{ 
					$transact_id=$row['transact_id'];
					?>
					<tr>
						<?php /*?>	<script type="text/javascript">
							function openWin(url, w, h)
							{
							var left = (screen.width/2)-(w/2);
							var top = (screen.height/2)-(h/2);
							w=window.open(url, '_blank', width='+w+', height='+h+', scrollbars='no', menubar='no', resizable='no', location='no', toolbar='no', top='+top+', left='+left+', 'false');
							w.focus();
							}
							</script><?php */?>
                            
                       
                        <td align="center"><?php echo date( 'd M Y' ,strtotime($row['transact_time']));?><p></td>
						<td align="center"><?php  echo $row['transact_id'];?><p></td>
						<td><?php $results= mysql_query("SELECT * FROM accounts WHERE account_id='$row[account_id]'") or die(mysql_error()); 
	 			$rows = mysql_fetch_array($results); echo $rows['account_name'];?><p></td>
                        <td><?php echo $row['description'];?><p></td>
						<td align="right"><?php if ($row['transact_type']=='Expense') { echo "Rs. ".$row['payment']; }?><p></td>
                        <td align="right"><?php if ($row['transact_type']=='Income') { echo "Rs. ".$row['payment']; }?><p></td>
						
					</tr>
					<?php } ?>
        				
      </table></td>
      </tr>

      </table></td>
  </tr>
  </table
>
  </div>
  <link rel="stylesheet" href="../css/jquery-ui.css">
  	<script src="../css/jquery-1.9.1.js"></script>
  	<script src="../css/jquery-ui.js"></script>
  	<link rel="stylesheet" href="../style.css">
  
<script>
$("#datepicker").datepicker({
showOn: "button",
buttonImage: "../images/calendar.png",
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: "yy-mm-dd",
showButtonPanel: true,

onClose: function(dateText, inst) {
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).val($.datepicker.formatDate('yy-mm', new Date(year, month, 1)));
        }
    });

$("#datepicker").focus(function () {
        $(".ui-datepicker-calendar").hide();
        $("#ui-datepicker-div").position({
            my: "center top",
            at: "center bottom",
            of: $(this)
        });
    });
		
</script>
  <!-- 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>
