//protected OrderMonth _orderMonth ;

        public virtual Dictionary <int, PurchasingLimits_V01> GetPurchasingLimits(string distributorId, string TIN)
        {
            try
            {
                //Fetch these records from Web Service

                var purchasingLimitManager = PurchasingLimitManager(distributorId);
                if (PurchasingLimitProvider.IsRestrictedByMarketingPlan(distributorId))
                {
                    purchasingLimitManager.PurchasingLimitsRestriction = PurchasingLimitRestrictionType.MarketingPlan;
                }
                int ordermonth = GetOrderMonth();

                purchasingLimitManager.SetPurchasingLimits(ordermonth);
                var currentLimits = PurchasingLimitProvider.GetCurrentPurchasingLimits(distributorId);
                //Get the current Limits if the exist
                var storedLimits = PurchasingLimitProvider.GetPurchasingLimitsFromStore(Country,
                                                                                        distributorId);
                //Get the saved limits for the DS and country

                PurchasingLimits_V01 theLimits = null;
                bool shouldUpdateStore         = false;
                if (null != currentLimits && null != storedLimits && storedLimits.Id > 0) //Decide if we use the stored limits
                {
                    //if (null == currentLimits)
                    //{
                    //    currentLimits = storedLimits;
                    //}
                    storedLimits.MaxEarningsLimit = currentLimits.MaxEarningsLimit;
                    storedLimits.maxVolumeLimit   = currentLimits.maxVolumeLimit;

                    if (IsBlackoutPeriod() || storedLimits.OutstandingOrders > 0 ||
                        PurchasingLimitProvider.GetDistributorPurchasingLimitsSource(Country, distributorId) ==
                        DistributorPurchasingLimitsSourceType.InternetOrdering)
                    {
                        if (HLConfigManager.Configurations.DOConfiguration.PurchasingLimitRestrictionType ==
                            PurchasingLimitRestrictionType.Quarterly)
                        {
                            var lastCutoff = GetLastQuarterlyCutoff();
                            if (storedLimits.LastRead < lastCutoff)
                            {
                                storedLimits = currentLimits;
                                if (null != storedLimits)
                                {
                                    PurchasingLimitProvider.UpdatePurchasingLimits(storedLimits, distributorId, true);
                                }
                                theLimits = storedLimits;
                            }
                        }
                        else
                        {
                            theLimits = storedLimits;
                            PurchasingLimitProvider.UpdatePurchasingLimits(theLimits, distributorId);
                        }
                    }
                    else
                    {
                        theLimits         = currentLimits;
                        shouldUpdateStore = true;
                    }
                }
                else
                {
                    theLimits = currentLimits;
                }

                if (null == theLimits) //We're bare and need the DS
                {
                    theLimits = new PurchasingLimits_V01();
                    var newLimits =
                        purchasingLimitManager.ReloadPurchasingLimits(ordermonth) as
                        PurchasingLimits_V01;

                    if (newLimits != null)
                    {
                        if (null != currentLimits) //if We're already init'ed resolve against current refreshed DS
                        {
                            if (currentLimits.RemainingVolume > newLimits.RemainingVolume)
                            {
                                currentLimits.RemainingVolume = newLimits.RemainingVolume;
                            }
                            if (currentLimits.RemainingEarnings > newLimits.RemainingEarnings)
                            {
                                currentLimits.RemainingEarnings = newLimits.RemainingEarnings;
                            }
                            currentLimits.MaxEarningsLimit = newLimits.MaxEarningsLimit;
                            currentLimits.maxVolumeLimit   = newLimits.maxVolumeLimit;
                            theLimits          = currentLimits;
                            theLimits.LastRead = DateTime.UtcNow;
                        }
                        else
                        {
                            //Probably first time in - refresh from DS.
                            theLimits.RemainingVolume   = newLimits.RemainingVolume;
                            theLimits.RemainingEarnings = newLimits.RemainingEarnings;
                            theLimits.MaxEarningsLimit  = newLimits.MaxEarningsLimit;
                            theLimits.maxVolumeLimit    = newLimits.maxVolumeLimit;
                            theLimits.LastRead          = DateTime.UtcNow;
                        }
                    }
                }

                if (null == storedLimits || shouldUpdateStore)
                {
                    PurchasingLimitProvider.UpdatePurchasingLimits(theLimits, distributorId, true);
                }
                else
                {
                    PurchasingLimitProvider.UpdatePurchasingLimits(theLimits, distributorId,
                                                                   ordermonth);
                }
                var theCurrentLimits = PurchasingLimitProvider.GetCurrentPurchasingLimits(distributorId);
                var limitsType       = PurchaseLimitType.Volume;

                if (HLConfigManager.Configurations.DOConfiguration.PurchasingLimitRestrictionType ==
                    PurchasingLimitRestrictionType.MarketingPlan && theCurrentLimits.maxVolumeLimit < 0)
                {
                    limitsType = PurchaseLimitType.None;
                }

                if (DistributorIsExemptFromPurchasingLimits(distributorId) &&
                    PurchasingLimitManager(distributorId).PurchasingLimitsRestriction !=
                    PurchasingLimitRestrictionType.MarketingPlan)
                {
                    limitsType = PurchaseLimitType.None;
                }
                else
                {
                    limitsType = PurchaseLimitType.Volume;
                }
                purchasingLimitManager.PurchasingLimits.Values.AsQueryable()
                .ToList()
                .ForEach(pl => pl.PurchaseLimitType = limitsType);

                return(purchasingLimitManager.PurchasingLimits);
            }
            catch (Exception ex)
            {
                LoggerHelper.Error(
                    string.Format(
                        "Messages:{0},StackTrace:{1},Locale:{2},DistributorId{3},Purchasing Months:{4}",
                        ex.Message, ex.StackTrace, Locale, distributorId, GetOrderMonth()));
                return(null);
            }
        }
        public override Dictionary <int, PurchasingLimits_V01> GetPurchasingLimits(string distributorId, string TIN)
        {
            if (DistributorIsExemptFromPurchasingLimits(distributorId))
            {
                return(null);
            }

            //Fetch these records from Web Service
            string country = DistributorProfileModel.ProcessingCountryCode;

            var purchasingLimitManager = PurchasingLimitManager(distributorId);

            purchasingLimitManager.SetPurchasingLimits(PurchasingLimitProvider.GetOrderMonth());
            PurchasingLimits_V01 currentLimits = PurchasingLimitProvider.GetCurrentPurchasingLimits(distributorId);
            //Get the current Limits if the exist
            PurchasingLimits_V01 storedLimits = PurchasingLimitProvider.GetPurchasingLimitsFromStore(Country,
                                                                                                     distributorId);
            //Get the saved limits for the DS and country

            PurchasingLimits_V01 theLimits = null;

            if (null != storedLimits && storedLimits.Id > 0) //Decide if we use the stored limits
            {
                storedLimits.MaxEarningsLimit = purchasingLimitManager.MaxEarningsLimit;
                storedLimits.maxVolumeLimit   = purchasingLimitManager.MaxPersonalConsumptionLimit;

                if (IsBlackoutPeriod() || storedLimits.OutstandingOrders > 0 ||
                    PurchasingLimitProvider.GetDistributorPurchasingLimitsSource(Country, distributorId) ==
                    DistributorPurchasingLimitsSourceType.InternetOrdering)
                {
                    theLimits = storedLimits;
                    PurchasingLimitProvider.UpdatePurchasingLimits(theLimits, distributorId);
                }
                else
                {
                    theLimits = currentLimits;
                }
            }
            else
            {
                theLimits = currentLimits;
            }

            if (null == theLimits) //We're bare and need the DS
            {
                theLimits = new PurchasingLimits_V01();
                var limit = purchasingLimitManager.ReloadPurchasingLimits(PurchasingLimitProvider.GetOrderMonth());
                if (null != currentLimits) //if We're already init'ed resolve against current refreshed DS
                {
                    if (currentLimits.RemainingVolume > purchasingLimitManager.RemainingPersonalConsumptionLimit)
                    {
                        currentLimits.RemainingVolume = purchasingLimitManager.RemainingPersonalConsumptionLimit;
                    }
                    if (currentLimits.RemainingEarnings > purchasingLimitManager.RemainingEarningsLimit)
                    {
                        currentLimits.RemainingEarnings = purchasingLimitManager.RemainingEarningsLimit;
                    }
                    currentLimits.MaxEarningsLimit = purchasingLimitManager.MaxEarningsLimit;
                    currentLimits.maxVolumeLimit   = purchasingLimitManager.MaxPersonalConsumptionLimit;
                    theLimits          = currentLimits;
                    theLimits.LastRead = DateTime.UtcNow;
                }
                else
                {
                    //Probably first time in - refresh from DS.
                    theLimits.RemainingVolume   = purchasingLimitManager.RemainingPersonalConsumptionLimit;
                    theLimits.RemainingEarnings = purchasingLimitManager.RemainingEarningsLimit;
                    theLimits.MaxEarningsLimit  = purchasingLimitManager.MaxEarningsLimit;
                    theLimits.maxVolumeLimit    = purchasingLimitManager.MaxPersonalConsumptionLimit;
                    theLimits.LastRead          = DateTime.UtcNow;
                }
            }

            if (null == storedLimits)
            {
                PurchasingLimitProvider.UpdatePurchasingLimits(theLimits, distributorId, country, true);
            }
            else
            {
                PurchasingLimitProvider.UpdatePurchasingLimits(theLimits, distributorId, PurchasingLimitProvider.GetOrderMonth());
            }
            var theCurrentLimits = PurchasingLimitProvider.GetCurrentPurchasingLimits(distributorId);
            var limitsType       = PurchaseLimitType.Volume;

            if (theCurrentLimits.maxVolumeLimit < 0)
            {
                limitsType = PurchaseLimitType.None;
            }

            purchasingLimitManager.PurchasingLimits.Values.AsQueryable().ToList().ForEach(pl => pl.PurchaseLimitType = limitsType);

            return(purchasingLimitManager.PurchasingLimits);
        }