public override bool PurchasingLimitsAreExceeded(string distributorId)
 {
     if (PurchasingLimitProvider.IsRestrictedByMarketingPlan(distributorId))
     {
         return(base.PurchasingLimitsAreExceeded(distributorId));
     }
     return(false);
 }
 public void ReloadPurchasingLimits(string distributorID)
 {
     try
     {
         if (PurchasingLimitProvider.IsRestrictedByMarketingPlan(distributorID))
         {
             CreatePurchasingLimits(GetDistributorPurchasingLimitsCollection());
         }
         if (PurchasingLimits != null)
         {
             SaveToCache();
         }
     }
     catch (Exception ex)
     {
         LoggerHelper.Error(ex.ToString());
     }
 }
        public override Dictionary <int, PurchasingLimits_V01> GetPurchasingLimits(string distributorId, string TIN)
        {
            Dictionary <int, PurchasingLimits_V01> currentLimits = base.GetPurchasingLimits(distributorId, TIN);
            PurchasingLimits_V01 theLimits  = currentLimits[PurchasingLimitProvider.GetOrderMonth()];
            PurchaseLimitType    limitsType = PurchaseLimitType.ProductCategory;
            var tins = DistributorOrderingProfileProvider.GetTinList(distributorId, true);

            List <string> codes = new List <string>(CountryType.SG.HmsCountryCodes);

            codes.Add(CountryType.SG.Key);

            if (tins != null)
            {
                TaxIdentification tid = null;
                if ((tid = tins.Find(t => t.IDType.Key == "SNID")) != null)
                {
                    limitsType = PurchaseLimitType.None;
                }
                else
                {
                    // no SNID
                    if (codes.Contains(DistributorProfileModel.ProcessingCountryCode))
                    {
                        limitsType = PurchaseLimitType.None;
                    }
                }
            }

            // DS with Dummy TIN No "S0000000S", can purchase any category of products up to 1100 vp per order
            if (tins != null && tins.Find(t => t.ID == "S0000000S") != null)
            {
                limitsType = PurchaseLimitType.Volume;
                theLimits.RemainingVolume = MaxVolPoints;
            }

            if (PurchasingLimitProvider.IsRestrictedByMarketingPlan(distributorId))
            {
                limitsType = PurchaseLimitType.Volume;
            }
            currentLimits.Values.AsQueryable().ToList().ForEach(pl => pl.PurchaseLimitType = limitsType);

            return(currentLimits);
        }
        public override Dictionary <int, PurchasingLimits_V01> GetPurchasingLimits(string distributorId, string TIN)
        {
            Dictionary <int, PurchasingLimits_V01> currentLimits = base.GetPurchasingLimits(distributorId, TIN);
            PurchasingLimits_V01 theLimits = currentLimits[PurchasingLimitProvider.GetOrderMonth()];

            theLimits.PurchaseSubType = TIN;

            if (PurchasingLimitProvider.IsRestrictedByMarketingPlan(distributorId))
            {
                theLimits.PurchaseLimitType = PurchaseLimitType.Volume;
                return(currentLimits);
            }
            if (DistributorIsExemptFromPurchasingLimits(distributorId))
            {
                theLimits.PurchaseLimitType = PurchaseLimitType.None;
            }
            else
            {
                theLimits.PurchaseType      = OrderPurchaseType.PersonalConsumption;
                theLimits.PurchaseLimitType = PurchaseLimitType.TotalPaid;
            }

            return(currentLimits);
        }
        protected override ShoppingCartRuleResult PerformRules(ShoppingCart_V02 cart, ShoppingCartRuleReason reason, ShoppingCartRuleResult Result)
        {
            decimal NewVolumePoints = 0m;
            decimal cartVolume      = 0m;

            if (reason == ShoppingCartRuleReason.CartItemsBeingAdded)
            {
                CatalogItem_V01 currentItem = CatalogProvider.GetCatalogItem(cart.CurrentItems[0].SKU, Country);
                if (currentItem == null)
                {
                    return(Result);
                }

                List <string> codes = new List <string>(CountryType.SG.HmsCountryCodes);
                codes.Add(CountryType.SG.Key);

                var tins = DistributorOrderingProfileProvider.GetTinList(cart.DistributorID, true);
                IPurchasingLimitManager manager = new PurchasingLimitManagerFactory().GetPurchasingLimitManager(cart.DistributorID);

                if (!codes.Contains(DistributorProfileModel.ProcessingCountryCode)) // foreign DS
                {
                    //Foreign DS without local National ID they cannot purchase "P" type products.
                    if (tins != null && tins.Find(t => t.IDType.Key == "SNID") == null)
                    {
                        if (currentItem.ProductType == ServiceProvider.CatalogSvc.ProductType.Product)
                        {
                            Result.Result = RulesResult.Failure;
                            Result.AddMessage(HttpContext.GetGlobalResourceObject(string.Format("{0}_Rules", HLConfigManager.Platform), "CantPurchase").ToString());
                            cart.RuleResults.Add(Result);
                            return(Result);
                        }
                    }
                    //Foreign DS with local National ID they can purchase P L and A type items.
                }
                if (PurchasingLimitProvider.IsRestrictedByMarketingPlan(cart.DistributorID))
                {
                    return(base.PerformRules(cart, reason, Result));
                }

                // DS with Dummy TIN No "S0000000S", can purchase any category of products up to 1100 vp per order
                if (tins != null && tins.Find(t => t.ID == "S0000000S") != null)
                {
                    MyHLShoppingCart myCart = cart as MyHLShoppingCart;
                    if (!string.IsNullOrEmpty(myCart.VolumeInCart.ToString()))
                    {
                        cartVolume = myCart.VolumeInCart;
                    }

                    NewVolumePoints = currentItem.VolumePoints * cart.CurrentItems[0].Quantity;

                    if (cartVolume + NewVolumePoints > MaxVolPoints)
                    {
                        Result.AddMessage(
                            string.Format(
                                HttpContext.GetGlobalResourceObject(
                                    string.Format("{0}_Rules", HLConfigManager.Platform), "VolumePointExceeds")
                                .ToString(), cart.CurrentItems[0].SKU));
                        Result.Result = RulesResult.Failure;
                        cart.RuleResults.Add(Result);
                        return(Result);
                    }
                    else
                    {
                        return(Result);
                    }
                }
            }

            return(Result);
        }
        //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 virtual bool PurchasingLimitsAreExceeded(string distributorId)
        {
            bool exceeded = false;

            if (!PurchasingLimitProvider.RequirePurchasingLimits(distributorId, Country))
            {
                return(exceeded);
            }

            var cart = ShoppingCartProvider.GetShoppingCart(distributorId, Locale, true);

            PurchasingLimits_V01 limits = null;

            if (HLConfigManager.Configurations.DOConfiguration.SaveDSSubType)
            {
                limits = PurchasingLimitProvider.GetPurchasingLimits(distributorId, cart.SelectedDSSubType);
            }
            else
            {
                limits = PurchasingLimitProvider.GetCurrentPurchasingLimits(distributorId);
            }
            if (null != cart && null != cart.Totals && cart.OrderCategory != ServiceProvider.CatalogSvc.OrderCategoryType.ETO)
            {
                bool restrictedByMarketingPlan = PurchasingLimitProvider.IsRestrictedByMarketingPlan(distributorId);
                if ((null != limits && (limits.PurchaseLimitType != PurchaseLimitType.None) ||
                     restrictedByMarketingPlan))
                {
                    if (limits.PurchaseType != OrderPurchaseType.Consignment ||
                        (limits.PurchaseType == OrderPurchaseType.Consignment && cart.OrderSubType == "B1"))
                    {
                        if (restrictedByMarketingPlan)
                        {
                            limits.PurchaseLimitType = (limits.maxVolumeLimit == -1)
                                                           ? PurchaseLimitType.None
                                                           : PurchaseLimitType.Volume;
                        }
                        else
                        {
                            limits.PurchaseLimitType = (limits.maxVolumeLimit == -1)
                                                           ? PurchaseLimitType.None
                                                           : limits.PurchaseLimitType;
                        }

                        switch (limits.PurchaseLimitType)
                        {
                        case PurchaseLimitType.Earnings:
                        {
                            exceeded = ((limits.RemainingEarnings - cart.ProductEarningsInCart) < 0);
                            break;
                        }

                        case PurchaseLimitType.Volume:
                        {
                            exceeded = ((limits.RemainingVolume - cart.VolumeInCart) < 0);
                            break;
                        }

                        case PurchaseLimitType.DiscountedRetail:
                        {
                            exceeded = ((limits.RemainingVolume - cart.ProductDiscountedRetailInCart) < 0);
                            break;
                        }

                        case PurchaseLimitType.TotalPaid:
                        {
                            if (null != cart.Totals)
                            {
                                exceeded = ((limits.RemainingVolume - (cart.Totals as OrderTotals_V01).AmountDue) < 0);
                            }
                            break;
                        }
                        }
                    }
                }
                //else if (ods.PurchasingLimitsRestriction == PurchasingLimitRestrictionType.MarketingPlan)
                //{
                //    exceeded = ((limits.RemainingVolume - cart.VolumeInCart) < 0);
                //}
            }

            return(exceeded);
        }
 public virtual bool DistributorIsExemptFromPurchasingLimits(string distributorId)
 {
     return(!PurchasingLimitProvider.IsRestrictedByMarketingPlan(distributorId));
 }
Esempio n. 9
0
        /// <summary>
        /// The IShoppingCart Rule Interface implementation
        /// </summary>
        /// <param name="cart">The current Shopping Cart</param>
        /// <param name="reason">The Rule invoke Reason</param>
        /// <param name="Result">The Rule Results collection</param>
        /// <returns>The cumulative rule results - including the results of this iteration</returns>
        protected override ShoppingCartRuleResult PerformRules(ShoppingCart_V02 cart, ShoppingCartRuleReason reason,
                                                               ShoppingCartRuleResult Result)
        {
            if (reason == ShoppingCartRuleReason.CartItemsBeingAdded)
            {
                if (cart.CurrentItems == null || cart.CurrentItems.Count == 0)
                {
                    Result.Result = RulesResult.Failure;
                    return(Result);
                }

                CatalogItem_V01 currentItem = CatalogProvider.GetCatalogItem(cart.CurrentItems[0].SKU, Country);

                List <string>     codes           = new List <string>(CountryType.TH.HmsCountryCodes);
                decimal           NewVolumePoints = 0m;
                decimal           cartVolume      = 0m;
                TaxIdentification tid             = null;
                string            dummyTin        = "TH00000000000";

                bool isCOPThai = codes.Contains(DistributorProfileModel.ProcessingCountryCode);

                var tins = DistributorOrderingProfileProvider.GetTinList(cart.DistributorID, true);
                isDummyTin = tins.Find(t => t.ID == dummyTin) != null;
                codes.Add(CountryType.TH.Key);
                tid = tins.Find(t => t.IDType.Key == "THID");

                //COP = Thai Tin = No TIN, Can place only L and A items
                //COP Not Thai, No Tin- Can place L & A  item
                if ((isCOPThai && tid == null) || (!isCOPThai && tid == null && !isDummyTin))
                {
                    if (currentItem.ProductType == ProductType.Product)
                    {
                        Result.Result = RulesResult.Failure;
                        Result.AddMessage(HttpContext.GetGlobalResourceObject(string.Format("{0}_Rules", HLConfigManager.Platform), "CantPurchase") as string);
                        cart.RuleResults.Add(Result);
                    }
                }
                // marketing plan
                if (PurchasingLimitProvider.IsRestrictedByMarketingPlan(cart.DistributorID))
                {
                    return(base.PerformRules(cart, reason, Result));
                }
                if (isCOPThai)      //COP Thai
                {
                    if (isDummyTin) //have dummyTIN
                    {
                        MyHLShoppingCart myCart = cart as MyHLShoppingCart;
                        if (!string.IsNullOrEmpty(myCart.VolumeInCart.ToString()))
                        {
                            cartVolume = myCart.VolumeInCart;
                        }
                        NewVolumePoints = currentItem.VolumePoints * cart.CurrentItems[0].Quantity;
                        if (cartVolume + NewVolumePoints > MaxVolPoints)
                        {
                            Result.AddMessage(string.Format(HttpContext.GetGlobalResourceObject(string.Format("{0}_Rules", HLConfigManager.Platform), "VolumePointExceeds").ToString(), cart.CurrentItems[0].SKU.ToString()));
                            Result.Result = RulesResult.Failure;
                            cart.RuleResults.Add(Result);
                        }
                    }
                }
                else
                {
                    //COP Not Thai, Tin =Dummy tin Code – Can place any item (P,L, A and Volume limitation is set 1050 VP per order.)
                    if (isDummyTin)//have dummyTIN
                    {
                        MyHLShoppingCart myCart = cart as MyHLShoppingCart;
                        if (!string.IsNullOrEmpty(myCart.VolumeInCart.ToString()))
                        {
                            cartVolume = myCart.VolumeInCart;
                        }
                        NewVolumePoints = currentItem.VolumePoints * cart.CurrentItems[0].Quantity;
                        if (cartVolume + NewVolumePoints > MaxVolPoints)
                        {
                            Result.AddMessage(string.Format(HttpContext.GetGlobalResourceObject(string.Format("{0}_Rules", HLConfigManager.Platform), "VolumePointExceeds").ToString(), cart.CurrentItems[0].SKU.ToString()));
                            Result.Result = RulesResult.Failure;
                            cart.RuleResults.Add(Result);
                        }
                    }
                }
            }
            return(Result);
        }
        protected override ShoppingCartRuleResult PerformRules(ShoppingCart_V02 cart, ShoppingCartRuleReason reason,
                                                               ShoppingCartRuleResult Result)
        {
            if (reason == ShoppingCartRuleReason.CartItemsBeingAdded)
            {
                try
                {
                    bool IsWithOutHKID = true;
                    bool IsForeignDS   = false;

                    var  currentItem = CatalogProvider.GetCatalogItem(cart.CurrentItems[0].SKU, Country);
                    bool IsProduct   = currentItem != null && currentItem.ProductType == ProductType.Product;


                    var codes = new List <string>(CountryType.HK.HmsCountryCodes);
                    codes.Add(CountryType.HK.Key);

                    if (codes.Contains(DistributorProfileModel.ProcessingCountryCode))
                    {
                        //Local DS
                        var tins = DistributorOrderingProfileProvider.GetTinList(cart.DistributorID, true);
                        if (tins.Count > 0)
                        {
                            var requiredTins = new List <string>(new[] { "HKID", "HKBL" });
                            var tin          =
                                (from t in tins from r in requiredTins where t.IDType.Key == r select t).ToList();
                            if (tin != null && tin.Count > 0)
                            {
                                IsWithOutHKID = false;
                            }
                        }
                    }
                    else
                    {
                        //Foreign DS
                        IsForeignDS = true;
                    }

                    //Rule
                    if (IsWithOutHKID && !IsForeignDS && IsProduct)
                    {
                        //DS with COM as HK without HKID OR HKBL can purchase “L” and “A” type items only.
                        Result.Result = RulesResult.Failure;
                        Result.AddMessage(
                            HttpContext.GetGlobalResourceObject(
                                string.Format("{0}_Rules", HLConfigManager.Platform), "CantPurchase") as string);
                        cart.RuleResults.Add(Result);
                    }
                    if (PurchasingLimitProvider.IsRestrictedByMarketingPlan(cart.DistributorID))
                    {
                        return(base.PerformRules(cart, reason, Result));
                    }
                }
                catch (Exception ex)
                {
                    LoggerHelper.Error(
                        string.Format(
                            "Error while performing Add to Cart Rule for Hong Kong distributor: {0}, Cart Id:{1}, \r\n{2}",
                            cart.DistributorID, cart.ShoppingCartID, ex));
                }
            }
            return(Result);
        }