using HRM.BO; using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; using Microsoft.Data.SqlClient; using System.Data; using System.Security.Cryptography.X509Certificates; using NPOI.SS.Formula.Functions; namespace HRM.DA { #region EmployeeOverTime Service public class EmployeeOverTimeService : ServiceTemplate, IEmployeeOverTimeService { #region Private functions and declaration #endregion public EmployeeOverTimeService() { } private void MapObject(EmployeeOverTime oEmployeeOverTime, DataReader oReader) { base.SetObjectID(oEmployeeOverTime, oReader.GetInt32("EmpOverTimeID").Value); oEmployeeOverTime.TermID = oReader.GetString("termID") == null ? 0 : oReader.GetInt32("termID").Value; oEmployeeOverTime.TermParameterID = oReader.GetString("TermParameterID") == null ? 0 : oReader.GetInt32("TermParameterID").Value; oEmployeeOverTime.EmployeeID = oReader.GetString("EmployeeID") == null ? 0 : oReader.GetInt32("EmployeeID").Value; oEmployeeOverTime.MonthDate = oReader.GetDateTime("monthDate").Value; oEmployeeOverTime.OTMonth = oReader.GetDateTime("OTMonth").Value; oEmployeeOverTime.OTHours = oReader.GetDouble("OTHours").Value; oEmployeeOverTime.Value = oReader.GetDouble("amount").Value; oEmployeeOverTime.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value; oEmployeeOverTime.CreatedDate = oReader.GetDateTime("CreationDate").Value; oEmployeeOverTime.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value; oEmployeeOverTime.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oEmployeeOverTime, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { EmployeeOverTime oEmployeeOverTime = new EmployeeOverTime(); MapObject(oEmployeeOverTime, oReader); return oEmployeeOverTime as T; } protected EmployeeOverTime CreateObject(DataReader oReader) { EmployeeOverTime oEmployeeOverTime = new EmployeeOverTime(); MapObject(oEmployeeOverTime, oReader); return oEmployeeOverTime; } #region Service implementation public EmployeeOverTime Get(int id) { EmployeeOverTime oEmployeeOverTime = new EmployeeOverTime(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(EmployeeOverTimeDA.Get(tc, id)); if (oreader.Read()) { oEmployeeOverTime = this.CreateObject(oreader); } oreader.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 oEmployeeOverTime; } public List Get(DateTime dSalaryMonth, int nPayrollTypeID) { List empOTs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeOverTimeDA.Get(tc, dSalaryMonth, nPayrollTypeID)); empOTs = this.CreateObjects(dr); 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 empOTs; } public List Get() { List empOTs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeOverTimeDA.Get(tc)); empOTs = this.CreateObjects(dr); 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 empOTs; } public List GetByEmpID(int nEmpID, DateTime dMonthDate) { List empOTs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeOverTimeDA.GetByEmpID(tc, nEmpID, dMonthDate)); empOTs = this.CreateObjects(dr); 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 empOTs; } public EmployeeOverTime GetEmpOvertimeByOTMonth(int nEmpID, DateTime dOTMonth, int TermID, int TermParameterID) { EmployeeOverTime empOT = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(EmployeeOverTimeDA.GetEmpOvertimeByOTMonth(tc, nEmpID, dOTMonth, TermID, TermParameterID)); if (dr.Read()) { empOT = this.CreateObject(dr); } 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 empOT; } public int Save(EmployeeOverTime oEmployeeOverTime) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); oEmployeeOverTime.ID = this.Save(tc, oEmployeeOverTime); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oEmployeeOverTime.ID; } public int Save(TransactionContext tc, EmployeeOverTime oEmployeeOverTime) { try { if (oEmployeeOverTime.IsNew) { int id = tc.GenerateID("EMPOVERTIME", "EmpOverTimeID"); base.SetObjectID(oEmployeeOverTime, id); EmployeeOverTimeDA.Insert(tc, oEmployeeOverTime); } else { EmployeeOverTimeDA.Update(tc, oEmployeeOverTime); } return oEmployeeOverTime.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void BulkSave(TransactionContext tc, List oempovertimes) { try { //sp_RENAME 'EMPOVERTIME.[EmployeeID]', 'EMPLOYEEID', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[MonthDate]', 'MONTHDATE', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[TermID]', 'TERMID', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[TermParameterID]', 'TERMPARAMETERID', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[OTHours]', 'OTHOURS', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[Amount]', 'AMOUNT', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[CreatedBy]', 'CREATEDBY', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[CreationDate]', 'CREATIONDATE', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[ModifiedBy]', 'MODIFIEDBY', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[ModifiedDate]', 'MODIFIEDDATE', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[OTMonth]', 'OTMONTH', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[EmpOverTimeID]', 'EMPOVERTIMEID', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[PayrollTypeID]', 'PAYROLLTYPEID', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[PRVMONTHHOURS]', 'PRVMONTHHOURS', 'COLUMN'; //sp_RENAME 'EMPOVERTIME.[CURRMONTHHOURS]', 'CURRMONTHHOURS', 'COLUMN'; int id = tc.GenerateID("EMPOVERTIME", "EmpOverTimeID"); int salaryPKID = tc.GenerateID("EMPOVERTIME", "EmpOverTimeID"); DataTable oitem = new DataTable("EMPOVERTIME"); oitem.Columns.Add(new DataColumn("EMPLOYEEID", typeof(int))); oitem.Columns.Add(new DataColumn("MONTHDATE", typeof(DateTime))); oitem.Columns.Add(new DataColumn("TERMID", typeof(int))); oitem.Columns.Add(new DataColumn("TERMPARAMETERID", typeof(int))); // NULL oitem.Columns.Add(new DataColumn("OTHOURS", typeof(double))); // NULL oitem.Columns.Add(new DataColumn("AMOUNT", typeof(double))); // NULL oitem.Columns.Add(new DataColumn("CREATEDBY", typeof(int))); oitem.Columns.Add(new DataColumn("CREATIONDATE", typeof(DateTime))); oitem.Columns.Add(new DataColumn("MODIFIEDBY", typeof(int))); oitem.Columns.Add(new DataColumn("MODIFIEDDATE", typeof(DateTime))); oitem.Columns.Add(new DataColumn("OTMONTH", typeof(DateTime))); // NULL oitem.Columns.Add(new DataColumn("EMPOVERTIMEID", typeof(int))); // NULL, oitem.Columns.Add(new DataColumn("PAYROLLTYPEID", typeof(int))); // NULL, oitem.Columns.Add(new DataColumn("PRVMONTHHOURS", typeof(double))); // NULL, oitem.Columns.Add(new DataColumn("CURRMONTHHOURS", typeof(double))); // NULL, foreach (EmployeeOverTime item in oempovertimes) { salaryPKID = salaryPKID + 1; item.ID = salaryPKID; oitem.Rows.Add( item.EmployeeID, item.MonthDate, item.TermID, item.TermParameterID, item.OTHours, item.Value, item.CreatedBy, item.CreatedDate, item.ModifiedBy, item.ModifiedDate, item.OTMonth, salaryPKID, item.PayrollTypeID, null, null ); } using (SqlBulkCopy bulkCopy = new SqlBulkCopy((SqlConnection)tc.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)tc.Transaction)) { bulkCopy.BulkCopyTimeout = 6000; // in seconds bulkCopy.ColumnMappings.Add("EMPLOYEEID", "EMPLOYEEID"); bulkCopy.ColumnMappings.Add("MONTHDATE", "MONTHDATE"); bulkCopy.ColumnMappings.Add("TERMID", "TERMID"); bulkCopy.ColumnMappings.Add("TERMPARAMETERID", "TERMPARAMETERID"); bulkCopy.ColumnMappings.Add("OTHOURS", "OTHOURS"); bulkCopy.ColumnMappings.Add("AMOUNT", "AMOUNT"); bulkCopy.ColumnMappings.Add("CREATEDBY", "CREATEDBY"); bulkCopy.ColumnMappings.Add("CREATIONDATE", "CREATIONDATE"); bulkCopy.ColumnMappings.Add("MODIFIEDBY", "MODIFIEDBY"); bulkCopy.ColumnMappings.Add("MODIFIEDDATE", "MODIFIEDDATE"); bulkCopy.ColumnMappings.Add("OTMONTH", "OTMONTH"); bulkCopy.ColumnMappings.Add("EMPOVERTIMEID", "EMPOVERTIMEID"); bulkCopy.ColumnMappings.Add("PAYROLLTYPEID", "PAYROLLTYPEID"); bulkCopy.ColumnMappings.Add("PRVMONTHHOURS", "PRVMONTHHOURS"); bulkCopy.ColumnMappings.Add("CURRMONTHHOURS", "CURRMONTHHOURS"); bulkCopy.DestinationTableName = "EMPOVERTIME"; bulkCopy.WriteToServer(oitem); } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void SaveByExcel(List _EmpOverTimes) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (_EmpOverTimes.Count > 0) EmployeeOverTimeDA.DeleteByMonth(tc, _EmpOverTimes[0].MonthDate); int id = tc.GenerateID("EMPOVERTIME", "EmpOverTimeID"); foreach (EmployeeOverTime oEmpOverTime in _EmpOverTimes) { if (oEmpOverTime.OTHours > 0) { base.SetObjectID(oEmpOverTime, id); EmployeeOverTimeDA.Insert(tc, oEmpOverTime); id = id + 1; } } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Save(List _EmpOverTimes) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (EmployeeOverTime oEmpOverTime in _EmpOverTimes) EmployeeOverTimeDA.DeleteByEmpID(tc, oEmpOverTime.EmployeeID, oEmpOverTime.MonthDate); int id = tc.GenerateID("EMPOVERTIME", "EmpOverTimeID"); foreach (EmployeeOverTime oEmpOverTime in _EmpOverTimes) { if (oEmpOverTime.OTHours > 0) { base.SetObjectID(oEmpOverTime, id); EmployeeOverTimeDA.Insert(tc, oEmpOverTime); id = id + 1; } } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void DeleteByMonth(DateTime dOTMonth) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); EmployeeOverTimeDA.DeleteByMonth(tc, dOTMonth); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Delete(TransactionContext tc, EmployeeOverTime empOvertime) { try { EmployeeOverTimeDA.Delete(tc, empOvertime.ID); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Delete(EmployeeOverTime empOvertime) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); EmployeeOverTimeDA.Delete(tc, empOvertime.ID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #endregion } #endregion }