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

$line_num = $_GET['line_num'];

$i=0;
while ($i<$line_num) {		
$production_no = $_GET['production_no'.$i];

if ($production_no!='') {
$mix_design_no = $_GET['mix_design_no'.$i];
$mix_design_name = $_GET['mix_design_name'.$i];
$sub_category = $_GET['sub_category'.$i];
$skip_quantity = $_GET['skip_quantity'.$i];
$est_quantity = $_GET['est_quantity'.$i];
$design_cost = $_GET['design_cost'.$i];
$item_type = $_GET['item_type'.$i];

	
	$table_name = $_SESSION['db_prefix']."_production_plan";
	$production_plan_query = "INSERT INTO $table_name (production_no, production_date, mix_design_name, mix_design_cost, batch_code, sub_category, item_type, planned) VALUES ('".$production_no."', '".$_SESSION['production_date']."', '".$mix_design_name."', '".$design_cost."', '".$_SESSION['batch_code']."', '".$sub_category."', '".$item_type."', 'yes' ) ";
	execsql($production_plan_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."', 'skip', '".$_SESSION['production_date']."', '".$skip_quantity."' ) ";
	execsql($production_query);
	
	$table_name = $_SESSION['db_prefix']."_production_plan_lines";
	$est_production_query = "INSERT INTO $table_name (production_no, mix_design_name, action, action_date, quantity) VALUES ('".$production_no."', '".$mix_design_name."', 'estimated', '".$_SESSION['production_date']."', '".$est_quantity."' ) ";
	execsql($est_production_query);
	
	$table_name = $_SESSION['db_prefix']."_store_items";
	$type_query = "SELECT item_type FROM $table_name WHERE  item_name = '".$mix_design_name."' ";
	$type_result = mySQL_query($type_query);
	$item_type = mysql_result($type_result, 0, "item_type");
	
	
//if ($sub_category=='Tiles') {
//	$table_name = $_SESSION['db_prefix']."_wet_tile_spec";
//	$quality_query = "INSERT INTO $table_name (production_no, production_date, mix_design_name, item_type) VALUES ('".$production_no."', '".$_SESSION['production_date']."', '".$mix_design_name."', '".$item_type."' ) ";
//	execsql($quality_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']."', '60', '".$_SESSION['production_date']."' ,'".$production_no."','','' )";
	$trackingResult = mySQL_query($trackingQuery);

}
	$i++; 
	}


	//$table_name = $_SESSION['db_prefix']."_dry_tile_spec";
//	$quality_query = "INSERT INTO $table_name (batch_code, production_date) VALUES ('".$_SESSION['batch_code']."', '".$_SESSION['production_date']."' ) ";
//	execsql($quality_query);
	
	$table_name = $_SESSION['db_prefix']."_production_plan_lines_temp";
	$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."' "; 
	execsql($delete_query);
	
	$_SESSION['sub_category'] = '';
	$_SESSION['store_item_id'] = '';
	$_SESSION['mix_design'] = '';
	$_SESSION['item_type'] = '';
	$_SESSION['number'] = '';
	$_SESSION['production_date']=date("Y-m-d");
	$_SESSION['batch_code'] = ''; 
	
	
	
	if (isset($_GET['new_button']) )
	   {
		 $action='production_planWizbase.php?error=0&p=1&s=0';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		 $action='production_plan_summary.php?production_date='.$_SESSION['production_date'];
	  }
	
?>

<script language="javascript" type="text/javascript">
        
		  window.location="<?php echo $action ?> ";
        
       </script>
