예제 #1
0
        private Task <Cart> BuildCartAsync(XDocument XMLDocument, List <Rules> Rules, List <Models.Inventory> ItemsInventory)
        {
            Cart Cart = new Cart();

            try
            {
                Cart.AddedInventory = new List <Models.Inventory>();
                var InventoryItems = Request.Cookies["InventoryItems"];
                if (InventoryItems != null)
                {
                    var ItemCodes = InventoryItems.Value.Split(new string[] { "+" }, StringSplitOptions.None);
                    foreach (var item in ItemCodes)
                    {
                        var Inventory = Fleets.Line.Helpers.Inventory.GetInventoryItem(XMLDocument, item);
                        if (!Cart.AddedInventory.Any(x => x.ProductId.ToUpper() == item.ToUpper()))
                        {
                            if (Inventory != null)
                            {
                                Cart.AddedInventory.Add(Inventory);
                            }
                        }
                        else
                        {
                            Cart.AddedInventory.Where(x => x.ProductId.ToUpper() == item.ToUpper()).ToList().ForEach(s => s.Quantity += 1);
                        }
                    }

                    //Add promotion rules
                    Dictionary <string, string> PromotionRules = new Dictionary <string, string>();
                    foreach (var rule in Rules)
                    {
                        PromotionRules.Add(rule.Key, rule.Value);
                    }

                    //Call promotion operations
                    AddOperation Operations      = new AddOperation();
                    var          InventoryScales = Operations.ApplyScale(ItemsInventory, InventoryItems.Value);
                    if (InventoryScales.Any())
                    {
                        var InventoryRules = Operations.ApplyRules(ItemsInventory, InventoryItems.Value, PromotionRules, InventoryScales);
                        Cart.OperationResult = InventoryRules;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(Task.FromResult(Cart));
        }