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 { /// /// 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. /// 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(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; } /// EmployeeNo → HistorySalaryMonthlyID of every row of one salary month, read in one query. private Dictionary GetIDsByEmployeeNo(TransactionContext tc, DateTime salaryMonth, int payrollTypeID) { Dictionary ids = new Dictionary(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 Get(DateTime salaryMonth, int payrollTypeID) { List items = new List(); DateTime month = GlobalFunctions.LastDateOfMonth(salaryMonth); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(HistorySalaryMonthlyDA.Get(tc, month, payrollTypeID)); items = this.CreateObjects(dr); dr.Close(); dr = new DataReader(HistorySalaryMonthlyDA.GetDetails(tc, month, payrollTypeID)); List details = this.CreateObjects(dr); dr.Close(); tc.End(); ILookup 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 GetEmployeeNos(DateTime salaryMonth, int payrollTypeID) { List employeeNos = new List(); 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 GetEmployeesByNo(List employeeNos) { List employees = new List(); 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 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; } /// /// 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. /// public int Save(List 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 existing = this.GetIDsByEmployeeNo(tc, month, payrollTypeID); List replaceIDs = new List(); 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 } }