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

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

$line_num = $_POST['line_num'];
$stock_take_date = $_POST['stock_take_date_text'];

$i=0;
while ($i<$line_num) {
	$count_id = $_POST['count_id_text'.$i];
	$product_item_id = $_POST['product_item_id_text'.$i];
	$counted_qty = $_POST['counted_qty_text'.$i];
	$item_name = $_POST['item_name_text'.$i];
	$batch_code = $_POST['batch_code_text'.$i];
	
	$table_name = $_SESSION['db_prefix']."_product_stock_count";
	$saved_query = "SELECT new_stock FROM $table_name WHERE count_id = '".$count_id."' ";
	$saved_result = $con->query($saved_query);
	$row = mysqli_fetch_assoc($saved_result);
	$saved_total = $row['new_stock'];
	
	
	$table_name = $_SESSION['db_prefix']."_product_stock_count";
	$count_update_query = "UPDATE $table_name SET new_stock = '".$counted_qty."' WHERE count_id = '".$count_id."' ";
	$count_update_result = $con->query($count_update_query);
	
	$table_name = $_SESSION['db_prefix']."_product_store_lines";
	$stock_update_query = "UPDATE $table_name SET quantity_in = '".$counted_qty."' + quantity_out WHERE product_item_id = '".$product_item_id."' AND batch_code = '".$batch_code."' ";
	$stock_update_result = $con->query($stock_update_query);
	
	$table_name = $_SESSION['db_prefix']."_product_store_lines";
	$stock_quantity_query = "SELECT SUM(quantity_in-quantity_out) AS stock_quantity FROM $table_name WHERE product_item_id = '".$product_item_id."'  ";
	$stock_quantity_result = $con->query($stock_quantity_query);
	$row = mysqli_fetch_assoc($stock_quantity_result);
	$stock_quantity = $row['stock_quantity'];
	
	$table_name3 = $_SESSION['db_prefix']."_product_store";
	$count_query = "UPDATE $table_name3 SET quantity='".$stock_quantity."' WHERE product_item_id = '".$product_item_id."' ";
	$count_result = $con->query($count_query);
	
	$difference = $counted_qty - $saved_total;
	
	if ($difference!=0) {
		
	$table_name4 = $_SESSION['db_prefix']."_product_history";
	$history_query = "INSERT INTO $table_name4 (product_item_id, entry, reference, reference_2, quantity, total, date, entryDate, userId) VALUES ('".$product_item_id."','12','5', '".$batch_code."', '".$difference."',  '".$stock_quantity."', '".$entry_date."', '".$entry_date."', '".$_SESSION['username']."')";
	$history_result = $con->query($history_query);
			
	if ($saved_total!=$counted_qty) {
		$table_name = $_SESSION['db_prefix']."_tracking_store";
	    $tracking_query = "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' )";
		$tracking_result = $con->query($tracking_query);
	}
	}
	
	$i++;
}
	
if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../store/store_main.php?date=&branch=&subCatSelect=Tiles";
		</script>
        <?php		 
	   }
	   
if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	        window.location="edit_sales_stock_count.php?stock_take_date=<?php echo $stock_take_date?>&show=1";
	   </script>
        <?php		 
	   }
	   ?>