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 WPPFSettlementService : ServiceTemplate
    {
        public WPPFSettlementService()
        {
        }

        #region Private methods

        private void MapObject(WPPFSettlement oWPPFSettlement, DataReader oReader)
        {
            base.SetObjectID(oWPPFSettlement, oReader.GetInt32("WPPFSettlementID").Value);
            oWPPFSettlement.MemberID = oReader.GetInt32("MemberID").Value;
            oWPPFSettlement.GLTranID = oReader.GetInt32("GLTranID").Value;
            oWPPFSettlement.GLTranInterestID = oReader.GetInt32("GLTranInterestID").Value;
            oWPPFSettlement.SettlementDate = oReader.GetDateTime("SettlementDate").Value;
            oWPPFSettlement.WPPFAmount = oReader.GetDecimal("WPPFAmount").Value;
            oWPPFSettlement.WPPFInterest = oReader.GetDecimal("WPPFInterest").Value;
            oWPPFSettlement.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oWPPFSettlement.CreatedBy = oReader.GetInt32("CreatedBy").Value;
            oWPPFSettlement.ModifiedDate = oReader.GetDateTime("ModifiedDate") != null
                ? oReader.GetDateTime("ModifiedDate").Value
                : DateTime.MinValue; //oReader.GetDateTime("ModifiedDate");
            oWPPFSettlement.ModifiedBy = oReader.GetInt32("ModifiedBy").Value != null
                ? oReader.GetInt32("ModifiedBy").Value
                : (0); // oReader.GetInt32("ModifiedBy");
            base.SetObjectState(oWPPFSettlement, ObjectState.Saved);
        }

        private WPPFSettlement CreateObject(DataReader oReader)
        {
            WPPFSettlement oWPPFSettlement = new WPPFSettlement();
            MapObject(oWPPFSettlement, oReader);
            return oWPPFSettlement;
        }

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

            return oWPPFSettlements;
        }

        #endregion Private methods


        #region Service implementation

        protected override T CreateObject<T>(DataReader dataReader)
        {
            WPPFSettlement oWPPFSettlement = new WPPFSettlement();

            MapObject(oWPPFSettlement, dataReader);

            return oWPPFSettlement as T;
        }

        public WPPFSettlement Get(int id)
        {
            WPPFSettlement oWPPFSettlement = new WPPFSettlement();

            #region Cache Header

            oWPPFSettlement = new WPPFSettlement();
            if (oWPPFSettlement != null)
                return oWPPFSettlement;

            #endregion

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(WPPFSettlementDA.Get(tc, id));
                if (dr.Read())
                {
                    oWPPFSettlement = this.CreateObject<WPPFSettlement>(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 oWPPFSettlement;
        }

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

                DataReader dataReader = new DataReader(WPPFSettlementDA.Get(tc));
                while (dataReader.Read())
                {
                    WPPFSettlement item = new WPPFSettlement();
                    item = this.CreateObject<WPPFSettlement>(dataReader);
                    oWPPFSettlements.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 oWPPFSettlements;
        }

        public List<WPPFSettlement> Get(string sSql)
        {
            List<WPPFSettlement> oWPPFSettlements = new List<WPPFSettlement>();

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

                DataReader dataReader = new DataReader(WPPFSettlementDA.Get(tc, sSql));
                while (dataReader.Read())
                {
                    WPPFSettlement item = this.CreateObject<WPPFSettlement>(dataReader);
                    oWPPFSettlements.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 oWPPFSettlements;
        }

        //public void Insert(WPPFSettlement oWPPFSettlement, 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 (oWPPFSettlement.IsNew)
        //        {
        //            oWPPFSettlement.CreatedBy = User.CurrentUser.ID;
        //            oWPPFSettlement.CreatedDate = GlobalFunctionDA.ServerDate(tc);

        //            int id = tc.GenerateID("WPPFSettlement", "WPPFSettlementID");
        //            base.SetObjectID(oWPPFSettlement, (id));
        //            oWPPFSettlement.GLTranID = Ease.CoreV35.Model.(glTranID);
        //            WPPFSettlementDA.Insert(tc, oWPPFSettlement);
        //        }
        //        else if (oWPPFSettlement.IsModified)
        //        {
        //            oWPPFSettlement.ModifiedBy = User.CurrentUser.ID;
        //            oWPPFSettlement.ModifiedDate = new PayrollGlobalFunctionservice().GetServerDate().Date;
        //            WPPFSettlementDA.Update(tc, oWPPFSettlement);
        //        }
        //        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(WPPFSettlement oWPPFSettlement, 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 (oWPPFSettlement.IsNew)
        //        {
        //            oWPPFSettlement.CreatedBy = User.CurrentUser.ID;
        //            oWPPFSettlement.CreatedDate = GlobalFunctionDA.ServerDate(tc);

        //            int id = tc.GenerateID("WPPFSettlement", "WPPFSettlementID");
        //            base.SetObjectID(oWPPFSettlement, (id));
        //            oWPPFSettlement.GLTranID = Ease.CoreV35.Model.(glTranID);
        //            oWPPFSettlement.GLTranInterestID = Ease.CoreV35.Model.(glTranInterestID);
        //            WPPFSettlementDA.Insert(tc, oWPPFSettlement);
        //        }
        //        else if (oWPPFSettlement.IsModified)
        //        {
        //            oWPPFSettlement.ModifiedBy = User.CurrentUser.ID;
        //            oWPPFSettlement.ModifiedDate = new PayrollGlobalFunctionservice().GetServerDate().Date;
        //            WPPFSettlementDA.Update(tc, oWPPFSettlement);
        //        }
        //        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 Save(WPPFSettlement oWPPFSettlement)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oWPPFSettlement.IsNew)
                {
                    oWPPFSettlement.CreatedBy = oWPPFSettlement.CreatedBy;
                    oWPPFSettlement.CreatedDate = DateTime.Now;

                    int id = tc.GenerateID("WPPFSettlement", "WPPFSettlementID");
                    base.SetObjectID(oWPPFSettlement, (id));
                    WPPFSettlementDA.Insert(tc, oWPPFSettlement);
                }
                else
                {
                    oWPPFSettlement.ModifiedBy = oWPPFSettlement.ModifiedBy;
                    oWPPFSettlement.ModifiedDate = DateTime.Now;
                    WPPFSettlementDA.Update(tc, oWPPFSettlement);
                }

                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(WPPFSettlement gfSettlement)
        //{
        //    int projectID = FM.BO.Common.User.CurrentMasterParam.ID;
        //    TransactionContext tc = null;
        //    decimal totalAmount = gfSettlement.WPPFAmount + gfSettlement.WPPFInterest;
        //    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.WPPFAmount, projectID);
        //        MembersTransactionDetailsDA.DeleteGFMemberSettlement(tc, gfSettlement.MemberID, gfSettlement.SettlementDate.Date, gfSettlement.WPPFInterest, projectID);
        //        MemberBalaneMonthlyDA.DeleteGFSettlementData(tc, gfSettlement.MemberID, gfSettlement.SettlementDate.Date, projectID, totalAmount);
        //        WPPFSettlementDA.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 WPPFSettlement" + e.Message);
        //        #endregion
        //    }
        //}

        #endregion Service implementation
    }
}