using System; using System.Data; using System.Linq; using Ease.Core.Model; using Ease.Core.DataAccess; using HRM.BO; using System.Collections.Generic; using Ease.Core.Utility; using Payroll.Service; namespace HRM.DA { #region JVSetup Service public class JVSetupService : ServiceTemplate, IJVSetupService { #region Private functions and declaration public JVSetupService() { } private void MapObject(JVSetup oJVSetup, DataReader oReader) { base.SetObjectID(oJVSetup, oReader.GetInt32("JVSetupID").Value); oJVSetup.Description = oReader.GetString("Description"); oJVSetup.GLCode = oReader.GetString("GLCode"); oJVSetup.IsConfirmed = oReader.GetBoolean("IsConfirmed").Value; oJVSetup.IsEmployeeDetail = oReader.GetBoolean("IsEmployeeDetail").Value; oJVSetup.JVTypeID = oReader.GetString("JVTypeID") == null ? 0 : oReader.GetInt32("JVTypeID").Value; oJVSetup.PayrollTypeID = oReader.GetString("PayrollTypeID") == null ? 0 : oReader.GetInt32("PayrollTypeID").Value; oJVSetup.MonthDate = oReader.GetDateTime("MonthDate").Value; oJVSetup.SequenceNo = oReader.GetInt32("SequenceNo").Value; oJVSetup.ShowCC = oReader.GetBoolean("ShowCC").Value; oJVSetup.BookToCC = oReader.GetBoolean("BookToCC").HasValue ? oReader.GetBoolean("BookToCC").Value : false; oJVSetup.BookToPC = oReader.GetBoolean("BookToPC").HasValue ? oReader.GetBoolean("BookToPC").Value : false; oJVSetup.EMPLOYEETYPE = (oReader.GetInt32("EMPLOYEETYPE").HasValue && oReader.GetInt32("EMPLOYEETYPE") != 0) ? (EnumJVEmpType)oReader.GetInt32("EMPLOYEETYPE") : EnumJVEmpType.RegardLess; oJVSetup.Side = oReader.GetInt32("Side").Value; oJVSetup.SideCode = oReader.GetString("SideCode"); oJVSetup.GroupCode = oReader.GetString("GroupCode"); this.SetObjectState(oJVSetup, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { JVSetup oJVSetup = new JVSetup(); MapObject(oJVSetup, oReader); return oJVSetup as T; } protected JVSetup CreateObject(DataReader oReader) { JVSetup oJVSetup = new JVSetup(); MapObject(oJVSetup, oReader); return oJVSetup; } #endregion #region Service implementation public JVSetup Get(int id) { JVSetup oJVSetup = new JVSetup(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(JVSetupDA.Get(tc, id)); if (oreader.Read()) { oJVSetup = 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 oJVSetup; } public List Getbypayrolltype(int payrolltypeid) { List oJVSetups = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(JVSetupDA.GetbyPayrollTypeid(tc, payrolltypeid)); oJVSetups = 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 oJVSetups; } public int GetSequenceNo() { int nSeq = 0; TransactionContext tc = null; try { tc = TransactionContext.Begin(); nSeq = JVSetupDA.GetSequenceNo(tc); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return nSeq; } public DateTime GetMaxDate(DateTime dGivenDate, int payrolltypeid) { DateTime dMaxDate = DateTime.Now; TransactionContext tc = null; try { tc = TransactionContext.Begin(); dMaxDate = JVSetupDA.GetMaxDate(tc, dGivenDate, payrolltypeid); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return dMaxDate; } public List GetByJVType(int jvSetupID) { List jvsetups = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(JVSetupDA.GetByJVType(tc, jvSetupID)); jvsetups = 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 jvsetups; } public List GetLatestSetupByJVType(int payrolltypeid, int jvtypeid ) { List jvsetups = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(JVSetupDA.GetLatestSetupByJVType(tc, payrolltypeid, jvtypeid)); jvsetups = this.CreateObjects(dr); dr.Close(); tc.End(); if (jvsetups.Count >0) { List items = new JVSetupDetailService().GetLatestSetupByJVType(payrolltypeid, jvtypeid); jvsetups.ForEach(x => { x.JVSetupDetails = items.FindAll(y => y.JVSetupID == x.ID); }); List ccitems = new JVSetupCCService().GetLatestSetupByJVType(payrolltypeid, jvtypeid); jvsetups.ForEach(x => { x.JVSetupCC = ccitems.FindAll(x => x.JVSetupID == x.ID); }); List gradeitems = new JVSetupGradeService().GetLatestSetupByJVType(payrolltypeid, jvtypeid); jvsetups.ForEach(x => { x.JVSetupGrades = gradeitems.FindAll(x => x.JVSetupID == x.ID); }); } } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return jvsetups; } public List GetByJVTypeAndEffectDate(int jvSetupID, DateTime dEffectDate) { List jvsetups = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(JVSetupDA.GetByJVTypeAndEffectDate(tc, jvSetupID, dEffectDate)); jvsetups = 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 jvsetups; } public List GetByMonthDate(DateTime dtMonthDate, int payrolltypeid) { List jvsetups = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(JVSetupDA.GetByMonthDate(tc, dtMonthDate, payrolltypeid)); jvsetups = 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 jvsetups; } public List GetChildByParentID(int JVSetupID) { List jvsetupdetails = new List(); try { JVSetupDetailService setupDetailService = new JVSetupDetailService(); jvsetupdetails = setupDetailService.GetByJVSetup(JVSetupID); } catch (Exception e) { #region Handle Exception ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return jvsetupdetails; } public List GetByJVTypeAndMonth(int jvTypeID, DateTime dDate) { List jvsetups = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(JVSetupDA.GetByJVTypeAndMonth(tc, jvTypeID, dDate)); jvsetups = 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 jvsetups; } public int Save(JVSetup oJVSetup) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oJVSetup.IsNew) { oJVSetup.CreatedDate = DateTime.Now; oJVSetup.CreatedBy = oJVSetup.CreatedBy; int id = tc.GenerateID("JVSetup", "JVSetupID"); base.SetObjectID(oJVSetup, id); JVSetupDA.Insert(tc, oJVSetup); foreach (JVSetupDetail jvSetupDetail in oJVSetup.JVSetupDetails) { jvSetupDetail.CreatedDate = DateTime.Now; jvSetupDetail.CreatedBy = oJVSetup.CreatedBy; jvSetupDetail.JVSetupID = oJVSetup.ID; int detailID = tc.GenerateID("JVSetupDetail", "JVSetupDetailID"); base.SetObjectID(jvSetupDetail, detailID); JVSetupDetailDA.Insert(tc, jvSetupDetail); } foreach (JVSetupCC jvSetupCC in oJVSetup.JVSetupCC) { jvSetupCC.CreatedDate = DateTime.Now; jvSetupCC.CreatedBy = jvSetupCC.CreatedBy; jvSetupCC.JVSetupID = oJVSetup.ID; int setCCID = tc.GenerateID("JVSetupCC", "JVSetupCCID"); base.SetObjectID(jvSetupCC, setCCID); JVSetupCCDA.Insert(tc, jvSetupCC); } foreach (JVSetupGrade jvSetupGrade in oJVSetup.JVSetupGrades) { jvSetupGrade.CreatedDate = DateTime.Now; jvSetupGrade.CreatedBy = jvSetupGrade.CreatedBy; jvSetupGrade.JVSetupID = oJVSetup.ID; int setGradeID = tc.GenerateID("JVSetupGrade", "JVSetupGradeID"); base.SetObjectID(jvSetupGrade, setGradeID); JVSetupGradeDA.Insert(tc, jvSetupGrade); } } else { JVSetupDA.Update(tc, oJVSetup); JVSetupDA.DeleteDetail(tc, oJVSetup.ID); JVSetupDA.DeleteCC(tc, oJVSetup.ID); JVSetupDA.DeleteGrades(tc, oJVSetup.ID); foreach (JVSetupDetail jvSetupDetail in oJVSetup.JVSetupDetails) { jvSetupDetail.ModifiedDate = DateTime.Now; jvSetupDetail.CreatedBy = jvSetupDetail.CreatedBy; jvSetupDetail.JVSetupID = oJVSetup.ID; int detailID = tc.GenerateID("JVSetupDetail", "JVSetupDetailID"); base.SetObjectID(jvSetupDetail, detailID); JVSetupDetailDA.Insert(tc, jvSetupDetail); } foreach (JVSetupCC jvSetupCC in oJVSetup.JVSetupCC) { jvSetupCC.CreatedDate = DateTime.Now; jvSetupCC.CreatedBy = jvSetupCC.CreatedBy; jvSetupCC.JVSetupID = oJVSetup.ID; int SetCCID = tc.GenerateID("JVSetupCC", "JVSetupCCID"); base.SetObjectID(jvSetupCC, SetCCID); JVSetupCCDA.Insert(tc, jvSetupCC); } foreach (JVSetupGrade jvSetupGrade in oJVSetup.JVSetupGrades) { jvSetupGrade.CreatedDate = DateTime.Now; jvSetupGrade.CreatedBy = jvSetupGrade.CreatedBy; jvSetupGrade.JVSetupID = oJVSetup.ID; int setGradeID = tc.GenerateID("JVSetupGrade", "JVSetupGradeID"); base.SetObjectID(jvSetupGrade, setGradeID); JVSetupGradeDA.Insert(tc, jvSetupGrade); } } tc.End(); return oJVSetup.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Insert JVSetup", e); #endregion } } public void UpdateSequence(List oJVSetups) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (JVSetup jvSetup in oJVSetups) { jvSetup.ModifiedBy = jvSetup.CreatedBy; jvSetup.ModifiedDate = DateTime.Now; JVSetupDA.UpdateSequence(tc, jvSetup); } tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Update Sequence No.", e); #endregion } } public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); JVSetupDA.DeleteGrades(tc, id); JVSetupDetailDA.DeleteByJVSetup(tc, id); JVSetupDA.DeleteCC(tc, id); JVSetupDA.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 } } public List Get() { throw new NotImplementedException(); } public DateTime GetMaxDate(DateTime dGivenDate) { throw new NotImplementedException(); } public List GetByMonthDate(DateTime dtMonthDate) { throw new NotImplementedException(); } #endregion } #endregion }