<?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'];
$skip_counted = $_GET['skip_counted_text'];
$counted = $_GET['counted_text'];

	
$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$saved_skip_query = "SELECT SUM(quantity) AS summed_quantity FROM $table_name WHERE production_no = '".$production_no."' AND action = 'skip_count' GROUP BY production_no ";
$saved_skip_result = mySQL_query($saved_skip_query);
$saved_skip = mysql_result($saved_skip_result, 0, "summed_quantity");


if ($saved_skip!=$skip_counted) {
	   $table_name = $_SESSION['db_prefix']."_production_plan_lines";		
	   $skip_update_query = "UPDATE $table_name SET quantity = '".$skip_counted."' WHERE production_no = '".$production_no."'  AND action = 'skip_count' ";
	   execsql($skip_update_query);
	   	  	   
	   $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']."', '74', '".$production_no."' ,'','".$saved_skip."','".$skip_counted."' )";
	   execsql($trackingQuery);
	   
	   $dif_counted = $skip_counted - $saved_skip;	     
	   
	   	$table_name = $_SESSION['db_prefix']."_production_plan";
		$mix_design_query = "SELECT  mix_design_name  FROM $table_name WHERE production_no = '".$production_no."' ";
		$mix_design_result = mySQL_query($mix_design_query);
		$mix_design_num = mysql_num_rows($mix_design_result);
		$mix_design_name = mysql_result($mix_design_result, 0, "mix_design_name");
	   
	  		$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 * $dif_counted;
			    $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."', '22', '".$production_no."', '".$used_quantity."',  '".$stock_quantity."', '".$entry_date."','".$entry_date."','".$_SESSION['username']."')";
				$historyResult = mySQL_query($historyQuery);
				
				$j++;
			}
	   
	   
		
}

$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$saved_counted_query = "SELECT SUM(quantity) AS summed_quantity FROM $table_name WHERE production_no = '".$production_no."' AND action = 'wet_count' GROUP BY production_no ";
$saved_counted_result = mySQL_query($saved_counted_query);
$saved_counted = mysql_result($saved_counted_result, 0, "summed_quantity");

if ($saved_counted!=$counted) {
	  		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";		
	   	$est_update_query = "UPDATE $table_name SET quantity = '".$counted."' WHERE production_no = '".$production_no."'  AND action = 'wet_count' ";
	   	execsql($est_update_query);
	   
	  	   
	   $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']."', '75', '".$production_no."' ,'','".$saved_counted."','".$counted."' )";
	   execsql($trackingQuery);
		
}




?>
       <script>
	 		window.location="../production/production_plan.php?production_no=<?php echo $production_no?>";
		</script>
