using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ease.Core;
using Ease.Core.Model;
using System.Data;




namespace HRM.BO
{

    public class OPIProcess : BasicBaseObject
    {
        //        #region Cache Store
        //        private static Cache _cache = new Cache(typeof(OPIProcess));
        //        #endregion

        //        //OPIProcessDetailItem oProcessDetailItem = null;

        #region Constructor
        public OPIProcess()
        {
            //_oPIMonth = DateTime.Today;
            //_processDate = DateTime.Today;
            //_oPIProcessDetails = null;
            //_payrollTypeID = Payroll.BO.SystemInformation.CurrentSysInfo.PayrollTypeID;
        }
        #endregion

        #region Properties

        public DateTime OPIMonth { get; set; }
        public int PayrollTypeID { get; set; }

        public DateTime ProcessDate { get; set; }

        public bool IsApproved { get; set; }
        public int ItemCount { get; set; }

        public List<OPIProcessDetail> OPIProcessDetails { get; set; }
        //#region OPIProcessDetail : List<OPIProcessDetail>
        //private List<OPIProcessDetail> _oPIProcessDetails;
        //public List<OPIProcessDetail> OPIProcessDetails
        //{
        //    get
        //    {
        //        if (_oPIProcessDetails == null && !this.ID.IsUnassigned && this.ID.Integer > 0)
        //        {
        //            _oPIProcessDetails = GetProcessDetails(this.ID);
        //        }
        //        return this._oPIProcessDetails;
        //    }
        //    set
        //    {
        //        _oPIProcessDetails = value;
        //    }
        //}
        //#endregion

        //        #region Service Factory IOPIProcessService : IOPIProcessService
        //        internal static IOPIProcessService Service
        //        {
        //            get
        //            {
        //                return Services.Factory.CreateService<IOPIProcessService>(typeof(IOPIProcessService));
        //            }
        //        }

        //        #endregion

        //        #endregion

        //        #region Functions
        //        public static OPIProcess Get(ID nOPIProcessID)
        //        {
        //            OPIProcess oOPIProcess = null;
        //            #region Cache Header
        //            oOPIProcess = (OPIProcess)_cache["Get", nOPIProcessID];
        //            if (oOPIProcess != null)
        //                return oOPIProcess;
        //            #endregion
        //            oOPIProcess = OPIProcess.Service.Get(nOPIProcessID,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            #region Cache Footer
        //            _cache.Add(oOPIProcess, "Get", nOPIProcessID);
        //            #endregion
        //            return oOPIProcess;
        //        }
        //        public static List<OPIProcess> Get()
        //        {
        //            #region Cache Header
        //            List<OPIProcess> oOPIProcesss = _cache["Get"] as List<OPIProcess>;
        //            if (oOPIProcesss != null)
        //                return oOPIProcesss;
        //            #endregion
        //            try
        //            {
        //                oOPIProcesss = Service.Get(SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            }
        //            catch (ServiceException e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }
        //            #region Cache Footer
        //            _cache.Add(oOPIProcesss, "Get");
        //            #endregion
        //            return oOPIProcesss;
        //        }
        //        public static List<OPIProcess> Get(DateTime dOPIMonth)
        //        {
        //            #region Cache Header
        //            List<OPIProcess> oProcesss = _cache["Get", dOPIMonth] as List<OPIProcess>;
        //            if (oProcesss != null)
        //                return oProcesss;
        //            #endregion

        //            try
        //            {
        //                oProcesss = Service.Get(dOPIMonth,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            }
        //            catch (ServiceException e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            #region Cache Footer
        //            _cache.Add(oProcesss, "Get", dOPIMonth);
        //            #endregion

        //            return oProcesss;
        //        }
        //        public static List<OPIProcessDetail> GetProcessDetails(ID opiProcessID)
        //        {
        //            #region Cache Header
        //            List<OPIProcessDetail> oProcessDetails = _cache["GetProcessDetails", opiProcessID] as List<OPIProcessDetail>;
        //            if (oProcessDetails != null)
        //                return oProcessDetails;
        //            #endregion

        //            try
        //            {
        //                oProcessDetails = Service.GetProcessDetails(opiProcessID,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            }
        //            catch (ServiceException e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            #region Cache Footer
        //            _cache.Add(oProcessDetails, "GetProcessDetails", opiProcessID);
        //            #endregion

        //            return oProcessDetails;
        //        }
        //        public static List<OPIProcessDetail> GetProcessDetailWithItems(ID opiProcessID)
        //        {
        //            #region Cache Header
        //            List<OPIProcessDetail> oProcessDetails = _cache["GetProcessDetailWithItems", opiProcessID] as List<OPIProcessDetail>;
        //            if (oProcessDetails != null)
        //                return oProcessDetails;
        //            #endregion

        //            try
        //            {
        //                oProcessDetails = Service.GetProcessDetailWithItems(opiProcessID,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            }
        //            catch (ServiceException e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            #region Cache Footer
        //            _cache.Add(oProcessDetails, "GetProcessDetailWithItems", opiProcessID);
        //            #endregion

        //            return oProcessDetails;
        //        }
        //        public static List<OPIProcessDetailItem> GetProcessDetailItems(ID nProcessDetailID)
        //        {
        //            #region Cache Header
        //            List<OPIProcessDetailItem> oProcessDetailItems = _cache["GetProcessDetailItems", nProcessDetailID] as List<OPIProcessDetailItem>;
        //            if (oProcessDetailItems != null)
        //                return oProcessDetailItems;
        //            #endregion

        //            try
        //            {
        //                oProcessDetailItems = Service.GetProcessDetailItems(nProcessDetailID,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            }
        //            catch (ServiceException e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            #region Cache Footer
        //            _cache.Add(oProcessDetailItems, "GetProcessDetailItems", nProcessDetailID);
        //            #endregion

        //            return oProcessDetailItems;
        //        }

        //        public static List<OPIProcessDetail> GetProcessDetails(DateTime processMonth)
        //        {

        //            List<OPIProcessDetail> oProcessDetailItems = null;
        //            try
        //            {
        //                oProcessDetailItems = Service.GetProcessDetails(processMonth,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            }
        //            catch (ServiceException e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }
        //            return oProcessDetailItems;
        //        }

        //        public static OPIProcessDetail GetDetail(ID employeeID, DateTime processMonth)
        //        {
        //            OPIProcessDetail oProcessDetail = null;
        //            try
        //            {
        //                oProcessDetail = Service.GetDetail(employeeID, processMonth,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            }
        //            catch (ServiceException e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }
        //            return oProcessDetail;
        //        }

        //        public static double GetProvisionAmountByEmpID(ID nEmpID)
        //        {
        //            return OPIProcess.Service.GetProvisionAmountByEmpID(nEmpID);
        //        }
        //        public static double GetPrevMonthAmount(ID nEmpID, ID nOPIItemID, DateTime dFromOpiMonth, DateTime dToOpiMonth)
        //        {
        //            return OPIProcess.Service.GetPrevMonthAmount(nEmpID, nOPIItemID, dFromOpiMonth, dToOpiMonth);
        //        }
        //        public static DataSet GetOPIRegister(string sOPIItemID, DateTime dOPIMonth, string sEmpID)
        //        {
        //            DataSet ds = null;

        //            try
        //            {
        //                ds = Service.GetOPIRegister(sOPIItemID, dOPIMonth, sEmpID);
        //            }
        //            catch (Exception e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            return ds;
        //        }

        //        public static DataSet GetDetailOtherPayrollItems(string sOPIItemID, DateTime dOPIMonth, string sEmpID)
        //        {
        //            DataSet ds = null;

        //            try
        //            {
        //                ds = Service.GetDetailOtherPayrollItems(sOPIItemID, dOPIMonth, sEmpID);
        //            }
        //            catch (Exception e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            return ds;
        //        }
        //        public static DataSet GetOPITotalValueRegister(DateTime dFromOPIMonth, DateTime dToOPIMonth, int nOPIItemID, string sEmpID)
        //        {
        //            DataSet ds = null;

        //            try
        //            {
        //                ds = Service.GetOPITotalValueRegister(dFromOPIMonth, dToOPIMonth, nOPIItemID, sEmpID);
        //            }
        //            catch (Exception e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            return ds;
        //        }
        //        public static DataSet GetDataSetOfOPIRegister(string sSearch)
        //        {
        //            DataSet dsOPIRegister = null;
        //            dsOPIRegister = OPIProcess.Service.GetDataSetOfOPIRegister(sSearch);
        //            return dsOPIRegister;
        //        }

        //        public ID Save()
        //        {
        //            this.SetAuditTrailProperties();
        //            return OPIProcess.Service.Save(this);
        //        }
        //        public static ID UpdateOnlyProcess(OPIProcess oProcess)
        //        {
        //            oProcess.SetAuditTrailProperties();
        //            return OPIProcess.Service.UpdateOnlyProcess(oProcess);
        //        }
        //        public static void Save(OPIProcess oProcess, List<OPIProcessDetail> oPDetails)
        //        {
        //            oProcess.SetAuditTrailProperties();
        //            OPIProcess.Service.Save(oProcess, oPDetails);
        //        }
        //        public void Delete()
        //        {
        //            OPIProcess.Service.Delete(this.ID,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //        }
        //        public static void Undo(DateTime opiMonth)
        //        {
        //            OPIProcess.Service.Undo(opiMonth,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //        }

        //        public void UpdateOPIDetail(List<OPIProcessDetailItem> oItems)
        //        {
        //            OPIProcess.Service.UpdateDetail(oItems);
        //        }
        //        #endregion

        //        public static List<OPIProcessDetailItem> GetOPiProcessItems(DateTime dateTime)
        //        {
        //            #region Cache Header
        //            List<OPIProcessDetailItem> oProcessDetailItems = _cache["GetOPiProcessItems", dateTime] as List<OPIProcessDetailItem>;
        //            if (oProcessDetailItems != null)
        //                return oProcessDetailItems;
        //            #endregion

        //            try
        //            {
        //                oProcessDetailItems = Service.GetOPiProcessItems(dateTime,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            }
        //            catch (ServiceException e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            #region Cache Footer
        //            _cache.Add(oProcessDetailItems, "GetOPiProcessItems", dateTime);
        //            #endregion

        //            return oProcessDetailItems;
        //        }
        //        public static List<OPIProcessDetailItem> GetLastMonthItems(int nEmployeeID)
        //        {
        //            #region Cache Header
        //            List<OPIProcessDetailItem> oOPIProcessDetailItems = _cache["GetLastMonthItems", nEmployeeID] as List<OPIProcessDetailItem>;
        //            if (oOPIProcessDetailItems != null)
        //                return oOPIProcessDetailItems;
        //            #endregion
        //            try
        //            {
        //                oOPIProcessDetailItems = Service.GetLastMonthItems(nEmployeeID,SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //            }
        //            catch (ServiceException e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }
        //            #region Cache Footer
        //            _cache.Add(oOPIProcessDetailItems, "GetLastMonthItems", nEmployeeID);
        //            #endregion
        //            return oOPIProcessDetailItems;
        //        }

        //        public static DataSet GetEmpOPIPaySlip(DateTime _SalaryMonth, string sEmpID)
        //        {

        //            DataSet ds = null;

        //            try
        //            {
        //                ds = Service.GetEmpOPIPaySlip(_SalaryMonth, sEmpID);
        //            }
        //            catch (Exception e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }


        //            return ds;
        //        }



        //        public static double GetAmountOnRange(Employee employee, DateTime fromDate, DateTime toDate,
        //          int opiID, int parameterid)
        //        {
        //                        double fractionMonthAmount = 0;
        //            double amount = 0;
        //            if (Ease.CoreV35.Utility.Global.DateFunctions.DateDiff("m", PayrollPayrollGlobalFunctions.PayrollFirstDateOfMonth(fromDate), PayrollPayrollGlobalFunctions.PayrollLastDateOfMonth(toDate)) > 2)
        //            {
        //                amount = OPIProcess.Service.GetPrevMonthAmount(employee.ID, ID.FromInteger(opiID),
        //                                    PayrollPayrollGlobalFunctions.PayrollFirstDateOfMonth(fromDate.AddMonths(1)), PayrollPayrollGlobalFunctions.PayrollLastDateOfMonth(toDate.AddMonths(-1)));
        //            }

        //            bool dataNotFound = false;
        //            if (fromDate != Global.DateFunctions.PayrollFirstDateOfMonth(fromDate))
        //            {
        //                EmployeeGradeSalary oge = EmployeeGradeSalary.Get(employee.ID, fromDate, EnumArrearType.NotPresent);
        //                if (oge != null)
        //                {
        //                    OpiParameter op = OpiParameter.Service.Get(ID.FromInteger(parameterid),SystemInformation.CurrentSysInfo.PayrollTypeID.Integer);
        //                    if (op != null)
        //                    {
        //                        oge.FractionofFromTo = PayrollPayrollGlobalFunctions.GetFractinalOfMonth(fromDate); //PayrollPayrollGlobalFunctions.GetFraction(oge.EffectDate, (DateTime)oge.TillDate);
        //                        fractionMonthAmount = op.GetGradeDefinedAmount(employee, oge.BasicSalary, oge.GrossSalary, oge.FractionofFromTo);
        //                        fractionMonthAmount = fractionMonthAmount * PayrollPayrollGlobalFunctions.GetFractinalOfMonth(fromDate);
        //                        dataNotFound = true;
        //                    }

        //                }
        //            }

        //            if (dataNotFound == false)
        //            {
        //                fractionMonthAmount = OPIProcess.Service.GetPrevMonthAmount(employee.ID, ID.FromInteger(opiID),
        //                PayrollPayrollGlobalFunctions.PayrollFirstDateOfMonth(fromDate), PayrollPayrollGlobalFunctions.PayrollLastDateOfMonth(fromDate));
        //            }

        //            if (Global.DateFunctions.PayrollFirstDateOfMonth(fromDate) != PayrollPayrollGlobalFunctions.PayrollFirstDateOfMonth(toDate)) // if cross two month 
        //            {
        //                fractionMonthAmount = fractionMonthAmount + OPIProcess.Service.GetPrevMonthAmount(employee.ID, ID.FromInteger(opiID),
        //                        PayrollPayrollGlobalFunctions.PayrollFirstDateOfMonth(toDate), PayrollPayrollGlobalFunctions.PayrollLastDateOfMonth(toDate)) * PayrollPayrollGlobalFunctions.GetFractinalOfTillDate(toDate);
        //            }
        //            return amount = amount + fractionMonthAmount;

        //        }

        //        public static DataSet GetIDLCOPIRegister(DateTime dOPIMonth, string sEmpID)
        //        {
        //            DataSet ds = null;

        //            try
        //            {
        //                ds = Service.GetIDLCOPIRegister( dOPIMonth, sEmpID);
        //            }
        //            catch (Exception e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            return ds;
        //        }

        //        public static DataSet GetItemWiseOPI(DateTime dFromOPIMonth, int opiItemID, string sEmpID)
        //        {
        //            DataSet ds = null;

        //            try
        //            {
        //                ds = Service.GetItemWiseOPI(dFromOPIMonth,opiItemID,sEmpID);
        //            }
        //            catch (Exception e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            return ds;
        //        }

        //        public static DataSet GetManagersOPI(DateTime dtFromOPI, DateTime dtToOPI, int opiItemID, string sEmpID)
        //        {
        //            DataSet ds = null;

        //            try
        //            {
        //                ds = Service.GetManagersOPI(dtFromOPI,dtToOPI, opiItemID, sEmpID);
        //            }
        //            catch (Exception e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            return ds;
        //        }
        //        public static DataSet GetOPIRegister(string sOPIItemID, DateTime dOPIMonth, DateTime dOPIMonth2, string sEmpID)
        //        {
        //            DataSet ds = null;

        //            try
        //            {
        //                ds = Service.GetOPIRegister(sOPIItemID, dOPIMonth, dOPIMonth2, sEmpID);
        //            }
        //            catch (Exception e)
        //            {
        //                throw new Exception(e.Message, e);
        //            }

        //            return ds;
        //        }

        //        public static OPIProcess Get(DateTime fromdate, ID empiD)
        //        {
        //            OPIProcess oOPIProcess = null;
        //            #region Cache Header
        //            oOPIProcess = (OPIProcess)_cache["Get", fromdate, empiD];
        //            if (oOPIProcess != null)
        //                return oOPIProcess;
        //            #endregion
        //            oOPIProcess = OPIProcess.Service.Get(fromdate, empiD);
        //            #region Cache Footer
        //            _cache.Add(oOPIProcess, "Get", fromdate, empiD);
        //            #endregion
        //            return oOPIProcess;
        //        }

        //        public static DateTime? GetLastPaidOPIMonth(int empID)
        //        {
        //            return OPIProcess.Service.GetLastPaidOPIMonth(empID);    
        //        }
            }
    #endregion

    #region IOPIProcess Service
    public interface IOPIProcessService
    {
        OPIProcess Get(int id, int payrollTypeID);
        List<OPIProcess> Get(int payrollTypeID);
        List<OPIProcess> Get(DateTime dOPIMonth, int payrollTypeID);
        List<OPIProcess> GetUnApprovedProcess(DateTime dOPIMonth, int payrollTypeID);
        List<OPIProcessDetail> GetProcessDetailWithItems(int opiProcessID, int payrollTypeID);
        List<OPIProcessDetail> GetProcessDetails(int opiProcessID, int payrollTypeID);
        List<OPIProcessDetail> GetProcessDetails(DateTime processMonth, int payrollTypeID);
        List<OPIProcessDetailItem> GetProcessDetailItems(int nProcessDetailID, int payrollTypeID);
        OPIProcessDetail GetDetail(int employeeID, DateTime OpiMonth, int payrollTypeID);

        double GetProvisionAmountByEmpID(int nEmpID);
        double GetPrevMonthAmount(int nEmpID, int nOPIItemID, DateTime dFromOpiMonth, DateTime dToOpiMonth);
        List<OPIProcessDetailItem> GetOPiProcessItems(DateTime dateTime, int payrollTypeID);
        List<OPIProcessDetailItem> GetLastMonthItems(int nEmployeeID, int payrollTypeID);
        DataSet GetDataSetOfOPIRegister(string sSearch);
        DataSet GetOPIRegister(string sOPIItemID, DateTime dOPIMonth, string sEmpID);
        DataSet GetOPITotalValueRegister(DateTime dFromOpiMonth, DateTime dToOpiMonth, int nOPIItemID, string sEmpID);
        //int Save(OPIProcess item);
        int UpdateOnlyProcess(OPIProcess oProcess);

        void Save(OPIProcess oProcess, List<OPIProcessDetail> oPDetails);
        void Delete(int id, int payrollTypeID);

        void Undo(DateTime OpiMonth, int payrollTypeID);
        void UpdateDetail(List<OPIProcessDetailItem> oOPIDetails);
        DataSet GetDetailOtherPayrollItems(string sOPIItemID, DateTime dOPIMonth, string sEmpID);

        DataSet GetEmpOPIPaySlip(DateTime _SalaryMonth, string sEmpID);

        DataSet GetIDLCOPIRegister(DateTime dOPIMonth, string sEmpID);

        DataSet GetItemWiseOPI(DateTime dFromOPIMonth, int opiItemID, string sEmpID);

        DataSet GetManagersOPI(DateTime dtFromOPI, DateTime dtToOPI, int opiItemID, string sEmpID);

        DateTime? GetLastPaidOPIMonth(int empID);

        DataSet GetOPIRegister(string sOPIItemID, DateTime dOPIMonth, DateTime dOPIMonth2, string sEmpID);
    }
    #endregion

}