<?php
session_start();
ob_start();

include('../include/config.php');
error_reporting(0);
$iduser=$_SESSION['user_idd'];
$date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
$timenow=$date->format('Y-m-d H:i:s');
$timenow2=$date->format('Y-m-d');
$month=$date->format('M');
$tpe='2021';

if(isset($_POST['id']))
{
$id=addslashes($_POST['id']);
$ba=addslashes($_POST['bas']);
$med=addslashes($_POST['med']);
$hou=addslashes($_POST['hou']);
$nh=addslashes($_POST['nh']);
$nss=addslashes($_POST['nss']);
$ref=addslashes($_POST['ref']);
$nita=addslashes($_POST['nita']);
$acc=addslashes($_POST['acc']);
$bnk=addslashes($_POST['bnk']);
$tot=addslashes($_POST['tot']);

//$lev=1.5*$tot/100;
$lev=0;

$nhif=0;
$nssf=0;
$taxa='';
$tax='';
$relief='2400';
$paye='';
$gtax=0;
$nhre=0;

if($nh==''){ $nh=0; }
if($med==''){ $med=0; }
if($ba==''){ $ba=0;  }
if($ref==''){ $ref=0;  }
if($nita==''){ $nita=0;  }
if($nss==''){ $nss=0;  }

$ticome=$tot;
$gross=$tot;
if($ticome<18000)
{
$nssf=	6*$ticome/100;
}else if($ticome>17999)
{
$nssf=1080;
}

$deducts=$ref+$nssf+$lev;
$pay=0;

if($ticome<6000)
{
$nhif=150;
}else if($ticome>5999 && $ticome<8000)
{
$nhif=300;

}else if($ticome>7999 && $ticome <12000)
{
$nhif=400;
}
else if($ticome>11999 && $ticome <15000)
{
$nhif=500;
}
else if($ticome>14999 && $ticome <20000)
{
$nhif=600;
}
else if($ticome>19999 && $ticome <25000)
{
$nhif=750;
}
else if($ticome>24999 && $ticome <30000)
{
$nhif=850;
}
else if($ticome>29999 && $ticome <35000)
{
$nhif=900;
}
else if($ticome>34999 && $ticome <40000)
{
$nhif=950;
}
else if($ticome>39999 && $ticome <45000)
{
$nhif=1000;
}

else if($ticome>44999 && $ticome <50000)
{
$nhif=1100;
}

else if($ticome>49999 && $ticome <60000)
{
$nhif=1200;
}

else if($ticome>59999 && $ticome <70000)
{
$nhif=1300;
}

else if($ticome>69999 && $ticome <80000)
{
$nhif=1400;
}

else if($ticome>79999 && $ticome <90000)
{
$nhif=1500;
}
else if($ticome>89999 && $ticome <100000)
{
$nhif=1600;
}

else if($ticome=100000 || $ticome > 100000)
{
$nhif=1700;
}

$nhre=$nhif*15/100;

if($tpe=='2021')
{
//Taxable income
 $taxa=$ticome-($nssf+$lev);
//Taxation process
 if($taxa < 24001)
 {
 $tax=$taxa*10/100;
 $tot=$tax;

 $paye=$tax-$relief;
 
 if($paye<0)
 {
 	$paye='0';
 }

 }else if($taxa>24000 && $taxa<32334)
 {
 	$tax1=24000*10/100;

    $exe=$taxa-24000;
 	$tax2=$exe*25/100;

    $tot=$tax1+$tax2;

 	$paye=$tot-$relief;


 } else if($taxa>32333)
 {
 	
    $tax1=24000*10/100;
 	$tax2=8333*25/100;

 	$exe=$taxa-32333;
 	$tax3=$exe*30/100;

    $tot=$tax1+$tax2+$tax3;
 	$paye=$tot-$relief;
 }
 
 
 $paye=$paye-$nhre;
 
 
 if($paye<0)
 {
 $paye=0;
 }
 
$gtax=$tot;
if($gtax<0)
{
  $gtax=0;
}

$td=$ref+$nssf+$nhif+$lev;
$net=$ticome-$td;

$query=mysqli_query($con,"SELECT * FROM paydetails where staff_id='$id'");
$num=mysqli_fetch_array($query);
if($num>0)
{
$sql=mysqli_query($con,"UPDATE paydetails SET Bank='$bnk',HLevy='$lev', Account='$acc', Refund='$ref',TD='$td', Gross='$gross',nhif='$nhif',nssf='$nssf',Medical='$med',Basic='$ba',House='$hou',NetPay='$net',NITA='$nita',Msg='success' WHERE staff_id='$id'");
if($sql)
{
$msg='updated payment details for an employee';
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','save','$month')");

if($sql3)
{
$result =mysqli_query($con,"SELECT * FROM paydetails where staff_id='$id'");
$row=mysqli_fetch_array($result);
echo json_encode($row);
}
}else
{
echo 'Unable to save';
} 
}else
{
 $sql=mysqli_query($con,"INSERT INTO paydetails(staff_id,Bank,Basic,HLevy,Account,Refund,TD,Gross,Medical,House,nhif,nssf,NetPay,NITA,Msg)values('$id','$bnk','$ba','$lev','$acc','$ref','$td','$gross','$med','$hou','$nhif','$nssf','$net','$nita','success')");  
 if($sql)
{
$msg='created Payment details for a staff';
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msg','$timenow','$timenow2','save','$month')");

if($sql3)
{

$result =mysqli_query($con,"SELECT * FROM paydetails where staff_id='$id'");
$row=mysqli_fetch_array($result);
echo json_encode($row);

}
}else
{
echo 'Unable to save';
} 
    
}
 //$sql=mysqli_query($con,"UPDATE paydetails SET Bank='$bank', Branch='$branch',Account='$acct', Basic='$basic', Commission='$comm', House='$house',Airtime='$airtime',nssf='$nssf' WHERE staff_id='$id'");

}else{
	echo "2022 Taxation not set";
}
}else
{
    echo 'Not set';
}

?>