From 7fc90dd86427d2bee29716f5102099ebe5101083 Mon Sep 17 00:00:00 2001 From: Mohaimen Hasan Date: Thu, 26 Nov 2026 11:35:51 +0600 Subject: [PATCH] LFA Calculator --- HRM.DA/DA/LFA/LFAPaymentDA.cs | 24 ++++++++++++++--- HRM.DA/Service/LFA/LFACalculator.cs | 14 +++++++--- HRM.DA/Service/LFA/LFAPaymentService.cs | 35 ++++++++++++++++--------- LFA_DB_Changes.sql | 12 +++++---- 4 files changed, 60 insertions(+), 25 deletions(-) diff --git a/HRM.DA/DA/LFA/LFAPaymentDA.cs b/HRM.DA/DA/LFA/LFAPaymentDA.cs index b79d62d..8a10b8a 100644 --- a/HRM.DA/DA/LFA/LFAPaymentDA.cs +++ b/HRM.DA/DA/LFA/LFAPaymentDA.cs @@ -75,7 +75,9 @@ namespace HRM.DA /// internal static IDataReader GetEmployees(TransactionContext tc, List 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); } + /// + /// 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. + /// + 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); + } + /// Payment History tab: every paid employee of the payment date range. internal static IDataReader GetHistory(TransactionContext tc, DateTime fromDate, DateTime toDate, int payrollTypeID) { diff --git a/HRM.DA/Service/LFA/LFACalculator.cs b/HRM.DA/Service/LFA/LFACalculator.cs index 7c3490e..b42f1d3 100644 --- a/HRM.DA/Service/LFA/LFACalculator.cs +++ b/HRM.DA/Service/LFA/LFACalculator.cs @@ -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. /// 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; } + /// 31 December of the LFA year. + 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); diff --git a/HRM.DA/Service/LFA/LFAPaymentService.cs b/HRM.DA/Service/LFA/LFAPaymentService.cs index 76c17cc..6b83dcd 100644 --- a/HRM.DA/Service/LFA/LFAPaymentService.cs +++ b/HRM.DA/Service/LFA/LFAPaymentService.cs @@ -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 lastDecBasics = new Dictionary(); - List gradeSalaries = new EmployeeGradeSalaryService().GetBasicOnDate(new DateTime(lfaYear.Year, 1, 1)); - if (gradeSalaries != null) - { - foreach (EmployeeGradeSalary gradeSalary in gradeSalaries) - { - lastDecBasics[gradeSalary.EmployeeID] = gradeSalary.BasicSalary; - } - } + Dictionary 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; } + /// + /// EmployeeID → basic on the date (LFAPaymentDA.GetBasicOnDate, the LFA copy of + /// EmployeeGradeSalaryService.GetBasicOnDate). Every row is read, the first one included. + /// + private static Dictionary GetBasicOnDate(TransactionContext tc, DateTime effectDate) + { + Dictionary basics = new Dictionary(); + 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) + /// LFAYear: the last date of the payment date's month (the LFA is for that date's calendar year). + private static DateTime GetLFAYear(DateTime paymentDate) { - return new DateTime(salaryMonth.Year, 12, 31); + return GlobalFunctions.LastDateOfMonth(paymentDate).Date; } private static string BatchPrefix(DateTime salaryMonth) diff --git a/LFA_DB_Changes.sql b/LFA_DB_Changes.sql index d4cb0a5..13a6ded 100644 --- a/LFA_DB_Changes.sql +++ b/LFA_DB_Changes.sql @@ -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);