 <?php
include("../include/session.php");
	
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	
	$table_name = $_SESSION['db_prefix']."_mix_design";
	$design_no_query = "SELECT design_no FROM $table_name ORDER BY design_no DESC LIMIT 1  ";
	$design_no_result = $con->query($design_no_query);
	$design_no_num = mysqli_num_rows($design_no_result);
	if ($design_no_num==0) {
		  $design_no = 1;
	}
	else {
		$row = mysqli_fetch_assoc($design_no_result);
		$design_no = $row['design_no'] + 1;
	}
	
	$design_cost = $_GET['design_cost'];
	
	
	$table_name = $_SESSION['db_prefix']."_mix_design";
	$mix_design_query = "INSERT INTO $table_name (design_no, product_type, mix_design, design_cost, est_qty) VALUES ('".$design_no."', '".$_SESSION['sub_category']."', '".$_SESSION['mix_design']."', '".$design_cost."', '".$_SESSION['number']."') ";
	$mix_design_result = $con->query($mix_design_query);
	
	$table_name = $_SESSION['db_prefix']."_mix_design_lines_temp";
	$mix_query = "SELECT sub_category, store_item_id, quantity FROM $table_name WHERE mix_design = '".$_SESSION['mix_design']."' AND user_name = '".$_SESSION['username']."' ";
	$mix_result = $con->query($mix_query);
	$mix_num = mysqli_num_rows($mix_result);
	
	$table_name = $_SESSION['db_prefix']."_product_store";
	$update_query = "UPDATE $table_name SET mix_design_id = '".$design_no."' WHERE product_item_id = '".$_SESSION['product_item_id']."' ";
	$update_result = $con->query($update_query);
	
		
	$i=0;
	while ($i<$mix_num) {
		$row = mysqli_fetch_assoc($mix_result);
		$sub_category = $row['sub_category'];
		$store_item_id = $row['store_item_id'];
		$quantity = $row['quantity'];
			   
	   	$table_name = $_SESSION['db_prefix']."_mix_design_lines";
		$mix_design_lines_query = "INSERT INTO $table_name (design_no, sub_category, mix_design, store_item_id, quantity) VALUES ('".$design_no."', '".$sub_category."', '".$_SESSION['mix_design']."', '".$store_item_id."', '".$quantity."') ";
		$mix_design_lines_result = $con->query($mix_design_lines_query);
	
	$i++;
	}

	$table_name = $_SESSION['db_prefix']."_tracking_production";
	$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' , '".$_SESSION['username']."', '64', '".$design_no."' ,'','','' )";
	$tracking_result = $con->query($tracking_query);
	
	$_SESSION['sub_category'] = '';
	$_SESSION['store_item_id'] = '';
	$_SESSION['mix_design'] = '';
	$_SESSION['number'] = '';
	 
	
	$table_name = $_SESSION['db_prefix']."_mix_design_lines_temp";
	$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."' "; 
	$delete_result = $con->query($delete_query);
	
	
	if (isset($_GET['new_button']) )
	   {
		 $action='new_mix_designWizbase.php?error=0&p=1&s=0';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='mix_design_display.php?design_no='.$design_no ;
	  }
	
?>
<?php
   
	 
?>

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