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

namespace Payroll.Service
{
    #region ClaimBasic Service
    public class ClaimBasicService : ServiceTemplate, IClaimBasicService
    {
        public ClaimBasicService() { }

        private void MapObject(ClaimBasic oClaimBasic, DataReader oReader)
        {
            base.SetObjectID(oClaimBasic,oReader.GetInt32("ClaimBasicID").Value);
            oClaimBasic.ItemCode = oReader.GetString("ItemCode");
            oClaimBasic.ItemName = oReader.GetString("ItemName");
            oClaimBasic.GLCode = oReader.GetString("GLCode");
            oClaimBasic.GLForCredit = oReader.GetString("GLForCredit");
            oClaimBasic.GLForDebit = oReader.GetString("GLForDebit");
            //oClaimBasic.Status = (EnumStatus)oReader.GetInt32("Status").Value;
            //oClaimBasic.ClaimType = (EnumClaimType)oReader.GetInt32("ClaimType").Value;
            oClaimBasic.IsPreApprovalNeeded = oReader.GetBoolean("IsPreApprovalNeeded", false);
            oClaimBasic.CreatedBy = oReader.GetString("CreatedBy") == null ? 0 : oReader.GetInt32("CreatedBy").Value;
            oClaimBasic.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
            oClaimBasic.ModifiedBy = oReader.GetString("ModifiedBy") == null ? null : oReader.GetInt32("ModifiedBy").Value;
            oClaimBasic.ModifiedDate = oReader.GetDateTime("ModifiedDate");
            //oClaimBasic.PayrollTypeID = oReader.GetString("payrollTypeID") == null ? null : ID.FromInteger(oReader.GetInt32("payrollTypeID").Value);
            oClaimBasic.GLSide = oReader.GetString("GLSide");
            oClaimBasic.GLSideCode = oReader.GetString("GLSideCode");
            oClaimBasic.PaidThroughSalary = oReader.GetBoolean("PaidThroughSalary", false);
            oClaimBasic.AllowDeductID = oReader.GetString("AllowDeductID") == null ? 0 : oReader.GetInt32("AllowDeductID").Value;
            oClaimBasic.IsField1 = oReader.GetBoolean("IsField1", false);
            oClaimBasic.IsField2 = oReader.GetBoolean("IsField2", false);
            oClaimBasic.IsField3 = oReader.GetBoolean("IsField3", false);
            oClaimBasic.IsField4 = oReader.GetBoolean("IsField4", false);
            oClaimBasic.IsField5 = oReader.GetBoolean("IsField5", false);
            oClaimBasic.IsField6 = oReader.GetBoolean("IsField6", false);
            oClaimBasic.Field1 = oReader.GetString("Field1", null);
            oClaimBasic.Field2 = oReader.GetString("Field2", null);
            oClaimBasic.Field3 = oReader.GetString("Field3", null);
            oClaimBasic.Field4 = oReader.GetString("Field4", null);
            oClaimBasic.Field5 = oReader.GetString("Field5", null);
            oClaimBasic.Field6 = oReader.GetString("Field6", null);
            oClaimBasic.Field1Type = (EnumFieldTypeStatus)oReader.GetInt16("Field1Type", 0);
            oClaimBasic.Field2Type = (EnumFieldTypeStatus)oReader.GetInt16("Field2Type", 0);
            oClaimBasic.Field3Type = (EnumFieldTypeStatus)oReader.GetInt16("Field3Type", 0);
            oClaimBasic.Field4Type = (EnumFieldTypeStatus)oReader.GetInt16("Field4Type", 0);
            oClaimBasic.Field5Type = (EnumFieldTypeStatus)oReader.GetInt16("Field5Type", 0);
            oClaimBasic.Field6Type = (EnumFieldTypeStatus)oReader.GetInt16("Field6Type", 0);
            oClaimBasic.IsField1Mandatory = oReader.GetBoolean("IsField1Mandatory", false);
            oClaimBasic.IsField2Mandatory = oReader.GetBoolean("IsField2Mandatory", false);
            oClaimBasic.IsField3Mandatory = oReader.GetBoolean("IsField3Mandatory", false);
            oClaimBasic.IsField4Mandatory = oReader.GetBoolean("IsField4Mandatory", false);
            oClaimBasic.IsField5Mandatory = oReader.GetBoolean("IsField5Mandatory", false);
            oClaimBasic.IsField6Mandatory = oReader.GetBoolean("IsField6Mandatory", false);
          

            this.SetObjectState(oClaimBasic, Ease.Core.ObjectState.Saved);
        }
        protected override T CreateObject<T>(DataReader oReader)
        {
            ClaimBasic oClaimBasic = new ClaimBasic();
            MapObject(oClaimBasic, oReader);
            return oClaimBasic as T;
        }
        protected ClaimBasic CreateObject(DataReader oReader)
        {
            ClaimBasic oClaimBasic = new ClaimBasic();
            MapObject(oClaimBasic, oReader);
            return oClaimBasic;
        }
        #region Service implementation
        public ClaimBasic Get(int id)
        {
            ClaimBasic oClaimBasic = null;
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                DataReader oreader = new DataReader(ClaimBasicDA.Get(tc, id));
                if (oreader.Read())
                {
                    oClaimBasic = this.CreateObject<ClaimBasic>(oreader);
                }
                oreader.Close();
                tc.End();

                if (oClaimBasic != null)
                {

                    oClaimBasic.ClaimBasicItems = new ClaimBasicItemService().GetByClaimBasicID(oClaimBasic.ID);
                }
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
            return oClaimBasic;
        }

        public List<ClaimBasic> GetByType(EnumClaimType type, int payrollTypeID)
        {
            #region Cache Header

            List<ClaimBasic> claimBasics = new List<ClaimBasic>();
            if (claimBasics != null)
                return claimBasics;

            #endregion

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

                DataReader dr = new DataReader(ClaimBasicDA.GetByType(tc, type, payrollTypeID));
                claimBasics = this.CreateObjects<ClaimBasic>(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 claimBasics;
        }

        public List<ClaimBasic> Get(EnumStatus status, int payrollTypeID)
        {
            List<ClaimBasic> claimBasics = null;

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

                DataReader dr = new DataReader(ClaimBasicDA.Get(tc, status, payrollTypeID));
                claimBasics = this.CreateObjects<ClaimBasic>(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 claimBasics;
        }

        public List<ClaimBasic> GetNew(EnumStatus status, int payrollTypeID)
        {
            List<ClaimBasic> claimBasics = new List<ClaimBasic>();

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

                DataReader dr = new DataReader(ClaimBasicDA.GetNew(tc, status, payrollTypeID));
                claimBasics = this.CreateObjects<ClaimBasic>(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 claimBasics;
        }

        public void Save(List<ClaimBasic> oClaimBasics)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                int id = tc.GenerateID("ClaimBasic", "ClaimBasicID");
                foreach (ClaimBasic oClaimBasic in oClaimBasics)
                {
                    if (oClaimBasic.IsNew)
                    {
                        base.SetObjectID(oClaimBasic, id);
                        ClaimBasicDA.Insert(tc, oClaimBasic);
                        id++;
                    }
                    else
                    {
                        ClaimBasicDA.Update(tc, oClaimBasic);
                    }
                }
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
        }

        public DataSet GetByClaimBasicIDs(string _claimBasicIDs, int payrollTypeId)
        {
            DataSet claimRequisitionsDSet = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                claimRequisitionsDSet = ClaimBasicDA.GetClaimReqsByIds(tc, _claimBasicIDs, payrollTypeId);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
            return claimRequisitionsDSet;
        }

        public DataSet GetByClaimBasicIDs()
        {
            DataSet claimRequisitionsDSet = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                claimRequisitionsDSet = ClaimBasicDA.GetClaimReqsByIds(tc);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
            return claimRequisitionsDSet;
        }

        public DataSet GetByClaimRegisterIDsForBankAdvice(string _claimBasicIDs, DateTime salaryMonth)
        {
            DataSet claimRequisitionsDSet = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                claimRequisitionsDSet = ClaimBasicDA.GetByClaimRegisterIDsForBankAdvice(tc, _claimBasicIDs, salaryMonth);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
            return claimRequisitionsDSet;
        }

        public DataSet GetByClaimRegisterIDs(string _claimBasicIDs)
        {
            DataSet claimRequisitionsDSet = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                claimRequisitionsDSet = ClaimBasicDA.GetByClaimRegisterIDs(tc, _claimBasicIDs);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
            return claimRequisitionsDSet;
        }

        public DataSet GetWithDesignation(int nDesignationID, int payrollTypeID)
        {
            DataSet claimWithGrade = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                claimWithGrade = ClaimBasicDA.GetWithDesignation(tc, nDesignationID, payrollTypeID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
            return claimWithGrade;
        }

        public DataSet IndividualClaimInformationWithDateRange(int empId, DateTime fromDate, DateTime toDate)
        {
            DataSet ds = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                ds = ClaimBasicDA.IndividualClaimInformationWithDateRange(tc, empId, fromDate, toDate);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
            return ds;
        }

        public DataSet IndividualClaimInformationWithRequisitionID(int nRequisitionID)
        {
            DataSet ds = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                ds = ClaimBasicDA.IndividualClaimInformationWithRequisitionID(tc, nRequisitionID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
            return ds;
        }
        public DataSet GetWithDesignation(int payrollTypeID)
        {
            DataSet claimWithGrade = new DataSet();
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin();
                claimWithGrade = ClaimBasicDA.GetWithDesignation(tc, payrollTypeID);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
            return claimWithGrade;
        }
        public int Save(ClaimBasic oClaimBasic)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);

                if (oClaimBasic.IsNew)
                {
                    int id = tc.GenerateID("ClaimBasic", "ClaimBasicID");
                    base.SetObjectID(oClaimBasic, id);
                    ClaimBasicDA.Insert(tc, oClaimBasic);
                }
                else
                {
                    ClaimBasicDA.Update(tc, oClaimBasic);
                }

                if (oClaimBasic.ClaimBasicItems != null && oClaimBasic.ClaimBasicItems.Count > 0)
                {
                   // ClaimBasicItemDA.DeleteByClaimBasicID(tc, oClaimBasic.ID);
                    foreach (ClaimBasicItem item in oClaimBasic.ClaimBasicItems)
                    {
                        item.ClaimBasicID = oClaimBasic.ID;
                    }

                    ClaimBasicItemService cbi = new ClaimBasicItemService();
                    cbi.Save(tc,oClaimBasic.ClaimBasicItems);
                }

                tc.End();
                return oClaimBasic.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);
                ClaimBasicDA.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 DeleteByType(EnumClaimType claimType)
        {
            TransactionContext tc = null;
            try
            {
                tc = TransactionContext.Begin(true);
                ClaimBasicDA.Delete(tc, claimType);
                tc.End();
            }
            catch (Exception e)
            {
                #region Handle Exception
                if (tc != null)
                    tc.HandleError();
                ExceptionLog.Write(e);
                throw new ServiceException(e.Message, e);
                #endregion
            }
        }

        public List<ClaimBasic> GetClaimBasicItemCount()
        {
            List<ClaimBasic> claimBasics = null;

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

                DataReader dr = new DataReader(ClaimBasicDA.GetClaimBasicItemCount(tc));
                claimBasics = new List<ClaimBasic>();
                while (dr.Read())
                {
                    ClaimBasic temp = new ClaimBasic();
                    temp.ID = dr.GetInt32("ClaimBasicID").Value;
                    temp.ItemCode = dr.GetString("ItemCode");
                    temp.ItemName = dr.GetString("ItemName");
                    temp.SubItemCount = dr.GetInt32("SubItemCount", 0);
                    claimBasics.Add(temp);
                }
                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 claimBasics;
        }

        #endregion
    }
    #endregion

  
}