<?php
include('../include/session.php');
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 

$production_no =$_GET['production_no_text'];
$mix_design_name =$_GET['mix_design_name_text'];
$skips =$_GET['skip_text'];
$count =$_GET['counted_text'];
$count_date =$_GET['count_date'];
$final =$_GET['final_group'];
$count_alert = $_GET['count_alert'];

$ip_address = $_SERVER['REMOTE_ADDR'];

$table_name = "test_tracking_log";
$track_check_query = "INSERT INTO $table_name (entry_date, user, company, page, location) VALUES ('".$entry_date."' ,'".$_SESSION['username']."','Radstone', 'wet_count_save.php', '".$ip_address."' )";
execsql($track_check_query);


$table_name = $_SESSION['db_prefix']."_production_plan";
$update_query = "UPDATE $table_name SET produced_wet = '".$final."' WHERE production_no = '".$production_no."' ";
execsql($update_query);

$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$skip_query = "INSERT INTO $table_name (production_no, mix_design_name, action, action_date, quantity) VALUES ('".$production_no."', '".$mix_design_name."', 'skip_count', '".$count_date."', '".$skips."' ) ";
execsql($skip_query);

$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$production_query = "INSERT INTO $table_name (production_no, mix_design_name, action, action_date, quantity) VALUES ('".$production_no."', '".$mix_design_name."', 'wet_count', '".$count_date."', '".$count."' ) ";
execsql($production_query);




$table_name = $_SESSION['db_prefix']."_mix_design_lines";
$components_query = "SELECT store_item_id, quantity FROM $table_name WHERE mix_design = '".$mix_design_name."'  ";
$components_result = mySQL_query($components_query);
$components_num = mysql_num_rows($components_result);
			$j=0;
			while ($j<$components_num) {
			  	$store_item_id = mysql_result($components_result, $j, "store_item_id");
			  	$quantity = mysql_result($components_result, $j, "quantity"); 
			  	$used_quantity = $quantity * $skips;
			  
				$table_name = $_SESSION['db_prefix']."_store_items";
				$stock_query = "UPDATE $table_name SET quantity = quantity-'".$used_quantity."' WHERE store_item_id = '".$store_item_id."' ";
				execsql($stock_query); 
				
				$table_name = $_SESSION['db_prefix']."_store_items";
				$stock_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
				$stock_result = mySQL_query($stock_query);
				$stock_quantity = mysql_result($stock_result, 0, "quantity");
				
				$table_name2 = $_SESSION['db_prefix']."_item_history";	
				$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '20', '".$production_no."', '-".$used_quantity."',  '".$stock_quantity."', '".$count_date."','".$entry_date."','".$_SESSION['username']."')";
				$historyResult = mySQL_query($historyQuery);
				
				$j++;
			}
	
	$table_name = $_SESSION['db_prefix']."_tracking_production";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' , '".$_SESSION['username']."', '61', '".$count_date."' ,'".$production_no."','".$skips."','".$count."' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	if ($count_alert!='no') {
	   $table_name = $_SESSION['db_prefix']."_tracking_production";
	   $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '87', '".$production_no."' ,'".$skips."','".$count_alert."','".$count."' )";
	   execsql($trackingQuery);
	   }		


?>

<script language="javascript" type="text/javascript">
          window.location="production_process.php";
</script>

