using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; using HRM.BO; using System.Linq; namespace HRM.DA { #region LeaveParameter Service public class LeaveParameterService : ServiceTemplate, ILeaveParameterService { #region Leave Process #region Private functions and declaration public LeaveParameterService() { } private void MapObject(LeaveParameter oLeaveParameter, DataReader oReader) { LeaveParameterDetail oDetail; oDetail = new LeaveParameterDetail(); base.SetObjectID(oLeaveParameter, oReader.GetInt32("LeaveParamId").Value); oLeaveParameter.LeaveId = oReader.GetInt32("LeaveId").Value; oLeaveParameter.MaxAccumulatedDays = oReader.GetDouble("AccumulatedLeave").Value; oLeaveParameter.IsActive = oReader.GetBoolean("IsActive").Value; oLeaveParameter.ApplicableFor = (LeaveApplicableType)oReader.GetInt32("ApplicableFor").Value; oLeaveParameter.ForfitedMonth = oReader.GetInt32("FortifiedMonth").Value; oLeaveParameter.AllowAdvance = oReader.GetBoolean("AllowAdvance").Value; oLeaveParameter.IgnoreHoliday = oReader.GetBoolean("IgnoreHolidays").Value; oLeaveParameter.CalculationType = (EnumLeaveCalculationType)oReader.GetInt32("MonthlyBalance").Value; oLeaveParameter.IsForfited = oReader.GetBoolean("IsForfited").Value; oLeaveParameter.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value; oLeaveParameter.CreatedDate = oReader.GetDateTime("CreationDate").GetValueOrDefault(); oLeaveParameter.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value; oLeaveParameter.ModifiedDate = oReader.GetDateTime("ModifiedDate").GetValueOrDefault(); oLeaveParameter.PayrollTypeID = oReader.GetInt32("PayrollTypeID").Value; //this.SetObjectState(oLeaveParameter, ObjectState.Saved); this.SetObjectState(oLeaveParameter, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { LeaveParameter oLeaveParameter = new LeaveParameter(); MapObject(oLeaveParameter, oReader); return oLeaveParameter as T; } private LeaveParameter CreateObject(DataReader oReader) { LeaveParameter oLeaveParameter = new LeaveParameter(); MapObject(oLeaveParameter, oReader); return oLeaveParameter; } private void MapDetailObject(LeaveParameterDetail oLeaveParameterDetail, DataReader oReader) { base.SetObjectID(oLeaveParameterDetail, oReader.GetInt32("ID").Value); oLeaveParameterDetail.LeaveParamId = oReader.GetInt32("LeaveParamId").Value; oLeaveParameterDetail.MaxDays = oReader.GetDouble("MaxDays").Value; //oLeaveParameterDetail.MaxGrndDays = oReader.GetDouble("MaxGrndDays").Value; oLeaveParameterDetail.MaxGrndDays = oReader.GetDouble("MaxGrndDays", true, 0.00); oLeaveParameterDetail.MaxCF = oReader.GetDouble("MaxCF").Value; oLeaveParameterDetail.MaxEncash = oReader.GetDouble("MaxEncash").Value; oLeaveParameterDetail.Year = oReader.GetInt32("Year").Value; this.SetObjectState(oLeaveParameterDetail, Ease.Core.ObjectState.Saved); } private void MapItemObject(LeaveParameter.paramItem oLeaveParameterDetail, DataReader oReader) { oLeaveParameterDetail.paramid = oReader.GetInt32("LeaveParamId").Value; oLeaveParameterDetail.id = oReader.GetInt32("LeaveParamItemId").Value; oLeaveParameterDetail.itemID = oReader.GetInt32("itemID").Value; oLeaveParameterDetail.ParamOnType = (EnumLeaveparamOn)oReader.GetInt32("ItemType").Value; } protected List CreateDetailObjects(DataReader oReader) { //LeaveParameterDetail oLeaveParameterDetail = new LeaveParameterDetail(); //MapDetailObject(oLeaveParameterDetail, oReader); //return oLeaveParameterDetail as T; List oLeaveParameterDetails = new List(); while (oReader.Read()) { LeaveParameterDetail oLeaveParameterDetail = new LeaveParameterDetail(); MapDetailObject(oLeaveParameterDetail, oReader); oLeaveParameterDetails.Add(oLeaveParameterDetail); } return oLeaveParameterDetails; } protected List CreateParamItems(DataReader oReader) { //LeaveParameterDetail oLeaveParameterDetail = new LeaveParameterDetail(); //MapDetailObject(oLeaveParameterDetail, oReader); //return oLeaveParameterDetail as T; List oLeaveParameterDetails = new List(); while (oReader.Read()) { LeaveParameter.paramItem oLeaveParameterDetail = new LeaveParameter.paramItem(); MapItemObject(oLeaveParameterDetail, oReader); oLeaveParameterDetails.Add(oLeaveParameterDetail); } return oLeaveParameterDetails; } private LeaveParameterDetail CreateDetailObject(DataReader oReader) { LeaveParameterDetail oLeaveParameterDetail = new LeaveParameterDetail(); MapDetailObject(oLeaveParameterDetail, oReader); return oLeaveParameterDetail; } #endregion public List GetUsedParam(int leaveparamid, int payrolltypeid) { List usedGrades = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveParameterDA.GetUsedParamItem(tc, leaveparamid, payrolltypeid)); usedGrades = this.CreateParamItems(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; } #region Service implementation public LeaveParameterDetail GetApplicableForEmployee(Employee oEmp, DateTime operationDate, List oAllDetails, int nParamID) { //#### change by Current date function TimeSpan ts = operationDate - oEmp.JoiningDate; double y = Math.Round((double)ts.Days / 365, 2); //int year = operationDate.Year - oEmp.JoiningDate.Year+1; //int number=(int)y; //double number1 =(double)(y-number); //float f =(float)y; int year = 0; //if (y > (int)y) // year = (int)y + 1; //else year = (int)y; int maxYear = -1; LeaveParameterDetail dtl = null; List oDetails = oAllDetails.FindAll(delegate(LeaveParameterDetail lpd) { return lpd.LeaveParamId == Convert.ToInt32(nParamID.ToString()); }); foreach (LeaveParameterDetail detail in oDetails) { if (detail.Year <= year && detail.Year >= maxYear) { maxYear = detail.Year; dtl = detail; } } return dtl; } public List Get() { List oLeaveParameters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveParameterDA.GetLeaveParam(tc)); oLeaveParameters = this.CreateObjects(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 oLeaveParameters; } public LeaveParameter Get(int id) { LeaveParameter oLeaveParameter = new LeaveParameter(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveParameterDA.GetLeaveParam(tc, id)); if (dr.Read()) { oLeaveParameter = CreateObject(dr); } dr.Close(); if (oLeaveParameter != null) { dr = new DataReader(LeaveParameterDA.GetDetails(tc, oLeaveParameter.ID)); oLeaveParameter.Details = CreateDetailObjects(dr); dr.Close(); //tc.End(); dr = new DataReader(LeaveParameterDA.GetParamItems(tc, oLeaveParameter.ID)); oLeaveParameter.ParamItems = CreateParamItems(dr); dr.Close(); tc.End(); } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave Parameter :" + e.Message, e); #endregion } return oLeaveParameter; } public List GetAllByPayrollTypeID(int PayrollTypeID) { List leaveParams = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveParameterDA.GetAllByPayrollTypeID(tc, PayrollTypeID)); leaveParams = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave Parameter :" + e.Message, e); #endregion } return leaveParams; } public List GetDetails() { #region Cache Header List oDetails = new List(); #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveParameterDA.GetDetails(tc)); oDetails = this.CreateDetailObjects(oreader); oreader.Close(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException("Failed to Get Leave Details: " + ex.Message, ex); #endregion } return oDetails; } private List GetAppParams(int itemID, EnumLeaveparamOn paramType, int? leaveID) { List leaveParams = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveParameterDA.GetApplicableParams(tc, itemID, paramType, leaveID)); leaveParams = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave Parameter :" + e.Message, e); #endregion } if (leaveID != null && leaveParams.Count > 1) { throw new Exception("Multiple parameter found for a leave"); } return leaveParams; } public List GetApplicableParams(int itemID, EnumLeaveparamOn paramType) { return this.GetAppParams(itemID, paramType, null); } public LeaveParameter GetApplicableParam(int itemID, EnumLeaveparamOn paramType, int LeaveID) { List lparams = null; lparams = this.GetAppParams(itemID, paramType,LeaveID); if (lparams.Count > 1) { throw new Exception("Multiple parameter found for a leave"); } if (lparams.Count == 0) return null; return lparams[0]; } public List GetApplicableParam(List Leaveparams, List oleaves, int gradeOrLocationID, EnumLeaveparamOn paramType, Employee emp) { List lparams = new List(); foreach (LeaveParameter param in Leaveparams) { if (param.ApplicableFor != LeaveApplicableType.Regardless) { if (param.ApplicableFor == LeaveApplicableType.Confirmed && emp.IsConfirmed == false) continue; if (param.ApplicableFor == LeaveApplicableType.Probetionary && emp.IsConfirmed == true) continue; } //param.Leave = oleaves.Find(x => x.ID == param.LeaveId); if (param.Leave == null) { //param.Leave = new LeaveService().Get(param.LeaveId); param.Leave = oleaves.Find(item => item.ID == param.LeaveId); } if (param.Leave.ApplicableFor != EnumGender.None) { if (param.Leave.ApplicableFor != emp.Gender) continue; } //var item = param.ParamItems.Find(y => y.itemID == gradeOrLocationID && paramType == y.ParamOnType); //if (item != null) //{ // lparams.Add(param); //} lparams.Add(param); } return lparams; } public List GetByParamId(int paramId) { List oLeaveParameters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveParameterDA.GetByParamId(tc, paramId)); oLeaveParameters = this.CreateObjects(oreader); oreader.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave Parameter : " + e.Message, e); #endregion } return oLeaveParameters; } public List Get(EnumStatus status, int payrollTypeID, int? leaveID, bool? withDetail = false) { List oLeaveParameters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveParameterDA.Get(tc, status, payrollTypeID, leaveID)); oLeaveParameters = this.CreateObjects(oreader); oreader.Close(); if (oLeaveParameters.Count > 0 && withDetail == true) { oreader = new DataReader(LeaveParameterDA.GetDetails(tc)); List details = this.CreateDetailObjects(oreader); foreach (LeaveParameter item in oLeaveParameters) { item.Details = details.FindAll(x => x.LeaveParamId == item.ID); } oreader.Close(); oreader = new DataReader(LeaveParameterDA.GetAllParamItems(tc)); List pItems = this.CreateParamItems(oreader); foreach (LeaveParameter item in oLeaveParameters) { item.ParamItems = pItems.FindAll(x => x.paramid == item.ID); } 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 oLeaveParameters; } public List GetDetails(int leaveParamId) { List oDetails = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveParameterDA.GetDetails(tc, leaveParamId)); oDetails = this.CreateDetailObjects(oreader); oreader.Close(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException("Failed to Get Leave Details: " + ex.Message, ex); #endregion } return oDetails; } public LeaveParameterDetail GetDetail(int paramID) { LeaveParameterDetail oLeaveDetail = new LeaveParameterDetail(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveParameterDA.GetDetails(tc, paramID)); if (dr.Read()) { oLeaveDetail = CreateDetailObject(dr); } dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave Parameter: " + e.Message, e); #endregion } return oLeaveDetail; } public int Save(LeaveParameter oLeaveParameter) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oLeaveParameter.IsNew) { int id = tc.GenerateID("LeaveParameter", "LEAVEPARAMID"); base.SetObjectID(oLeaveParameter, id); LeaveParameterDA.Insert(tc, oLeaveParameter); } else { LeaveParameterDA.Update(tc, oLeaveParameter); } LeaveParameterDA.DeleteDetails(tc, oLeaveParameter.ID); foreach (LeaveParameterDetail oDetail in oLeaveParameter.Details) { oDetail.LeaveParamId = oLeaveParameter.ID; this.SetObjectID(oDetail, LeaveParameterDA.GetNewDetailID(tc)); LeaveParameterDA.InsertChildren(tc, oDetail); } LeaveParameterDA.DeleteParamItems(tc, oLeaveParameter.ID); foreach (LeaveParameter.paramItem item in oLeaveParameter.ParamItems) { item.paramid = oLeaveParameter.ID; item.ParamOnType = oLeaveParameter.ParamOnType; LeaveParameterDA.InsertParamItem(tc, item); } // SetupDetailService sdetailService = new SetupDetailService(); //sdetailService.Insert(tc, EnumParameterSetup.Leave, oLeaveParameter.SetupDetails, oLeaveParameter.ID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Insert Leave Parameter :" + e.Message, e); #endregion } return oLeaveParameter.ID; } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); SetupDetailService sd = new SetupDetailService(); sd.Delete(tc, EnumParameterSetup.Leave, id); LeaveParameterDA.DeleteDetails(tc, id); //LeaveParameterDA.DeleteLeaveGrades(tc, id); //LeaveParameterDA.DeleteLeavePayItems(tc, id); //LeaveParameterDA.DeleteLeaveSBU(tc, id); //LeaveParameterDA.DeleteLeaveLocation(tc, id); //LeaveParameterDA.DeleteLeaveFunction(tc, id); //tc.End(); //tc = TransactionContext.Begin(true, UtilityDA.Contex); LeaveParameterDA.Delete(tc, id); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Delete Leave Parameter: " + e.Message, e); #endregion } } public static List ApplicableParams(Employee oEmp, List setupTypes, List details, List leaveParamss) { List leaveParamIds = ApplicableParametersForReport(oEmp, setupTypes, details); List leaveParams = new List(); foreach (LeaveParameter lparam in leaveParamss) { lparam.Leave = new LeaveService().Get(lparam.LeaveId); if (lparam.Leave.ApplicableFor != (EnumGender)oEmp.Gender && lparam.Leave.ApplicableFor != EnumGender.Other && lparam.Leave.ApplicableFor != EnumGender.None) { continue; } bool found = false; foreach (int id in leaveParamIds) { if (id == lparam.ID) { found = true; break; } } if (found) { leaveParams.Add(lparam); //if (lparam.IsProbationaryApplicable) //{ // if (!oEmp.IsConfirmed) // { // leaveParams.Add(lparam); // } //} //if (lparam.IsConfirmedApplicable) //{ // if (oEmp.IsConfirmed) // { // leaveParams.Add(lparam); // } //} //if (lparam.IsRegardlessApplicable) //{ // if (leaveParams.Find(x => x.ID == lparam.ID) == null) // leaveParams.Add(lparam); //} //switch (lparam.ApplicableFor) //{ // case LeaveApplicableType.Probetionary: // if (!oEmp.IsConfirmed) // { // leaveParams.Add(lparam); // } // break; // case LeaveApplicableType.Confirmed: // if (oEmp.IsConfirmed) // { // leaveParams.Add(lparam); // } // break; // case LeaveApplicableType.Regardless: // leaveParams.Add(lparam); // break; // default: // break; //} } } return leaveParams; } public static List ApplicableParametersForReport(Employee oEmp, List setupTypes, List details) { List parametersID = new List(); //List setupTypes = SetupDetail.GetTypes(setup); if (setupTypes == null) return parametersID; //ObjectsTemplate details = SetupDetail.GetParameters(setup); foreach (EnmSetupManagerTranType type in setupTypes) { int nTranID = SetupDetailService.GetTranID(oEmp, type); if (nTranID == 0) continue; var setupIds = (from detail in details where detail.TranType == type && detail.TranID == nTranID select detail.SetupID).Distinct(); foreach (var id in setupIds) parametersID.Add(id); } return parametersID; } public List Get(bool isActive, int payrollTypeID) { TransactionContext tc = null; List oLeaveParameters = new List(); try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveParameterDA.Get(tc, isActive, payrollTypeID)); oLeaveParameters = this.CreateObjects(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 oLeaveParameters; } public LeaveParameter ApplicableParam(Employee oEmp, int leaveID) { LeaveParameter oLeaveParameter = new LeaveParameter(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveParameterDA.Get(tc, oEmp, leaveID)); if (dr.Read()) { oLeaveParameter = CreateObject(dr); } dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave Parameter :" + e.Message, e); #endregion } return oLeaveParameter; } public List ApplicableParamsForReport(Employee oEmp, List setupTypes, List details, List leaveParamss) { List leaveParamIds = ApplicableParametersForReport(oEmp, setupTypes, details); //ObjectsTemplate leaveParamss = LeaveParameter.Get(); List leaveParams = new List(); foreach (LeaveParameter lparam in leaveParamss) { lparam.Leave = new LeaveService().Get(lparam.LeaveId); if (lparam.Leave.ApplicableFor != (EnumGender)oEmp.Gender && lparam.Leave.ApplicableFor != EnumGender.Other && lparam.Leave.ApplicableFor != EnumGender.None) { continue; } bool found = false; foreach (int id in leaveParamIds) { if (id == lparam.ID) { found = true; break; } } if (found) { switch (lparam.ApplicableFor) { case LeaveApplicableType.Probetionary: if (!oEmp.IsConfirmed) { leaveParams.Add(lparam); } break; case LeaveApplicableType.Confirmed: if (oEmp.IsConfirmed) { leaveParams.Add(lparam); } break; case LeaveApplicableType.Regardless: leaveParams.Add(lparam); break; default: break; } } } return leaveParams; } #endregion public LeaveParameterDetail GetApplicableForEmployee2(Employee oEmp, DateTime operationDate, List oAllDetails, int nParamID) { //#### change by Current date function TimeSpan ts = operationDate - oEmp.JoiningDate; double y = Math.Round((double)ts.Days / 365, 2); //int year = operationDate.Year - oEmp.JoiningDate.Year+1; //int number=(int)y; //double number1 =(double)(y-number); //float f =(float)y; int year = 0; double n = y - (int)y; //if (y > (int)y) // year = (int)y + 1; //else if (n > 0) year = (int)y + 1; else year = (int)y; int maxYear = -1; LeaveParameterDetail dtl = null; List oDetails = oAllDetails.FindAll(delegate (LeaveParameterDetail lpd) { return lpd.LeaveParamId == Convert.ToInt32(nParamID.ToString()); }); foreach (LeaveParameterDetail detail in oDetails) { if (detail.Year <= year && detail.Year >= maxYear) { maxYear = detail.Year; dtl = detail; } } return dtl; } #endregion } #endregion }