<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php 
include ("../navigation/header.php");

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

if ($permission_num==1) {

include ("../navigation/store_sidebar.php");

$stock_take_date = $_GET['stock_take_date'];
$stock_take_date = date("Y-m-d", strtotime($stock_take_date ));


$table_name = $_SESSION['db_prefix']."_product_stock_count";
$stock_take_date_query = "SELECT DISTINCT stock_take_date FROM $table_name  ORDER BY stock_take_date DESC";
	$stock_take_date_result = $con->query($stock_take_date_query);
	$stock_take_date_num = mysqli_num_rows($stock_take_date_result);
	$w = 0;
	$arrId2 = array ();
	$arrName2 = array ();
	while($row = $stock_take_date_result->fetch_assoc()) {
		$arrId2[$w] = $row['stock_take_date'];
		$arrName2[$w] = $row['stock_take_date'];
		$w++;
	}

$table_name = $_SESSION['db_prefix']."_product_stock_count";
$stock_take_query = "SELECT product_item_id, product_item_name, batch_code, old_stock, new_stock, unit_price   FROM $table_name WHERE stock_take_date = '".$stock_take_date."' AND sub_category = 'Production' ";
$stock_take_result = $con->query($stock_take_query);

?>

</head>

<body>

<div class="main_frame">
 
  <div class="page_head_frame">
    <label class="label_page_head">Sales Product stock report</label></div>
<div class="page_middle_frame">


<form action="products_stock_report.php" method="get">
<div class="page_inside_frame">&nbsp;
  <select name="stock_take_date" class="select200" id="stock_take_date">
    <option value="<?php echo $stock_take_date ?>" selected="selected"><?php echo date("l, d-m-Y", strtotime($stock_take_date )) ?></option>
    <?php
        	for ($j = 0; $j < count($arrId2); $j++) {
    		echo "<option value=" . "\"" . $arrId2[$j] . "\"" . ">" . date("l, d-m-Y", strtotime($arrName2[$j] )) . "</option>";
	}
?>
  </select>
  <input type="submit" name="date_select_button" id="date_select_button" value="Select" class="imagebutton">
</div>
</form>
<br /><br /><br />
<div class="page_inside_frame">
Stock report for <?php echo date("l, d-m-Y", strtotime($stock_take_date )) ?><br />
<br />
<div >
Blocks in curing
  <table width="680" id="table_display">
 <thead>
  <tr>
    <th width="208">Product</th>
    <th width="141">Batch code</th>
    <th width="86">Unit price</th>
    <th width="86">Old stock</th>
    <th width="86">New stock</th>
    <th width="67">+ / -</th>
    <th width="67">Loss/gain</th>
    </tr>
 </thead>
<?php
$i=0;
$loss_gain_tot = 0;
while($row = $stock_take_result->fetch_assoc()) {
$product_item_id = $row["product_item_id"];
$product_item_name = $row["product_item_name"];
$batch_code = $row["batch_code"];

$old_stock = $row["old_stock"];
$new_stock = $row["new_stock"];
$unit_price = $row["unit_price"];
$difference = $new_stock - $old_stock;

$loss_gain = $difference * $unit_price;
$loss_gain_tot = $loss_gain_tot + $loss_gain;
?>
 
  <tr>
    <td><?php echo $product_item_name ?></td>
    <td align="left"><?php echo $batch_code ?></td>
    <td align="right"><?php echo number_format($unit_price, 2, '.', ',') ?></td>
    <td align="right"><?php echo $old_stock ?></td>
    <td align="right"><?php echo $new_stock ?></td>
    <td align="right"><?php if ($difference<0) {echo '<font color=red>'.$difference.'</font>'; } else { echo $difference; } ?></td>
    <td align="right"><?php if ($difference<0) {echo '<font color=red>'.number_format($loss_gain, 2, '.', ',').'</font>'; } else { echo number_format($loss_gain, 2, '.', ','); } ?></td>
    </tr>
<?php
$i++;
}
?>
<tfoot>
<tr>
    <td colspan="6" align="right">Total loss/gain:&nbsp;&nbsp;</td>
    <td align="right"><?php if ($loss_gain_tot<0) {echo '<font color=red>'.number_format($loss_gain_tot, 2, '.', ',').'</font>'; } else { echo number_format($loss_gain_tot, 2, '.', ','); } ?></td>
  </tr>
</tfoot>
</table>


</div>

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

