Esempio 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.IsGift })
                        .Select(x => new
            {
                Code      = x.Key.Code,
                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)
            {
                IShipment shipment = _orderFactory.CreateShipment();
                form.Shipments.Add(shipment);
                shipment.ShippingAddress = _addressBookService.ConvertToAddress(address);

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

            ValidateCart(cart);
        }