using HRM.BO; using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; namespace HRM.DA { public class CostcenterService : ServiceTemplate, ICostcenterService { public CostcenterService() { } private void MapObject(Costcenter oCostcenter, DataReader oReader) { base.SetObjectID(oCostcenter, oReader.GetInt32("CrgID").Value); oCostcenter.Code = oReader.GetString("code"); oCostcenter.GrsName = oReader.GetString("GrsName"); oCostcenter.Name = oReader.GetString("description"); oCostcenter.NameInBangla = oReader.GetString("descriptioninbangla", true, null); oCostcenter.ProfitCenterName = oReader.GetString("ProfitCenterName"); oCostcenter.ParentID = oReader.GetInt32("parentID"); oCostcenter.PayrollTypeID = oReader.GetInt32("PayrollTypeID").Value; oCostcenter.ParentsID = oReader.GetString("parentsID"); oCostcenter.Tier = oReader.GetInt32("Tier").Value; oCostcenter.Sequence = oReader.GetInt32("SequenceNo").Value; oCostcenter.Status = (EnumStatus)oReader.GetInt32("Status").Value; oCostcenter.CreatedBy = oReader.GetInt32("CreatedBy", 0); oCostcenter.CreatedDate = oReader.GetDateTime("CreationDate").Value; oCostcenter.ModifiedBy = oReader.GetInt32("ModifiedBy", 0); oCostcenter.ModifiedDate = oReader.GetDateTime("ModifiedDate"); oCostcenter.CodeName = oCostcenter.Name + " [" + oCostcenter.Code + "]"; this.SetObjectState(oCostcenter, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Costcenter oCostcenter = new Costcenter(); MapObject(oCostcenter, oReader); return oCostcenter as T; } #region Service implementation public Costcenter Get(TransactionContext tc, int id) { Costcenter oCostcenter = null; try { DataReader oreader = new DataReader(CostcenterDA.Get(tc, id)); if (oreader.Read()) { oCostcenter = this.CreateObject(oreader); } oreader.Close(); } catch (Exception e) { #region Handle Exception ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oCostcenter; } public Costcenter Get(int id) { Costcenter oCostcenter = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(CostcenterDA.Get(tc, id)); if (oreader.Read()) { oCostcenter = 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 oCostcenter; } public Costcenter Get(string code, int payrollTypeID) { Costcenter oCostcenter = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(CostcenterDA.Get(tc, code, payrollTypeID)); if (oreader.Read()) { oCostcenter = 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 oCostcenter; } //public List Get(int payrollTypeID) //{ // List costcenters = new List(); // TransactionContext tc = null; // try // { // tc = TransactionContext.Begin(); // DataReader dr = new DataReader(CostcenterDA.Get(tc, payrollTypeID)); // costcenters = 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 costcenters; //} public List Get(EnumStatus status, int payrollTypeID) { List costcenters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.Get(tc, status, payrollTypeID)); costcenters = 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 costcenters; } public List Get() { List costcenters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.Get(tc)); costcenters = 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 costcenters; } public List GetParents(EnumStatus status, int payrollTypeID) { List costcenters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.GetParents(tc, status, payrollTypeID)); costcenters = 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 costcenters; } public List Get(EnumStatus status, int tier, int payrollTypeID) { List costcenters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.Get(tc, status, tier, payrollTypeID)); costcenters = 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 costcenters; } public List GetAllCostCenter(int payrollTypeID, EnumStatus status, string code, string name) { List designations = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.GetAllCostCenter(tc, payrollTypeID, status, code, name)); designations = 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 designations; } public List GetChilds(int parentID, int payrollTypeID) { List costcenters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.GetChilds(tc, parentID, payrollTypeID)); costcenters = 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 costcenters; } public List GetByTire(int nTire, int payrollTypeID) { List costcenters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.GetByTire(tc, nTire, payrollTypeID)); costcenters = 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 costcenters; } public List GetByTireAndProfitCenter(int nTire, string sProfitCenter, int payrollTypeID) { List costcenters = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CostcenterDA.GetByTireAndProfitCenter(tc, nTire, sProfitCenter, payrollTypeID)); costcenters = 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 costcenters; } public string GetNextCode(int tier, int parentid) { TransactionContext tc = null; string code = ""; string parentCode = ""; try { if (parentid != -1) { Costcenter parent = this.Get((int)parentid); if (parent != null) parentCode = parent.Code; } tc = TransactionContext.Begin(); code = GlobalFunctionService.GetMaxCodeofTree(tc, "department", "codeautogenerate", "Department", "Code", "DepartmentID", "PARENTID", parentCode, tier); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return code; } public int Save(Costcenter oCostcenter, int payrollTypeID) { TransactionContext tc = null; try { if (oCostcenter.Code == string.Empty) { oCostcenter.Code = this.GetNextCode(oCostcenter.Tier, oCostcenter.ParentID ==null? -1: (int)oCostcenter.ParentID); } oCostcenter.PayrollTypeID = payrollTypeID; tc = TransactionContext.Begin(true); if (oCostcenter.IsNew) { // base.SetObjectID(oCostcenter, ID.FromInteger(tc.GenerateID("CRG", "CRGID"))); int id = tc.GenerateID("CRG", "CRGID"); base.SetObjectID(oCostcenter, id); int sequenceId = tc.GenerateID("CRG", "SequenceNO"); oCostcenter.Sequence = sequenceId; CostcenterDA.Insert(tc, oCostcenter, payrollTypeID); } else { CostcenterDA.Update(tc, oCostcenter, payrollTypeID); } tc.End(); return oCostcenter.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public int Save(Costcenter oCostcenter) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oCostcenter.IsNew) { // base.SetObjectID(oCostcenter, ID.FromInteger(tc.GenerateID("CRG", "CRGID"))); int id = tc.GenerateID("CRG", "CRGID"); base.SetObjectID(oCostcenter, id); int sequenceId = tc.GenerateID("CRG", "SequenceNO"); oCostcenter.Sequence = sequenceId; CostcenterDA.Insert(tc, oCostcenter); } else { CostcenterDA.Update(tc, oCostcenter); } tc.End(); return oCostcenter.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public static void SaveForUpload(TransactionContext tc, List crgs, List saveItems) { try { string parentcode = ""; foreach (Costcenter item in crgs) { if (saveItems.FindIndex(x => x.ID == item.ID) < 0) { int seqNo = tc.GenerateID("crg", "SequenceNo"); item.Sequence = seqNo; parentcode = string.Empty; if (item.ParentID != null && item.ParentID != 0) parentcode = crgs.Find(o => o.ID == item.ParentID).Code; if (item.Code == string.Empty) { item.Code = GlobalFunctionService.GetMaxCodeofTree(tc, "costcenter", "codeautogenerate", "crg", "Code", "crgid", "PARENTID", parentcode, item.Tier); } item.CreatedDate = DateTime.Now; CostcenterDA.Insert(tc, item, item.PayrollTypeID); } else { //CostcenterDA.Update(tc, item); } } } 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); CostcenterDA.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 } }