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

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


$emp_id = $_POST['emp_id_text'];

$table_name = $_SESSION['db_prefix']."_permissions";
$view_query = "SELECT access_id  FROM  $table_name  WHERE access_id ='". $_SESSION['access_id']."' AND page_name = 'HR-wages' ";
$view_result = $con->query($view_query);
$view_num = mysqli_num_rows($view_result);
if ($view_num!=0) {
	$view='yes';
}
else {
$view='no';
}	

$table_name = $_SESSION['db_prefix']."_employee";
$saved_employee_query = "SELECT * FROM $table_name WHERE emp_id = '".$emp_id."' ";
$saved_employee_result = $con->query($saved_employee_query);
$row = mysqli_fetch_assoc($saved_employee_result);
$id_number_saved = $row["id_number"];
$emp_first_name_saved = $row["emp_first_name"];
$emp_last_name_saved = $row["emp_last_name"];
$birth_date_saved = $row["birth_date"];
$gender_saved = $row["gender"];
$bag_box_saved = $row["bag_box"];
$box_no_saved = $row["box_no"];
$postal_place_saved = $row["postal_place"];
$phys_address_saved = $row["phys_address"];
$phys_place_saved = $row["phys_place"];
$emergency_name_saved = $row["emergency_name"];
$emergency_phone_saved = $row["emergency_phone"];
$position_saved = $row["position"];
$pay_period_saved = $row["pay_period"];
$rate_saved = $row["rate"];
$bank_saved = $row["bank"];
$bank_branch_saved = $row["bank_branch"];
$bank_account_saved = $row["bank_account"];
$clothing_size_saved = $row["clothing_size"];
$shoe_size_saved = $row["shoe_size"];
$cell_saved = $row["cell"];
$phone_saved = $row["phone"];
$email_saved = $row["email"];
$country_saved = $row["country"];
$marital_status_saved = $row["marital_status"];
$department_saved = $row["department"];
$position = $row["position"];
$employ_date_saved = $row["employ_date"];
$end_employ_date_saved = $row["end_employ_date"];
$premature_end_date_saved = $row["premature_end_date"];
//$citizen = $row[""];
//$nationality = $row[""];
$notes_saved = $row["notes"];
$pay_unit_saved = $row["pay_unit"];
$leave_days_saved = $row["leave_days"];
$time_keeping_saved = $row["time_keeping"];
$time_zone_saved = $row["time_zone"];

$nationality_saved = $row["nationality"];
$expatriate_saved = $row["expatriate"];
$permit_number_saved = $row["permit_number"];
$permit_date_saved = $row["permit_date"];


$id_number = $_POST['id_number_text'];
$emp_first_name = $_POST['first_name_text'];
$emp_last_name = $_POST['last_name_text'];
$birth_date = $_POST['birth_date'];
$gender = $_POST['gender_select'];
$bag_box = $_POST['bag_box_select'];
$box_no = $_POST['box_no_text'];
$postal_place = $_POST['post_place_select'];
$phys_address = $_POST['phys_address_text'];
$phys_place = $_POST['phys_place_select'];
$emergency_name = $_POST['emergency_contact_text'];
$emergency_phone = $_POST['emergency_cell_text'];
$position = $_POST['position_select'];
$pay_period = $_POST['pay_period_select'];
if ($view=='yes') {
$rate = $_POST['rate_text'];
}
else {
	$rate = $rate_saved;
}
$bank = $_POST['bank_select'];
$bank_branch = $_POST['branch_select'];
$bank_account = $_POST['acc_number_text'];
$clothing_size = $_POST['clothing_size_select'];
$shoe_size = $_POST['shoe_size_select'];
$cell = $_POST['cell_text'];
$phone = $_POST['phone_text'];
$email = $_POST['email_text'];
$country = $_POST['country_select'];
$marital_status = $_POST['marital_status_select'];
$department = $_POST['department_select'];
$position = $_POST['position_select'];
$employ_date = $_POST['employ_date'];
$end_employ_date = $_POST['end_employ_date'];
$premature_end_date = $_POST['premature_end_date'];
//$citizen = $_POST['_text'];
//$nationality = $_POST['_text'];
$notes = $_POST['notes_text'];
$pay_unit = $_POST['pay_unit_select'];
$leave_days = $_POST['leave_days_select'];
$time_keeping = $_POST['time_keeping_select'];
$time_zone = $_POST['time_zone'];

$nationality = $_POST['nationality_select'];
$expatriate = $_POST['expatriate_select'];
$permit_number = $_POST['permit_number_text'];
$permit_date = $_POST['permit_date'];

$table_name = $_SESSION['db_prefix']."_employee";
$employee_query = "UPDATE $table_name SET emp_first_name = '".$emp_first_name."', emp_last_name = '".$emp_last_name."', gender = '".$gender."', id_number = '".$id_number."', birth_date = '".$birth_date."', marital_status = '".$marital_status."', bag_box = '".$bag_box."', box_no = '".$box_no."', postal_place = '".$postal_place."', phys_address = '".$phys_address."', phys_place = '".$phys_place."', country = '".$country."', phone = '".$phone."', cell = '".$cell."', email = '".$email."', emergency_name = '".$emergency_name."', emergency_phone = '".$emergency_phone."', employ_date = '".$employ_date."', end_employ_date = '".$end_employ_date."', premature_end_date = '".$premature_end_date."', position = '".$position."', department = '".$department."', pay_period = '".$pay_period."', rate = '".$rate."', leave_days = '".$leave_days."', time_keeping = '".$time_keeping."', time_zone = '".$time_zone."', pay_unit = '".$pay_unit."', clothing_size = '".$clothing_size."', shoe_size = '".$shoe_size."', bank = '".$bank."', bank_branch = '".$bank_branch."', bank_account = '".$bank_account."', notes = '".$notes."', nationality = '".$nationality."', expatriate = '".$expatriate."', permit_number = '".$permit_number."', permit_date = '".$permit_date."' WHERE emp_id = '".$emp_id."' ";
$employee_result = $con->query($employee_query);


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

if ($id_number_saved!=$id_number) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$id_number_saved."','".$id_number."','Id number' )";
$tracking_result = $con->query($tracking_query);
}

if ($emp_first_name_saved!=$emp_first_name) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$emp_first_name_saved."','".$emp_first_name."','First name' )";
$tracking_result = $con->query($tracking_query);
}

if ($emp_last_name_saved!=$emp_last_name) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$emp_last_name_saved."','".$emp_last_name."','Last name' )";
$tracking_result = $con->query($tracking_query);
}

if ($birth_date_saved!=$birth_date) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$birth_date_saved."','".$birth_date."','Dob' )";
$tracking_result = $con->query($tracking_query);
}

if ($gender_saved!=$gender) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$gender_saved."','".$gender."','Gender' )";
$tracking_result = $con->query($tracking_query);
}

if ($bag_box_saved!=$bag_box) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$bag_box_saved."','".$bag_box."','Postal address' )";
$tracking_result = $con->query($tracking_query);
}

if ($box_no_saved!=$box_no) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$box_no_saved."','".$box_no."','Postal address' )";
$tracking_result = $con->query($tracking_query);
}

if ($postal_place_saved!=$postal_place) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$postal_place_saved."','".$postal_place."','Postal place' )";
$tracking_result = $con->query($tracking_query);
}

if ($phys_address_saved!=$phys_address) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$phys_address_saved."','".$phys_address."','Residential address' )";
$tracking_result = $con->query($tracking_query);
}

if ($phys_place_saved!=$phys_place) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$phys_place_saved."','".$phys_place."','Residential place' )";
$tracking_result = $con->query($tracking_query);
}

if ($emergency_name_saved!=$emergency_name) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$emergency_name_saved."','".$emergency_name."','Emergency contact' )";
$tracking_result = $con->query($tracking_query);
}

if ($emergency_phone_saved!=$emergency_phone) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$emergency_phone_saved."','".$emergency_phone."','Emergency phone' )";
$tracking_result = $con->query($tracking_query);
}

if ($position_saved!=$position) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$position_saved."','".$position."','Position' )";
$tracking_result = $con->query($tracking_query);
}

if ($pay_period_saved!=$pay_period) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$pay_period_saved."','".$pay_period."','Pay period' )";
$tracking_result = $con->query($tracking_query);
}

if ($rate_saved!=$rate) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$rate_saved."','".$rate."','Rate/salary' )";
$tracking_result = $con->query($tracking_query);
}

if ($bank_saved!=$bank) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$bank_saved."','".$bank."','Bank' )";
$tracking_result = $con->query($tracking_query);
}

if ($bank_branch_saved!=$bank_branch) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$bank_branch_saved."','".$bank_branch."','Bank branch' )";
$tracking_result = $con->query($tracking_query);
}

if ($bank_account_saved!=$bank_account) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$bank_account_saved."','".$bank_account."','Bank account' )";
$tracking_result = $con->query($tracking_query);
}

if ($clothing_size_saved!=$clothing_size) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$clothing_size_saved."','".$clothing_size."','Clothing size' )";
$tracking_result = $con->query($tracking_query);
}

if ($shoe_size_saved!=$shoe_size) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$shoe_size_saved."','".$shoe_size."','Shoe size' )";
$tracking_result = $con->query($tracking_query);
}

if ($cell_saved!=$cell) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$cell_saved."','".$cell."','Cell' )";
$tracking_result = $con->query($tracking_query);
}

if ($phone_saved!=$phone) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$phone_saved."','".$phone."','Phone' )";
$tracking_result = $con->query($tracking_query);
}

if ($email_saved!=$email) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$email_saved."','".$email."','Email' )";
$tracking_result = $con->query($tracking_query);
}

if ($country_saved!=$country) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$country_saved."','".$country."','Country' )";
$tracking_result = $con->query($tracking_query);
}

if ($marital_status_saved!=$marital_status) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$marital_status_saved."','".$marital_status."','Marital status' )";
$tracking_result = $con->query($tracking_query);
}

if ($department_saved!=$department) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$department_saved."','".$department."','Department' )";
$tracking_result = $con->query($tracking_query);
}

if ($position_saved!=$position) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$position_saved."','".$position."','Position' )";
$tracking_result = $con->query($tracking_query);
}

if ($employ_date_saved!=$employ_date) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$employ_date_saved."','".$employ_date."','Employment date' )";
$tracking_result = $con->query($tracking_query);
}

if ($end_employ_date_saved!=$end_employ_date) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$end_employ_date_saved."','".$end_employ_date."','End employment date' )";
$tracking_result = $con->query($tracking_query);
}

if ($premature_end_date_saved!=$premature_end_date) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$end_employ_date_saved."','".$end_employ_date."','Premature end employment date' )";
$tracking_result = $con->query($tracking_query);
}

if ($notes_saved!=$notes) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$notes_saved."','".$notes."','Notes' )";
$tracking_result = $con->query($tracking_query);
}

if ($pay_unit_saved!=$pay_unit) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$pay_unit_saved."','".$pay_unit."','Pay unit' )";
$tracking_result = $con->query($tracking_query);
}

if ($leave_days_saved!=$leave_days) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$leave_days_saved."','".$leave_days."','Leave days' )";
$tracking_result = $con->query($tracking_query);
}


if ($time_keeping_saved!=$time_keeping) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$time_keeping_saved."','".$time_keeping."','Time keeping' )";
$tracking_result = $con->query($tracking_query);
}

if ($time_zone_saved!=$time_zone) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$time_zone_saved."','".$time_zone."','Time zone' )";
$tracking_result = $con->query($tracking_query);
}


if ($nationality_saved!=$nationality) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$nationality_saved."','".$nationality."','Nationality' )";
$tracking_result = $con->query($tracking_query);
}

if ($expatriate_saved!=$expatriate) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$expatriate_saved."','".$expatriate."','Expatriate' )";
$tracking_result = $con->query($tracking_query);
}

if ($permit_number_saved!=$permit_number) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$permit_number_saved."','".$permit_number."','Permit number' )";
$tracking_result = $con->query($tracking_query);
}

if ($permit_date_saved!=$permit_date) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '21', '".$emp_id."' ,'".$permit_date_saved."','".$permit_date."','Permit exp date' )";
$tracking_result = $con->query($tracking_query);
}


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../hr_module/hr_main.php?date=0";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="employee_details_edit.php?emp_id=<?php echo $emp_id?>"
	   </script>
        <?php		 
	   }
	   ?>

