<?php

   include('connect.php');
   include('logging.php');

if( isset( $_POST['payrollid'] ) )
{

$payrollid=$_POST['payrollid'];
$employeeid=$_POST['employeeid'];
$sickleavestaken= $_POST['sickleavestaken'];
$sickleavesbalance= $_POST['sickleavesbalance'];
$localleavestaken= $_POST['localleavestaken'];
$localleavesbalance= $_POST['localleavesbalance'];
$basicSal=$_POST['basicSal'];
$travellingAdjust=$_POST['travellingAdjust'];
$numWrkDays=$_POST['numWrkDays'];
$travellingAmt=$_POST['travellingAmt'];
$specialBonus=$_POST['specialBonus'];
$taxableAllowance=$_POST['taxableAllowance'];
$nonTaxableAllowance=$_POST['nonTaxableAllowance'];
$carbenefitvalue=$_POST['carbenefitvalue'];
$totalcarallowance=$_POST['totalcarallowance'];
$housingallowance=$_POST['housingallowance'];
$telephoneallowance=$_POST['telephoneallowance'];
$driverallowance=$_POST['driverallowance'];
$otherallowance=$_POST['otherallowance'];
$commission=$_POST['commission'];
$overtime=$_POST['overtime'];
$totalPackage=$_POST['totalPackage'];
$WorkingDaysValue=$_POST['WorkingDaysValue'];
$absencesInput=$_POST['absencesInput'];
$absencesValue=$_POST['absencesValue'];
$nsfEmp=$_POST['nsfEmp'];
$payeEmp=$_POST['payeEmp'];
$solidaritylevy=$_POST['solidaritylevy'];
$loandeduction=$_POST['loandeduction'];
$medicaldeduction=$_POST['medicaldeduction'];
$oneoffdeduction=$_POST['oneoffdeduction'];
$totalDed=$_POST['totalDed'];
$netSalary=$_POST['netSalary'];
$remuneration=$_POST['remuneration'];
$allowances=$_POST['allowances'];
$employerCSG=$_POST['employerCSG'];
$employerNSF=$_POST['employerNSF'];
$employerLEVY=$_POST['employerLEVY'];
$employerPRGF=$_POST['employerPRGF'];
$employeeCSG=$_POST['employeeCSG'];
$employeeNSF=$_POST['employeeNSF'];
$totalCSGVal=$_POST['totalCSGVal'];
$totalNSFVal=$_POST['totalNSFVal'];
$totalMRAVal=$_POST['totalMRAVal'];

$checkForSavedPayroll = "SELECT * FROM payrollbasicdetails WHERE payrollid = '$payrollid' AND employeeid = '$employeeid';";

$resultcheckForSavedPayroll = mysqli_query($db,$checkForSavedPayroll);

$numcheckSavedPayroll = mysqli_num_rows($resultcheckForSavedPayroll);

if ($numcheckSavedPayroll == 0) {

	$insertdata=" INSERT INTO payrollbasicdetails (
		payrollid,
		employeeid,
		s_taken,
		s_balance,
		l_taken,
		l_balance,
		basicSal,
		travellingAdjust,
		numberWrkDays,
		travellingAmt,
		specialBonus,
		taxableAllowance,
		nonTaxableAllowance,
		carbenefitvalue,
		totalcarallowance,
		housingallowance,
		telephoneallowance,
		driverallowance,
		otherallowance,
		commission,
		overtime,
		totalPackage,
		WorkingDaysValue,
		absencesInput,
		absencesValue,
		nsfEmp,
		payeEmp,
		solidaritylevy,
		loan,
		medical,
		oneOffDeduction,
		totalDed,
		netSalary,
		remuneration,
		allowances,
		employerCSG,
		employerNSF,
		employerLEVY,
		employerPRGF,
		employeeCSG,
		employeeNSF,
		totalCSGVal,
		totalNSFVal,
		totalMRAVal
		)
		VALUES( 
		'$payrollid',
		'$employeeid',
		'$sickleavestaken',
		'$sickleavesbalance',
		'$localleavestaken',
		'$localleavesbalance',
		'$basicSal',
		'$travellingAdjust',
		'$numWrkDays',
		'$travellingAmt',
		'$specialBonus',
		'$taxableAllowance',
		'$nonTaxableAllowance',
		'$carbenefitvalue',
		'$totalcarallowance',
		'$housingallowance',
		'$telephoneallowance',
		'$driverallowance',
		'$otherallowance',
		'$commission',
		'$overtime',
		'$totalPackage',
		'$WorkingDaysValue',
		'$absencesInput',
		'$absencesValue',
		'$nsfEmp',		
		'$payeEmp',
		'$solidaritylevy',
		'$loandeduction',
		'$medicaldeduction',
		'$oneoffdeduction',
		'$totalDed',
		'$netSalary',
		'$remuneration',
		'$allowances',
		'$employerCSG',
		'$employerNSF',
		'$employerLEVY',
		'$employerPRGF',
		'$employeeCSG',
		'$employeeNSF',
		'$totalCSGVal',
		'$totalNSFVal',
		'$totalMRAVal'

		) ";

		if(mysqli_query($db, $insertdata)) {
      	echo 'Data for employee inserted successfully';
      } else {
         echo "Error: " . mysqli_error($db);
    }
} else {
	$insertdata=" UPDATE payrollbasicdetails 
	SET basicSal = '$basicSal',	
		s_taken = '$sickleavestaken',
		s_balance = '$sickleavesbalance',
		l_taken = '$localleavestaken',
		l_balance = '$localleavesbalance',		
		travellingAdjust = '$travellingAdjust',
		numberWrkDays = '$numWrkDays',
		travellingAmt = '$travellingAmt',
		specialBonus = '$specialBonus',
		taxableAllowance = '$taxableAllowance',
		totalcarallowance = '$totalcarallowance',
		nonTaxableAllowance = '$nonTaxableAllowance',
		commission = '$commission',
		overtime = '$overtime',
		totalPackage = '$totalPackage',
		housingallowance = '$housingallowance',
		telephoneallowance = '$telephoneallowance',
		driverallowance = '$driverallowance',
		otherallowance = '$otherallowance',
		WorkingDaysValue = '$WorkingDaysValue',
		absencesInput = '$absencesInput',
		absencesValue = '$absencesValue',
		nsfEmp = '$nsfEmp',
		payeEmp = '$payeEmp',
		solidaritylevy = '$solidaritylevy',
		loan = '$loandeduction',
		medical = '$medicaldeduction',
		oneOffDeduction = '$oneoffdeduction',
		totalDed = '$totalDed',
		netSalary = '$netSalary',
		remuneration = '$remuneration',
		allowances = '$allowances',
		employerCSG = '$employerCSG',
		employerNSF = '$employerNSF',
		employerLEVY = '$employerLEVY',
		employerPRGF = '$employerPRGF',
		employeeCSG = '$employeeCSG',
		employeeNSF = '$employeeNSF',
		totalCSGVal = '$totalCSGVal',
		totalNSFVal = '$totalNSFVal',
		totalMRAVal = '$totalMRAVal'
	WHERE employeeid = $employeeid
	AND payrollid = $payrollid;";

	if(mysqli_query($db, $insertdata)) {
      	echo 'Payroll data for employee updated successfully';
      } else {
         echo "Error: " . mysqli_error($db);
    }


}



    

} 
?>