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

$design_no = $_GET['design_no_text'];

$table_name = $_SESSION['db_prefix']."_mix_design";
$saved_est_design_query = "SELECT est_qty, packaging_design  FROM $table_name WHERE design_no = '".$design_no."' ";
$saved_est_design_result = mySQL_query($saved_est_design_query);
$saved_est_design_num = mysql_num_rows($saved_est_design_result);
$saved_est_qty = mysql_result($saved_est_design_result, 0, 'est_qty');
$saved_packaging_design = mysql_result($saved_est_design_result, 0, 'packaging_design');
$est_qty  = $_GET['est_qty_text'];
$packaging_design  = $_GET['design_select'];

if ($saved_est_qty!=$est_qty) {
	   	
	   $table_name = $_SESSION['db_prefix']."_mix_design";
	   $est_query = "UPDATE $table_name SET est_qty = '".$est_qty."'  WHERE design_no = '".$design_no."' ";
	   execsql($est_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']."', '69', '".$design_no."' ,'".$saved_est_qty."', '".$est_qty."', 'Quantity' )";
	   execsql($trackingQuery);
		
}

if ($saved_packaging_design!=$packaging_design) {
	   	
	   $table_name = $_SESSION['db_prefix']."_mix_design";
	   $est_query = "UPDATE $table_name SET packaging_design = '".$packaging_design."'  WHERE design_no = '".$design_no."' ";
	   execsql($est_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']."', '85', '".$design_no."' ,'".$saved_packaging_design."', '".$packaging_design."', 'Packaging design' )";
	   execsql($trackingQuery);
		
}

$table_name = $_SESSION['db_prefix']."_mix_design_lines";
$saved_mix_design_query = "SELECT store_item_id, quantity FROM $table_name WHERE design_no = '".$design_no."' ";
$saved_mix_design_result = mySQL_query($saved_mix_design_query);
$saved_mix_design_num = mysql_num_rows($saved_mix_design_result);
$i=0;
while ($i<$saved_mix_design_num) {
$saved_store_item_id = mysql_result($saved_mix_design_result, $i, 'store_item_id');
$saved_quantity = mysql_result($saved_mix_design_result, $i, 'quantity');

$store_item_id = $_GET['store_item_id'.$i];
$quantity = $_GET['quantity_text'.$i];


if ($quantity<>0) {

if ($saved_store_item_id!=$store_item_id) {
	   	
	   $table_name = $_SESSION['db_prefix']."_mix_design_lines";
	   $mix_update_query = "UPDATE $table_name SET store_item_id = '".$store_item_id."'  WHERE design_no = '".$design_no."'  AND store_item_id = '".$saved_store_item_id."' ";
	   execsql($mix_update_query);
	   
	   $table_name = $_SESSION['db_prefix']."_store_items";
	   $name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
	   $name_result = mySQL_query($name_query);
	   $saved = mysql_result($name_result, 0, "item_name");
	  
	   
	   $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']."', '65', '".$design_no."' ,'".$saved_store_item_id."','".$store_item_id."','Item' )";
	   execsql($trackingQuery);
		
}

$table_name = $_SESSION['db_prefix']."_mix_design_lines";
$update_query = "UPDATE $table_name SET quantity = '".$quantity."' WHERE design_no = '".$design_no."' AND store_item_id ='".$store_item_id."' ";
execsql($update_query);

if ($saved_quantity!=$quantity) {
		$table_name = $_SESSION['db_prefix']."_store_items";
		$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
		$name_result = mySQL_query($name_query);
		$item = mysql_result($name_result, 0, "item_name");
	
	   $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']."', '66
	   ', '".$design_no."' ,'".$saved_quantity."','".$quantity."','Quantity for ".$item."' )";
	   execsql($trackingQuery);
		
		
}
}
else {
	$table_name = $_SESSION['db_prefix']."_store_items";
	$name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$saved_store_item_id."'  ";
	$name_result = mySQL_query($name_query);
	$item = mysql_result($name_result, 0, "item_name");
		
	$table_name = $_SESSION['db_prefix']."_mix_design_lines";
	$delete_query = "DELETE FROM $table_name WHERE design_no = '".$design_no."'  AND store_item_id = '".$store_item_id."' ";
	execsql($delete_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']."', '67', '".$design_no."' ,'','','".$item."' )";
	execsql($trackingQuery);
		
		
}		
$i++;
}

//$table_name = $_SESSION['db_prefix']."_mix_design_lines";
//$cost_query = "SELECT store_item_id, quantity FROM $table_name WHERE design_no = '".$design_no."' ";
//$cost_result = mySQL_query($cost_query);
//$cost_num = mysql_num_rows($cost_result);
//$design_cost = 0;
//$k=0;
//while ($k<$cost_num) {
//	$store_item_id = mysql_result($cost_result, $k, "store_item_id");
//	$quantity = mysql_result($cost_result, $k, "quantity");
//	
//	$table_name = $_SESSION['db_prefix']."_store_items";
//	$item_query = "SELECT price FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
//	$item_result = mySQL_query($item_query);
//	$price = mysql_result($item_result, 0, "price");
//	
//	$design_cost = $design_cost + ($quantity*$price);
//	
//	$k++;
//}
//
//$table_name = $_SESSION['db_prefix']."_mix_design";
//$design_cost_query = "UPDATE $table_name SET design_cost = '".$design_cost."' WHERE design_no = '".$design_no."' ";
//execsql($design_cost_query);
	
	

if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 		window.location="../production/mix_design_display.php?design_no=<?php echo $design_no?>";
		</script>
        <?php		 
	   }
	   
	if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	        window.location="mix_design_edit.php?design_no=<?php echo $design_no?>";
	   </script>
        <?php		 
	   }
	   ?>

