<?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 = $con->query($preset_query);
$row = mysqli_fetch_assoc($preset_result);
$exchange_rate_saved = $row["preset_value"];


$table_name = $_SESSION['db_prefix']."_presets";
$preset_query = "SELECT  preset_value  FROM $table_name WHERE preset_name = 'VAT' ";
$preset_result = $con->query($preset_query);
$row = mysqli_fetch_assoc($preset_result);
$vat_rate_saved = $row["preset_value"];


$table_name = $_SESSION['db_prefix']."_presets";
$preset_query = "SELECT  preset_value  FROM $table_name WHERE preset_name = 'production_margin' ";
$preset_result = $con->query($preset_query);
$row = mysqli_fetch_assoc($preset_result);
$margin_saved = $row["preset_value"];

$table_name = $_SESSION['db_prefix']."_presets";
$preset_query = "SELECT  preset_value  FROM $table_name WHERE preset_name = 'packing_margin' ";
$preset_result = $con->query($preset_query);
$row = mysqli_fetch_assoc($preset_result);
$packing_margin_saved = $row["preset_value"];


$exchange_rate = $_GET['exchange_text'];
$vat_rate = $_GET['vat_text'];
$margin = $_GET['margin_text'];
$packing_margin = $_GET['packing_margin_text'];



$table_name = $_SESSION['db_prefix']."_presets";
$edit_query = "UPDATE $table_name SET preset_value = '".$exchange_rate."' WHERE preset_name = 'Exchange rate' ";
$edit_result = $con->query($edit_query);

$table_name = $_SESSION['db_prefix']."_presets";
$edit_query = "UPDATE $table_name SET preset_value = '".$vat_rate."' WHERE preset_name = 'VAT' ";
$edit_result = $con->query($edit_query);

$table_name = $_SESSION['db_prefix']."_presets";
$edit_query = "UPDATE $table_name SET preset_value = '".$margin."' WHERE preset_name = 'production_margin' ";
$edit_result = $con->query($edit_query);

$table_name = $_SESSION['db_prefix']."_presets";
$edit_query = "UPDATE $table_name SET preset_value = '".$packing_margin."' WHERE preset_name = 'packing_margin' ";
$edit_result = $con->query($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 = $con->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 = $con->query($trackingQuery);
}

if ($margin_saved!=$margin) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '120', '".$margin_saved."','".$margin."','', 'Production margin' )";
$trackingResult = $con->query($trackingQuery);
}

if ($packing_margin_saved!=$packing_margin) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '120', '".$packing_margin_saved."','".$packing_margin."','', 'Packing margin' )";
$trackingResult = $con->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 = $con->query($clockin1_query);
$row = mysqli_fetch_assoc($clockin1_result);
$clockin1_saved = $row["preset_value"];

$clockin1 = ($clockin_hour_text*60) + $clockin_min_text;
$preset_query = "UPDATE $table_name SET preset_value = '".$clockin1."' WHERE preset_name = 'clockin1' ";
$preset_result = $con->query($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 = $con->query($trackingQuery);
}

$grace1_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'grace1' ";
$grace1_result = $con->query($grace1_query);
$row = mysqli_fetch_assoc($grace1_result);
$grace1_saved = $row["preset_value"];

$preset_query = "UPDATE $table_name SET preset_value = '".$grace1."' WHERE preset_name = 'grace1' ";
$preset_result = $con->query($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 = $con->query($trackingQuery);
}

$clockout1_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout1' ";
$clockout1_result = $con->query($clockout1_query);
$row = mysqli_fetch_assoc($clockout1_result);
$clockout1_saved = $row["preset_value"];
$clockout1 = ($clockout_hour_text*60) + $clockout_min_text;
$preset_query = "UPDATE $table_name SET preset_value = '".$clockout1."' WHERE preset_name = 'clockout1' ";
$preset_result = $con->query($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 = $con->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 = $con->query($clockin2_query);
$row = mysqli_fetch_assoc($clockin2_result);
$clockin2_saved = $row["preset_value"];
$preset_query = "UPDATE $table_name SET preset_value = '".$clockin2."' WHERE preset_name = 'clockin2' ";
$preset_result = $con->query($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 = $con->query($trackingQuery);
}

$grace2_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'grace2' ";
$grace2_result = $con->query($grace2_query);
$row = mysqli_fetch_assoc($grace2_result);
$grace2_saved = $row["preset_value"];
$preset_query = "UPDATE $table_name SET preset_value = '".$grace2."' WHERE preset_name = 'grace2' ";
$preset_result = $con->query($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 = $con->query($trackingQuery);
}

$clockout2_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'clockout2' ";
$clockout2_result = $con->query($clockout2_query);
$row = mysqli_fetch_assoc($clockout2_result);
$clockout2_saved = $row["preset_value"];
$preset_query = "UPDATE $table_name SET preset_value = '".$clockout2."' WHERE preset_name = 'clockout2' ";
$preset_result = $con->query($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 = $con->query($trackingQuery);
}

$lunch_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'lunch' ";
$lunch_result = $con->query($lunch_query);
$row = mysqli_fetch_assoc($lunch_result);
$lunch_saved = $row["preset_value"];
$preset_query = "UPDATE $table_name SET preset_value = '".$lunch."' WHERE preset_name = 'lunch' ";
$preset_result = $con->query($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 = $con->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		 
	   }
	   ?>

