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

$production_no = $_GET['production_no_text'];
$store_item_id = $_GET['store_item_id'];
$line_num = $_GET['line_num'];
$pallets_line_num = $_GET['pallets_line_num'];

$i=0;
while ($i<$line_num) {
	$plan_line_id = $_GET['plan_line_id'.$i];
	$saved_quantity = $_GET['saved_quantity'.$i];
	$quantity = $_GET['count_text'.$i];
	$count_date = $_GET['count_date'.$i];
	$difference_qty =  $quantity - $saved_quantity;
	if ($quantity!='') {
	  
	   $table_name = $_SESSION['db_prefix']."_production_plan_lines";		
	   $depallet_update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE plan_line_id = '".$plan_line_id."' ";
	   execsql($depallet_update_query);
	   
	   
	   	$table_name2 = $_SESSION['db_prefix']."_store_items";
   		$stock_total_query = "SELECT quantity FROM $table_name2 WHERE store_item_id = '".$store_item_id."'";
		$stock_total_result = mySQL_query($stock_total_query);
   		$total_quantity = mysql_result($stock_total_result ,0, "quantity");
		$total_quantity = $total_quantity + $difference_qty;
      	
      
   		$stockQuery = "UPDATE $table_name2 SET quantity = '".$total_quantity."' , lastModDate = '".$entry_date."'  WHERE store_item_id = '".$store_item_id."'";
		$stockResult = mySQL_query($stockQuery);
		
		$table_name3 = $_SESSION['db_prefix']."_item_history";
		$historyQuery = "INSERT INTO $table_name3 (store_item_id, entry, reference, reference_2, quantity, total, date, entryDate, userId) VALUES ('".$store_item_id."', '25', '".$production_no."', '', '".$difference_qty."', '".$total_quantity."','".$entry_date."','".$entry_date."', '".$_SESSION['username']."')";
		execsql($historyQuery);

	   	   
		   	  	   
	   $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']."', '76', '".$production_no."' ,'".$count_date."','".$saved_quantity."','".$quantity."' )";
	   execsql($trackingQuery);
	   
	   $table_name = $_SESSION['db_prefix']."_tracking_store";
	   $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '28', '".$production_no."' ,'".$count_date."','".$saved_quantity."','".$quantity."' )";
	   execsql($trackingQuery);
}
$i++;
}


$j=0;
while ($j<$pallets_line_num) {
	$plan_line_id = $_GET['pallets_line_id'.$j];
	$saved_quantity = $_GET['pallet_saved_quantity'.$j];
	$pallets = $_GET['pallet_count_text'.$j];
	$count_date = $_GET['count_date'.$j];
	
	if ($pallets!='') {
	  
	   $table_name = $_SESSION['db_prefix']."_production_plan_lines";		
	   $pallet_update_query = "UPDATE $table_name SET quantity = '".$pallets."' WHERE plan_line_id = '".$plan_line_id."' ";
	   execsql($pallet_update_query);
	   
	   $dif_counted = $pallets - $saved_quantity;	
	   
	   	   
	   	$table_name = $_SESSION['db_prefix']."_production_plan";
		$mix_design_query = "SELECT  mix_design_name  FROM $table_name WHERE production_no = '".$production_no."' ";
		$mix_design_result = mySQL_query($mix_design_query);
		$mix_design_num = mysql_num_rows($mix_design_result);
		$mix_design_name = mysql_result($mix_design_result, 0, "mix_design_name");
		
		$table_name = $_SESSION['db_prefix']."_mix_design";
		$packaging_query = "SELECT packaging_design FROM $table_name WHERE mix_design = '".$mix_design_name."' ";
		$packaging_result = mySQL_query($packaging_query);
		$packaging = mysql_result($packaging_result, 0, "packaging_design");
		
	   	$table_name = $_SESSION['db_prefix']."_packaging_design_lines";
		$components_query = "SELECT store_item_id, quantity FROM $table_name WHERE packaging_design = '".$packaging."' AND store_item_id <> '10001' ";
		
						
			$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");
			  	$pack_quantity = mysql_result($components_result, $j, "quantity"); 
				
			  	$used_quantity = $pack_quantity * $dif_counted;
				
			    $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."', '23', '".$production_no."', '".$used_quantity."',  '".$stock_quantity."', '".$entry_date."','".$entry_date."','".$_SESSION['username']."')";
				$historyResult = mySQL_query($historyQuery);
				
				$j++;
			}
	   

	   $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']."', '84', '".$production_no."' ,'".$count_date."','".$saved_quantity."','".$pallets."' )";
	   execsql($trackingQuery);
}
$j++;
}

?>
       <script>
	 	   window.location="../production/production_plan.php?production_no=<?php echo $production_no?>";
		</script>
