HRM-10/HRM.DA/Service/LFA/LFACalculator.cs
Mohaimen Hasan 0f5b150e98 LFA Undo
2026-11-26 15:13:21 +06:00

148 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using HRM.BO;
namespace HRM.DA
{
/// <summary>
/// The LFA policy: who is eligible and for how much. No database access.
/// - An employee must be live, confirmed and have completed 6 months of service on the payment date,
/// and gets LFA once in a calendar year.
/// - The full amount is one month's basic as of 31 December of the preceding year (365 days).
/// - 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
{
private const int ServiceMonths = 6;
private const double DaysOfYear = 365;
/// <summary>Every reason the employee cannot be paid; empty = eligible.</summary>
internal static List<string> Validate(LFAEmployee employee, DateTime lfaYear, DateTime paymentDate, int payrollTypeID)
{
List<string> problems = new List<string>();
if (employee.PayrollTypeID != payrollTypeID)
{
// another payroll type's employee is not shown: it reads as not found
problems.Add("Employee is not found.");
return problems;
}
if (employee.Status != EnumEmployeeStatus.Live)
problems.Add("Employee is not active.");
if (!employee.IsConfirmed)
problems.Add("Employee is not confirmed.");
DateTime eligibleFrom = employee.JoiningDate.Date.AddMonths(ServiceMonths);
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.");
if (!string.IsNullOrEmpty(employee.PaidBatchNumber))
problems.Add("LFA of " + lfaYear.Year + " is already processed in batch " + employee.PaidBatchNumber + ".");
if (!IsJoinedInYear(employee, lfaYear) && employee.LastDecBasic <= 0)
problems.Add("Basic salary as of " + DateText(LastDec(lfaYear)) + " is not found.");
if ((IsJoinedInYear(employee, lfaYear) || HasPreviousYear(employee, lfaYear)) && employee.JoiningBasic <= 0)
problems.Add("Joining basic salary is not found.");
return problems;
}
/// <summary>The detail row of an eligible employee (see Validate).</summary>
internal static LFAPaymentDetail Calculate(LFAEmployee employee, DateTime lfaYear)
{
bool joinedInYear = IsJoinedInYear(employee, lfaYear);
double currentYear = joinedInYear
? ProRata(employee.JoiningBasic, employee.JoiningDate, YearEnd(lfaYear))
: employee.LastDecBasic;
double prevYear = HasPreviousYear(employee, lfaYear)
? ProRata(employee.JoiningBasic, employee.JoiningDate, LastDec(lfaYear))
: 0;
LFAPaymentDetail detail = new LFAPaymentDetail();
detail.EmployeeID = employee.EmployeeID;
detail.LFAYear = lfaYear;
detail.JoiningDate = employee.JoiningDate;
detail.ConfirmationDate = employee.ConfirmationDate;
detail.GradeID = employee.GradeID;
detail.DesignationID = employee.DesignationID;
detail.DepartmentID = employee.DepartmentID;
detail.CostCenterID = employee.CostCenterID;
detail.BranchID = employee.BranchID;
detail.AccountNo = employee.AccountNo;
detail.JoiningBasic = employee.JoiningBasic;
detail.LastDecBasic = joinedInYear ? (double?)null : employee.LastDecBasic;
detail.BasicSalary = joinedInYear ? employee.JoiningBasic : employee.LastDecBasic;
detail.CalculatedAmount = Math.Round(prevYear + currentYear, 2, MidpointRounding.AwayFromZero);
detail.Amount = Math.Round(detail.CalculatedAmount, 0, MidpointRounding.AwayFromZero); // paid in whole taka
return detail;
}
/// <summary>
/// Completed service from the joining date to the date (the payment date), e.g. "2 Years 9 Months 27 Days".
/// </summary>
internal static string ServiceLength(DateTime joiningDate, DateTime asOf)
{
DateTime from = joiningDate.Date;
DateTime to = asOf.Date;
if (to <= from)
return "0 Days";
int totalMonths = (to.Year - from.Year) * 12 + to.Month - from.Month;
if (from.AddMonths(totalMonths) > to)
totalMonths--;
int days = (to - from.AddMonths(totalMonths)).Days;
int years = totalMonths / 12;
int months = totalMonths % 12;
List<string> parts = new List<string>();
if (years > 0)
parts.Add(years + (years == 1 ? " Year" : " Years"));
if (months > 0)
parts.Add(months + (months == 1 ? " Month" : " Months"));
if (days > 0 || parts.Count == 0)
parts.Add(days + (days == 1 ? " Day" : " Days"));
return string.Join(" ", parts);
}
/// <summary>Days from the joining date to the year end, the joining date itself not counted, over 365.</summary>
private static double ProRata(double amount, DateTime joiningDate, DateTime yearEnd)
{
return amount / DaysOfYear * (yearEnd.Date - joiningDate.Date).Days;
}
private static bool IsJoinedInYear(LFAEmployee employee, DateTime lfaYear)
{
return employee.JoiningDate.Year == lfaYear.Year;
}
/// <summary>Joined in the preceding year, not eligible within that year, and not paid for it.</summary>
private static bool HasPreviousYear(LFAEmployee employee, DateTime lfaYear)
{
return employee.JoiningDate.Year == lfaYear.Year - 1
&& employee.JoiningDate.Date.AddMonths(ServiceMonths) > LastDec(lfaYear)
&& !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);
}
private static string DateText(DateTime date)
{
return date.ToString("dd-MMM-yyyy");
}
}
}