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

$production_date = $_GET['production_date'];



$table_name = $_SESSION['db_prefix']."_production_plan";
$saved_plan_query = "SELECT production_no FROM $table_name WHERE production_date = '".$production_date."' ";
$saved_plan_result = mySQL_query($saved_plan_query);
$saved_plan_num = mysql_num_rows($saved_plan_result);
$i=0;
while ($i<$saved_plan_num) {
$saved_production_no = mysql_result($saved_plan_result, $i, 'production_no');

$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$check_query = "SELECT  production_no   FROM $table_name WHERE production_no = '".$saved_production_no."' AND action = 'skip' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);



if ($check_num==1) {
	
$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$saved_qty_query = "SELECT mix_design_name, quantity FROM $table_name WHERE production_no = '".$saved_production_no."' AND action = 'skip' ";
$saved_qty_result = mySQL_query($saved_qty_query);
$saved_mix_design_name = mysql_result($saved_qty_result, 0, "mix_design_name");
$saved_quantity = mysql_result($saved_qty_result, 0, "quantity");	

$production_no = $_GET['production_no'.$i];
$quantity = $_GET['quantity_text'.$i];

if ($saved_quantity!=$quantity) {
	
	   	$table_name = $_SESSION['db_prefix']."_mix_design";
		$design_name_query = "SELECT design_no, est_qty FROM $table_name WHERE mix_design = '".$saved_mix_design_name."' ";
		$design_name_result = mySQL_query($design_name_query);
		$design_name_num = mysql_num_rows($design_name_result);
		$est_qty = mysql_result($design_name_result, 0, "est_qty");
		$mix_design_no = mysql_result($design_name_result, 0, "design_no");
		
		$new_est_qty = $est_qty * $quantity;
		
	  
	   $table_name = $_SESSION['db_prefix']."_production_plan_lines";
	   $quantity_update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE production_no = '".$production_no."'  AND action = 'skip' ";
	   execsql($quantity_update_query);
	   
	   $table_name = $_SESSION['db_prefix']."_production_plan_lines";
	   $est_quantity_update_query = "UPDATE $table_name SET quantity = '".$new_est_qty."' WHERE production_no = '".$production_no."'  AND action = 'estimated' ";
	   execsql($est_quantity_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."' ,'".$mix_design_no."' ,'".$saved_quantity."', '".$quantity."' )";
		execsql($trackingQuery);
}
}
else {
		$production_no = $_GET['production_no'.$i];
		$mix_design_name = $_GET['mix_design_name'.$i];
		$quantity = $_GET['quantity_text'.$i];
		$table_name = $_SESSION['db_prefix']."_production_plan";
		$check_query = "SELECT cancelled FROM $table_name WHERE production_no = '".$production_no."' ";
		$check_result = mySQL_query($check_query);
		$check = mysql_result($check_result, 0, "cancelled");
	
		if ($check=='no') {
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$insert_query = "INSERT INTO $table_name (production_no, mix_design_name, action, action_date, quantity) VALUES ('".$production_no."', '".$mix_design_name."', 'skip', '".$entry_date."', '".$quantity."' ) ";
		execsql($insert_query);
		
		$table_name = $_SESSION['db_prefix']."_mix_design";
		$number_query = "SELECT est_qty FROM $table_name WHERE mix_design = '".$mix_design_name."'  ";
		$number_result = mySQL_query($number_query);
		$est_qty = mysql_result($number_result, 0, "est_qty");
		$est_quantity = $quantity * $est_qty;
				
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$insert_query = "INSERT INTO $table_name (production_no, mix_design_name, action, action_date, quantity) VALUES ('".$production_no."', '".$mix_design_name."', 'estimated', '".$entry_date."', '".$est_quantity."' ) ";
		execsql($insert_query);
		
		
		
		
		}
}
$i++;
}

if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 	   window.location="production_main.php?date=";
		</script>
        <?php		 
	   }
	   
	if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	       window.location="edit_production_plan.php?production_date=<?php echo $production_date?>&e=";
	   </script>
        <?php		 
	   }

	   ?>

