LFA Calculator

This commit is contained in:
Mohaimen Hasan 2026-11-26 11:35:51 +06:00
parent 88235461bf
commit 7fc90dd864
4 changed files with 60 additions and 25 deletions

View File

@ -75,7 +75,9 @@ namespace HRM.DA
/// </summary>
internal static IDataReader GetEmployees(TransactionContext tc, List<string> employeeNos, DateTime lfaYear)
{
DateTime lastDec = new DateTime(lfaYear.Year - 1, 12, 31);
// LFAYear is a month end, so "this year" / "last year" are date ranges of the calendar year
DateTime yearStart = new DateTime(lfaYear.Year, 1, 1);
DateTime lastYearStart = yearStart.AddYears(-1);
string sql = SQLParser.MakeSQL(@"SET NOCOUNT ON;
IF OBJECT_ID('tempdb..#lfaEmp') IS NOT NULL DROP TABLE #lfaEmp;
@ -109,12 +111,13 @@ namespace HRM.DA
ON cc.EMPLOYEEID = e.EMPLOYEEID AND cc.RowNo = 1
LEFT JOIN (SELECT dt.EmployeeID, p.BatchNumber
FROM LFAPaymentDetail dt INNER JOIN LFAPayment p ON p.LFAPaymentID = dt.LFAPaymentID
WHERE dt.LFAYear = %d) py ON py.EmployeeID = e.EMPLOYEEID
LEFT JOIN LFAPaymentDetail pv ON pv.EmployeeID = e.EMPLOYEEID AND pv.LFAYear = %d
WHERE dt.LFAYear >= %d AND dt.LFAYear < %d) py ON py.EmployeeID = e.EMPLOYEEID
OUTER APPLY (SELECT TOP 1 x.LFAPaymentDetailID FROM LFAPaymentDetail x
WHERE x.EmployeeID = e.EMPLOYEEID AND x.LFAYear >= %d AND x.LFAYear < %d) pv
ORDER BY e.EMPLOYEENO;
DROP TABLE #lfaEmp;",
true, lfaYear, lastDec);
true, yearStart, yearStart.AddYears(1), lastYearStart, yearStart);
// must be a SqlParameter[]: Ease.Core casts the array to SqlParameter[]
SqlParameter[] parameters =
@ -124,6 +127,19 @@ namespace HRM.DA
return tc.ExecuteReader(CommandType.Text, sql, parameters);
}
/// <summary>
/// Every employee's basic on a date: the latest salary record that starts before it. A copy of
/// EmployeeGradeSalaryDA.GetBasicOnDate, kept for LFA so that it runs in the LFA transaction.
/// </summary>
internal static IDataReader GetBasicOnDate(TransactionContext tc, DateTime effectdate)
{
return tc.ExecuteReader(@"SELECT g.EMPLOYEEID, g.BASICSALARY FROM GRADESALARYASSIGNMENT g
where g.empGradeSalaryid = (select Max(mx.empGradeSalaryid) from GRADESALARYASSIGNMENT mx
where g.EMPLOYEEID = mx.EMPLOYEEID and mx.EFFECTDATE < %d )
order by g.EMPLOYEEID ",
effectdate);
}
/// <summary>Payment History tab: every paid employee of the payment date range.</summary>
internal static IDataReader GetHistory(TransactionContext tc, DateTime fromDate, DateTime toDate, int payrollTypeID)
{

View File

@ -12,6 +12,8 @@ namespace HRM.DA
/// - Joined in the LFA year: joining basic, pro-rata from the joining date to 31 December.
/// - Joined in the preceding year and had not completed 6 months by its 31 December: that year's
/// pro-rata on the joining basic, plus the current year's full amount.
/// lfaYear is the last date of the payment date's month; the year it falls in is the LFA year, and every
/// pro-rata runs to 31 December of that year.
/// </summary>
internal static class LFACalculator
{
@ -37,8 +39,8 @@ namespace HRM.DA
problems.Add("Employee is not confirmed.");
DateTime eligibleFrom = employee.JoiningDate.Date.AddMonths(ServiceMonths);
if (employee.JoiningDate.Date > lfaYear)
problems.Add("Employee joined after " + DateText(lfaYear) + ".");
if (employee.JoiningDate.Date > YearEnd(lfaYear))
problems.Add("Employee joined after " + DateText(YearEnd(lfaYear)) + ".");
else if (eligibleFrom > paymentDate.Date)
problems.Add("6 months of service will be completed on " + DateText(eligibleFrom) + ", after the payment date.");
@ -58,7 +60,7 @@ namespace HRM.DA
{
bool joinedInYear = IsJoinedInYear(employee, lfaYear);
double currentYear = joinedInYear
? ProRata(employee.JoiningBasic, employee.JoiningDate, lfaYear)
? ProRata(employee.JoiningBasic, employee.JoiningDate, YearEnd(lfaYear))
: employee.LastDecBasic;
double prevYear = HasPreviousYear(employee, lfaYear)
? ProRata(employee.JoiningBasic, employee.JoiningDate, LastDec(lfaYear))
@ -102,6 +104,12 @@ namespace HRM.DA
&& !employee.IsPaidLastYear;
}
/// <summary>31 December of the LFA year.</summary>
private static DateTime YearEnd(DateTime lfaYear)
{
return new DateTime(lfaYear.Year, 12, 31);
}
private static DateTime LastDec(DateTime lfaYear)
{
return new DateTime(lfaYear.Year - 1, 12, 31);

View File

@ -311,7 +311,7 @@ namespace HRM.DA
return details;
}
DateTime lfaYear = GetLFAYear(result.SalaryMonth);
DateTime lfaYear = GetLFAYear(paymentDate);
foreach (LFAPaymentInput input in inputs)
{
input.EmployeeNo = (input.EmployeeNo ?? "").Trim();
@ -336,15 +336,7 @@ namespace HRM.DA
// Basic as of 31 December of the preceding year: the salary record that starts before 1 January
// (the latest entry), as the bonus reads it.
Dictionary<int, double> lastDecBasics = new Dictionary<int, double>();
List<EmployeeGradeSalary> gradeSalaries = new EmployeeGradeSalaryService().GetBasicOnDate(new DateTime(lfaYear.Year, 1, 1));
if (gradeSalaries != null)
{
foreach (EmployeeGradeSalary gradeSalary in gradeSalaries)
{
lastDecBasics[gradeSalary.EmployeeID] = gradeSalary.BasicSalary;
}
}
Dictionary<int, double> lastDecBasics = GetBasicOnDate(tc, new DateTime(lfaYear.Year, 1, 1));
foreach (LFAEmployee employee in employees.Values)
{
double basic;
@ -391,6 +383,22 @@ namespace HRM.DA
return details;
}
/// <summary>
/// EmployeeID → basic on the date (LFAPaymentDA.GetBasicOnDate, the LFA copy of
/// EmployeeGradeSalaryService.GetBasicOnDate). Every row is read, the first one included.
/// </summary>
private static Dictionary<int, double> GetBasicOnDate(TransactionContext tc, DateTime effectDate)
{
Dictionary<int, double> basics = new Dictionary<int, double>();
DataReader dr = new DataReader(LFAPaymentDA.GetBasicOnDate(tc, effectDate));
while (dr.Read())
{
basics[dr.GetInt32("EMPLOYEEID").Value] = dr.GetDouble("BASICSALARY", 0);
}
dr.Close();
return basics;
}
private static LFAPaymentRow ToRow(LFAEmployee employee, LFAPaymentInput input, LFAPaymentDetail detail,
DateTime paymentDate)
{
@ -459,7 +467,7 @@ namespace HRM.DA
oPayment.BatchNumber = BatchNumber(prefix, batchNo);
oPayment.PayrollTypeID = payrollTypeID;
oPayment.SalaryMonth = validation.SalaryMonth;
oPayment.LFAYear = GetLFAYear(validation.SalaryMonth);
oPayment.LFAYear = GetLFAYear(request.PaymentDate.Value);
oPayment.PaymentDate = request.PaymentDate.Value.Date;
oPayment.Remarks = string.IsNullOrWhiteSpace(request.Remarks) ? null : request.Remarks.Trim();
oPayment.CreatedBy = userID;
@ -524,9 +532,10 @@ namespace HRM.DA
return GlobalFunctions.LastDateOfMonth(payrollType.NextPayProcessDate).Date;
}
private static DateTime GetLFAYear(DateTime salaryMonth)
/// <summary>LFAYear: the last date of the payment date's month (the LFA is for that date's calendar year).</summary>
private static DateTime GetLFAYear(DateTime paymentDate)
{
return new DateTime(salaryMonth.Year, 12, 31);
return GlobalFunctions.LastDateOfMonth(paymentDate).Date;
}
private static string BatchPrefix(DateTime salaryMonth)

View File

@ -9,9 +9,10 @@
- Part 0 DROPS the three tables, Parts 1-3 create them again. The drop is
refused, and nothing runs, when any of the tables already holds rows.
- LFAYear is a DATETIME in all three tables: the last date of the calendar
year the LFA is for (2026 = 31-Dec-2026), as SalaryMonth is the last date
of its month.
- LFAYear is a DATETIME in all three tables: the last date of the Payment
Date's month (paid 08-Dec-2026 = 31-Dec-2026), as SalaryMonth is the last
date of its month. The LFA is for the calendar year of that date, so
"once in a calendar year" is enforced on YEAR(LFAYear) (LFACalendarYear).
- IDs are generated by the application (tc.GenerateID), no IDENTITY.
- A detail row is a snapshot: grade, department, bank account and basic are
kept as they were when the batch was processed.
@ -75,7 +76,7 @@ CREATE TABLE dbo.LFAPayment (
BatchNumber VARCHAR(30) NOT NULL, -- LFA-2026-001
PayrollTypeID INT NOT NULL,
SalaryMonth DATETIME NOT NULL, -- last date of PAYROLLTYPE.NEXTPAYPROCESSDATE's month
LFAYear DATETIME NOT NULL, -- last date of the calendar year the LFA is for
LFAYear DATETIME NOT NULL, -- last date of PaymentDate's month
PaymentDate DATETIME NOT NULL,
Remarks NVARCHAR(500) NULL,
IsTaxUpdated BIT NOT NULL CONSTRAINT DF_LFAPayment_IsTaxUpdated DEFAULT (0), -- 1 = the batch is already in the income tax data
@ -104,6 +105,7 @@ CREATE TABLE dbo.LFAPaymentDetail (
LFAPaymentID INT NOT NULL,
EmployeeID INT NOT NULL,
LFAYear DATETIME NOT NULL, -- same value as LFAPayment.LFAYear
LFACalendarYear AS (YEAR(LFAYear)) PERSISTED, -- computed, never written: for the once-a-year index
/* employee snapshot */
JoiningDate DATETIME NOT NULL,
@ -136,7 +138,7 @@ CREATE TABLE dbo.LFAPaymentDetail (
);
GO
/* policy: LFA once in a calendar year (Undo deletes the rows, which frees the employee again) */
CREATE UNIQUE INDEX UX_LFAPayDtl_EmployeeYear ON dbo.LFAPaymentDetail (EmployeeID, LFAYear);
CREATE UNIQUE INDEX UX_LFAPayDtl_EmployeeYear ON dbo.LFAPaymentDetail (EmployeeID, LFACalendarYear);
GO
/* the rows of one batch */
CREATE INDEX IX_LFAPayDtl_Payment ON dbo.LFAPaymentDetail (LFAPaymentID);