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

date_default_timezone_set('Africa/Gaborone');
$entryDate = date("Y-m-d H:i:s"); 

$today = date("Y-m-d");

$table_name = $_SESSION['db_prefix']."_presets";
$preset_query = "SELECT  preset_value  FROM $table_name WHERE preset_name = 'Exchange rate' ";
$preset_result = mySQL_query($preset_query);
$exchange_rate_saved = mysql_result($preset_result, 0, "preset_value");

$table_name = $_SESSION['db_prefix']."_presets";
$preset_query = "SELECT  preset_value  FROM $table_name WHERE preset_name = 'VAT' ";
$preset_result = mySQL_query($preset_query);
$vat_rate_saved = mysql_result($preset_result, 0, "preset_value");

$table_name = $_SESSION['db_prefix']."_presets";
$preset_query = "SELECT  preset_value  FROM $table_name WHERE preset_name = 'Press points' ";
$preset_result = mySQL_query($preset_query);
$press_points_saved = mysql_result($preset_result, 0, "preset_value");

$table_name = $_SESSION['db_prefix']."_presets";
$preset_query = "SELECT  preset_value  FROM $table_name WHERE preset_name = 'Plate area' ";
$preset_result = mySQL_query($preset_query);
$plate_area_saved = mysql_result($preset_result, 0, "preset_value");

$table_name = $_SESSION['db_prefix']."_presets";
$follow_up_query = "SELECT  preset_value  FROM $table_name WHERE preset_name = 'follow_up' ";
$follow_up_result = mySQL_query($follow_up_query);
$follow_up_saved = mysql_result($preset_result, 0, "preset_value");



$exchange_rate = $_GET['exchange_text'];
$vat_rate = $_GET['vat_text'];
$press_points = $_GET['press_points_text'];
$plate_area = $_GET['plate_area_text'];
$follow_up = $_GET['follow_up_text'];


$table_name = $_SESSION['db_prefix']."_presets";
$edit_query = "UPDATE $table_name SET preset_value = '".$exchange_rate."' WHERE preset_name = 'Exchange rate' ";
execsql($edit_query);

$table_name = $_SESSION['db_prefix']."_presets";
$edit_query = "UPDATE $table_name SET preset_value = '".$vat_rate."' WHERE preset_name = 'VAT' ";
execsql($edit_query);

$table_name = $_SESSION['db_prefix']."_presets";
$edit_query = "UPDATE $table_name SET preset_value = '".$press_points."' WHERE preset_name = 'Press points' ";
execsql($edit_query);

$table_name = $_SESSION['db_prefix']."_presets";
$edit_query = "UPDATE $table_name SET preset_value = '".$plate_area."' WHERE preset_name = 'Plate area' ";
execsql($edit_query);

$table_name = $_SESSION['db_prefix']."_presets";
$edit_query = "UPDATE $table_name SET preset_value = '".$follow_up."' WHERE preset_name = 'follow_up' ";
execsql($edit_query);



$table_name = $_SESSION['db_prefix']."_tracking_admin";

if ($exchange_rate_saved!=$exchange_rate) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '120', '".$exchange_rate_saved."','".$exchange_rate."','', 'Exchange rate' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($vat_rate_saved!=$vat_rate) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '120', '".$vat_rate_saved."','".$vat_rate."','', 'VAT rate' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($press_points_saved!=$press_points) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '120', '".$press_points_saved."','".$press_points."','', 'Press points' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($plate_area_saved!=$plate_area) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '120', '".$plate_area_saved."','".$plate_area."','', 'Plate area' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($follow_up_saved!=$follow_up) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '120', '".$follow_up_saved."','".$follow_up."','', 'Follow up period' )";
$trackingResult = mySQL_query($trackingQuery);
}

$clockin_hour_text = $_GET['clockin_hour_text'];
$clockin_min_text = $_GET['clockin_min_text'];
$clockout_hour_text = $_GET['clockout_hour_text'];
$clockout_min_text = $_GET['clockout_min_text'];
$grace1 = $_GET['grace_min_text'];
$lunch = $_GET['lunch_text'];

$table_name = $_SESSION['db_prefix']."_presets";
$table_name2 = $_SESSION['db_prefix']."_tracking_admin";

$clockin1_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockin1' ";
$clockin1_result = mySQL_query($clockin1_query);
$clockin1_saved = mysql_result($clockin1_result, 0, "preset_value");
$clockin1 = ($clockin_hour_text*60) + $clockin_min_text;
$preset_query = "UPDATE $table_name SET preset_value = '".$clockin1."' WHERE preset_name = 'clockin1' ";
execsql($preset_query);
if ($clockin1!=$clockin1_saved) {
$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '123', '".$clockin1_saved."' ,'".$clockin1."','zone 1','clock in time' )";
$trackingResult = mySQL_query($trackingQuery);
}

$grace1_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'grace1' ";
$grace1_result = mySQL_query($grace1_query);
$grace1_saved = mysql_result($grace1_result, 0, "preset_value");
$preset_query = "UPDATE $table_name SET preset_value = '".$grace1."' WHERE preset_name = 'grace1' ";
execsql($preset_query);
if ($grace1!=$grace1_saved) {
$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '123', '".$grace1_saved."' ,'".$grace1."','zone 1','grace period 1' )";
$trackingResult = mySQL_query($trackingQuery);
}

$clockout1_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout1' ";
$clockout1_result = mySQL_query($clockout1_query);
$clockout1_saved = mysql_result($clockout1_result, 0, "preset_value");
$clockout1 = ($clockout_hour_text*60) + $clockout_min_text;
$preset_query = "UPDATE $table_name SET preset_value = '".$clockout1."' WHERE preset_name = 'clockout1' ";
execsql($preset_query);
if ($clockout1!=$clockout1_saved) {
$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '123', '".$clockout1_saved."' ,'".$clockout1."','zone 1','clock out time' )";
$trackingResult = mySQL_query($trackingQuery);
}

$clockin_hour_2_text = $_GET['clockin_hour_2_text'];
$clockin_min_2_text = $_GET['clockin_min_2_text'];
$clockout_hour_2_text = $_GET['clockout_hour_2_text'];
$clockout_min_2_text = $_GET['clockout_min_2_text'];
$grace2 = $_GET['grace_min2_text'];

$clockin2 = ($clockin_hour_2_text*60) + $clockin_min_2_text;
$clockout2 = ($clockout_hour_2_text*60) + $clockout_min_2_text;

$clockin2_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockin2' ";
$clockin2_result = mySQL_query($clockin2_query);
$clockin2_saved = mysql_result($clockin2_result, 0, "preset_value");
$preset_query = "UPDATE $table_name SET preset_value = '".$clockin2."' WHERE preset_name = 'clockin2' ";
execsql($preset_query);
if ($clockin2!=$clockin2_saved) {
$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '123', '".$clockin2_saved."' ,'".$clockin2."','zone 2','clock in time' )";
$trackingResult = mySQL_query($trackingQuery);
}

$grace2_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'grace2' ";
$grace2_result = mySQL_query($grace2_query);
$grace2_saved = mysql_result($grace2_result, 0, "preset_value");
$preset_query = "UPDATE $table_name SET preset_value = '".$grace2."' WHERE preset_name = 'grace2' ";
execsql($preset_query);
if ($grace2!=$grace2_saved) {
$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '123', '".$grace2_saved."' ,'".$grace2."','zone 1','grace period 2' )";
$trackingResult = mySQL_query($trackingQuery);
}

$clockout2_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout2' ";
$clockout2_result = mySQL_query($clockout2_query);
$clockout2_saved = mysql_result($clockout2_result, 0, "preset_value");
$preset_query = "UPDATE $table_name SET preset_value = '".$clockout2."' WHERE preset_name = 'clockout2' ";
execsql($preset_query);
if ($clockout2!=$clockout2_saved) {
$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '123', '".$clockout2_saved."' ,'".$clockout2."','zone 2','clock in time' )";
$trackingResult = mySQL_query($trackingQuery);
}

$lunch_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'lunch' ";
$lunch_result = mySQL_query($lunch_query);
$lunch_saved = mysql_result($lunch_result, 0, "preset_value");
$preset_query = "UPDATE $table_name SET preset_value = '".$lunch."' WHERE preset_name = 'lunch' ";
execsql($preset_query);
if ($lunch!=$lunch_saved) {
$trackingQuery = "INSERT INTO $table_name2 (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '123', '".$lunch_saved."' ,'".$lunch."','zone 1','lunch time' )";
$trackingResult = mySQL_query($trackingQuery);
}



	if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="user_list.php";
		</script>
        <?php		 
	   }
	   
	   if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	          window.location="presets.php";
	   </script>
        <?php		 
	   }
	   ?>

