282 lines
11 KiB
C#
282 lines
11 KiB
C#
using Ease.Core.DataAccess;
|
|
using Ease.Core.Model;
|
|
using Ease.Core.Utility;
|
|
using HRM.BO;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace HRM.DA
|
|
{
|
|
/// <summary>
|
|
/// Serves the HistorySalaryMonthly / HistorySalaryMonthlyDetail holding tables, which keep
|
|
/// one salary month of uploaded salary data per employee ("Salary Master Data Bulk Upload").
|
|
/// Nothing here reads or writes SalaryMonthly or any other payroll table.
|
|
/// </summary>
|
|
public class HistorySalaryMonthlyService : ServiceTemplate, IHistorySalaryMonthlyService
|
|
{
|
|
#region Private functions and declaration
|
|
|
|
public HistorySalaryMonthlyService()
|
|
{
|
|
}
|
|
|
|
private void MapObject(HistorySalaryMonthly item, DataReader oReader)
|
|
{
|
|
base.SetObjectID(item, oReader.GetInt32("HistorySalaryMonthlyID").Value);
|
|
item.SalaryMonth = oReader.GetDateTime("SalaryMonth").Value;
|
|
item.EmployeeNo = oReader.GetString("EmployeeNo");
|
|
item.Name = oReader.GetString("Name");
|
|
item.Department = oReader.GetString("Department");
|
|
item.Designation = oReader.GetString("Designation");
|
|
item.Location = oReader.GetString("Location");
|
|
item.CostCenter = oReader.GetString("CostCenter");
|
|
item.Grade = oReader.GetString("Grade");
|
|
item.IsConfirm = oReader.GetString("IsConfirm");
|
|
item.PayrollTypeID = oReader.GetInt32("PayrollTypeID", 0);
|
|
item.CreatedBy = oReader.GetInt32("CreatedBy", 0);
|
|
item.CreatedDate = oReader.GetDateTime("CreatedDate", DateTime.MinValue);
|
|
item.ModifiedBy = oReader.GetInt32("ModifiedBy");
|
|
item.ModifiedDate = oReader.GetDateTime("ModifiedDate");
|
|
this.SetObjectState(item, Ease.Core.ObjectState.Saved);
|
|
}
|
|
|
|
private void MapDetailObject(HistorySalaryMonthlyDetail item, DataReader oReader)
|
|
{
|
|
base.SetObjectID(item, oReader.GetInt32("HistorySalaryMonthlyDetailID").Value);
|
|
item.HistorySalaryMonthlyID = oReader.GetInt32("HistorySalaryMonthlyID", 0);
|
|
item.EmployeeNo = oReader.GetString("EmployeeNo");
|
|
item.ItemName = oReader.GetString("ItemName");
|
|
item.ItemSide = oReader.GetString("ItemSide");
|
|
item.Amount = oReader.GetDouble("Amount");
|
|
item.Sequence = oReader.GetInt32("Sequence", 0);
|
|
this.SetObjectState(item, Ease.Core.ObjectState.Saved);
|
|
}
|
|
|
|
protected override T CreateObject<T>(DataReader oReader)
|
|
{
|
|
if (typeof(T) == typeof(HistorySalaryMonthlyDetail))
|
|
{
|
|
HistorySalaryMonthlyDetail detail = new HistorySalaryMonthlyDetail();
|
|
MapDetailObject(detail, oReader);
|
|
return detail as T;
|
|
}
|
|
|
|
HistorySalaryMonthly item = new HistorySalaryMonthly();
|
|
MapObject(item, oReader);
|
|
return item as T;
|
|
}
|
|
|
|
/// <summary>EmployeeNo → HistorySalaryMonthlyID of every row of one salary month, read in one query.</summary>
|
|
private Dictionary<string, int> GetIDsByEmployeeNo(TransactionContext tc, DateTime salaryMonth, int payrollTypeID)
|
|
{
|
|
Dictionary<string, int> ids = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
|
|
DataReader dr = new DataReader(HistorySalaryMonthlyDA.GetEmployeeNos(tc, salaryMonth, payrollTypeID));
|
|
while (dr.Read())
|
|
{
|
|
string employeeNo = dr.GetString("EmployeeNo");
|
|
int? id = dr.GetInt32("HistorySalaryMonthlyID");
|
|
if (string.IsNullOrWhiteSpace(employeeNo) || id == null) continue;
|
|
ids[employeeNo.Trim()] = id.Value;
|
|
}
|
|
|
|
dr.Close();
|
|
return ids;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Service implementation
|
|
|
|
public List<HistorySalaryMonthly> Get(DateTime salaryMonth, int payrollTypeID)
|
|
{
|
|
List<HistorySalaryMonthly> items = new List<HistorySalaryMonthly>();
|
|
DateTime month = GlobalFunctions.LastDateOfMonth(salaryMonth);
|
|
|
|
TransactionContext tc = null;
|
|
try
|
|
{
|
|
tc = TransactionContext.Begin();
|
|
DataReader dr = new DataReader(HistorySalaryMonthlyDA.Get(tc, month, payrollTypeID));
|
|
items = this.CreateObjects<HistorySalaryMonthly>(dr);
|
|
dr.Close();
|
|
|
|
dr = new DataReader(HistorySalaryMonthlyDA.GetDetails(tc, month, payrollTypeID));
|
|
List<HistorySalaryMonthlyDetail> details = this.CreateObjects<HistorySalaryMonthlyDetail>(dr);
|
|
dr.Close();
|
|
tc.End();
|
|
|
|
ILookup<int, HistorySalaryMonthlyDetail> byHeader = details.ToLookup(x => x.HistorySalaryMonthlyID);
|
|
foreach (HistorySalaryMonthly item in items)
|
|
{
|
|
item.Details = byHeader[item.ID].ToList();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
#region Handle Exception
|
|
|
|
if (tc != null)
|
|
tc.HandleError();
|
|
ExceptionLog.Write(e);
|
|
throw new ServiceException(e.Message, e);
|
|
|
|
#endregion
|
|
}
|
|
|
|
return items;
|
|
}
|
|
|
|
public List<string> GetEmployeeNos(DateTime salaryMonth, int payrollTypeID)
|
|
{
|
|
List<string> employeeNos = new List<string>();
|
|
DateTime month = GlobalFunctions.LastDateOfMonth(salaryMonth);
|
|
|
|
TransactionContext tc = null;
|
|
try
|
|
{
|
|
tc = TransactionContext.Begin();
|
|
employeeNos = this.GetIDsByEmployeeNo(tc, month, payrollTypeID).Keys.ToList();
|
|
tc.End();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
#region Handle Exception
|
|
|
|
if (tc != null)
|
|
tc.HandleError();
|
|
ExceptionLog.Write(e);
|
|
throw new ServiceException(e.Message, e);
|
|
|
|
#endregion
|
|
}
|
|
|
|
return employeeNos;
|
|
}
|
|
|
|
public List<Employee> GetEmployeesByNo(List<string> employeeNos)
|
|
{
|
|
List<Employee> employees = new List<Employee>();
|
|
if (employeeNos == null || employeeNos.Count == 0)
|
|
return employees;
|
|
|
|
TransactionContext tc = null;
|
|
try
|
|
{
|
|
tc = TransactionContext.Begin();
|
|
// In chunks, so the IN list stays a reasonable size for a large upload.
|
|
List<string> distinct = employeeNos.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
|
for (int i = 0; i < distinct.Count; i += 500)
|
|
{
|
|
DataReader dr = new DataReader(HistorySalaryMonthlyDA.GetEmployeesByNo(tc, distinct.Skip(i).Take(500)));
|
|
while (dr.Read())
|
|
{
|
|
Employee employee = new Employee();
|
|
base.SetObjectID(employee, dr.GetInt32("EmployeeID").Value);
|
|
employee.EmployeeNo = dr.GetString("EmployeeNo");
|
|
employee.Name = dr.GetString("Name");
|
|
employee.PayrollTypeID = dr.GetInt32("PayrollTypeID", 0);
|
|
employee.Status = (EnumEmployeeStatus)dr.GetInt32("Status", 0);
|
|
employees.Add(employee);
|
|
}
|
|
dr.Close();
|
|
}
|
|
tc.End();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
#region Handle Exception
|
|
|
|
if (tc != null)
|
|
tc.HandleError();
|
|
ExceptionLog.Write(e);
|
|
throw new ServiceException(e.Message, e);
|
|
|
|
#endregion
|
|
}
|
|
|
|
return employees;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Saves every row in one transaction — all rows or none. An employee who already has a
|
|
/// row for the salary month (same payroll type) has that row and its items deleted first,
|
|
/// so uploading a corrected file simply replaces what was there. Other employees' rows of
|
|
/// the month are not touched.
|
|
/// </summary>
|
|
public int Save(List<HistorySalaryMonthly> items, DateTime salaryMonth, int payrollTypeID, int userID)
|
|
{
|
|
if (items == null || items.Count == 0)
|
|
throw new ServiceException("No salary data to save.");
|
|
if (payrollTypeID <= 0)
|
|
throw new ServiceException("No payroll type is selected.");
|
|
|
|
DateTime month = GlobalFunctions.LastDateOfMonth(salaryMonth);
|
|
int replaced = 0;
|
|
|
|
TransactionContext tc = null;
|
|
try
|
|
{
|
|
tc = TransactionContext.Begin(true);
|
|
|
|
Dictionary<string, int> existing = this.GetIDsByEmployeeNo(tc, month, payrollTypeID);
|
|
List<int> replaceIDs = new List<int>();
|
|
foreach (HistorySalaryMonthly item in items)
|
|
{
|
|
int existingID;
|
|
if (existing.TryGetValue((item.EmployeeNo ?? string.Empty).Trim(), out existingID))
|
|
replaceIDs.Add(existingID);
|
|
}
|
|
|
|
replaced = replaceIDs.Count;
|
|
// In chunks, so the IN list stays a reasonable size for a large upload.
|
|
for (int i = 0; i < replaceIDs.Count; i += 500)
|
|
{
|
|
HistorySalaryMonthlyDA.Delete(tc, string.Join(",", replaceIDs.Skip(i).Take(500)));
|
|
}
|
|
|
|
int id = tc.GenerateID("HistorySalaryMonthly", "HistorySalaryMonthlyID");
|
|
int detailID = tc.GenerateID("HistorySalaryMonthlyDetail", "HistorySalaryMonthlyDetailID");
|
|
DateTime now = DateTime.Now;
|
|
foreach (HistorySalaryMonthly item in items)
|
|
{
|
|
base.SetObjectID(item, id);
|
|
item.SalaryMonth = month;
|
|
item.EmployeeNo = (item.EmployeeNo ?? string.Empty).Trim();
|
|
item.PayrollTypeID = payrollTypeID;
|
|
item.CreatedBy = userID;
|
|
item.CreatedDate = now;
|
|
HistorySalaryMonthlyDA.Insert(tc, item);
|
|
|
|
foreach (HistorySalaryMonthlyDetail detail in item.Details)
|
|
{
|
|
base.SetObjectID(detail, detailID);
|
|
detail.HistorySalaryMonthlyID = item.ID;
|
|
detail.EmployeeNo = item.EmployeeNo;
|
|
detailID++;
|
|
}
|
|
HistorySalaryMonthlyDA.InsertDetails(tc, item.Details);
|
|
id++;
|
|
}
|
|
|
|
tc.End();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
#region Handle Exception
|
|
|
|
if (tc != null)
|
|
tc.HandleError();
|
|
ExceptionLog.Write(e);
|
|
throw new ServiceException(e.Message, e);
|
|
|
|
#endregion
|
|
}
|
|
|
|
return replaced;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|