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


namespace HRM.DA
{
    public class TaxChallanService : ServiceTemplate, ITaxChallanService
    {
        public TaxChallanService()
        {
        }

        private void MapObject(TaxChallan oTaxChallan, DataReader oReader)
        {
            base.SetObjectID(oTaxChallan, oReader.GetInt32("ChallenID").Value);
            oTaxChallan.SalaryMonthly = oReader.GetDateTime("salaryMonthly").Value;
            oTaxChallan.EmployeeID = oReader.GetInt32("EmployeeID", 0);
            oTaxChallan.TaxParameterID = oReader.GetInt32("taxParamID", 0);
            oTaxChallan.ChallanNo = oReader.GetString("challenNo");
            oTaxChallan.Amount = oReader.GetDouble("amount").Value;
            oTaxChallan.DepositDate = oReader.GetDateTime("depositDate").Value;
            oTaxChallan.CreatedBy = oReader.GetInt32("CreatedBy", 0);
            oTaxChallan.CreatedDate = oReader.GetDateTime("CreationDate").Value;
            oTaxChallan.ModifiedBy = oReader.GetInt32("ModifiedBy");
            oTaxChallan.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            this.SetObjectState(oTaxChallan, Ease.Core.ObjectState.Saved);
        }

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

        protected TaxChallan CreateObject(DataReader oReader)
        {
            TaxChallan oTaxChallan = new TaxChallan();
            MapObject(oTaxChallan, oReader);
            return oTaxChallan;
        }

        #region Service implementation

        public TaxChallan Get(int id)
        {
            TaxChallan oTaxChallan = null;

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

        public List<TaxChallan> Get()
        {
            List<TaxChallan> taxChallans = new List<TaxChallan>();

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

                DataReader dr = new DataReader(TaxChallanDA.Get(tc));
                taxChallans = this.CreateObjects<TaxChallan>(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 taxChallans;
        }

        public List<TaxChallan> Get(int nEmpID, int nTaxParamID)
        {
            List<TaxChallan> taxChallans = new List<TaxChallan>();

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

                DataReader dr = new DataReader(TaxChallanDA.Get(tc, nEmpID, nTaxParamID));
                taxChallans = this.CreateObjects<TaxChallan>(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 taxChallans;
        }
        public List<TaxChallan> Get(string sEmpIDs, int nTaxParamID)
        {
            List<TaxChallan> taxChallans = new List<TaxChallan>();

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

                DataReader dr = new DataReader(TaxChallanDA.Get(tc, sEmpIDs, nTaxParamID));
                taxChallans = this.CreateObjects<TaxChallan>(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 taxChallans;
        }
        public List<TaxChallan> GetByTaxParamId(int nTaxParamID)
        {
            List<TaxChallan> taxChallans = new List<TaxChallan>();

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

                DataReader dr = new DataReader(TaxChallanDA.GetByTaxParamId(tc, nTaxParamID));
                taxChallans = this.CreateObjects<TaxChallan>(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 taxChallans;
        }

        public int Save(TaxChallan oTaxChallan)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                if (oTaxChallan.IsNew)
                {
                    int id = tc.GenerateID("TaxChallan", "ChallenID");
                    base.SetObjectID(oTaxChallan, (id));
                    TaxChallanDA.DeleteByEmpIDandChallanNo(tc, oTaxChallan.EmployeeID, oTaxChallan.ChallanNo);
                    TaxChallanDA.Insert(tc, oTaxChallan);
                }
                else
                {
                    TaxChallanDA.Update(tc, oTaxChallan);
                }

                tc.End();
                return oTaxChallan.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);
                TaxChallanDA.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 DeleteAllByTaxparamID(int nTaxparamID)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                TaxChallanDA.DeleteAllByTaxparamID(tc, nTaxparamID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

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

                #endregion
            }
        }

        public DataTable GetChalaCountByTaxParamID(int nTaxParamID)
        {
            DataTable dataTableChalanCount = new DataTable("ChalanCount");
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();

                DataSet dataSet = TaxChallanDA.GetChalaCountByTaxParamID(tc, nTaxParamID);
                dataTableChalanCount = dataSet.Tables[0];
                dataSet.Dispose();
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception

                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);

                throw new ServiceException(e.Message, e);

                #endregion
            }

            return dataTableChalanCount;
        }

        #endregion
    }
}