using HRM.BO; using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; using System.Linq; namespace HRM.DA { #region Leave Year Service public class LeaveYearService : ServiceTemplate, ILeaveYearService { #region Private functions and declaration public LeaveYearService() { } private void MapObject(LeaveYear oLeaveYear, DataReader oReader) { base.SetObjectID(oLeaveYear, oReader.GetInt32("LeaveYearID").Value); oLeaveYear.Name = oReader.GetString("Name"); oLeaveYear.StartDate = oReader.GetDateTime("StartDate").Value; oLeaveYear.EndDate = oReader.GetDateTime("EndDate").Value; oLeaveYear.IsCurrent = oReader.GetBoolean("IsCurrent").Value; oLeaveYear.IsEnded = oReader.GetBoolean("IsEnded").Value; oLeaveYear.Status = (EnumStatus)oReader.GetInt32("Status").Value; oLeaveYear.Sequence = oReader.GetInt32("SequenceNo").Value; oLeaveYear.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value; oLeaveYear.CreatedDate = oReader.GetDateTime("CreatedDate").Value; oLeaveYear.ModifiedBy = oReader.GetString("ModifiedBy") == null ? 0 : oReader.GetInt32("ModifiedBy").Value; oLeaveYear.ModifiedDate = oReader.GetDateTime("ModifiedDate"); oLeaveYear.PayrollTypeId = oReader.GetInt32("PAYROLLTYPEID").Value; var eStartDate = oReader.GetDateTime("EncashmentStartDate", true, DateTime.MinValue); if (eStartDate != DateTime.MinValue) oLeaveYear.EncashmentStartDate = oReader.GetDateTime("EncashmentStartDate"); else oLeaveYear.EncashmentStartDate = null; var eEndDate = oReader.GetDateTime("EncashmentEndDate", true, DateTime.MinValue); if (eEndDate != DateTime.MinValue) oLeaveYear.EncashmentEndDate = oReader.GetDateTime("EncashmentEndDate"); else oLeaveYear.EncashmentEndDate = null; oLeaveYear.IsEncashmentActive = oReader.GetBoolean("IsEncashmentActive",true, false); this.SetObjectState(oLeaveYear, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { LeaveYear oLeaveYear = new LeaveYear(); MapObject(oLeaveYear, oReader); return oLeaveYear as T; } LeaveYear CreateObject(DataReader oReader) { LeaveYear oLeaveYear = new LeaveYear(); MapObject(oLeaveYear, oReader); return oLeaveYear; } #endregion #region Service Implementation public LeaveYear Get(int id) { LeaveYear oLeaveYear = new LeaveYear(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveYearDA.Get(tc, id)); if (oreader.Read()) { oLeaveYear = this.CreateObject(oreader); } oreader.Close(); tc.End(); } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException("Failed to Get Leave Year: " + ex.Message, ex); #endregion } return oLeaveYear; } public List Get() { List oLeaveYears = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveYearDA.Get(tc)); oLeaveYears = 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 oLeaveYears; } public List GetByPayrollTypeId(int id) { List oLeaveYears = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveYearDA.GetByPayrollType(tc, id)); oLeaveYears = 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 oLeaveYears; } public List Get(EnumStatus status) { List leaveYears = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveYearDA.Get(tc, status)); leaveYears = 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 leaveYears; } public List Get(EnumStatus status, int payrollTypeId) { List leaveYears = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(LeaveYearDA.Get(tc, status, payrollTypeId)); leaveYears = 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 leaveYears; } public LeaveYear GetIDByName(string sLeaveYear) { LeaveYear oLeaveYear = new LeaveYear(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveYearDA.GetIDByName(tc, sLeaveYear)); if (oreader.Read()) { oLeaveYear = this.CreateObject(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: " + e.Message, e); #endregion } return oLeaveYear; } public LeaveYear GetCurrentYear(int payrolltypeid) { LeaveYear oLeaveYear = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveYearDA.GetCurrentYear(tc, payrolltypeid)); if (oreader.Read()) { oLeaveYear = this.CreateObject(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 Year: " + e.Message, e); #endregion } return oLeaveYear; } public LeaveYear GetCurrentYear(string year) { LeaveYear oLeaveYear = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveYearDA.GetCurrentYear(tc, year)); if (oreader.Read()) { oLeaveYear = this.CreateObject(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 Year: " + e.Message, e); #endregion } return oLeaveYear; } public LeaveYear GetLastYear(int payrolltypeid) { try { List leaveYears = Get(EnumStatus.Active, payrolltypeid); LeaveYear currentYear = leaveYears.FirstOrDefault(x => x.IsCurrent == true); if(currentYear ==null) { throw new Exception("Current year didn't find"); } foreach (LeaveYear oItem in leaveYears) { if (oItem.EndDate.AddDays(1) == currentYear.StartDate) { return oItem; } } } catch (Exception e) { #region Handle Exception ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave Year: " + e.Message, e); #endregion } return null; } public LeaveYear LastLeaveYear(LeaveYear oCurrYear) { try { List leaveYears = Get(); foreach (LeaveYear oItem in leaveYears) { if (oItem.EndDate.AddDays(1) == oCurrYear.StartDate) { return oItem; } } } catch (Exception e) { #region Handle Exception ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave Year: " + e.Message, e); #endregion } return null; } public LeaveYear GetLastLeaveYear(LeaveYear oCurrYear, int payrollTypeId) { TransactionContext tc = null; LeaveYear lasteLeaveYear = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(LeaveYearDA.GetLastLeaveYear(tc, oCurrYear, payrollTypeId)); if (oreader.Read()) { lasteLeaveYear = this.CreateObject(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 Year: " + e.Message, e); #endregion } return lasteLeaveYear; } public void UpdateYearEndValue(TransactionContext tc, LeaveYear leaveYear) { try { LeaveYearDA.UpdateYearEndStatus(tc, leaveYear); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Insert Leave Year: " + e.Message, e); #endregion } } public void UpdateCurrYearStatus(TransactionContext tc, LeaveYear leaveYear) { try { LeaveYearDA.UpdateCurrYearStatus(tc, leaveYear); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to update current year status; reason:" + e.Message, e); #endregion } } internal void UpdateLeaveBalance(TransactionContext tc, int employeeID, int payrollTypeID, int currentPayrollid) { try { List olFirstPayYears = new List(); LeaveYear lFirstPayPrev = new LeaveYear(); List olSecondPayYears = new List(); LeaveYear lSecPayPrev = new LeaveYear(); DataReader dr = new DataReader(LeaveYearDA.Get(tc, currentPayrollid)); olFirstPayYears = this.CreateObjects(dr); dr.Close(); DataReader dr1 = new DataReader(LeaveYearDA.Get(tc, payrollTypeID)); olSecondPayYears = this.CreateObjects(dr1); dr1.Close(); LeaveYear oFirstL = (from p in olFirstPayYears where p.IsCurrent == true select p).FirstOrDefault(); LeaveYear oSecondL = (from p in olSecondPayYears where p.IsCurrent == true select p).FirstOrDefault(); if (oFirstL != null && oSecondL != null) { foreach (LeaveYear oItem in olFirstPayYears) { if (oItem.EndDate.AddDays(1) == oFirstL.StartDate) { lFirstPayPrev = oItem; break; } } foreach (LeaveYear oItem in olSecondPayYears) { if (oItem.EndDate.AddDays(1) == oSecondL.StartDate) { lSecPayPrev = oItem; break; } } LeaveYearDA.UpdateLeaveStatusID(tc, lFirstPayPrev, lSecPayPrev, oFirstL, oSecondL, employeeID, payrollTypeID, currentPayrollid); } } catch (Exception ex) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(ex); throw new ServiceException("Failed to Update Leave Transfer Information: " + ex.Message, ex); #endregion } } public int Save(LeaveYear oLeaveYear) { TransactionContext tc = null; try { if(oLeaveYear.Name == "") { oLeaveYear.Name = oLeaveYear.StartDate.ToString("dd MMM yy") + "-" + oLeaveYear.EndDate.ToString("dd MMM yy"); } tc = TransactionContext.Begin(true); if (oLeaveYear.IsNew) { int id = tc.GenerateID("LeaveYear", "LeaveYearID"); base.SetObjectID(oLeaveYear, id); int seqNo = tc.GenerateID("LeaveYear", "SequenceNO"); oLeaveYear.Sequence = seqNo; LeaveYearDA.Insert(tc, oLeaveYear); } else { LeaveYearDA.Update(tc, oLeaveYear); } tc.End(); return oLeaveYear.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Insert Leave Year: " + e.Message, e); #endregion } } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); LeaveYearDA.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 Year: " + e.Message, e); #endregion } } public int CountCurrentYear() { int res = 0; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); res = LeaveYearDA.CountCurrentYear(tc); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Leave Process Information: " + e.Message, e); #endregion } return res; } #endregion } #endregion }