<?php 	

require_once '../../Server/connection.php';

$valid['success'] = array('success' => false, 'messages' => array());

if($_POST) {	

	$editFirstName = addslashes(strtoupper($_POST['editFirstName']));
	$editMiddleName = addslashes(strtoupper($_POST['editMiddleName']));
	$editLastName = addslashes(strtoupper($_POST['editLastName']));
	$editEmail = addslashes(strtolower($_POST['editEmail']));
	$editPhoneNumber = $_POST['editPhoneNumber'];
	$editdes = $_POST['editdes'];
	$editSect = $_POST['editSect'];
	$editDept = $_POST['editdept'];
	
	$editGender = $_POST['editGender'];
    $editMarital = $_POST['editMarital'];
	
	$bankName = $_POST['editbankName'];
    $bankAccountNo = $_POST['editbankAccountNo'];
	
	 $heslb_status = $_POST['editheslbStatus'];
  
    //staff gross salary (before Deductions)
    $basicSalary = $_POST['editbasicSalary'];
	
	  if($heslb_status=='Yes'){
	 $HeslbStatus =1; 
	 $EmployeeHeslbContri = 0.15 * $basicSalary;
	 $EmployeeNssfContri = 0.1 * $basicSalary;
	 $EmployerNssfContri = 0.1 * $basicSalary;
	 $EmpNhifContri = 0.03 * $basicSalary;
	 if($EmpNhifContri>40000){
	 $EmployeeNhifContri = $EmpNhifContri;
	 $EmployerNhifContri = 0.03 * $basicSalary;
	 $EmployerWcfContri = 0.006 * $basicSalary;
	 }else{
	 $EmployeeNhifContri = 0;
     $EmployerNhifContri = 40000;
     $EmployerWcfContri = 0.006 * $basicSalary;	 
	 }
	 $taxableAmount = $basicSalary -($EmployeeNssfContri+$EmployeeNhifContri);
	 if($taxableAmount>0){
		 $paye = 0;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>270000){
		 $paye = ($taxableAmount - 270000)* 0.08;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>520000){
		 $paye = (($taxableAmount - 520000)* 0.2) + 22500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>760000){
		 $paye = (($taxableAmount - 760000)* 0.25) + 70500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else{
		 $paye = (($taxableAmount - 1000000)* 0.3) + 130500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }
  }else{
	 $HeslbStatus =0;  
	 $EmployeeHeslbContri =0;
	 $EmployeeNssfContri = 0.1 * $basicSalary;
	 $EmployerNssfContri = 0.1 * $basicSalary;
	 $EmpNhifContri = 0.03 * $basicSalary;
	 if($EmpNhifContri>40000){
	 $EmployeeNhifContri = $EmpNhifContri;
	 $EmployerNhifContri = 0.03 * $basicSalary;
	 $EmployerWcfContri = 0.006 * $basicSalary;
	 }else{
	 $EmployeeNhifContri = 0;
     $EmployerNhifContri = 0.03 * $basicSalary;
	 $EmployerWcfContri = 0.006 * $basicSalary;	 
	 }
	 $taxableAmount = $basicSalary -($EmployeeNssfContri+$EmployeeNhifContri);
	 if($taxableAmount>0){
		 $paye = 0;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>270000){
		 $paye = ($taxableAmount - 270000)* 0.08;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>520000){
		 $paye = (($taxableAmount - 520000)* 0.2) + 22500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else if($taxableAmount>760000){
		 $paye = (($taxableAmount - 760000)* 0.25) + 70500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }else{
		 $paye = (($taxableAmount - 1000000)* 0.3) + 130500;
		 $netPay = $taxableAmount -($paye+$EmployeeHeslbContri);
	 }
  }
	
	$birthDate = $_POST['editBirthDate'];
    $employDate = $_POST['editEmployDate'];
    $editBirth_date = date('Y-m-d', strtotime($birthDate));
    $editEmployment_date = date('Y-m-d', strtotime($employDate));
	
	
	$n = substr($editPhoneNumber,3);
    $n1 = '0';
    $editPhoneNumber11 = $n1.$n;
	
	
    $staffId = $_POST['editCategoriesId'];

	$sql = "UPDATE staff SET firstname = '$editFirstName',middlename='$editMiddleName',lastname='$editLastName',gender_id='$editGender',email='$editEmail',phone_number='$editPhoneNumber11',birth_date='$editBirth_date',emply_date='$editEmployment_date',marital_id='$editMarital',dept_id='$editDept',sect_id='$editSect',des_id='$editdes',BasicSalary='$basicSalary',EmployeeNssfContri='$EmployeeNssfContri',EmployeeNhifContri='$EmployeeNhifContri',taxableAmount='$taxableAmount',Paye='$paye',NetPay='$netPay',EmployerNhifContri='$EmployerNhifContri',EmployerWcfContri='$EmployerWcfContri',EmployerNssfContri='$EmployerNssfContri',heslbBeneficiaryStatus='$HeslbStatus',bankName='$bankName',BankAccountNo='$bankAccountNo' WHERE staff_id = '$staffId'";
	
	//$sql1 = "UPDATE users SET dept_id='$editDept' WHERE staff_id = '$staffId'";

	if($conn->query($sql) === TRUE) {
	 	$valid['success'] = true;
		$valid['messages'] = "Staff is Successfully Updated";	
	} else {
	 	$valid['success'] = false;
	 	$valid['messages'] = "Error while updating Staff";
	}
	 
	$conn->close();

	echo json_encode($valid);
 
} // /if $_POST