321 lines
19 KiB
C#
321 lines
19 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using Ease.Core.DataAccess;
|
|
using Ease.Core.DataAccess.SQL;
|
|
using HRM.BO;
|
|
using Microsoft.Data.SqlClient;
|
|
|
|
namespace HRM.DA
|
|
{
|
|
#region LFAPaymentDA
|
|
|
|
internal class LFAPaymentDA
|
|
{
|
|
#region Constructor
|
|
|
|
private LFAPaymentDA()
|
|
{
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Insert function
|
|
|
|
internal static void Insert(TransactionContext tc, LFAPayment item)
|
|
{
|
|
tc.ExecuteNonQuery(
|
|
"INSERT INTO LFAPayment(LFAPaymentID, BatchNumber, PayrollTypeID, SalaryMonth, LFAYear, PaymentDate," +
|
|
" Remarks, IsTaxUpdated, WFStatus, ApprovedBy, ApprovedDate, CreatedBy, CreationDate)" +
|
|
" VALUES(%n, %s, %n, %d, %d, %d, %u, %b, %n, %n, %D, %n, %D)",
|
|
item.ID, item.BatchNumber, item.PayrollTypeID, item.SalaryMonth, item.LFAYear, item.PaymentDate,
|
|
item.Remarks, item.IsTaxUpdated, (int?)item.WFStatus, item.ApprovedBy,
|
|
DataReader.GetNullValue(item.ApprovedDate), item.CreatedBy, item.CreatedDate);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Update / Delete function
|
|
|
|
/// <summary>Stamps who changed the batch (some of its employees were undone).</summary>
|
|
internal static void UpdateModified(TransactionContext tc, int lfaPaymentID, int modifiedBy, DateTime modifiedDate)
|
|
{
|
|
tc.ExecuteNonQuery("UPDATE LFAPayment SET ModifiedBy=%n, ModifiedDate=%D WHERE LFAPaymentID=%n",
|
|
modifiedBy, modifiedDate, lfaPaymentID);
|
|
}
|
|
|
|
/// <summary>Sets the batch's approval status (Not_yet_Initiated while it is in the workflow), as SalaryProcess.WFStatus.</summary>
|
|
internal static void UpdateWFStatus(TransactionContext tc, int lfaPaymentID, enumwfStatus? wfStatus)
|
|
{
|
|
tc.ExecuteNonQuery("UPDATE LFAPayment SET WFStatus=%n WHERE LFAPaymentID=%n", (int?)wfStatus, lfaPaymentID);
|
|
}
|
|
|
|
/// <summary>The batch is approved: Direct Approve, or the last approver of the workflow.</summary>
|
|
internal static void Approve(TransactionContext tc, int lfaPaymentID, int approvedBy, DateTime approvedDate)
|
|
{
|
|
tc.ExecuteNonQuery("UPDATE LFAPayment SET WFStatus=%n, ApprovedBy=%n, ApprovedDate=%D WHERE LFAPaymentID=%n",
|
|
(int)enumwfStatus.Approve, approvedBy, approvedDate, lfaPaymentID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes the approval workflow of an undone batch (WFTYPE 33 steps, their approvers and notifications).
|
|
/// Batch IDs are reused (MAX + 1), so an old workflow must never stay behind for a new batch to inherit.
|
|
/// </summary>
|
|
internal static void DeleteWorkflow(TransactionContext tc, int lfaPaymentID)
|
|
{
|
|
const string trans = "SELECT WFMOVEMENTTRANID FROM WFMOVEMENTTRAN WHERE WFTYPEID=%n AND OBJECTID=%n";
|
|
tc.ExecuteNonQuery("DELETE FROM WFMOVEMENTTASK WHERE WFMOVEMENTTRANID IN (" + trans + ")",
|
|
LFAPaymentApproval.WFTypeID, lfaPaymentID);
|
|
tc.ExecuteNonQuery("DELETE FROM WFMOVEMENTNEXT WHERE WFMOVEMENTTRANID IN (" + trans + ")",
|
|
LFAPaymentApproval.WFTypeID, lfaPaymentID);
|
|
tc.ExecuteNonQuery("DELETE FROM WFMOVEMENTTRAN WHERE WFTYPEID=%n AND OBJECTID=%n",
|
|
LFAPaymentApproval.WFTypeID, lfaPaymentID);
|
|
}
|
|
|
|
/// <summary>Deletes the batch header; its detail rows must be deleted first.</summary>
|
|
internal static void Delete(TransactionContext tc, int lfaPaymentID)
|
|
{
|
|
tc.ExecuteNonQuery("DELETE FROM LFAPayment WHERE LFAPaymentID=%n", lfaPaymentID);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Get Function
|
|
|
|
internal static IDataReader Get(TransactionContext tc, int nID)
|
|
{
|
|
return tc.ExecuteReader("SELECT * FROM LFAPayment WHERE LFAPaymentID=%n", nID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The batch, locked until the transaction ends: an approval or a second undo of the same batch
|
|
/// waits instead of running at the same time.
|
|
/// </summary>
|
|
internal static IDataReader GetForUpdate(TransactionContext tc, int nID)
|
|
{
|
|
return tc.ExecuteReader("SELECT * FROM LFAPayment WITH (UPDLOCK, ROWLOCK) WHERE LFAPaymentID=%n", nID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// LFA Payment Undo and Approval: every batch of the payroll type with its head count, total and approval
|
|
/// workflow state (latest step, who has it now, whether it is back with the initiator), newest first.
|
|
/// </summary>
|
|
internal static IDataReader GetBatches(TransactionContext tc, int payrollTypeID)
|
|
{
|
|
return tc.ExecuteReader(
|
|
@"SELECT p.LFAPaymentID, p.BatchNumber, p.SalaryMonth, p.LFAYear, p.PaymentDate, p.Remarks, p.WFStatus,
|
|
p.IsTaxUpdated, p.CreationDate, ISNULL(u.UserName, u.LoginID) AS CreatedByName,
|
|
p.ApprovedDate, ISNULL(ua.UserName, ua.LoginID) AS ApprovedByName,
|
|
ISNULL(s.HeadCount, 0) AS HeadCount, ISNULL(s.PaidCount, 0) AS PaidCount,
|
|
ISNULL(s.TotalAmount, 0) AS TotalAmount,
|
|
lt.WFMOVEMENTTRANID AS WFMovementTranID, lt.UNIQUENUMBER AS UniqueNumber,
|
|
CASE WHEN EXISTS (SELECT 1 FROM WFMOVEMENTNEXT n
|
|
WHERE n.WFMOVEMENTTRANID = lt.WFMOVEMENTTRANID AND n.STATUS = %n
|
|
AND n.EMPLOYEEID = it.InitiatorEmpID) THEN 1 ELSE 0 END AS IsAtInitiator,
|
|
STUFF((SELECT ', ' + e.NAME + ' [' + e.EMPLOYEENO + ']'
|
|
FROM WFMOVEMENTNEXT n INNER JOIN EMPLOYEE e ON e.EMPLOYEEID = n.EMPLOYEEID
|
|
WHERE n.WFMOVEMENTTRANID = lt.WFMOVEMENTTRANID AND n.STATUS = %n
|
|
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') AS PendingWith
|
|
FROM LFAPayment p
|
|
LEFT JOIN (SELECT dt.LFAPaymentID, COUNT(*) AS HeadCount, SUM(dt.Amount) AS TotalAmount,
|
|
SUM(CASE WHEN dt.PaymentStatus = %n THEN 1 ELSE 0 END) AS PaidCount
|
|
FROM LFAPaymentDetail dt GROUP BY dt.LFAPaymentID) s ON s.LFAPaymentID = p.LFAPaymentID
|
|
LEFT JOIN Users u ON u.UserID = p.CreatedBy
|
|
LEFT JOIN Users ua ON ua.UserID = p.ApprovedBy
|
|
OUTER APPLY (SELECT TOP 1 t.WFMOVEMENTTRANID, t.UNIQUENUMBER FROM WFMOVEMENTTRAN t
|
|
WHERE t.WFTYPEID = %n AND t.OBJECTID = p.LFAPaymentID
|
|
ORDER BY t.WFMOVEMENTTRANID DESC) lt
|
|
OUTER APPLY (SELECT TOP 1 f.FROMEMPLOYEEID AS InitiatorEmpID FROM WFMOVEMENTTRAN f
|
|
WHERE f.UNIQUENUMBER = lt.UNIQUENUMBER ORDER BY f.WFMOVEMENTTRANID) it
|
|
WHERE p.PayrollTypeID=%n
|
|
ORDER BY p.PaymentDate DESC, p.LFAPaymentID DESC",
|
|
(int)EnumwfStatus.Received, (int)EnumwfStatus.Received, (int)EnumLFAPaymentStatus.Paid,
|
|
LFAPaymentApproval.WFTypeID, payrollTypeID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The approval workflow state of one batch: its latest step (WFMovementTranID, UniqueNumber), the
|
|
/// employee who submitted it (InitiatorEmpID) and whether it is back with them now (IsAtInitiator).
|
|
/// No row when the batch was never submitted.
|
|
/// </summary>
|
|
internal static IDataReader GetWorkflowState(TransactionContext tc, int lfaPaymentID)
|
|
{
|
|
return tc.ExecuteReader(
|
|
@"SELECT lt.WFMOVEMENTTRANID AS WFMovementTranID, lt.UNIQUENUMBER AS UniqueNumber, it.InitiatorEmpID,
|
|
CASE WHEN EXISTS (SELECT 1 FROM WFMOVEMENTNEXT n
|
|
WHERE n.WFMOVEMENTTRANID = lt.WFMOVEMENTTRANID AND n.STATUS = %n
|
|
AND n.EMPLOYEEID = it.InitiatorEmpID) THEN 1 ELSE 0 END AS IsAtInitiator
|
|
FROM (SELECT TOP 1 t.WFMOVEMENTTRANID, t.UNIQUENUMBER FROM WFMOVEMENTTRAN t
|
|
WHERE t.WFTYPEID = %n AND t.OBJECTID = %n ORDER BY t.WFMOVEMENTTRANID DESC) lt
|
|
OUTER APPLY (SELECT TOP 1 f.FROMEMPLOYEEID AS InitiatorEmpID FROM WFMOVEMENTTRAN f
|
|
WHERE f.UNIQUENUMBER = lt.UNIQUENUMBER ORDER BY f.WFMOVEMENTTRANID) it",
|
|
(int)EnumwfStatus.Received, LFAPaymentApproval.WFTypeID, lfaPaymentID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// True when the employee is a pending approver of this step and the step is still the latest one of
|
|
/// its workflow (an approver who already acted, or a step that moved on, cannot act again).
|
|
/// </summary>
|
|
internal static bool IsPendingApprover(TransactionContext tc, int wfMovementTranID, int employeeID)
|
|
{
|
|
object found = tc.ExecuteScalar(
|
|
@"SELECT COUNT(*) FROM WFMOVEMENTTRAN t
|
|
INNER JOIN WFMOVEMENTNEXT n ON n.WFMOVEMENTTRANID = t.WFMOVEMENTTRANID
|
|
WHERE t.WFMOVEMENTTRANID = %n AND t.WFTYPEID = %n AND n.EMPLOYEEID = %n AND n.STATUS = %n
|
|
AND t.WFMOVEMENTTRANID = (SELECT MAX(x.WFMOVEMENTTRANID) FROM WFMOVEMENTTRAN x
|
|
WHERE x.UNIQUENUMBER = t.UNIQUENUMBER)",
|
|
wfMovementTranID, LFAPaymentApproval.WFTypeID, employeeID, (int)EnumwfStatus.Received);
|
|
return found != null && found != DBNull.Value && Convert.ToInt32(found) > 0;
|
|
}
|
|
|
|
/// <summary>The highest running number of the batches that start with this prefix; 0 = none yet.</summary>
|
|
internal static int GetLastBatchNo(TransactionContext tc, int payrollTypeID, string batchPrefix)
|
|
{
|
|
object last = tc.ExecuteScalar(
|
|
"SELECT MAX(CAST(SUBSTRING(BatchNumber, %n, 20) AS INT)) FROM LFAPayment" +
|
|
" WHERE PayrollTypeID=%n AND LEFT(BatchNumber, %n)=%s",
|
|
batchPrefix.Length + 1, payrollTypeID, batchPrefix.Length, batchPrefix);
|
|
return last == null || last == DBNull.Value ? 0 : Convert.ToInt32(last);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The same, holding the payroll type's batches until the transaction ends: two users processing at
|
|
/// the same time then wait for each other instead of taking the same number or the same employee.
|
|
/// </summary>
|
|
internal static int GetLastBatchNoForUpdate(TransactionContext tc, int payrollTypeID, string batchPrefix)
|
|
{
|
|
object last = tc.ExecuteScalar(
|
|
"SELECT MAX(CAST(SUBSTRING(BatchNumber, %n, 20) AS INT)) FROM LFAPayment WITH (UPDLOCK, HOLDLOCK)" +
|
|
" WHERE PayrollTypeID=%n AND LEFT(BatchNumber, %n)=%s",
|
|
batchPrefix.Length + 1, payrollTypeID, batchPrefix.Length, batchPrefix);
|
|
return last == null || last == DBNull.Value ? 0 : Convert.ToInt32(last);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Everything the LFA calculation needs of the given Employee Nos, whatever their payroll type and
|
|
/// status (the caller says why one cannot be paid). The numbers go in as one parameter, as in
|
|
/// EmployeeDA.GetByEmpIDList (STRING_SPLIT, SQL Server 2016+).
|
|
/// - joining salary: the employee's first salary record;
|
|
/// (the 31 December basic comes from EmployeeGradeSalaryService.GetBasicOnDate, as the bonus uses it);
|
|
/// - cost center: the current one with the highest percentage.
|
|
/// </summary>
|
|
internal static IDataReader GetEmployees(TransactionContext tc, List<string> employeeNos, DateTime lfaYear)
|
|
{
|
|
// 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;
|
|
CREATE TABLE #lfaEmp (EmployeeID int NOT NULL PRIMARY KEY);
|
|
INSERT INTO #lfaEmp (EmployeeID)
|
|
SELECT e.EMPLOYEEID FROM EMPLOYEE e
|
|
WHERE e.EMPLOYEENO IN (SELECT value FROM STRING_SPLIT(@employeeNos, ',') WHERE value <> '');
|
|
|
|
SELECT e.EMPLOYEEID, e.EMPLOYEENO, e.NAME, e.PAYROLLTYPEID, e.STATUS, e.ISCONFIRMED, e.JOININGDATE,
|
|
e.DATEOFCONFIRMATION, e.GRADEID, g.DESCRIPTION AS GradeName, e.DESIGNATIONID, ds.NAME AS DesignationName,
|
|
e.DEPARTMENTID, d.DESCRIPTION AS DepartmentName, pd.DESCRIPTION AS DivisionName,
|
|
cc.COSTCENTERID, e.BRANCHID, e.ACCOUNTNO,
|
|
ISNULL(jn.BASICSALARY, 0) AS JoiningBasic,
|
|
py.BatchNumber AS PaidBatchNumber,
|
|
CASE WHEN pv.LFAPaymentDetailID IS NULL THEN 0 ELSE 1 END AS IsPaidLastYear
|
|
FROM #lfaEmp t
|
|
INNER JOIN EMPLOYEE e ON e.EMPLOYEEID = t.EmployeeID
|
|
LEFT JOIN GRADES g ON g.GRADEID = e.GRADEID
|
|
LEFT JOIN DESIGNATION ds ON ds.DESIGNATIONID = e.DESIGNATIONID
|
|
LEFT JOIN DEPARTMENT d ON d.DEPARTMENTID = e.DEPARTMENTID
|
|
LEFT JOIN DEPARTMENT pd ON pd.DEPARTMENTID = d.PARENTID
|
|
LEFT JOIN (SELECT s.EMPLOYEEID, s.BASICSALARY,
|
|
ROW_NUMBER() OVER (PARTITION BY s.EMPLOYEEID ORDER BY s.EFFECTDATE, s.GRADESALARYID) AS RowNo
|
|
FROM GRADESALARYASSIGNMENT s INNER JOIN #lfaEmp x ON x.EmployeeID = s.EMPLOYEEID) jn
|
|
ON jn.EMPLOYEEID = e.EMPLOYEEID AND jn.RowNo = 1
|
|
LEFT JOIN (SELECT c.EMPLOYEEID, c.COSTCENTERID,
|
|
ROW_NUMBER() OVER (PARTITION BY c.EMPLOYEEID ORDER BY c.PERCENTAGE DESC, c.ID DESC) AS RowNo
|
|
FROM EMPCOSTCENTER c INNER JOIN #lfaEmp x ON x.EmployeeID = c.EMPLOYEEID
|
|
WHERE c.CurrentCC = %b) cc
|
|
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 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, yearStart, yearStart.AddYears(1), lastYearStart, yearStart);
|
|
|
|
// must be a SqlParameter[]: Ease.Core casts the array to SqlParameter[]
|
|
SqlParameter[] parameters =
|
|
{
|
|
SqlHelperExtension.CreateInParam("@employeeNos", SqlDbType.VarChar, string.Join(",", employeeNos), -1) // varchar(max)
|
|
};
|
|
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)
|
|
{
|
|
return tc.ExecuteReader(
|
|
@"SELECT dt.LFAPaymentDetailID, p.BatchNumber, p.PaymentDate, e.EMPLOYEENO, e.NAME, ds.NAME AS DesignationName,
|
|
d.DESCRIPTION AS DepartmentName, dt.JoiningDate, dt.BasicSalary, dt.Amount, dt.PaymentStatus
|
|
FROM LFAPayment p
|
|
INNER JOIN LFAPaymentDetail dt ON dt.LFAPaymentID = p.LFAPaymentID
|
|
INNER JOIN EMPLOYEE e ON e.EMPLOYEEID = dt.EmployeeID
|
|
LEFT JOIN DESIGNATION ds ON ds.DESIGNATIONID = dt.DesignationID
|
|
LEFT JOIN DEPARTMENT d ON d.DEPARTMENTID = dt.DepartmentID
|
|
WHERE p.PayrollTypeID=%n AND p.PaymentDate BETWEEN %d AND %d
|
|
ORDER BY p.PaymentDate DESC, p.LFAPaymentID DESC, e.EMPLOYEENO",
|
|
payrollTypeID, fromDate, toDate);
|
|
}
|
|
|
|
/// <summary>
|
|
/// LFA Bank Advice: the employees of one batch with an amount to pay, with the bank account kept on the
|
|
/// detail row when the batch was processed. empIDs = joined ints made by the service (empty = whole batch);
|
|
/// bankID 0 = every bank.
|
|
/// </summary>
|
|
internal static DataSet GetBankAdvice(TransactionContext tc, int lfaPaymentID, string empIDs, int bankID, int payrollTypeID)
|
|
{
|
|
string sql = SQLParser.MakeSQL(
|
|
@"SELECT e.EMPLOYEENO, e.NAME, e.EMAILADDRESS, ds.NAME AS DesignationName, d.DESCRIPTION AS DepartmentName,
|
|
ISNULL(cc.DESCRIPTION, '') AS CostCenter, dt.AccountNo, ISNULL(bk.NAME, '') AS BankName,
|
|
ISNULL(br.NAME, '') AS BranchName, ISNULL(br.ROUTINGNO, '') AS RoutingNo, dt.Amount,
|
|
p.BatchNumber, p.PaymentDate, p.LFAYear
|
|
FROM LFAPayment p
|
|
INNER JOIN LFAPaymentDetail dt ON dt.LFAPaymentID = p.LFAPaymentID
|
|
INNER JOIN EMPLOYEE e ON e.EMPLOYEEID = dt.EmployeeID
|
|
LEFT JOIN DESIGNATION ds ON ds.DESIGNATIONID = dt.DesignationID
|
|
LEFT JOIN DEPARTMENT d ON d.DEPARTMENTID = dt.DepartmentID
|
|
LEFT JOIN CRG cc ON cc.CRGID = dt.CostCenterID
|
|
LEFT JOIN BRANCHES br ON br.BRANCHID = dt.BranchID
|
|
LEFT JOIN BANKS bk ON bk.BANKID = br.BANKID
|
|
WHERE p.LFAPaymentID=%n AND p.PayrollTypeID=%n AND dt.Amount > 0",
|
|
lfaPaymentID, payrollTypeID);
|
|
|
|
if (!string.IsNullOrEmpty(empIDs))
|
|
sql += SQLParser.MakeSQL(" AND dt.EmployeeID IN (%q)", empIDs);
|
|
if (bankID > 0)
|
|
sql += SQLParser.MakeSQL(" AND br.BANKID=%n", bankID);
|
|
sql += " ORDER BY bk.NAME, e.EMPLOYEENO";
|
|
|
|
return tc.ExecuteDataSet(sql);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
#endregion
|
|
}
|