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

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



try
{
	$retrieveProdOb = new classProduct;
	$product_array = $retrieveProdOb->retrieveActiveProductsThroughFilter('','',$_SESSION['security']['org_id']);
}
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" -->All Products Stock Report<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
              <?php
			if(isset($error_message)&&$error_message!='')
			{
				?>
              <span class="message_red"><?php echo $error_message;?></span>
              <?php
			}
			?>

<section class="wrapper">

      <!-- page start-->
      <div class="row">
        <div class="col-sm-12">
          <section class="panel">
            <div class="panel-body">
              <div class="adv-table">
                <p style="font-family:Tahoma, Geneva, sans-serif; font-size:16px;">Session: <?php echo date('Y',time());?>, Date <?php echo date('d-M-Y',time());?></p>
                <table  class="bottomBorder" width="100%">
                  <thead>
                    <tr>
            			<th width="44%" align="left"> Products</th>
            			<th width="14%" align="right">Last Closing</th>
            			<th width="13%" align="right">Stock In</th>
           			  	<th width="11%" align="right">Stock Out</th>
           			  	<th width="14%" align="right">Ending Stock</th>
                    </tr>
                  </thead>
                  
                  <tbody>
                  
                  	<?php
					if(isset($product_array[0]['product_id'])&&$product_array[0]['product_id']!='')
					{
						$location_id = $_SESSION['security']['location_id'];
						
						for($p=0;$p<count($product_array);$p++)
						{
							$product_id = $product_array[$p]['product_id'];
							$product_name = $product_array[$p]['product_name'];
							$medicine_type = $product_array[$p]['medicine_type'];
							
							
							$productUnitQtyOb = new classProduct;
							$product_detail_array = $productUnitQtyOb->retrieveProductLocationRelationship($product_id,$_SESSION['security']['location_id']);
							$unit_qtyinstock = $product_detail_array[0]['unit_qtyinstock'];
							
							$lastClosingQtyOb = new classProductClosingReconcilation;
							$lastClosingQtyOb->setProductID($product_id);
							$last_closing_array = $lastClosingQtyOb->retrieveLastClosingQtyOfProduct();
							
							if(isset($last_closing_array[0]['closing_date'])&&$last_closing_array[0]['closing_date']!='')
							{
								$start_time = $last_closing_array[0]['closing_date'];
								$end_time = time();
								
								$lastClosingQty = $last_closing_array[0]['unit_phy_qtyinstock'];
							}
							else
							{
								$start_time = '';
								$end_time = '';
								$lastClosingQty = 0;
							}
							
							$recInvOb = new classReceiveInvoice;
							$receive_items_array = $recInvOb->retrieveSumOfInvoiceItemsByProductID($product_id,$location_id,$start_time,$end_time);
							$rec_unitqty_recieved = $receive_items_array[0]['SUM(unitqty_recieved)'];
							$rec_net_total = $receive_items_array[0]['SUM(net_total)'];
							
							
							$saleInvOb = new classSaleInvoice;
							$sale_items_array = $saleInvOb->retrieveSumOfInvoiceItemsByProductID($product_id,$location_id,$start_time,$end_time);
							$sale_unitsaleqty = $sale_items_array[0]['SUM(unitsaleqty)'];
							$sale_net_total = $sale_items_array[0]['SUM(net_total)'];
							
							?>
							<tr>
                                <td align="left"><?php echo $product_name;?></td>
                              	<td align="right"><?php echo number_format($lastClosingQty,0);?></td>
                                <td align="right"><?php echo number_format($rec_unitqty_recieved,0);?></td>
                                <td align="right"><?php echo number_format($sale_unitsaleqty,0);?></td>
                                <td align="right"><?php echo number_format($unit_qtyinstock,0);?></td>
							</tr>
                            <?php
						}
					}
					?>
                            
                  </tbody>
                  
                  <tfoot>
                  </tfoot>
                </table>
                <!--<div class="row-fluid">
                <table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td><div class="span6" style="margin-top: -27px;">
                    <div class="dataTables_info" id="hidden-table-info_info">Showing 1 to 10 of 57 entries</div>
                  </div></td>
    <td><div class="span6">
                    <div class="dataTables_paginate paging_bootstrap pagination">
                      <ul>
                        <li class="prev disabled"><a href="#">? Previous</a></li>
                        <li class="active"><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">5</a></li>
                        <li class="next"><a href="#">Next ? </a></li>
                      </ul>
                    </div>
                  </div></td>
  </tr>
</table>
                  
                </div>-->
              </div>
            </div>
          </section>
        </div>
      </div>
      <!-- page end-->
    </section>
  <!-- page end-->
  
<!-- 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>
<script type="text/javascript" src="../datepicker/jquery-1.9.1.js"></script>
<script type="text/javascript" src="../datepicker/jquery-ui.js"></script>