public PriceRule Create(PriceRuleType priceRuleType)
        {
            switch (priceRuleType)
            {
            case PriceRuleType.Each:
                return(new EachPriceRule());

            default:
                throw new NotImplementedException($"PriceRule could not be determined by the given priceRuleType: {priceRuleType}");
            }
        }
 public PriceRule Create(PriceRuleType priceRuleType)
 {
     try
     {
         return((PriceRule)Activator.CreateInstance(
                    Type.GetType($"ShoppingCart2.Business.PriceRules.{priceRuleType}PriceRule")));
     }
     catch
     {
         return(null);
     }
 }
Beispiel #3
0
        public KeyValuePair <int, decimal> GetCalculationPriceAndRule(int productID, int quantity, Connector connector, PriceRuleType ruleType)
        {
            //TODO: Add load options

            var repoContent    = _scope.Repository <Content>();
            var repoAssortment = _scope.Repository <VendorAssortment>();

            var vendorAssortment = _scope.Repository <VendorAssortment>().GetAll(va => va.ProductID == productID).ToList();


            var contentProduct = repoContent.GetSingle(x => x.ProductID == productID && x.ConnectorID == connector.ConnectorID);

            if (contentProduct == null)
            {
                return(new KeyValuePair <int, decimal>(0, 0));
            }

            var connectorPriceRules = (from cp in _scope.Repository <ContentPrice>().GetAllAsQueryable()
                                       join cpg in _scope.Repository <ContentProductGroup>().GetAllAsQueryable(x => x.MasterGroupMappingID != null)

                                       on cp.ConnectorID equals cpg.ConnectorID


                                       where (cp.ProductID.HasValue && cp.ProductID.Value == cpg.ProductID) &&
                                       cp.ConnectorID == connector.ConnectorID &&
                                       cp.PriceRuleType == (int)ruleType &&
                                       ((!cp.BrandID.HasValue || cp.BrandID.Value == contentProduct.Product.BrandID) ||
                                        (!cp.ProductID.HasValue || cp.ProductID.Value == productID) ||
                                        cpg.MasterGroupMapping.ProductGroupID == cp.ProductGroupID)
                                       select cp).OrderBy(x => x.ContentPriceRuleIndex).ToList();

            var vendorPrices = (from p in repoContent.GetAllAsQueryable()
                                join ass in repoAssortment.GetAllAsQueryable()
                                on new { p.ContentProduct.VendorID, p.ProductID } equals new { ass.VendorID, ass.ProductID }
                                where p.ProductID == productID && p.ConnectorID == connector.ConnectorID
                                select ass).SelectMany(x => x.VendorPrices);

            if (vendorPrices.Count() < 1)
            {
                return(new KeyValuePair <int, decimal>(0, 0));
            }

            List <VendorPrice> price = vendorPrices.ToList();

            if (connectorPriceRules.Count() > 0)
            {
                foreach (var pRule in connectorPriceRules)
                {
                    if (pRule.MinimumQuantity.HasValue)
                    {
                        price = price.Where(x => x.MinimumQuantity == pRule.MinimumQuantity.Value).ToList();
                    }

                    foreach (var p in price.Where(x => x.Price.HasValue))
                    {
                        if (pRule.FixedPrice.HasValue)
                        {
                            p.Price = pRule.FixedPrice.Value;
                        }
                        else
                        {
                            switch (ruleType)
                            {
                            case PriceRuleType.UnitPrice:
                                if (pRule.Margin == "%")
                                {
                                    if (pRule.UnitPriceIncrease.HasValue)
                                    {
                                        p.Price = p.Price * pRule.UnitPriceIncrease.Value;
                                    }

                                    if (pRule.CostPriceIncrease.HasValue && p.CostPrice.HasValue)
                                    {
                                        p.Price = p.CostPrice * pRule.CostPriceIncrease;
                                    }
                                }
                                else if (pRule.Margin == "+")
                                {
                                    if (pRule.UnitPriceIncrease.HasValue)
                                    {
                                        p.Price = p.Price + pRule.UnitPriceIncrease.Value;
                                    }

                                    if (pRule.CostPriceIncrease.HasValue && p.CostPrice.HasValue)
                                    {
                                        p.Price = p.CostPrice + pRule.CostPriceIncrease;
                                    }
                                }
                                break;

                            case PriceRuleType.CostPrice:
                                if (pRule.Margin == "%")
                                {
                                    if (pRule.UnitPriceIncrease.HasValue)
                                    {
                                        p.CostPrice = p.Price * pRule.UnitPriceIncrease.Value;
                                    }

                                    if (pRule.CostPriceIncrease.HasValue && p.CostPrice.HasValue)
                                    {
                                        p.CostPrice = p.CostPrice * pRule.CostPriceIncrease;
                                    }
                                }
                                else if (pRule.Margin == "+")
                                {
                                    if (pRule.UnitPriceIncrease.HasValue)
                                    {
                                        p.CostPrice = p.Price + pRule.UnitPriceIncrease.Value;
                                    }

                                    if (pRule.CostPriceIncrease.HasValue && p.CostPrice.HasValue)
                                    {
                                        p.CostPrice = p.CostPrice + pRule.CostPriceIncrease;
                                    }
                                }
                                break;
                            }
                        }

                        p.CalculatedPriceRuleID = pRule.ContentPriceRuleID;
                    }
                }
            }

            var vendorList = (from vl in _scope.Repository <PreferredConnectorVendor>().GetAllAsQueryable()
                              where vl.ConnectorID == connector.ConnectorID
                              orderby vl.isPreferred descending
                              select vl).ToList();

            switch (ruleType)
            {
            case PriceRuleType.UnitPrice:
                var returnPrice = (from vass in price
                                   join vl in vendorList on vass.VendorAssortment.VendorID equals vl.VendorID
                                   where vass.Price != null
                                   orderby vl.isPreferred descending
                                   select vass).FirstOrDefault();

                if (returnPrice != null)
                {
                    return(new KeyValuePair <int, decimal>(returnPrice.CalculatedPriceRuleID, returnPrice.Price.Value));
                }
                break;

            case PriceRuleType.CostPrice:
                var returnCostPrice = (from vass in price
                                       join vl in vendorList on vass.VendorAssortment.VendorID equals vl.VendorID
                                       where vass.CostPrice != null
                                       orderby vl.isPreferred descending
                                       select vass).FirstOrDefault();

                if (returnCostPrice != null)
                {
                    return(new KeyValuePair <int, decimal>(returnCostPrice.CalculatedPriceRuleID, returnCostPrice.CostPrice.Value));
                }
                break;
            }
            return(new KeyValuePair <int, decimal>(0, 0));
        }
Beispiel #4
0
 public decimal CalculatePrice(int productID, int quantity, Connector connector, PriceRuleType ruleType)
 {
     return(GetCalculationPriceAndRule(productID, quantity, connector, ruleType).Value);
 }