using Ease.Core.DataAccess; using Ease.Core.Model; using Ease.Core.Utility; using System; using System.Collections.Generic; using System.Data; using System.Linq; using HRM.BO; namespace HRM.DA { #region GL Service public class GLService : ServiceTemplate, IGLService { #region Private functions and declaration public GLService() { } private void MapObject(GL oGL, DataReader oReader) { base.SetObjectID(oGL, oReader.GetInt32("GLID").Value); oGL.GLCode = oReader.GetString("GLCODE"); oGL.GLDescription = oReader.GetString("GLDESCRIPTION"); oGL.GLType = (EnumGLType)oReader.GetInt32("GLTYPE"); oGL.GLHeadType = (EnumGLHeadType)oReader.GetInt32("GLHEADTYPE"); oGL.OwnerID = oReader.GetInt32("OWNERID"); oGL.TierNo = oReader.GetInt32("TIERNO").Value; oGL.PositionNo = oReader.GetInt32("POSITIONNO").Value; oGL.LegacyCode = oReader.GetString("LEGACYCODE"); oGL.FundTypeID = oReader.GetInt32("FUNDTYPEID").Value; oGL.UserObjectID = oReader.GetInt32("USEROBJECTID"); oGL.UserObjectID = oReader.GetInt32("UserObjectID"); this.SetObjectState(oGL, Ease.Core.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { GL oGL = new GL(); MapObject(oGL, oReader); return oGL as T; } protected GL CreateObject(DataReader oReader) { GL oGL = new GL(); MapObject(oGL, oReader); return oGL; } #endregion #region Service implementation public GL Get(int id) { GL oGL = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(GLDA.Get(tc, id)); if (oreader.Read()) { oGL = 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 oGL; } public GL Get(string code) { GL oGL = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(GLDA.Get(tc, code)); if (oreader.Read()) { oGL = 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 oGL; } public GL Get(TransactionContext tc, int id) { GL oGL = null; try { DataReader oreader = new DataReader(GLDA.Get(tc, id)); if (oreader.Read()) { oGL = this.CreateObject(oreader); } oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oGL; } public double GetBalancePL(int nGLID, DateTime fromDate, DateTime toDate) { double nDebitBalance = 0, nCreditBalnace = 0, nCurrentBalance = 0; GL gl = new GL(); GLService gLService = new GLService(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); // gl = gLService.Get(tc, nGLID); nCurrentBalance = 0; string childKeys = string.Empty; //childKeys = getBottomMostChildKeys(tc, gl); nDebitBalance = GLTranDA.GetTranSideBalanceGLProfitAndLoss(tc, nGLID.ToString(), EnumTranSide.Debit, fromDate, toDate); nCreditBalnace = GLTranDA.GetTranSideBalanceGLProfitAndLoss(tc, nGLID.ToString(), EnumTranSide.Credit, fromDate, toDate); if (gl.GLType == EnumGLType.Asset || gl.GLType == EnumGLType.Expenditure) { nCurrentBalance = nCurrentBalance - (nDebitBalance - nCreditBalnace); } else { nCurrentBalance = nCurrentBalance - (nCreditBalnace - nDebitBalance); } tc.End(); } catch (Exception e) { throw new ServiceException("Failed to Get Balance ALL", e); } return nCurrentBalance; } public DataTable GetCheckList(DateTime fromVoucherDate, DateTime toVoucherDate, int voucherType, int fundTypeID) { DataSet ds; TransactionContext tc = null; try { tc = TransactionContext.Begin(true); ds = GLDA.GetCheckList(tc, fromVoucherDate, toVoucherDate, voucherType, fundTypeID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); throw new ServiceException(e.Message, e); #endregion } return ds.Tables[0]; } public GL Get(string code, int fundtypeid) { GL oGL = null; TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(GLDA.Get(tc, code)); if (oreader.Read()) { oGL = this.CreateObject(oreader); } oreader.Close(); if(oGL !=null) oGL.Children = this.GetByOwnerID(tc,oGL.ID); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } return oGL; } public List GetGLByFundTypeID(int fundTypeID) { List oGLs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(GLDA.GetByFundTypeID(tc, fundTypeID)); oGLs = 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 oGLs; } public List GetByTier(int tierNo, int fundTypeID) { List oGLs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(GLDA.GetByTierNo(tc, tierNo, fundTypeID)); oGLs = 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 oGLs; } //public List GetByTier(int tierNo) //{ // List oGLs = new List(); // TransactionContext tc = null; // try // { // tc = TransactionContext.Begin(); // DataReader dr = new DataReader(GLDA.GetByTierNo(tc, tierNo)); // oGLs = 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 oGLs; //} public List Get(EnumGLType type, int tierNo, int fundtypeid ) { List oGLs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(GLDA.Get(tc, type, tierNo, fundtypeid)); oGLs = 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 oGLs; } public List GetbyFundType(int FundTypeid) { List oGLs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(GLDA.GetByFundTypeID(tc, FundTypeid)); oGLs = 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 oGLs; } public List GetRoot() { List oGLs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(GLDA.GetRoot(tc)); oGLs = 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 oGLs; } public List GetGLByUserObjectID(int nGLID, int FundTypeID) { List oGLs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(GLDA.GetGLByUserObjectID(tc, nGLID, FundTypeID)); oGLs = 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 oGLs; } public int Save(GL oGL) { TransactionContext tc = null; try { int id = 0; tc = TransactionContext.Begin(true); if (oGL.IsNew) { id = tc.GenerateID("FM_GL", "GLID"); base.SetObjectID(oGL, id); GLDA.Insert(tc, oGL); } else { GLDA.Update(tc, oGL); } tc.End(); return id; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #region Private Functions private void UpdatePositionNo(TransactionContext tc, int glID, int positionNo) { GLDA.UpdatePositionNo(tc, glID, positionNo); } #endregion public void Delete(int id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); GLDA.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 " + e.Message); #endregion } } public List GetByTierNo(int level, int fundTypID) { List oGLs = new List(); TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(GLDA.GetByTierNoFundType(tc, level, fundTypID)); oGLs = 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 oGLs; } private string getGLIDs(int level, int fundTypeID) { string ids = string.Empty; List gls = GetByTierNo(level, fundTypeID); foreach (GL gl in gls) { ids = ids + "," + gl.ID.ToString(); } ids = ids.Substring(1); return ids; } public DataTable GetGLs(int uptoLevel, int fundTypeID) { string ids = string.Empty; string sqlStr = string.Empty; ids = getGLIDs(uptoLevel, fundTypeID); if (uptoLevel == 2) // 2 use instand of SystemConfig.GroupLevel { sqlStr = "SELECT GLID, TierNo, GLType, GLCode, GLDescription FROM FM_GL WHERE GLID IN(" + ids + ") ORDER BY GLCode"; } if (uptoLevel == 3) // 3 use instead of SystemConfig.SubGroupLevel { sqlStr = "SELECT GLID, TierNo, GLType, GLCode, GLDescription FROM FM_GL WHERE GLID IN(" + ids + ") " + "OR GLID IN(SELECT DISTINCT(OwnerID) FROM FM_GL WHERE GLID IN(" + ids + ")) ORDER BY GLCode"; } //if (uptoLevel == SystemConfig.SubSubGroupLevel) //{ // sqlStr = "SELECT GLID, TierNo, GLType, GLCode, GLDescription FROM GL WHERE GLID IN(" + ids + ") " + // "OR GLID IN(SELECT DISTINCT(OwnerID) FROM GL WHERE GLID IN(" + ids + ")) " + // "OR GLID IN(SELECT DISTINCT(OwnerID) FROM GL WHERE GLID IN(SELECT DISTINCT(OwnerID) FROM GL WHERE GLID IN(" + ids + "))) ORDER BY GLCode"; //} if (uptoLevel == 4) // 4 use instead of SystemConfig.PostingLevel { //sqlStr = "SELECT GLID, TierNo, GLType, GLCode, GLDescription FROM FM_GL where (GLID IN(" + ids + ") AND (GLID IN(SELECT DISTINCT(GLID) FROM FM_GLTranDetail) OR GLID IN(SELECT DISTINCT(GLID) FROM FM_GLTranDetail))) " + // "OR GLID IN(SELECT DISTINCT(OwnerID) FROM FM_GL WHERE GLID IN(" + ids + ")) " + // "OR GLID IN(SELECT DISTINCT(OwnerID) FROM FM_GL WHERE GLID IN(SELECT DISTINCT(OwnerID) FROM FM_GL WHERE GLID IN(" + ids + "))) " // //+"OR GLID IN(SELECT DISTINCT(OwnerID) FROM GL WHERE GLID IN(SELECT DISTINCT(OwnerID) FROM GL WHERE GLID IN(SELECT DISTINCT(OwnerID) FROM GL WHERE GLID IN(" + ids + "))))" // + " ORDER BY GLCode"; sqlStr = "SELECT GLID, TierNo, GLType, GLCode, GLDescription FROM FM_GL where FundTypeID = " + fundTypeID + " And TierNo = " + uptoLevel + " ORDER BY GLCode"; } DataSet ds = new DataSet(); TransactionContext tc = null; try { tc = TransactionContext.Begin(true); ds = GLDA.GetGLs(tc,sqlStr); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Data", e); #endregion } return ds.Tables[0]; } internal List GetByOwnerID(TransactionContext tc, int nOwnerID) { List oGLs = new List(); try { DataReader oReader =new DataReader(GLDA.GetByOwnerID(tc, nOwnerID)); oGLs = this.CreateObjects(oReader); oReader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get GL", e); #endregion } return oGLs; } public List GetByOwnerID(int nOwnerID) { TransactionContext tc = null; List oGLs = new List(); try { tc = TransactionContext.Begin(); DataReader oReader = new DataReader(GLDA.GetByOwnerID(tc, nOwnerID)); oGLs = 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 GL", e); #endregion } return oGLs; } private string getBottomMostChildKeys(TransactionContext tc, GL gl) { string bmcks = string.Empty; List children = new GLService().GetByOwnerID(tc, gl.ID); if (children.Count == 0) return gl.ID.ToString(); else { foreach (GL c in children) { bmcks += "," + getBottomMostChildKeys(tc, c); } bmcks = bmcks.Substring(1); return bmcks; } } public decimal GetTranSideBalanceExceptYearEnd(int nGLID, EnumTranSide tranSide, DateTime fromTranDate, DateTime toTranDate) { decimal Balance = 0; GL gl = new GL(); GLService gLService = new GLService(); TransactionContext tc = null; try { // gl = gLService.Get(nGLID); tc = TransactionContext.Begin(true); string childKeys = string.Empty; //// childKeys = getBottomMostChildKeys(tc, gl); Balance = GLTranDA.GetTranSideBalanceExceptYearEnd(tc, nGLID.ToString(), tranSide, fromTranDate, toTranDate); tc.End(); } catch (Exception e) { throw new ServiceException("Failed to Get Balance :", e); } return Balance; } public string GetMaxGLCodeByParent(int id) { TransactionContext tc = null; string MaxGLCode = ""; try { tc = TransactionContext.Begin(); MaxGLCode = GLDA.GetMaxGLCodeByParent(tc, id); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException("Failed to Get Max GL Code", e); #endregion } return MaxGLCode; } #endregion } #endregion }