Ejemplo n.º 1
0
        public void RecreateLineItemsBasedOnShipments(ICart cart, IEnumerable <CartItemViewModel> cartItems, IEnumerable <AddressModel> addresses)
        {
            var form  = cart.GetFirstForm();
            var items = cartItems
                        .GroupBy(x => new { x.AddressId, x.Code, x.DisplayName, x.IsGift })
                        .Select(x => new
            {
                Code        = x.Key.Code,
                DisplayName = x.Key.DisplayName,
                AddressId   = x.Key.AddressId,
                Quantity    = x.Count(),
                IsGift      = x.Key.IsGift
            });

            foreach (var shipment in form.Shipments)
            {
                shipment.LineItems.Clear();
            }

            form.Shipments.Clear();

            foreach (var address in addresses)
            {
                var shipment = cart.CreateShipment(_orderGroupFactory);
                form.Shipments.Add(shipment);
                shipment.ShippingAddress = _addressBookService.ConvertToAddress(address, cart);

                foreach (var item in items.Where(x => x.AddressId == address.AddressId))
                {
                    var lineItem = cart.CreateLineItem(item.Code, _orderGroupFactory);
                    lineItem.DisplayName = item.DisplayName;
                    lineItem.IsGift      = item.IsGift;
                    lineItem.Quantity    = item.Quantity;
                    shipment.LineItems.Add(lineItem);
                }
            }

            ValidateCart(cart);
        }
Ejemplo n.º 2
0
        public AddToCartResult AddToCart(ICart cart, string code, string warehouseCode, decimal quantity)
        {
            var result       = new AddToCartResult();
            var contentLink  = _referenceConverter.GetContentLink(code);
            var entryContent = _contentLoader.Get <EntryContentBase>(contentLink);

            if (entryContent is BundleContent)
            {
                foreach (var relation in _relationRepository.GetChildren <BundleEntry>(contentLink))
                {
                    var entry           = _contentLoader.Get <EntryContentBase>(relation.Child);
                    var recursiveResult = AddToCart(cart, entry.Code, warehouseCode, relation.Quantity ?? 1);
                    if (recursiveResult.EntriesAddedToCart)
                    {
                        result.EntriesAddedToCart = true;
                    }

                    foreach (var message in recursiveResult.ValidationMessages)
                    {
                        result.ValidationMessages.Add(message);
                    }
                }

                return(result);
            }

            ILineItem lineItem = null;

            if (!string.IsNullOrEmpty(warehouseCode))
            {
                IShipment shipment = null;
                if (cart.GetFirstForm().Shipments.Count == 1 && string.IsNullOrEmpty(cart.GetFirstForm().Shipments.First().WarehouseCode))
                {
                    shipment = cart.GetFirstForm().Shipments.First();
                    shipment.WarehouseCode = warehouseCode;
                }
                else
                {
                    shipment = cart.GetFirstForm().Shipments.FirstOrDefault(s => s.WarehouseCode.Equals(warehouseCode, StringComparison.CurrentCultureIgnoreCase));
                }
                if (shipment == null)
                {
                    shipment = cart.CreateShipment(_orderGroupFactory);
                    shipment.WarehouseCode = warehouseCode;
                    cart.AddShipment(shipment);
                }

                lineItem = shipment.LineItems.FirstOrDefault(x => x.Code == code && !x.IsGift);

                if (lineItem == null)
                {
                    //this is just like AddNewLineItem but doesn't add there - adds to shipment instead
                    lineItem             = cart.CreateLineItem(code, _orderGroupFactory);
                    lineItem.Quantity    = quantity;
                    lineItem.DisplayName = entryContent.DisplayName;
                    //cart.AddLineItem(newLineItem, _orderGroupFactory);

                    var price = _pricingService.GetPrice(code);
                    if (price != null)
                    {
                        lineItem.PlacedPrice = price.UnitPrice.Amount;
                    }

                    cart.AddLineItem(shipment, lineItem);
                }
                else
                {
                    cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity);
                }
            }
            else
            {
                lineItem = cart.GetAllLineItems().FirstOrDefault(x => x.Code == code && !x.IsGift);

                if (lineItem == null)
                {
                    lineItem = AddNewLineItem(cart, code, quantity, entryContent.DisplayName);
                }
                else
                {
                    var shipment = cart.GetFirstShipment();
                    cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity);
                }
            }

            var validationIssues = ValidateCart(cart);

            AddValidationMessagesToResult(result, lineItem, validationIssues);

            return(result);
        }