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


if (isset($_GET['count_button']) )  {
	
$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' 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!=$counted) {
	   $table_name = $_SESSION['db_prefix']."_production_plan_lines";		
	   $skip_update_query = "UPDATE $table_name SET quantity = '".$counted."' WHERE production_no = '".$production_no."'  AND action = 'skip' ";
	   execsql($skip_update_query);
	   
	   	$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_name = mysql_result($mix_design_result, 0, "mix_design_name");
	   
	   	$table_name = $_SESSION['db_prefix']."_mix_design";
		$est_query = "SELECT est_qty FROM $table_name WHERE mix_design = '".$mix_design_name."' ";
		$est_result = mySQL_query($est_query);
		$est_qty = mysql_result($est_result, 0, "est_qty")*$counted;
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";		
	   	$est_update_query = "UPDATE $table_name SET quantity = '".$est_qty."' WHERE production_no = '".$production_no."'  AND action = 'estimated' ";
	   	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']."', '72', '".$production_no."' ,'','".$saved_skip."','".$counted."' )";
	   execsql($trackingQuery);
?>	   
	   <script>
	 		window.location="../production/production_plan.php?production_no=<?php echo $production_no?>";
		</script>
<?php		
}
}

if (isset($_GET['cancel_button']) )  {
	
		$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_name = mysql_result($mix_design_result, 0, "mix_design_name");
	   
	   	$table_name = $_SESSION['db_prefix']."_production_plan";
		$delete_query = "DELETE FROM $table_name WHERE production_no = '".$production_no."'  "; 
		execsql($delete_query);
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$delete_query = "DELETE FROM $table_name WHERE production_no = '".$production_no."'  "; 
		execsql($delete_query);
		
		$table_name = $_SESSION['db_prefix']."_wet_tile_spec";
		$delete_query = "DELETE FROM $table_name WHERE production_no = '".$production_no."'  ";
		execsql($delete_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']."', '73', '".$production_no."' ,'".$mix_design_name."','','' )";
	   execsql($trackingQuery);
	   
?>

<script>
	 		window.location="../production/production_main.php";
		</script>
        
<?	   
	
	
}

?>
       
