<?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']."_packaging_design";
$saved_est_design_query = "SELECT pack_qty, est_qty FROM $table_name WHERE design_no = '".$design_no."' ";
$saved_est_design_result = $con->query($saved_est_design_query);
$row = mysqli_fetch_assoc($saved_est_design_result);
$saved_pack_qty = $row['pack_qty'];
$saved_est_qty = $row['est_qty'];

$pack_qty  = $_GET['pack_qty_select'];
$est_qty  = $_GET['est_qty_text'];

if ($saved_pack_qty!=$pack_qty) {
	   	
	   $table_name = $_SESSION['db_prefix']."_packaging_design";
	   $pack_query = "UPDATE $table_name SET pack_qty = '".$pack_qty."'  WHERE design_no = '".$design_no."' ";
	   $pack_result = $con->query($pack_query);
	   	  
	   
	   $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']."', '82', '".$design_no."' ,'".$saved_pack_qty."', '".$pack_qty."', 'Pack units' )";
	   $tracking_result = $con->query($tracking_query);
		
}

if ($saved_est_qty!=$est_qty) {
	   	
	   $table_name = $_SESSION['db_prefix']."_packaging_design";
	   $est_query = "UPDATE $table_name SET est_qty = '".$est_qty."'  WHERE design_no = '".$design_no."' ";
	   $est_result = $con->query($est_query);
	   	  
	   
	   $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']."', '82', '".$design_no."' ,'".$saved_est_qty."', '".$est_qty."', 'Quantity' )";
	   $tracking_result = $con->query($tracking_query);
		
}

$table_name = $_SESSION['db_prefix']."_packaging_design";
$saved_est_design_query = "SELECT est_qty FROM $table_name WHERE design_no = '".$design_no."' ";
$saved_est_design_result = $con->query($saved_est_design_query);
$saved_est_design_num = mysqli_num_rows($saved_est_design_result);



$table_name = $_SESSION['db_prefix']."_packaging_design_lines";
$saved_packaging_design_query = "SELECT store_item_id, quantity, used_for FROM $table_name WHERE design_no = '".$design_no."' ";
$saved_packaging_design_result = $con->query($saved_packaging_design_query);

$i=0;
while($row = $saved_packaging_design_result->fetch_assoc()) {
$saved_store_item_id= $row["store_item_id"];
$saved_quantity = $row["quantity"];
$saved_used_for = $row["used_for"];

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


if ($quantity<>0) {

if ($saved_store_item_id!=$store_item_id) {
	   	
	   $table_name = $_SESSION['db_prefix']."_packaging_design_lines";
	   $packaging_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."' ";
	   $packaging_update_result = $con->query($packaging_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 = $con->query($name_query);
	   $row = mysqli_fetch_assoc($name_result);
	   $saved = $row['item_name'];
     	   
	   $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']."', '78', '".$design_no."' ,'".$saved_store_item_id."','".$store_item_id."','Item' )";
	   $tracking_result = $con->query($tracking_query);
		
}

$table_name = $_SESSION['db_prefix']."_packaging_design_lines";
$update_query = "UPDATE $table_name SET quantity = '".$quantity."', used_for = '".$used_for."' WHERE design_no = '".$design_no."' AND store_item_id ='".$store_item_id."' ";
$update_result = $con->query($update_query);

if ($saved_used_for!=$used_for) {
		$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 = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$item = $row['item_name'];

	
	   $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']."', '79
	   ', '".$design_no."' ,'".$saved_used_for."','".$used_for."','<b>".$item."</b> used for ' )";
	   $tracking_result = $con->query($tracking_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 = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$item = $row['item_name'];

	
	   $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']."', '79
	   ', '".$design_no."' ,'".$saved_quantity."','".$quantity."','Quantity for ".$item."' )";
	   $tracking_result = $con->query($tracking_query);
		
		
}
}
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 = $con->query($name_query);
	$row = mysqli_fetch_assoc($name_result);
	$item = $row['item_name'];

		
	$table_name = $_SESSION['db_prefix']."_packaging_design_lines";
	$delete_query = "DELETE FROM $table_name WHERE design_no = '".$design_no."'  AND store_item_id = '".$store_item_id."' ";
	$delete_result = $con->query($delete_query);
	
	$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']."', '80', '".$design_no."' ,'','','".$item."' )";
	$tracking_result = $con->query($tracking_query);
		
		
}		
$i++;
}

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

