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


if(isset($_GET['closingrepid'])&&$_GET['closingrepid']!='')
{
	$_SESSION['temp']['productclosing_id'] = (base64_decode($_GET['closingrepid']));
}

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


if(isset($_POST['reconcile'])&&$_POST['reconcile']=='Reconcile')
{
	$reconcileOb = new classProductClosingReconcilation;
	
	$reconcileOb->setClosingReportID($productclosing_id);
	$reconcileOb->setLocationID($_SESSION['security']['location_id']);
	
	for($r=0;$r<count($_POST['product_id']);$r++)
	{
		$reconcileOb->setProductID($_POST['product_id'][$r]);
		$reconcileOb->setReconcileItemQuantity($_POST['variation'][$r]);
		$reconcileOb->setClosingItemCount($_POST['unit_phy_qtyinstock'][$r]);
		$reconcileOb->setReconcileTime(time());
		$reconcileOb->reconcileProducts();
	}
	
	$updateReconcileRepOb = new classProductClosingReconcilation;
	$updateReconcileRepOb->setReconcileBy($_SESSION['security']['user_id']);
	$updateReconcileRepOb->setReconcileStatus('reconciled');
	$updateReconcileRepOb->setReconcileTime(time());
	$updateReconcileRepOb->setClosingReportID($productclosing_id);
	$updateReconcileRepOb->setClosingReportStatus('closed');
	$updateReconcileRepOb->updateReconcileReport();
	
}
/*echo '<pre>';
print_r($_POST);
print_r($_SESSION);
echo '</pre>';
exit;
*/
try
{
	$singleRepOb = new classProductClosingReconcilation;
	
	$singleRepOb->setClosingReportID($productclosing_id);
	$last_closereport_detail = $singleRepOb->retrieveClosingReportByReportID();
	$end_time= $last_closereport_detail[0]['closing_end'];
	
	$singleRepOb->setLocationID($_SESSION['security']['location_id']);
	$secondlast_closereport_detail = $singleRepOb->retrieveSecondLastClosingReport();
	$start_time = $secondlast_closereport_detail[0]['closing_end'];
	$secondlast_closingreport_id = $secondlast_closereport_detail[0]['productclosing_id'];
	
	$closing_items = $singleRepOb->retrieveClosingItemByClosingReportID();
}
catch (MyException $e)
{
	$error_message = $e->getUserMsg();
}	

/*echo '<pre>';
print_r($secondlast_closereport_detail);
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" -->PRODUCT VARIATION REPORT<!-- InstanceEndEditable --></h2>
            </header>

  <br />
  
<!-- InstanceBeginEditable name="main" -->
  <!-- page start-->
  
 <table  class="bottomBorder" cellpadding="0" cellspacing="0" width="100%">
                	
                  <thead>
                    <tr>
                        <th width="5%" align="center" >ID</th>
                        <th width="15%" align="center" >Product Name</th>
                        <th width="10%" align="center" >Last Closing Qty</th>
                        <th width="10%" align="center" >Products Received</th>
                        <th width="10%" align="center" >Products Sold</th>
                        <th width="10%" align="center" >System Qty in Stock</th>
                        <th width="10%" align="center" >Physical Qty in Stock</th>
                        <th width="10%" align="center" >Variation</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php
				  if(isset($closing_items[0]['closingitems_id']))
				  {
					  $location_id = $_SESSION['security']['location_id'];
					  
					  for($ci=0;$ci<count($closing_items);$ci++)
					  {
						  	$product_id = $closing_items[$ci]['product_id'];
						  	$unit_phy_qtyinstock = $closing_items[$ci]['unit_phy_qtyinstock'];
							$unit_qtyinstock = $closing_items[$ci]['unit_qtyinstock'];
							
							$variation = $unit_phy_qtyinstock-$unit_qtyinstock;
							
							############# receive single product detail #########################
							$prodOb = new classProduct;
							$prodOb->setProductID($product_id);
					 		$product_array = $prodOb->retrieveProductByProductID();
							
							$product_name = $product_array[0]['product_name'];
							$medicine_type = $product_array[0]['medicine_type'];
						  
						  	############# receive products this month #########################
							$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)'];
							
							############# sale products this month #########################
							$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)'];
							
							############# last closing quantity of products #########################
							if(isset($secondlast_closingreport_id))
							{
								$oldQtyOb = new classProductClosingReconcilation;
								$oldQtyOb->setClosingReportID($secondlast_closingreport_id);
								$oldQtyOb->setProductID($product_id);
								$last_closing_detail = $oldQtyOb->retrieveClosingItemByProductIDAndClosingReportID();
								$last_closing_qty = $last_closing_detail[0]['unit_phy_qtyinstock'];
							}
							else
							{
								$last_closing_qty = 0;
							}
							
						  	?>
                            
							<tr class="gradeX">
							  <td align="center"><?php echo $product_id;?></td>
							  <td align="center"><?php echo $product_name;?></td>
							  <td align="right"><?php echo $last_closing_qty;?></td>
							  <td align="right"><?php echo $rec_unitqty_recieved;?></td>
							  <td align="right"><?php echo $sale_unitsaleqty;?></td>
							  <td align="right"><?php echo $unit_qtyinstock;?></td>
							  <td align="right"><?php echo $unit_phy_qtyinstock;?></td>
							  <td align="right"><?php echo $variation;?></td>
							</tr>
                            <?php
							if($variation!=0)
							{
								?>
								<input type="hidden" name="variation[]" value="<?php echo $variation?>">
								<input type="hidden" name="unit_phy_qtyinstock[]" value="<?php echo $unit_phy_qtyinstock?>">
                                <input type="hidden" name="product_id[]" value="<?php echo $product_id?>">
                                <?php
							}
						}
				  }
				  ?>
                  </tbody>
                  <tfoot>
                  </tfoot>
                  
                </table>
  <!-- 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>