<?php
include('../include/session.php');

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$line_num = $_GET['line_num'];
$count_date = $_GET['count_date_text'];

$i=0;
while ($i<$line_num) {
	$stockId = $_GET['stockId_text'.$i];
	$store_item_id = $_GET['store_item_id_text'.$i];
	$counted_qty = $_GET['counted_qty_text'.$i];
	$item_name = $_GET['item_name_text'.$i];
	
	$table_name = $_SESSION['db_prefix']."_stockloss";
	$saved_query = "SELECT total FROM $table_name WHERE stockId = '".$stockId."' ";
	$saved_result = mySQL_query($saved_query);
	$saved_total = mysql_result($saved_result, 0, "total");
	
	$table_name = $_SESSION['db_prefix']."_stockloss";
	$count_update_query = "UPDATE $table_name SET total = '".$counted_qty."' WHERE stockId = '".$stockId."' ";
	execsql($count_update_query);
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$stock_update_query = "UPDATE $table_name SET quantity = '".$counted_qty."' WHERE store_item_id = '".$store_item_id."' ";
	execsql($stock_update_query);
	
	if ($saved_total!=$counted_qty) {
		$table_name = $_SESSION['db_prefix']."_tracking_store";
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '29', '".$item_name."' ,'".$saved_total."','".$counted_qty."','Stock count' )";
		$trackingResult = mySQL_query($trackingQuery);
}
	
	$i++;
}
	
if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../store/store_main.php?date=&branch=&subCatSelect=Tiles";
		</script>
        <?php		 
	   }
	   
if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	          window.location="edit_stock_count.php?count_date=<?php echo $count_date?>&show=1";
	   </script>
        <?php		 
	   }
	   ?>