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;

namespace HRM.DA
{
    #region FSTranDetail Service

    public class FSTranDetailService : ServiceTemplate, IFSTranDetailService
    {
        #region Private functions and declaratio

        #endregion

        public FSTranDetailService()
        {
        }

        private void MapObject(FSTranDetail oFSTranDetail, DataReader oReader)
        {
            base.SetObjectID(oFSTranDetail, (oReader.GetInt32("FSTranDetailID").Value));
            oFSTranDetail.Amount = oReader.GetDouble("ChangeTaxAmount").Value;
            oFSTranDetail.AmountType = (EnumValueType)oReader.GetInt32("AmountType").Value;
            oFSTranDetail.ChangedValue = oReader.GetDouble("ChangedValue").Value;
            oFSTranDetail.Description = oReader.GetString("Description");
            oFSTranDetail.FsTranType = (EnumFSTranType)oReader.GetInt32("FsTranType").Value;
            oFSTranDetail.ItemCode = (EnumFSItemCode)oReader.GetInt32("ItemCode").Value;
            oFSTranDetail.ItemID = oReader.GetInt32("ItemID");
            oFSTranDetail.Remarks = oReader.GetString("Remarks");
            oFSTranDetail.Side = (EnumSide)oReader.GetInt32("Side").Value;
            oFSTranDetail.TranID = oReader.GetInt32("TranID", 0);
            oFSTranDetail.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oFSTranDetail.CreatedDate = oReader.GetDateTime("CREATIONDATE").HasValue
                ? oReader.GetDateTime("CREATIONDATE").Value
                : DateTime.MinValue;
            oFSTranDetail.ModifiedBy = oReader.GetInt32("ModifiedBy", 0);
            oFSTranDetail.ModifiedDate = oReader.GetDateTime("ModifiedDate").HasValue
                ? oReader.GetDateTime("ModifiedDate").Value
                : (DateTime?)null;
            this.SetObjectState(oFSTranDetail, Ease.Core.ObjectState.Saved);
        }

        protected override T CreateObject<T>(DataReader oReader)
        {
            FSTranDetail oFSTranDetail = new FSTranDetail();
            MapObject(oFSTranDetail, oReader);
            return oFSTranDetail as T;
        }

        protected FSTranDetail CreateObject(DataReader oReader)
        {
            FSTranDetail oFSTranDetail = new FSTranDetail();
            MapObject(oFSTranDetail, oReader);
            return oFSTranDetail;
        }

        #region Service implementation  Detail

        public FSTranDetail Get(int id)
        {
            FSTranDetail oFSTranDetail = new FSTranDetail();

            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(FSTranDetailDA.Get(tc, id));
                if (oreader.Read())
                {
                    oFSTranDetail = this.CreateObject<FSTranDetail>(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 oFSTranDetail;
        }


        public List<FSTranDetail> Get()
        {
            List<FSTranDetail> FSTranDetails = new List<FSTranDetail>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(FSTranDetailDA.Get(tc));
                FSTranDetails = this.CreateObjects<FSTranDetail>(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 FSTranDetails;
        }

        public FSTranDetail GetGratuity(int id)
        {
            FSTranDetail oFSTranDetail = new FSTranDetail();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(FSTranDetailDA.GetGratuity(tc, id));
                if (oreader.Read())
                {
                    oFSTranDetail = this.CreateObject<FSTranDetail>(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 oFSTranDetail;
        }


        public FSTranDetail GetFinalSettlement(int id)
        {
            FSTranDetail oFSTranDetail = new FSTranDetail();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(FSTranDetailDA.GetFinalSettlement(tc, id));
                if (oreader.Read())
                {
                    oFSTranDetail = this.CreateObject<FSTranDetail>(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 oFSTranDetail;
        }

        public List<FSTranDetail> GetDetail(int nTranID)
        {
            List<FSTranDetail> FSTranDetails = new List<FSTranDetail>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(FSTranDetailDA.GetDetail(tc, nTranID));
                FSTranDetails = this.CreateObjects<FSTranDetail>(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 FSTranDetails;
        }


        public List<FSTranDetail> GetDetail(TransactionContext tc, int nTranID)
        {
            List<FSTranDetail> FSTranDetails = new List<FSTranDetail>();
            try
            {
                DataReader dr = new DataReader(FSTranDetailDA.GetDetail(tc, nTranID));
                FSTranDetails = this.CreateObjects<FSTranDetail>(dr);
                dr.Close();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }

            return FSTranDetails;
        }

        public List<FSTranDetail> GetDetailAmountType(int nTranID)
        {
            List<FSTranDetail> FSTranDetails = new List<FSTranDetail>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(FSTranDetailDA.GetDetailAmountType(tc, nTranID));
                FSTranDetails = this.CreateObjects<FSTranDetail>(dr);
                dr.Close();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }

            return FSTranDetails;
        }

        public List<FSTranDetail> GetDetail(TransactionContext tc, DateTime fromDate, DateTime toDate)
        {
            List<FSTranDetail> FSTranDetails = new List<FSTranDetail>();
            try
            {
                DataReader dr = new DataReader(FSTranDetailDA.GetDetail(tc, fromDate, toDate));
                FSTranDetails = this.CreateObjects<FSTranDetail>(dr);
                dr.Close();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }

            return FSTranDetails;
        }

        public List<FSTranDetail> GetDetailsBytranIDs(TransactionContext tc,string ids)
        {
            List<FSTranDetail> FSTranDetails = new List<FSTranDetail>();
            try
            {
                DataReader dr = new DataReader(FSTranDetailDA.GetDetail(tc, ids));
                FSTranDetails = this.CreateObjects<FSTranDetail>(dr);
                dr.Close();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);

                #endregion
            }

            return FSTranDetails;
        }


        public List<FSTranDetail> GetDetailByEmp(int nEmpID)
        {
            List<FSTranDetail> FSTranDetails = new List<FSTranDetail>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader dr = new DataReader(FSTranDetailDA.GetDetailByEmp(tc, nEmpID));
                FSTranDetails = this.CreateObjects<FSTranDetail>(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 FSTranDetails;
        }

        public List<FSTranDetail> Get(EnumStatus status)
        {
            List<FSTranDetail> FSTranDetails = new List<FSTranDetail>();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataReader dr = new DataReader(FSTranDetailDA.Get(tc, status));
                FSTranDetails = this.CreateObjects<FSTranDetail>(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 FSTranDetails;
        }


        public int Save(FSTranDetail oFSTranDetail)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                //if (oFSTranDetail.IsNew)
                //{
                int id = tc.GenerateID("FSTranDetail", "FSTranDetailID");
                base.SetObjectID(oFSTranDetail, (id));
                FSTranDetailDA.Insert(tc, oFSTranDetail);
                //}
                //else
                //{
                //    FSTranDetailDA.Update(tc, oFSTranDetail);
                //}
                tc.End();
                return oFSTranDetail.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(TransactionContext tc, FSTranDetail oFSTranDetail)
        {
            try
            {
                //tc = TransactionContext.Begin(true);
                //if (oFSTranDetail.IsNew)
                //{
                int id = tc.GenerateID("FSTranDetail", "FSTranDetailID");
                base.SetObjectID(oFSTranDetail, (id));
                FSTranDetailDA.Insert(tc, oFSTranDetail);
                //}
                //else
                //{
                //    FSTranDetailDA.Update(tc, oFSTranDetail);
                //}
                //tc.End();
                return oFSTranDetail.ID;
            }
            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);
                FSTranDetailDA.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
            }
        }

        public void DeleteByUserId(int trandId, int userId)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                FSTranDetailDA.DeleteByUserId(tc, trandId, userId);
                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
}