using HRM.BO; using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; namespace HRM.DA { #region TermParameter Service public class TermParameterService : ServiceTemplate, ITermParameterService { #region Private functions and declaration #endregion public TermParameterService() { } private void MapObject(TermParameter oTermParameter, DataReader oReader) { base.SetObjectID(oTermParameter, oReader.GetInt32("TermParameterID").Value); oTermParameter.TermID = oReader.GetString("termID") == null ? 0 : oReader.GetInt32("termID").Value; oTermParameter.EffectDate = oReader.GetDateTime("effectDate").Value; oTermParameter.Amount = oReader.GetDouble("amount").Value; oTermParameter.Type = (EnumOverTimeType)oReader.GetInt32("type").Value; oTermParameter.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value; oTermParameter.CreatedDate = oReader.GetDateTime("CreationDate").Value; //oTermParameter.housInMonth = oReader.GetInt32("housInMonth").Value; oTermParameter.housInMonth = oReader.GetInt32("housInMonth", 0); oTermParameter.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value; oTermParameter.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oTermParameter, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { TermParameter oTermParameter = new TermParameter(); MapObject(oTermParameter, oReader); return oTermParameter as T; } protected TermParameter CreateObject(DataReader oReader) { TermParameter oTermParameter = new TermParameter(); MapObject(oTermParameter, oReader); return oTermParameter; } private void MapObjectForTEGrade(TermParameter.TermEntityGrade oTermEGrade, DataReader oReader) { base.SetObjectID(oTermEGrade, oReader.GetInt32("TermentityID").Value); oTermEGrade.TermEntityID = oReader.GetString("TermentityID") == null ? 0 : oReader.GetInt32("TermentityID").Value; oTermEGrade.GradeID = oReader.GetString("GRADEID") == null ? 0 : oReader.GetInt32("GRADEID").Value; this.SetObjectState(oTermEGrade, Ease.Core.ObjectState.Saved); } //protected override T CreateObject1(DataReader oReader) //{ // TermEntityGrade oTermEGrade = new TermEntityGrade(); // MapObjectForTEGrade(oTermEGrade, oReader); // return oTermEGrade as T; //} protected TermParameter.TermEntityGrade CreateObjectForTEGrade(DataReader oReader) { TermParameter.TermEntityGrade oTermEGrade = new TermParameter.TermEntityGrade(); MapObjectForTEGrade(oTermEGrade, oReader); return oTermEGrade; } private List FillChildren(DataReader dr) { List _TerEGrades = new List(); while (dr.Read()) { TermParameter.TermEntityGrade item = this.CreateObjectForTEGrade(dr); _TerEGrades.Add(item); } return _TerEGrades; } #region Service implementation public TermParameter Get(int id) { TermParameter oTermParameter = new TermParameter(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(TermParameterDA.Get(tc, id)); if (oreader.Read()) { oTermParameter = this.CreateObject(oreader); } oreader.Close(); if (oTermParameter != null) { DataReader ord = new DataReader(TermParameterDA.GetGrades(tc, id)); oTermParameter.TermEntityGrades = this.FillChildren(ord); ord.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 oTermParameter; } public List Get(bool IncludeChildren, int payrolltypeId) { List termParams = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(TermParameterDA.GetByPayrollTypeID(tc, payrolltypeId)); termParams = this.CreateObjects(dr); dr.Close(); if (IncludeChildren) { SetupDetailService sdService = new SetupDetailService(); foreach (TermParameter item in termParams) { item.SetupDetails = sdService.GetParametersById(EnumParameterSetup.OT, item.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 termParams; } public List GetUsedGrades(int termID) { List usedGrades = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(TermParameterDA.GetUsedGrades(tc, termID)); usedGrades = this.FillChildren(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 usedGrades; } //public List ApplicableParameters(int gradeID) //{ // EmployeeSetupParameter empParameter = new EmployeeSetupParameter(EnumParameterSetup.OT); // empParameter.Employee = employee; // empParameter.ParameterIDs = SetupManager.ApplicableParameters(employee, EnumParameterSetup.OT); // return empParameter; //} //public List ApplicableParameters(List employees) //{ // List details = new SetupDetailService().GetParameters(EnumParameterSetup.OT); // List types = new SetupDetailService().GetTypes(EnumParameterSetup.OT); // List empParametes = new List(); // foreach (Employee emp in employees) // { // EmployeeSetupParameter empparameter = new EmployeeSetupParameter(EnumParameterSetup.OT); // empparameter.Employee = emp; // empparameter.ParameterIDs = SetupManager.ApplicableParameters(emp, EnumParameterSetup.OT, types, details); // empParametes.Add(empparameter); // } // return empParametes; //} public List GetByPayrollTypeID(int payrollTypeID) { List termParams = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(TermParameterDA.GetByPayrollTypeID(tc, payrollTypeID)); termParams = 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 termParams; } public List GetwithDetail(int payrollTypeID) { List termParams = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(TermParameterDA.GetByPayrollTypeID(tc, payrollTypeID)); termParams = this.CreateObjects(dr); dr.Close(); if(termParams !=null) { dr = new DataReader(TermParameterDA.GetGrades(tc)); List grs = FillChildren(dr); termParams.ForEach(x => { x.TermEntityGrades = grs.FindAll(y => y.TermEntityID == x.ID); }); 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 termParams; } public List GetByGradeID(int nGradeID) { List termParams = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(TermParameterDA.GetByGradeID(tc, nGradeID)); termParams = 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 termParams; } public List GetGrades(int nID) { List termEntityGrades = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(TermParameterDA.GetGrades(tc, nID)); termEntityGrades = FillChildren(dr); //termEntityGrades = 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 termEntityGrades; } public int Save(TermParameter oTermParameter) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oTermParameter.IsNew) { int id = tc.GenerateID("TermParameter", "TermParameterID"); base.SetObjectID(oTermParameter, id); TermParameterDA.Insert(tc, oTermParameter); } else { TermParameterDA.Update(tc, oTermParameter); TermParameterDA.DeleteGrade(tc, oTermParameter.ID); } foreach (TermParameter.TermEntityGrade teGrade in oTermParameter.TermEntityGrades) { teGrade.TermEntityID = oTermParameter.ID; TermParameterDA.Insert(tc, teGrade); } //SetupDetailService sdetailService = new SetupDetailService(); //sdetailService.Insert(tc, EnumParameterSetup.OT, // oTermParameter.SetupDetails, oTermParameter.ID); tc.End(); return oTermParameter.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(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); SetupDetailService sd = new SetupDetailService(); sd.Delete(tc, EnumParameterSetup.OT, id); TermParameterDA.Delete(tc, 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 public double ConvertToHourlyRate(double nBasic, double hourlyPercent = 100) { double amount = 0; nBasic = Math.Round(nBasic); int hours = ConfigurationManager.GetIntValue("overtime", "hoursonmonth", EnumConfigurationType.Logic); if (hours <= 0) hours = 208; double doubleHOur = Convert.ToDouble(hours); amount = (nBasic / doubleHOur) * (hourlyPercent / 100.00); return Math.Round(amount, 2); } } #endregion }