<?php
include($_SERVER['DOCUMENT_ROOT'] . "/API-Others/connection/conn.php");

$idd = isset($_POST['idd']) ? $_POST['idd'] : '';
$Image = isset($_POST['Image']) ? $_POST['Image'] : '';
$Title = isset($_POST['Title']) ? $_POST['Title'] : '';
$FirstName = isset($_POST['FirstName']) ? $_POST['FirstName'] : '';
$LastName = isset($_POST['LastName']) ? $_POST['LastName'] : '';
$Gender = isset($_POST['Gender']) ? $_POST['Gender'] : '';
$StaffNumber = isset($_POST['StaffNumber']) ? $_POST['StaffNumber'] : '';
$StaffType = isset($_POST['StaffType']) ? $_POST['StaffType'] : '';
$Role = isset($_POST['Role']) ? $_POST['Role'] : '';
$MobileNumber = isset($_POST['MobileNumber']) ? $_POST['MobileNumber'] : '';
$EmergencyContact = isset($_POST['EmergencyContact']) ? $_POST['EmergencyContact'] : '';
$SsnitNumber = isset($_POST['SsnitNumber']) ? $_POST['SsnitNumber'] : '';
$TinNumber = isset($_POST['TinNumber']) ? $_POST['TinNumber'] : '';
$Bank = isset($_POST['Bank']) ? $_POST['Bank'] : '';
$BasicSalary = isset($_POST['BasicSalary']) ? $_POST['BasicSalary'] : '';
$Allowances = isset($_POST['Allowances']) ? $_POST['Allowances'] : '';
$GrossSalary = isset($_POST['GrossSalary']) ? $_POST['GrossSalary'] : '';
$SsnitRelief = isset($_POST['SsnitRelief']) ? $_POST['SsnitRelief'] : '';
$TaxablePay = isset($_POST['TaxablePay']) ? $_POST['TaxablePay'] : '';
$TotalTax = isset($_POST['TotalTax']) ? $_POST['TotalTax'] : '';
$PAYE = isset($_POST['PAYE']) ? $_POST['PAYE'] : '';
$SsnitContribution = isset($_POST['SsnitContribution']) ? $_POST['SsnitContribution'] : '';
$OtherDeductions = isset($_POST['OtherDeductions']) ? $_POST['OtherDeductions'] : '';
$NetSalary = isset($_POST['NetSalary']) ? $_POST['NetSalary'] : '';
$StaffSsnitContributionTier1 = isset($_POST['StaffSsnitContributionTier1']) ? $_POST['StaffSsnitContributionTier1'] : '';
$StaffSsnitContributionTier2 = isset($_POST['StaffSsnitContributionTier2']) ? $_POST['StaffSsnitContributionTier2'] : '';
$EmployerSsnitContributionTier1 = isset($_POST['EmployerSsnitContributionTier1']) ? $_POST['EmployerSsnitContributionTier1'] : '';
$TotalContribution = isset($_POST['TotalContribution']) ? $_POST['TotalContribution'] : '';
$Year = isset($_POST['Year']) ? $_POST['Year'] : '';
$Month = isset($_POST['Month']) ? $_POST['Month'] : '';
$Date = DATE("Y-m-d");



$check = mysqli_query($conn, "SELECT * FROM payroll WHERE unique_user_id='$idd' AND Year='$Year' AND Month='$Month'");
$affected_row = mysqli_affected_rows($conn);
if ($affected_row == 1) {
    $sql = ("UPDATE payroll SET  title = '$Title', first_name = '$FirstName', last_name = '$LastName', gender = '$Gender', role = '$Role', phone = '$MobileNumber', EmergencyContact = '$EmergencyContact', SsnitNumber = '$SsnitNumber', TinNumber = '$TinNumber', Bank = '$Bank', BasicSalary = '$BasicSalary', Allowances = '$Allowances', GrossSalary = '$GrossSalary', SsnitRelief = '$SsnitRelief', TaxablePay = '$TaxablePay', TotalTax = '$TotalTax', PAYE = '$PAYE', SsnitContribution = '$SsnitContribution', OtherDeductions = '$OtherDeductions', NetSalary = '$NetSalary', StaffSsnitContributionTier1 = '$StaffSsnitContributionTier1', StaffSsnitContributionTier2 = '$StaffSsnitContributionTier2', EmployerSsnitContributionTier1 = '$EmployerSsnitContributionTier1', TotalSsnitContribution = '$TotalContribution', UserId = '$staffid', LastActivity = '$last_activity_update' WHERE unique_user_id='$idd' AND Year='$Year' AND Month='$Month'");
    if (mysqli_query($conn, $sql)) {
        echo 2;
    } else {
        echo "Error: " . mysqli_error($conn);
    }
} else {
    $sql = ("INSERT INTO payroll (unique_user_id, title, first_name, last_name, gender, role, phone, EmergencyContact, SsnitNumber, TinNumber, Bank, BasicSalary, Allowances, GrossSalary, SsnitRelief, TaxablePay, TotalTax, PAYE, SsnitContribution, OtherDeductions, NetSalary, StaffSsnitContributionTier1, StaffSsnitContributionTier2, EmployerSsnitContributionTier1, TotalSsnitContribution, Year, Month, Date, UserId, LastActivity) VALUES('$idd', '$Title', '$FirstName', '$LastName', '$Gender', '$Role', '$MobileNumber', '$EmergencyContact', '$SsnitNumber','$TinNumber', '$Bank', '$BasicSalary', '$Allowances', '$GrossSalary', '$SsnitRelief', '$TaxablePay', '$TotalTax', '$PAYE', '$SsnitContribution', '$OtherDeductions', '$NetSalary', '$StaffSsnitContributionTier1', '$StaffSsnitContributionTier2', '$EmployerSsnitContributionTier1', '$TotalContribution', '$Year', '$Month','$Date','$idd', '$last_activity_new_entry')");
    if (mysqli_query($conn, $sql)) {
        echo 1;
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}
