using System; using System.Data; using System.Linq; using Ease.CoreV35; using Ease.CoreV35.Model; using Ease.CoreV35.DataAccess; using System.Collections.Generic; using Payroll.BO; using Ease.CoreV35.Caching; namespace Payroll.Service { #region JVSetup Service [Serializable] public class JVSetupService : ServiceTemplate, IJVSetupService { #region Private functions and declaration Cache _cache = new Cache(typeof(JVSetup)); public JVSetupService() { } private void MapObject(JVSetup oJVSetup, DataReader oReader) { base.SetObjectID(oJVSetup, ID.FromInteger(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 ? null : ID.FromInteger(oReader.GetInt32("JVTypeID").Value); oJVSetup.PayrollTypeID = oReader.GetString("PayrollTypeID") == null ? null : ID.FromInteger(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"); oJVSetup.EmployeeID = oReader.GetString("EmployeeID") == null ? null : ID.FromInteger(oReader.GetInt32("EmployeeID").Value); this.SetObjectState(oJVSetup, Ease.CoreV35.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(ID id) { JVSetup oJVSetup = new JVSetup(); #region Cache Header oJVSetup = (JVSetup)_cache["Get", id]; if (oJVSetup != null) return oJVSetup; #endregion 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 } #region Cache Footer _cache.Add(oJVSetup, "Get", id); #endregion return oJVSetup; } public ObjectsTemplate Get() { #region Cache Header ObjectsTemplate oJVSetups = _cache["Get"] as ObjectsTemplate; if (oJVSetups != null) return oJVSetups; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(JVSetupDA.Get(tc)); 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 } #region Cache Footer _cache.Add(oJVSetups, "Get"); #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) { DateTime dMaxDate = DateTime.Now; TransactionContext tc = null; try { tc = TransactionContext.Begin(); dMaxDate = JVSetupDA.GetMaxDate(tc, dGivenDate); 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 ObjectsTemplate GetByJVType(ID jvSetupID) { #region Cache Header ObjectsTemplate jvsetups = _cache["GetByJVType", jvSetupID] as ObjectsTemplate; if (jvsetups != null) return jvsetups; #endregion 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 } #region Cache Footer _cache.Add(jvsetups, "GetByJVType", jvSetupID); #endregion return jvsetups; } public ObjectsTemplate GetByJVTypeAndEffectDate(ID jvSetupID, DateTime dEffectDate) { #region Cache Header ObjectsTemplate jvsetups = _cache["GetByJVTypeAndEffectDate", jvSetupID, dEffectDate] as ObjectsTemplate; if (jvsetups != null) return jvsetups; #endregion 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 } #region Cache Footer _cache.Add(jvsetups, "GetByJVTypeAndEffectDate", jvSetupID, dEffectDate); #endregion return jvsetups; } public ObjectsTemplate GetByMonthDate(DateTime dtMonthDate) { #region Cache Header ObjectsTemplate jvsetups = _cache["GetByMonthDate", dtMonthDate] as ObjectsTemplate; if (jvsetups != null) return jvsetups; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(JVSetupDA.GetByMonthDate(tc, dtMonthDate)); 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 } #region Cache Footer _cache.Add(jvsetups, "GetByMonthDate", dtMonthDate); #endregion return jvsetups; } public ObjectsTemplate GetChildByParentID(ID JVSetupID) { #region Cache Header ObjectsTemplate jvsetupdetails = _cache["GetChildByParentID", JVSetupID] as ObjectsTemplate; if (jvsetupdetails != null) return jvsetupdetails; #endregion 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 } #region Cache Footer _cache.Add(jvsetupdetails, "GetChildByParentID", JVSetupID); #endregion return jvsetupdetails; } public ObjectsTemplate GetByJVTypeAndMonth(ID jvTypeID, DateTime dDate) { #region Cache Header ObjectsTemplate jvsetups = _cache["GetByJVTypeAndMonth", jvTypeID, dDate] as ObjectsTemplate; if (jvsetups != null) return jvsetups; #endregion 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 } #region Cache Footer _cache.Add(jvsetups, "GetByJVTypeAndMonth", jvTypeID, dDate); #endregion return jvsetups; } public ID Save(JVSetup oJVSetup) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oJVSetup.IsNew) { oJVSetup.CreatedDate = DateTime.Now; oJVSetup.CreatedBy = User.CurrentUser.ID; int id = tc.GenerateID("JVSetup", "JVSetupID"); base.SetObjectID(oJVSetup, ID.FromInteger(id)); JVSetupDA.Insert(tc, oJVSetup); foreach (JVSetupDetail jvSetupDetail in oJVSetup.JVSetupDetails) { jvSetupDetail.CreatedDate = DateTime.Now; jvSetupDetail.CreatedBy = User.CurrentUser.ID; jvSetupDetail.JVSetupID = oJVSetup.ID; int detailID = tc.GenerateID("JVSetupDetail", "JVSetupDetailID"); base.SetObjectID(jvSetupDetail, ID.FromInteger(detailID)); JVSetupDetailDA.Insert(tc, jvSetupDetail); } foreach (JVSetupCC jvSetupCC in oJVSetup.JVSetupCC) { jvSetupCC.CreatedDate = DateTime.Now; jvSetupCC.CreatedBy = User.CurrentUser.ID; jvSetupCC.JVSetupID = oJVSetup.ID; int setCCID = tc.GenerateID("JVSetupCC", "JVSetupCCID"); base.SetObjectID(jvSetupCC, ID.FromInteger(setCCID)); JVSetupCCDA.Insert(tc, jvSetupCC); } } else { JVSetupDA.Update(tc, oJVSetup); JVSetupDA.DeleteDetail(tc, oJVSetup.ID); JVSetupDA.DeleteCC(tc, oJVSetup.ID); foreach (JVSetupDetail jvSetupDetail in oJVSetup.JVSetupDetails) { jvSetupDetail.ModifiedDate = DateTime.Now; jvSetupDetail.CreatedBy = User.CurrentUser.ID; jvSetupDetail.JVSetupID = oJVSetup.ID; int detailID = tc.GenerateID("JVSetupDetail", "JVSetupDetailID"); base.SetObjectID(jvSetupDetail, ID.FromInteger(detailID)); JVSetupDetailDA.Insert(tc, jvSetupDetail); } foreach (JVSetupCC jvSetupCC in oJVSetup.JVSetupCC) { jvSetupCC.CreatedDate = DateTime.Now; jvSetupCC.CreatedBy = User.CurrentUser.ID; jvSetupCC.JVSetupID = oJVSetup.ID; int SetCCID = tc.GenerateID("JVSetupCC", "JVSetupCCID"); base.SetObjectID(jvSetupCC, ID.FromInteger(SetCCID)); JVSetupCCDA.Insert(tc, jvSetupCC); } } tc.End(); return oJVSetup.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Save JVSetup", e); #endregion } } public void UpdateSequence(ObjectsTemplate oJVSetups) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); foreach (JVSetup jvSetup in oJVSetups) { jvSetup.ModifiedBy = User.CurrentUser.ID; 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(ID id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); 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 } } #endregion } #endregion }