<?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']."_packaging_design";
	$design_no_query = "SELECT design_no FROM $table_name ORDER BY design_no DESC LIMIT 1  ";
	$design_no_result = mySQL_query($design_no_query);
	$design_no_num = mysql_num_rows($design_no_result);
	if ($design_no_num==0) {
		  $design_no = 1;
	}
	else {
		$design_no = mysql_result($design_no_result, 0, "design_no") + 1;
	}
		
	
	$table_name = $_SESSION['db_prefix']."_packaging_design";
	$packaging_design_query = "INSERT INTO $table_name (design_no, packaging_design, est_qty) VALUES ('".$design_no."', '".$_SESSION['packaging_design']."', '".$_SESSION['number']."') ";
	execsql($packaging_design_query);
	
	$table_name = $_SESSION['db_prefix']."_packaging_design_lines_temp";
	$mix_query = "SELECT store_item_id, quantity FROM $table_name WHERE packaging_design = '".$_SESSION['packaging_design']."' AND user_name = '".$_SESSION['username']."' ";
	$mix_result = mySQL_query($mix_query);
	$mix_num = mysql_num_rows($mix_result);
	
	//$table_name = $_SESSION['db_prefix']."_store_items";
//	$update_query = "UPDATE $table_name SET design_id = '".$design_no."' WHERE store_item_id = '".$_SESSION['store_item_id']."' ";
//	execsql($update_query);
	
	$i=0;
	while ($i<$mix_num) {
       	$store_item_id = mysql_result($mix_result, $i, "store_item_id");
	   	$quantity = mysql_result($mix_result, $i, "quantity");
	   
	   	$table_name = $_SESSION['db_prefix']."_packaging_design_lines";
		$packaging_design_lines_query = "INSERT INTO $table_name (design_no, packaging_design, store_item_id, quantity) VALUES ('".$design_no."', '".$_SESSION['packaging_design']."', '".$store_item_id."', '".$quantity."') ";
		execsql($packaging_design_lines_query);
	
	$i++;
	}

	$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']."', '77', '".$design_no."' ,'','','' )";
	$trackingResult = mySQL_query($trackingQuery);
	
	$_SESSION['sub_category'] = '';
	$_SESSION['store_item_id'] = '';
	$_SESSION['packaging_design'] = '';
	$_SESSION['number'] = '';
	 
	
	$table_name = $_SESSION['db_prefix']."_packaging_design_lines_temp";
	$delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."' "; 
	execsql($delete_query);
	
	
	if (isset($_GET['new_button']) )
	   {
		 $action='packing_designWizbase.php?error=0&p=1&s=0';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='packaging_design_display.php?design_no='.$design_no ;
	  }
	
?>
<?php
   
	 
?>

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