<?php
include("../include/session.php");

	
		
	$user_id = $_SESSION['username'];
	
	date_default_timezone_set('Africa/Gaborone');
	$entry_date = date("Y-m-d H:i:s", time()); 
	
	$table_name = $_SESSION['db_prefix']."_production_plan_lines";
	$production_line_query = "SELECT * FROM $table_name WHERE production_no = '".$_SESSION['production_no']."' ";
	$production_line_result = mySQL_query($production_line_query);
	$_SESSION['production_line_num'] = mysql_num_rows($production_line_result);
	 
	$i=0;
	while ($i<$_SESSION['production_line_num']) {
		   $mix_design_no =  mysql_result($production_line_result, $i, "mix_design_no");
		   $mix_design_name = mysql_result($production_line_result, $i, "mix_design_name");
		   
		   
		    $table_name = $_SESSION['db_prefix']."_depallet_count_temp";
			$produced_query = "SELECT depallet_quantity, pallets FROM $table_name WHERE production_no = '".$_SESSION['production_no']."' AND mix_design_no = '".$mix_design_no."' and user_name = '".$_SESSION['username']."'  ";
			$produced_result = mySQL_query($produced_query);
			$produced_num = mysql_num_rows($produced_result);
			if ($produced_num>0) {
			$depallet_quantity = mysql_result($produced_result, 0, "depallet_quantity");
			$pallets = mysql_result($produced_result, 0, "pallets");
			}
			else {
				$depallet_quantity='';
			}
			
			$table_name = $_SESSION['db_prefix']."_store_items";
			$update_query = "UPDATE $table_name SET quantity = quantity+'".$depallet_quantity."' WHERE item_name = '".$mix_design_name."' ";
			execsql($update_query);
			
			$table_name = $_SESSION['db_prefix']."_store_items";
			$stock_query = "SELECT store_item_id, quantity  FROM $table_name WHERE item_name = '".$mix_design_name."' ";
			$stock_result = mySQL_query($stock_query);
			$stock_num = mysql_num_rows($stock_result);
			$store_item_id = mysql_result($stock_result, 0, "store_item_id");
			$stock_quantity = mysql_result($stock_result, 0, "quantity");
						
					   
		    $table_name2 = $_SESSION['db_prefix']."_item_history";	
			$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, reference_2,  quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '21', '".$_SESSION['production_no']."', '".$_SESSION['batch_code']."', '".$depallet_quantity."',  '".$stock_quantity."', '".$_SESSION['production_date']."','".$entry_date."','".$_SESSION['username']."')";
			echo '<br>*****'.$historyQuery;
			$historyResult = mySQL_query($historyQuery);
		   
		  							
			$table_name = $_SESSION['db_prefix']."_production_plan_lines";
			$update_query = "UPDATE $table_name SET depalleted = '".$depallet_quantity."', pallets = '".$pallets."', depallet_date = '".$_SESSION['depallet_date']."'  WHERE production_no = '".$_SESSION['production_no']."' AND mix_design_no = '".$mix_design_no."' ";
			execsql($update_query);


			$table_name = $_SESSION['db_prefix']."_packaging_design_lines";
			$components_query = "SELECT store_item_id, quantity FROM $table_name WHERE design_no = '".$mix_design_no."'  ";
			$components_result = mySQL_query($components_query);
			$components_num = mysql_num_rows($components_result);
			$j=0;
			while ($j<$components_num) {
			  	$store_item_id = mysql_result($components_result, $j, "store_item_id");
			  	$quantity = mysql_result($components_result, $j, "quantity"); 
			  	$used_quantity = $quantity * $pallets;
			  
				$table_name = $_SESSION['db_prefix']."_store_items";
				$stock_query = "UPDATE $table_name SET quantity = quantity-'".$used_quantity."' WHERE store_item_id = '".$store_item_id."' ";
				execsql($stock_query); 
				
				$table_name = $_SESSION['db_prefix']."_store_items";
				$stock_query = "SELECT quantity FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
				$stock_result = mySQL_query($stock_query);
				$stock_quantity = mysql_result($stock_result, 0, "quantity");
				
				$table_name2 = $_SESSION['db_prefix']."_item_history";	
				$historyQuery = "INSERT INTO $table_name2 (store_item_id, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '20', '".$_SESSION['production_no']."', '-".$used_quantity."',  '".$stock_quantity."', '".$_SESSION['production_date']."','".$entry_date."','".$_SESSION['username']."')";
				$historyResult = mySQL_query($historyQuery);
				
				$j++;
			}
			$i++;
	}
			
$table_name = $_SESSION['db_prefix']."_production_plan";
$plan_query = "UPDATE $table_name SET depalleted = 'yes' WHERE production_no = '".$_SESSION['production_no']."' ";
execsql($plan_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']."', '62', '".$_SESSION['production_date']."' ,'".$_SESSION['production_no']."','','' )";
$trackingResult = mySQL_query($trackingQuery);	    	
			
	
	
	if (isset($_GET['new_button']) )
	   {
		 $action='depallet_countWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
	   $action='day_production_display.php?production_no='.$_SESSION['production_no'];
	  }
	
	 
 	 $_SESSION['production_no']='';
	 $_SESSION['production_line_num']=0;
	 
	 $table_name = $_SESSION['db_prefix']."_depallet_count_temp";
	 $delete_query = "DELETE FROM $table_name WHERE user_name = '".$_SESSION['username']."' "; 
	 execsql($delete_query);

	?> 

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



