<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
include ("../navigation/header.php");
$page_name = "Stores-main";
include ("../navigation/access.php");


$table_name = $_SESSION['db_prefix']."_product_stock_count";
$product_date_query = "SELECT stock_take_date FROM $table_name WHERE sub_category = 'Production' ORDER BY stock_take_date DESC LIMIT 1 ";
$product_date_result = $con->query($product_date_query);
$product_date_num = mysqli_num_rows($product_date_result);
if ($product_date_num!=0) {
$row = mysqli_fetch_assoc($product_date_result);
$product_stock_date = $row["stock_take_date"];

$table_name = $_SESSION['db_prefix']."_product_stock_count";
$product_query = "
SELECT
product_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE sub_category = 'Production' 
AND stock_take_date = '".$product_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$product_result = $con->query($product_query);
$product_num = mysqli_num_rows($product_result);
if ($product_num>0) {
$row = mysqli_fetch_assoc($product_result);
$product_item_id = $row["product_item_id"];
$product_difference = $row["difference"];	

$table_name = $_SESSION['db_prefix']."_product_store";
$name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id."'  ";
$name_result = $con->query($name_query);
$row = mysqli_fetch_assoc($name_result);
$product_name = $row["item_name"];

}
else {
	$product_difference = '0';
	$product_name = '';
}
}
else {
	$product_difference = '0';
	$product_name = '';
	$product_stock_date = date("Y-m-d");
}


$table_name = $_SESSION['db_prefix']."_product_stock_count";
$sales_date_query = "SELECT stock_take_date FROM $table_name WHERE sub_category = 'Sales product' ORDER BY stock_take_date DESC LIMIT 1 ";
$sales_date_result = $con->query($sales_date_query);
$sales_date_num = mysqli_num_rows($sales_date_result);
if ($sales_date_num!=0) {
$row = mysqli_fetch_assoc($sales_date_result);
$sales_stock_date = $row["stock_take_date"];


$sales_name = $_SESSION['db_prefix']."_product_stock_count";
$sales_query = "
SELECT
product_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE sub_category = 'Sales product' 
AND stock_take_date = '".$sales_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$sales_result = $con->query($sales_query);
$sales_num = mysqli_num_rows($sales_result);

if ($sales_num>0) {
$row = mysqli_fetch_assoc($sales_result);
$product_item_id = $row["product_item_id"];
$sales_difference = $row["difference"];	


$table_name = $_SESSION['db_prefix']."_product_store";
$name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id."'  ";
$name_result = $con->query($name_query);
$row = mysqli_fetch_assoc($name_result);
$sales_name = $row["item_name"];
}
else { 
$sales_difference = '0';
$sales_name = '';
}
}
else {
	$sales_difference = '0';
	$sales_name = '';
	$sales_stock_date = date("Y-m-d");
}



$table_name = $_SESSION['db_prefix']."_material_stock_count";
$direct_date_query = "SELECT stock_take_date FROM $table_name WHERE category = 'Direct material' ORDER BY stock_take_date DESC LIMIT 1 ";
$direct_date_result = $con->query($direct_date_query);
$direct_date_num = mysqli_num_rows($direct_date_result);
if ($direct_date_num!=0) {
$row = mysqli_fetch_assoc($direct_date_result);
$direct_stock_date = $row["stock_take_date"];


$direct_name = $_SESSION['db_prefix']."_material_stock_count";
$direct_query = "
SELECT
store_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE category = 'Direct material' 
AND stock_take_date = '".$direct_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$direct_result = $con->query($direct_query);
$direct_num = mysqli_num_rows($direct_result);

if ($direct_num>0) {
$row = mysqli_fetch_assoc($direct_result);
$store_item_id = $row["store_item_id"];
$direct_difference = $row["difference"];


$table_name = $_SESSION['db_prefix']."_store_items";
$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
$name_result = $con->query($name_query);
$row = mysqli_fetch_assoc($name_result);
$direct_name = $row["item_name"];
}
else { 
$direct_difference = '0';
$direct_name = '';
}
}
else {
	$direct_difference = '0';
	$direct_name = '';
	$direct_stock_date = date("Y-m-d");
}


$table_name = $_SESSION['db_prefix']."_general_stock_count";
$general_date_query = "SELECT stock_take_date FROM $table_name WHERE category = 'Stores' ORDER BY stock_take_date DESC LIMIT 1 ";
$general_date_result = $con->query($general_date_query);
$general_date_num = mysqli_num_rows($general_date_result);
if ($general_date_num!=0) {
$row = mysqli_fetch_assoc($general_date_result);
$general_stock_date = $row["stock_take_date"];


$general_name = $_SESSION['db_prefix']."_general_stock_count";
$general_query = "
SELECT
store_item_id,
ABS(SUM((new_stock - old_stock)*unit_price)) AS difference_2, 
SUM((new_stock - old_stock)*unit_price) AS difference
FROM $table_name 
WHERE category = 'Stores' 
AND stock_take_date = '".$general_stock_date."'
ORDER BY difference_2 DESC
LIMIT 1
"
;
$general_result = $con->query($general_query);
$general_num = mysqli_num_rows($general_result);

if ($general_num>0) {
$row = mysqli_fetch_assoc($general_result);
$store_item_id = $row["store_item_id"];
$general_difference = $row["difference"];


$table_name = $_SESSION['db_prefix']."_store_items";
$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
$name_result = $con->query($name_query);
$row = mysqli_fetch_assoc($name_result);
$general_name = $row["item_name"];
}
else { 
$general_difference = '0';
$general_name = '';
}
}
else {
	$general_difference = '0';
	$general_name = '';
	$general_stock_date = date("Y-m-d");
}

?>



<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
</script>


</head>
<?php
if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");
?>
<body>

<div class="main_frame">
<table width="630" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td height="52" colspan="4" align="left" valign="top"><div class="page_head_frame">
        <table width="630" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="385"><label class="label_page_head">Select stock count report</label></td>
            <td align="right">&nbsp;</td>
          </tr>
        </table>
      </div>
       <br />
<br /><br />
<br />

<div class="page_middle_frame">
<form action="" method="get" >
  
  <div class="page_inside_frame" style="margin-top:15px; width:675px;">
  <br />
  <div class="page_inside_frame" style="width:150px; float:left; text-align:center; height:160px; margin-left:10px;">
    <input name="production_button" type="button" class="imagebutton" id="production_button" onclick="MM_goToURL('parent','production_stock_report.php?stock_take_date=<?php echo $product_stock_date?>');return document.MM_returnValue" value="Packed products" />
    
    <br />
    <br />
     difference recorded:<br />
    P<?php echo number_format($product_difference, 2, '.', ',') ?><br />
    <br /><br />
Last stock take:<br />
<?php if ($product_stock_date!='0000-00-00') {echo date("d-m-Y", strtotime($product_stock_date));} else { echo 'No reocords';}?>
</div>
  <!--<div class="page_inside_frame" style="width:150px; float:left; text-align:center; margin-left:20px;  height:160px;">
    <input name="sales_button" type="button" class="imagebutton" id="sales_button" onclick="MM_goToURL('parent','products_stock_report.php?stock_take_date=<?php echo $sales_stock_date?>');return document.MM_returnValue" value="Sales products" /><br />
    <br />
    difference recorded:<br />
    P<?php echo number_format($sales_difference, 2, '.', ',') ?><br />
    <br /><br />
Last stock take:<br />
<?php if ($sales_stock_date!='0000-00-00') {echo date("d-m-Y", strtotime($sales_stock_date));} else { echo 'No reocords';}?>
  </div>-->
  
</div>
 
<div class="page_inside_frame" style="margin-top:15px; width:675px;">
<div class="page_inside_frame" style="width:150px; float:left; text-align:center; height:160px; margin-left:20px;">
  <input name="direct_button" type="button" class="imagebutton" id="direct_button" onclick="MM_goToURL('parent','material_stock_report.php?stock_take_date=<?php echo $direct_stock_date?>');return document.MM_returnValue" value="Direct materials" />
  <br />
  <br />
  difference recorded:<br />
  P<?php echo number_format($direct_difference, 2, '.', ',') ?><br />
  <br />
  <br />
  Last stock take:<br />
  <?php if ($direct_stock_date!='0000-00-00') {echo date("d-m-Y", strtotime($direct_stock_date));} else { echo 'No records';}?>
</div>

<div class="page_inside_frame" style="width:150px; float:left; text-align:center; height:160px; margin-left:20px;
  ">
  <input name="general_button" type="button" class="imagebutton" id="general_button" onclick="MM_goToURL('parent','general_stock_report.php?stock_take_date=<?php echo $general_stock_date?>');return document.MM_returnValue" value="General stores" />
  <br />
  <br />
  difference recorded:<br />
  P<?php echo number_format($general_difference, 2, '.', ',') ?><br />
  <br />
  <br />
  Last stock take:<br />
  <?php if ($general_stock_date!='0000-00-00') {echo date("d-m-Y", strtotime($general_stock_date));} else { echo 'No records';}?>
</div>
</div>
</form>
</div>

    </td>
    </tr>
    </table>
</div>

<?php include ('../navigation/side_bar.php'); ?>


<?php } else {
    $_SESSION['denied_return'] = "store/store_main.php";	
    include ("../navigation/store_sidebar.php");?>
    <?php include ('../navigation/side_bar.php'); ?>
	<body onLoad="accessdenied(); return false">	
	</body>
<?php }
?>
</html>
</body>