using System;
using System.Data;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core;
using System.Collections.Generic;
using Ease.Core.Utility;
using HRM.BO;
using HRM.DA;


namespace HRM.DA.Fund
{
    [Serializable]
    public class GFSettlementService : ServiceTemplate
    {
        public GFSettlementService()
        {
        }

        #region Private methods

        private void MapObject(GFSettlement oGFSettlement, DataReader oReader)
        {
            base.SetObjectID(oGFSettlement, oReader.GetInt32("GFSettlementID").Value);
            oGFSettlement.MemberID = oReader.GetInt32("MemberID").Value;
            oGFSettlement.GLTranID = oReader.GetInt32("GLTranID").Value;
            oGFSettlement.GLTranInterestID = oReader.GetInt32("GLTranInterestID").Value;
            oGFSettlement.SettlementDate = oReader.GetDateTime("SettlementDate").Value;
            oGFSettlement.GFAmount = oReader.GetDecimal("GFAmount").Value;
            oGFSettlement.GFInterest = oReader.GetDecimal("GFInterest").Value;
            oGFSettlement.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oGFSettlement.CreatedBy = oReader.GetInt32("CreatedBy").Value;
            oGFSettlement.ModifiedDate = oReader.GetDateTime("ModifiedDate") != null
                ? oReader.GetDateTime("ModifiedDate").Value
                : DateTime.MinValue; //oReader.GetDateTime("ModifiedDate");
            oGFSettlement.ModifiedBy = oReader.GetInt32("ModifiedBy").Value != null
                ? oReader.GetInt32("ModifiedBy").Value
                : (0); // oReader.GetInt32("ModifiedBy");
            base.SetObjectState(oGFSettlement, ObjectState.Saved);
        }

        private GFSettlement CreateObject(DataReader oReader)
        {
            GFSettlement oGFSettlement = new GFSettlement();
            MapObject(oGFSettlement, oReader);
            return oGFSettlement;
        }

        private List<GFSettlement> CreateObjects(IDataReader oReader)
        {
            List<GFSettlement> oGFSettlements = new List<GFSettlement>();
            DataReader oreader = new DataReader(oReader);
            while (oReader.Read())
            {
                GFSettlement oItem = CreateObject(oreader);
                oGFSettlements.Add(oItem);
            }

            return oGFSettlements;
        }

        #endregion Private methods


        #region Service implementation

        protected override T CreateObject<T>(DataReader dataReader)
        {
            GFSettlement oGFSettlement = new GFSettlement();

            MapObject(oGFSettlement, dataReader);

            return oGFSettlement as T;
        }

        public GFSettlement Get(int id)
        {
            GFSettlement oGFSettlement = new GFSettlement();

            #region Cache Header

            oGFSettlement = new GFSettlement();
            if (oGFSettlement != null)
                return oGFSettlement;

            #endregion

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(GFSettlementDA.Get(tc, id));
                if (dr.Read())
                {
                    oGFSettlement = this.CreateObject<GFSettlement>(dr);
                }

                dr.Close();
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to GetFinalSettlement", e);

                #endregion
            }

            #region Cache Footer

            #endregion

            return oGFSettlement;
        }

        public List<GFSettlement> Get()
        {
            List<GFSettlement> oGFSettlements = new List<GFSettlement>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dataReader = new DataReader(GFSettlementDA.Get(tc));
                while (dataReader.Read())
                {
                    GFSettlement item = new GFSettlement();
                    item = this.CreateObject<GFSettlement>(dataReader);
                    oGFSettlements.Add(item);
                }

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to GetFinalSettlement", e);

                #endregion
            }

            #region Cache Footer

            #endregion

            return oGFSettlements;
        }

        public List<GFSettlement> Get(string sSql)
        {
            List<GFSettlement> oGFSettlements = new List<GFSettlement>();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dataReader = new DataReader(GFSettlementDA.Get(tc, sSql));
                while (dataReader.Read())
                {
                    GFSettlement item = this.CreateObject<GFSettlement>(dataReader);
                    oGFSettlements.Add(item);
                }

                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException("Failed to GetFinalSettlement", e);

                #endregion
            }

            return oGFSettlements;
        }

        //public void Insert(GFSettlement oGFSettlement, GLTran glTran, List<MembersTransactionDetails> memberTrans, List<MemberBalaneMonthly> membersMonthlyBalances)
        //{
        //    TransactionContext tc = null;
        //    Ease.Core.DataAccess.TransactionContext tc1 = null;
        //    try
        //    {
        //        tc = TransactionContext.Begin(true);
        //        tc1 = Ease.Core.DataAccess.TransactionContext.Begin(true);
        //        EaseFAS.Service.GLTranService glTranService = new EaseFAS.Service.GLTranService();
        //        MembersTransactionDetailsService meberTranDetailService = new MembersTransactionDetailsService();
        //        Ease.Core.Framework.ID glTranID = null;
        //        glTranID = glTranService.Insert(glTran, tc1);
        //        if (oGFSettlement.IsNew)
        //        {
        //            oGFSettlement.CreatedBy = User.CurrentUser.ID;
        //            oGFSettlement.CreatedDate = GlobalFunctionDA.ServerDate(tc);

        //            int id = tc.GenerateID("GFSettlement", "GFSettlementID");
        //            base.SetObjectID(oGFSettlement, (id));
        //            oGFSettlement.GLTranID = Ease.CoreV35.Model.(glTranID);
        //            GFSettlementDA.Insert(tc, oGFSettlement);
        //        }
        //        else if (oGFSettlement.IsModified)
        //        {
        //            oGFSettlement.ModifiedBy = User.CurrentUser.ID;
        //            oGFSettlement.ModifiedDate = new PayrollGlobalFunctionservice().GetServerDate().Date;
        //            GFSettlementDA.Update(tc, oGFSettlement);
        //        }
        //        meberTranDetailService.UploadOpeningTranForGF(tc, memberTrans, membersMonthlyBalances);
        //        tc.End();
        //        tc1.End();
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception
        //        if (tc != null)
        //            tc.HandleError();

        //        if (tc1 != null)
        //            tc1.HandleError();
        //        ExceptionLog.Write(e);
        //        throw new ServiceException("Failed to Insert GF settlement: " + e.Message);
        //        #endregion
        //    }
        //}

        //public void Insert(GFSettlement oGFSettlement, GLTran glTran, GLTran glTranInterest, List<MembersTransactionDetails> memberTrans, List<MemberBalaneMonthly> membersMonthlyBalances)
        //{
        //    TransactionContext tc = null;
        //    Ease.Core.DataAccess.TransactionContext tc1 = null;
        //    try
        //    {
        //        tc = TransactionContext.Begin(true);
        //        tc1 = Ease.Core.DataAccess.TransactionContext.Begin(true);
        //        EaseFAS.Service.GLTranService glTranService = new EaseFAS.Service.GLTranService();
        //        MembersTransactionDetailsService meberTranDetailService = new MembersTransactionDetailsService();
        //        Ease.Core.Framework.ID glTranID = null;
        //        Ease.Core.Framework.ID glTranInterestID = null;
        //        glTranID = glTranService.Insert(glTran, tc1);
        //        if (glTranInterest != null && glTranInterest.DebitAmount > 0)
        //            glTranInterestID = glTranService.Insert(glTranInterest, tc1);
        //        else
        //            glTranInterestID = Ease.Core.Framework.(0);
        //        if (oGFSettlement.IsNew)
        //        {
        //            oGFSettlement.CreatedBy = User.CurrentUser.ID;
        //            oGFSettlement.CreatedDate = GlobalFunctionDA.ServerDate(tc);

        //            int id = tc.GenerateID("GFSettlement", "GFSettlementID");
        //            base.SetObjectID(oGFSettlement, (id));
        //            oGFSettlement.GLTranID = Ease.CoreV35.Model.(glTranID);
        //            oGFSettlement.GLTranInterestID = Ease.CoreV35.Model.(glTranInterestID);
        //            GFSettlementDA.Insert(tc, oGFSettlement);
        //        }
        //        else if (oGFSettlement.IsModified)
        //        {
        //            oGFSettlement.ModifiedBy = User.CurrentUser.ID;
        //            oGFSettlement.ModifiedDate = new PayrollGlobalFunctionservice().GetServerDate().Date;
        //            GFSettlementDA.Update(tc, oGFSettlement);
        //        }
        //        meberTranDetailService.UploadOpeningTranForGF(tc, memberTrans, membersMonthlyBalances);
        //        tc.End();
        //        tc1.End();
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception
        //        if (tc != null)
        //            tc.HandleError();

        //        if (tc1 != null)
        //            tc1.HandleError();
        //        ExceptionLog.Write(e);
        //        throw new ServiceException("Failed to Insert GF settlement: " + e.Message);
        //        #endregion
        //    }
        //}

        //public void Insert(GFSettlement oGFSettlement)
        //{
        //    TransactionContext tc = null;
        //    try
        //    {
        //        tc = TransactionContext.Begin(true);
        //        if (oGFSettlement.IsNew)
        //        {
        //            oGFSettlement.CreatedBy = User.CurrentUser.ID;
        //            oGFSettlement.CreatedDate = GlobalFunctionDA.ServerDate(tc);

        //            int id = tc.GenerateID("GFSettlement", "GFSettlementID");
        //            base.SetObjectID(oGFSettlement, (id));
        //            GFSettlementDA.Insert(tc, oGFSettlement);
        //        }
        //        else if (oGFSettlement.IsModified)
        //        {
        //            oGFSettlement.ModifiedBy = User.CurrentUser.ID;
        //            oGFSettlement.ModifiedDate = new PayrollGlobalFunctionservice().GetServerDate().Date;
        //            GFSettlementDA.Update(tc, oGFSettlement);
        //        }
        //        tc.End();
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception
        //        if (tc != null)
        //            tc.HandleError();
        //        ExceptionLog.Write(e);
        //        throw new ServiceException("Failed to Insert GF settlement: " + e.Message);
        //        #endregion
        //    }
        //}

        //public void DeleteSettlement(GFSettlement gfSettlement)
        //{
        //    int projectID = FM.BO.Common.User.CurrentMasterParam.ID;
        //    TransactionContext tc = null;
        //    decimal totalAmount = gfSettlement.GFAmount + gfSettlement.GFInterest;
        //    Ease.Core.DataAccess.TransactionContext tc1 = null;
        //    try
        //    {
        //        tc = TransactionContext.Begin(true);
        //        tc1 = Ease.Core.DataAccess.TransactionContext.Begin(true);
        //        EaseFAS.Service.GLTranService glTranService = new EaseFAS.Service.GLTranService();
        //        glTranService.Delete(tc1, Ease.Core.Framework.(gfSettlement.GLTranID));
        //        MembersTransactionDetailsDA.DeleteGFMemberSettlement(tc, gfSettlement.MemberID, gfSettlement.SettlementDate.Date, gfSettlement.GFAmount, projectID);
        //        MembersTransactionDetailsDA.DeleteGFMemberSettlement(tc, gfSettlement.MemberID, gfSettlement.SettlementDate.Date, gfSettlement.GFInterest, projectID);
        //        MemberBalaneMonthlyDA.DeleteGFSettlementData(tc, gfSettlement.MemberID, gfSettlement.SettlementDate.Date, projectID, totalAmount);
        //        GFSettlementDA.Delete(tc, gfSettlement.ID);
        //        tc.End();
        //        tc1.End();
        //    }
        //    catch (Exception e)
        //    {
        //        #region Handle Exception
        //        if (tc != null)
        //            tc.HandleError();
        //        if (tc1 != null)
        //            tc1.HandleError();
        //        ExceptionLog.Write(e);
        //        throw new ServiceException("Failed to Delete GFSettlement" + e.Message);
        //        #endregion
        //    }
        //}

        #endregion Service implementation
    }
}