private string GetTaxNewSchool(ShirtVariation currentContent)
        {
            IMarket market    = _currentMarket.GetCurrentMarket();
            Guid    currCust  = CustomerContext.Current.CurrentContactId;
            string  bogusCart = "BogusCart";

            ICart cart = _orderRepository.LoadOrCreateCart <ICart>(
                currCust, bogusCart);

            ILineItem lineItem = _orderGroupFactory.CreateLineItem(currentContent.Code, cart);

            lineItem.Quantity      = 1;
            lineItem.PlacedPrice   = GetCustomerPricingPrice(currentContent).UnitPrice.Amount;
            lineItem.TaxCategoryId = currentContent.TaxCategoryId;
            cart.AddLineItem(lineItem);

            IOrderAddress bogusAddress = _orderGroupFactory.CreateOrderAddress(cart);

            bogusAddress.CountryCode = "sv";
            bogusAddress.City        = "Stockholm";
            bogusAddress.CountryName = "Sweden";

            string str = String.Empty;
            //str += _taxCalc.Service.GetTaxTotal(cart, market, market.DefaultCurrency).Amount.ToString();

            var taxValues = Enumerable.Empty <ITaxValue>();

            taxValues = OrderContext.Current.GetTaxes(Guid.Empty, currentContent.theTaxCategory, "sv", bogusAddress);

            str = _taxCalc.Service.GetSalesTax(lineItem, market, bogusAddress, new Money(0m, "SEK")).ToString();


            return(str);
        }
Ejemplo n.º 2
0
        public bool AddToCart(ICart cart, string code, out string warningMessage)
        {
            warningMessage = string.Empty;

            var lineItem = cart.GetAllLineItems().FirstOrDefault(x => x.Code == code);

            if (lineItem == null)
            {
                lineItem          = cart.CreateLineItem(code, _orderGroupFactory);
                lineItem.Quantity = 1;
                cart.AddLineItem(lineItem, _orderGroupFactory);
            }
            else
            {
                var shipment = cart.GetFirstShipment();
                cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + 1);
            }

            var validationIssues = ValidateCart(cart);

            foreach (var validationIssue in validationIssues)
            {
                warningMessage += String.Format("Line Item with code {0} ", lineItem.Code);
                warningMessage  = validationIssue.Value.Aggregate(warningMessage, (current, issue) => current + String.Format("{0}, ", issue));
                warningMessage  = warningMessage.Substring(0, warningMessage.Length - 2);
            }

            if (validationIssues.HasItemBeenRemoved(lineItem))
            {
                return(false);
            }

            return(GetFirstLineItem(cart, code) != null);
        }
Ejemplo n.º 3
0
        private void UpdateLineItemSku(ICart cart, int shipmentId, string oldCode, string newCode, decimal quantity)
        {
            RemoveLineItem(cart, shipmentId, oldCode);

            //merge same sku's
            var newLineItem = GetFirstLineItem(cart, newCode);

            if (newLineItem != null)
            {
                var shipment = cart.GetFirstForm().Shipments.First(s => s.ShipmentId == shipmentId || shipmentId == 0);
                cart.UpdateLineItemQuantity(shipment, newLineItem, newLineItem.Quantity + quantity);
            }
            else
            {
                newLineItem          = cart.CreateLineItem(newCode, _orderGroupFactory);
                newLineItem.Quantity = quantity;
                cart.AddLineItem(newLineItem, _orderGroupFactory);

                var price = _pricingService.GetCurrentPrice(newCode);
                if (price.HasValue)
                {
                    newLineItem.PlacedPrice = price.Value.Amount;
                }
            }

            ValidateCart(cart);
        }
        private Dictionary <ILineItem, List <ValidationIssue> > UpdateLineItemSku(ICart cart, int shipmentId, string oldCode, string newCode, decimal quantity)
        {
            RemoveLineItem(cart, shipmentId, oldCode);

            //merge same sku's
            var newLineItem = GetFirstLineItem(cart, newCode);

            if (newLineItem != null)
            {
                var shipment = cart.GetFirstForm().Shipments.First(s => s.ShipmentId == shipmentId || shipmentId == 0);
                cart.UpdateLineItemQuantity(shipment, newLineItem, newLineItem.Quantity + quantity);
            }
            else
            {
                newLineItem          = cart.CreateLineItem(newCode, _orderGroupFactory);
                newLineItem.Quantity = quantity;
                cart.AddLineItem(newLineItem, _orderGroupFactory);

                var price = PriceCalculationService.GetSalePrice(newCode, cart.MarketId, _currentMarket.GetCurrentMarket().DefaultCurrency);
                if (price != null)
                {
                    newLineItem.PlacedPrice = price.UnitPrice.Amount;
                }
            }

            return(ValidateCart(cart));
        }
Ejemplo n.º 5
0
        public void AddToWishList(ShirtVariation currentContent)
        {
            ICart myWishList = _orderRepository.LoadOrCreateCart <ICart>
                                   (PrincipalInfo.CurrentPrincipal.GetContactId(), "WishList");

            ILineItem lineItem = _orderFactory.CreateLineItem(currentContent.Code, myWishList);

            myWishList.AddLineItem(lineItem);
            _orderRepository.Save(myWishList);
        }
Ejemplo n.º 6
0
        public void AddLineItem()
        {
            var lineItem = _cart.CreateLineItem(Constants.VariationCode, _orderGroupFactory);

            lineItem.Quantity             = 1;
            lineItem.IsInventoryAllocated = true;
            lineItem.PlacedPrice          = 100;
            lineItem.Properties["Custom"] = true;
            _cart.AddLineItem(lineItem, _orderGroupFactory);
            _orderRepository.Save(_cart);
        }
Ejemplo n.º 7
0
        public ICart PlaceOrderToCart(IPurchaseOrder purchaseOrder, ICart cart)
        {
            ICart returnCart = cart;
            var   lineItems  = purchaseOrder.GetAllLineItems();

            foreach (var lineItem in lineItems)
            {
                cart.AddLineItem(lineItem);
                lineItem.IsInventoryAllocated = false;
            }
            return(returnCart);
        }
        // RoCe - Needs an update
        public void AddToWishList(ShirtVariation currentContent)
        {
            // Optional lab in Mod. D ... just create a WishList for inspection in CM
            ICart myWishList = _orderRepository.LoadOrCreateCart <ICart>
                                   (PrincipalInfo.CurrentPrincipal.GetContactId(), "WishList");

            ILineItem lineItem = _orderFactory.CreateLineItem(currentContent.Code, myWishList);

            myWishList.AddLineItem(lineItem);
            _orderRepository.Save(myWishList);

            // If using "CreateCart" (an identical cart) - we get:
            // ... A cart with same CustomerId, Name, and MarketId already exist.
            // ...Creating duplicated cart is not allowed :)
        }
Ejemplo n.º 9
0
        private ILineItem AddNewLineItem(ICart cart, string newCode, decimal quantity, string displayName)
        {
            var newLineItem = cart.CreateLineItem(newCode, _orderGroupFactory);
            newLineItem.Quantity = quantity;
            newLineItem.DisplayName = displayName;
            cart.AddLineItem(newLineItem, _orderGroupFactory);

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

            return newLineItem;
        }
Ejemplo n.º 10
0
        public ILineItem AddLineItem(ICart cart, string newCode, decimal quantity, string displayName)
        {
            var newLineItem = cart.CreateLineItem(newCode, _orderGroupFactory.Service);

            newLineItem.Quantity    = quantity;
            newLineItem.DisplayName = displayName;
            cart.AddLineItem(newLineItem, _orderGroupFactory.Service);

            var price = _priceService.Service.GetDefaultPrice(cart.MarketId, DateTime.Today, new CatalogKey(newCode),
                                                              cart.Currency);

            newLineItem.PlacedPrice = price.UnitPrice.Amount;

            return(newLineItem);
        }
Ejemplo n.º 11
0
        public AddToCartResult AddToCart(ICart cart, string code, 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.GetRelationsBySource <BundleEntry>(contentLink))
                {
                    var entry           = _contentLoader.Get <EntryContentBase>(relation.Target);
                    var recursiveResult = AddToCart(cart, entry.Code, relation.Quantity ?? 1);
                    if (recursiveResult.EntriesAddedToCart)
                    {
                        result.EntriesAddedToCart = true;
                    }

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

                return(result);
            }

            var lineItem = cart.GetAllLineItems().FirstOrDefault(x => x.Code == code);

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

            var validationIssues = ValidateCart(cart);

            AddValidationMessagesToResult(result, lineItem, validationIssues);

            return(result);
        }
Ejemplo n.º 12
0
        private static ICart Insert(this ICart cart, string code, int quantity, string displayName)
        {
            var lineItem = cart.CreateLineItem(code, _orderGroupFactory);

            lineItem.DisplayName = displayName;
            lineItem.Quantity    = quantity;
            cart.AddLineItem(lineItem, _orderGroupFactory);

            var price = _customerPriceService.GetPrice(code);

            if (price != null)
            {
                lineItem.PlacedPrice = price.UnitPrice.Amount;
            }

            return(cart);
        }
        public ActionResult Reorder(int orderGroupId = 0)
        {
            var purchaseOrder = OrderContext.Current.GetPurchaseOrderById(orderGroupId);
            var form          = purchaseOrder.GetFirstForm();
            var list          = form.GetAllLineItems();

            string warningMessage = string.Empty;

            ICart Cart = _cartService.LoadOrCreateCart(_cartService.DefaultCartName);

            foreach (var item in list)
            {
                Cart.AddLineItem(item);
            }

            var order = _orderRepository.Save(Cart);

            return(Json(new { result = true }));
        }
        private static void AddNewLineItem(ICart cart, string newCode, decimal quantity, string displayName)
        {
            var newLineItem = cart.CreateLineItem(newCode);

            newLineItem.Quantity    = quantity;
            newLineItem.DisplayName = displayName;
            cart.AddLineItem(newLineItem);

            var price = _priceService.Service.GetPrices(cart.MarketId, DateTime.Now, new CatalogKey(newCode),
                                                        new PriceFilter {
                Currencies = new[] { cart.Currency }
            })
                        .OrderBy(x => x.UnitPrice.Amount).FirstOrDefault();

            if (price != null)
            {
                newLineItem.PlacedPrice = price.UnitPrice.Amount;
            }
        }
Ejemplo n.º 15
0
        public ActionResult Reorder(int orderGroupId = 0)
        {
            var purchaseOrder = OrderContext.Current.Get <PurchaseOrder>(orderGroupId);
            var form          = purchaseOrder.GetFirstForm();
            var list          = form.GetAllLineItems();

            string warningMessage = string.Empty;

            ICart Cart = _cartService.LoadOrCreateCart(_cartService.DefaultCartName);

            foreach (var item in list)
            {
                Cart.AddLineItem(item);
            }

            var order = _orderRepository.Save(Cart);

            return(Redirect(Url.ContentUrl(_contentLoader.Get <CommerceHomePage>(ContentReference.StartPage).CheckoutPage)));
        }
        private string GetTaxNewSchool(ShirtVariation currentContent)
        {
            IMarket market    = _currentMarket.GetCurrentMarket();
            Guid    currCust  = CustomerContext.Current.CurrentContactId;
            string  bogusCart = "BogusCart";

            ICart cart = _orderRepository.LoadOrCreateCart <ICart>(
                currCust, bogusCart);

            ILineItem lineItem = _orderGroupFactory.CreateLineItem(currentContent.Code, cart);

            lineItem.Quantity    = 1;
            lineItem.PlacedPrice = GetCustomerPricingPrice(currentContent).UnitPrice.Amount;
            cart.AddLineItem(lineItem);

            string str = String.Empty;

            str += _taxCalc.Service.GetTaxTotal(cart, market, market.DefaultCurrency).Amount.ToString();

            return(str);
        }
Ejemplo n.º 17
0
        private void GetTaxInfo(DemoMarketsViewModel viewModel)
        {
            ICart cart = _orderRepository.LoadOrCreateCart <ICart>(CustomerContext.Current.CurrentContactId, "BogusCart");

            IOrderAddress bogusAddress = _orderGroupFactory.CreateOrderAddress(cart);

            bogusAddress.CountryCode = viewModel.SelectedMarket.Countries.FirstOrDefault();
            bogusAddress.City        = "Stockholm";

            ILineItem lineItem = _orderGroupFactory.CreateLineItem(viewModel.Shirt.Code, cart);

            lineItem.Quantity      = 1;
            lineItem.PlacedPrice   = viewModel.Shirt.GetDefaultPrice().UnitPrice;
            lineItem.TaxCategoryId = viewModel.Shirt.TaxCategoryId;
            cart.AddLineItem(lineItem);

            viewModel.TaxAmount = _taxCalculator.GetSalesTax(lineItem, viewModel.SelectedMarket,
                                                             bogusAddress, new Money(lineItem.PlacedPrice, viewModel.SelectedMarket.DefaultCurrency));

            viewModel.TaxAmountOldSchool = GetTaxOldSchool(viewModel, bogusAddress);
        }
Ejemplo n.º 18
0
        private ILineItem GetOrAdjustLineItem(ICart cart, string code, decimal quantity, string monogram)
        {
            var item = cart.GetAllLineItems()
                       .FirstOrDefault(x => x.Code.Equals(code, StringComparison.OrdinalIgnoreCase));

            if (item != null)
            {
                item.Quantity += quantity;
            }
            else
            {
                item          = _orderGroupFactory.CreateLineItem(code, cart);
                item.Quantity = quantity;

                if (_lineItemValidator.Validate(item, cart.Market, (i, issue) => { }))
                {
                    cart.AddLineItem(item);
                }
            }

            item.Properties["Monogram"] = monogram;
            return(item);
        }
        private void CheckInventory2()
        {
            string entryCode = "PriceTest_1";

            ICart cart = _repo.Service.LoadOrCreateCart <ICart>(
                PrincipalInfo.CurrentPrincipal.GetContactId(), "Default");
            ILineItem li = _factory.Service.CreateLineItem(entryCode, cart);

            li.Quantity = 1;
            cart.AddLineItem(li);
            cart.GetFirstShipment().WarehouseCode = "test";
            _orderRepository.Save(cart);

            var validationIssues = new Dictionary <ILineItem, ValidationIssue>();

            cart.AdjustInventoryOrRemoveLineItems((item, issue) => validationIssues.Add(item, issue));

            /*
             * _processor.Service.AdjustInventoryOrRemoveLineItem(cart.GetFirstShipment()
             *  , OrderStatus.InProgress, (item, issue) => validationIssues.Add(item, issue));
             * // is li removed? did'nt get "issue"
             */
            _orderRepository.Save(cart);
        }
Ejemplo n.º 20
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);
        }
        private AddToCartResult AddItemToShipment(ICart cart, EntryContentBase entryContent, decimal quantity, int shipmentId, string deliveryMethodId, string warehouseCode)
        {
            var result = new AddToCartResult();

            IWarehouse warehouse = null;

            if (!string.IsNullOrEmpty(warehouseCode))
            {
                warehouse = _warehouseRepository.Get(warehouseCode);
            }

            if (entryContent is BundleContent)
            {
                foreach (var relation in _relationRepository.GetChildren <BundleEntry>(entryContent.ContentLink))
                {
                    var entry           = _contentLoader.Get <EntryContentBase>(relation.Child);
                    var recursiveResult = AddItemToShipment(cart, entry, (relation.Quantity ?? 1) * quantity, shipmentId, deliveryMethodId, warehouseCode);
                    if (recursiveResult.EntriesAddedToCart)
                    {
                        result.EntriesAddedToCart = true;
                    }

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

                return(result);
            }

            var form = cart.GetFirstForm();

            if (form == null)
            {
                form      = _orderGroupFactory.CreateOrderForm(cart);
                form.Name = cart.Name;
                cart.Forms.Add(form);
            }

            var shipment = form.Shipments.FirstOrDefault(x => x.ShipmentId == shipmentId);

            if (shipment == null)
            {
                shipment = _orderGroupFactory.CreateShipment(cart);
                shipment.WarehouseCode    = warehouseCode;
                shipment.ShippingMethodId = new Guid(deliveryMethodId);
                shipment.ShippingAddress  = GetOrderAddressFromWarehosue(cart, warehouse);
                cart.GetFirstForm().Shipments.Add(shipment);
            }

            var     lineItem = shipment.LineItems.FirstOrDefault(x => x.Code == entryContent.Code);
            decimal originalLineItemQuantity = 0;

            if (lineItem == null)
            {
                lineItem             = cart.CreateLineItem(entryContent.Code, _orderGroupFactory);
                lineItem.DisplayName = entryContent.DisplayName;
                lineItem.Quantity    = quantity;
                cart.AddLineItem(shipment, lineItem);
            }
            else
            {
                originalLineItemQuantity = lineItem.Quantity;
                cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity);
            }

            var validationIssues = ValidateCart(cart);
            var newLineItem      = shipment.LineItems.FirstOrDefault(x => x.Code == entryContent.Code);
            var isAdded          = (newLineItem != null ? newLineItem.Quantity : 0) - originalLineItemQuantity > 0;

            AddValidationMessagesToResult(result, lineItem, validationIssues, isAdded);

            return(result);
        }
        public AddToCartResult AddToCart(ICart cart, EntryContentBase entryContent, decimal quantity, string deliveryMethod, string warehouseCode, List <string> dynamicVariantOptionCodes)
        {
            var result  = new AddToCartResult();
            var contact = PrincipalInfo.CurrentPrincipal.GetCustomerContact();

            if (contact?.OwnerId != null)
            {
                var org = cart.GetString("OwnerOrg");
                if (string.IsNullOrEmpty(org))
                {
                    cart.Properties["OwnerOrg"] = contact.OwnerId.Value.ToString().ToLower();
                }
            }

            IWarehouse warehouse = null;

            if (deliveryMethod.Equals("instore") && !string.IsNullOrEmpty(warehouseCode))
            {
                warehouse = _warehouseRepository.Get(warehouseCode);
            }

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

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

                return(result);
            }

            var form = cart.GetFirstForm();

            if (form == null)
            {
                form      = _orderGroupFactory.CreateOrderForm(cart);
                form.Name = cart.Name;
                cart.Forms.Add(form);
            }

            var shipment = cart.GetFirstForm().Shipments.FirstOrDefault(x => string.IsNullOrEmpty(warehouseCode) || (x.WarehouseCode == warehouseCode && x.ShippingMethodId == InStorePickupInfoModel.MethodId));

            if (warehouse != null)
            {
                if (shipment != null && !shipment.LineItems.Any())
                {
                    shipment.WarehouseCode    = warehouseCode;
                    shipment.ShippingMethodId = InStorePickupInfoModel.MethodId;
                    shipment.ShippingAddress  = GetOrderAddressFromWarehosue(cart, warehouse);
                }
                else
                {
                    shipment = form.Shipments.FirstOrDefault(x => !string.IsNullOrEmpty(x.WarehouseCode) && x.WarehouseCode.Equals(warehouse.Code));
                    if (shipment == null)
                    {
                        if (cart.GetFirstShipment().LineItems.Count > 0)
                        {
                            shipment = _orderGroupFactory.CreateShipment(cart);
                        }
                        else
                        {
                            shipment = cart.GetFirstShipment();
                        }

                        shipment.WarehouseCode    = warehouseCode;
                        shipment.ShippingMethodId = InStorePickupInfoModel.MethodId;
                        shipment.ShippingAddress  = GetOrderAddressFromWarehosue(cart, warehouse);

                        if (cart.GetFirstShipment().LineItems.Count > 0)
                        {
                            cart.GetFirstForm().Shipments.Add(shipment);
                        }
                    }
                }
            }

            if (shipment == null)
            {
                var cartFirstShipment = cart.GetFirstShipment();
                if (cartFirstShipment == null)
                {
                    shipment = _orderGroupFactory.CreateShipment(cart);
                    cart.GetFirstForm().Shipments.Add(shipment);
                }
                else
                {
                    if (cartFirstShipment.LineItems.Count > 0)
                    {
                        shipment = _orderGroupFactory.CreateShipment(cart);
                        cart.GetFirstForm().Shipments.Add(shipment);
                    }
                    else
                    {
                        shipment = cartFirstShipment;
                    }
                }
            }

            var     lineItem = shipment.LineItems.FirstOrDefault(x => x.Code == entryContent.Code);
            decimal originalLineItemQuantity = 0;

            if (lineItem == null)
            {
                lineItem = cart.CreateLineItem(entryContent.Code, _orderGroupFactory);
                var lineDisplayName = entryContent.DisplayName;
                if (dynamicVariantOptionCodes?.Count > 0)
                {
                    lineItem.Properties[VariantOptionCodesProperty] = string.Join(",", dynamicVariantOptionCodes.OrderBy(x => x));
                    lineDisplayName += " - " + lineItem.Properties[VariantOptionCodesProperty];
                }

                lineItem.DisplayName = lineDisplayName;
                lineItem.Quantity    = quantity;
                cart.AddLineItem(shipment, lineItem);
            }
            else
            {
                if (lineItem.Properties[VariantOptionCodesProperty] != null)
                {
                    var variantOptionCodesLineItem = lineItem.Properties[VariantOptionCodesProperty].ToString().Split(',');
                    var intersectCodes             = variantOptionCodesLineItem.Intersect(dynamicVariantOptionCodes);

                    if (intersectCodes != null && intersectCodes.Any() &&
                        intersectCodes.Count() == variantOptionCodesLineItem.Length &&
                        intersectCodes.Count() == dynamicVariantOptionCodes.Count)
                    {
                        originalLineItemQuantity = lineItem.Quantity;
                        cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity);
                    }
                    else
                    {
                        lineItem = cart.CreateLineItem(entryContent.Code, _orderGroupFactory);
                        lineItem.Properties[VariantOptionCodesProperty] = string.Join(",", dynamicVariantOptionCodes.OrderBy(x => x));
                        lineItem.DisplayName = entryContent.DisplayName + " - " + lineItem.Properties[VariantOptionCodesProperty];
                        lineItem.Quantity    = quantity;
                        cart.AddLineItem(shipment, lineItem);
                    }
                }
                else
                {
                    originalLineItemQuantity = lineItem.Quantity;
                    cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity);
                }
            }

            var validationIssues = ValidateCart(cart);
            var newLineItem      = shipment.LineItems.FirstOrDefault(x => x.Code == entryContent.Code);
            var isAdded          = (newLineItem != null ? newLineItem.Quantity : 0) - originalLineItemQuantity > 0;

            AddValidationMessagesToResult(result, lineItem, validationIssues, isAdded);

            return(result);
        }
        public CheckoutControllerTests()
        {
            _controllerExceptionHandlerMock = new Mock<ControllerExceptionHandler>();
            _requestContextMock = new Mock<RequestContext>();
            _httpRequestBaseMock = new Mock<HttpRequestBase>();
            _httpContextBaseMock = new Mock<HttpContextBase>();
            _contentRepositoryMock = new Mock<IContentRepository>();
            _mailServiceMock = new Mock<IMailService>();
            _localizationService = new MemoryLocalizationService();
            _currencyServiceMock = new Mock<ICurrencyService>();
            _customerContextFacadeMock = new Mock<CustomerContextFacade>();
            _orderRepositoryMock = new Mock<IOrderRepository>();
            _orderGroupCalculatorMock = new Mock<IOrderGroupCalculator>();
            _paymentProcessorMock = new Mock<IPaymentProcessor>();
            _promotionEngineMock = new Mock<IPromotionEngine>();
            _cartServiceMock = new Mock<ICartService>();
            _addressBookServiceMock = new Mock<IAddressBookService>();
            _orderSummaryViewModelFactoryMock = new Mock<OrderSummaryViewModelFactory>(null, null, null, null);
            _checkoutViewModelFactoryMock = new Mock<CheckoutViewModelFactory>(null, null, null, null, null, null, null, null);
            _orderFactoryMock = new Mock<IOrderFactory>();
            _cart = new FakeCart(null, new Currency("USD"));
            _exceptionContext = new ExceptionContext
            {
                HttpContext = _httpContextBaseMock.Object,
                RequestContext = _requestContextMock.Object
            };
            var customerId = Guid.NewGuid();
            var orderReference = new OrderReference(1, "PurchaseOrder", customerId, typeof(InMemoryPurchaseOrder));
            var purchaseOrder = new InMemoryPurchaseOrder { Name = orderReference.Name, Currency = _cart.Currency, CustomerId = customerId, OrderLink = orderReference };

            var paymentMock = new Mock<ICreditCardPayment>();
            paymentMock.SetupGet(x => x.CreditCardNumber).Returns("423465654");
            paymentMock.SetupGet(x => x.Status).Returns(PaymentStatus.Pending.ToString());

            _httpContextBaseMock.Setup(x => x.Request).Returns(_httpRequestBaseMock.Object);

            _subject = new CheckoutControllerForTest(_contentRepositoryMock.Object, _mailServiceMock.Object, _localizationService,
                _currencyServiceMock.Object, _controllerExceptionHandlerMock.Object, _customerContextFacadeMock.Object, _orderRepositoryMock.Object,
                _checkoutViewModelFactoryMock.Object, _orderGroupCalculatorMock.Object, _paymentProcessorMock.Object, _promotionEngineMock.Object,
                _cartServiceMock.Object, _addressBookServiceMock.Object, _orderSummaryViewModelFactoryMock.Object, _orderFactoryMock.Object);

            _checkoutViewModelFactoryMock
                .Setup(x => x.CreateCheckoutViewModel(It.IsAny<ICart>(), It.IsAny<CheckoutPage>(), It.IsAny<PaymentMethodViewModel<PaymentMethodBase>>()))
                .Returns((ICart cart, CheckoutPage currentPage, PaymentMethodViewModel<PaymentMethodBase> paymentMethodViewModel) => CreateCheckoutViewModel(currentPage, paymentMethodViewModel));

            _orderFactoryMock.Setup(x => x.CreateCardPayment()).Returns(paymentMock.Object);
            _orderRepositoryMock.Setup(x => x.SaveAsPurchaseOrder(_cart)).Returns(orderReference);
            _orderRepositoryMock.Setup(x => x.Load<IPurchaseOrder>(orderReference.OrderGroupId)).Returns(purchaseOrder);

            _contentRepositoryMock.Setup(x => x.Get<StartPage>(It.IsAny<ContentReference>())).Returns(new StartPage());
            _contentRepositoryMock.Setup(x => x.GetChildren<OrderConfirmationPage>(It.IsAny<ContentReference>())).Returns(new[] { new OrderConfirmationPageForTest() { Language = new CultureInfo("en-US") } });

            _cartServiceMock.Setup(x => x.LoadCart(It.IsAny<string>())).Returns(_cart);
            _cartServiceMock.Setup(x => x.ValidateCart(It.IsAny<ICart>())).Returns(new Dictionary<ILineItem, List<ValidationIssue>>());
            _cartServiceMock.Setup(x => x.RequestInventory(It.IsAny<ICart>())).Returns(new Dictionary<ILineItem, List<ValidationIssue>>());

            _cart.AddLineItem(new InMemoryLineItem(), _orderFactoryMock.Object);
        }
Ejemplo n.º 24
0
        public bool AddToCart(ICart cart, string code, out string warningMessage)
        {
            warningMessage = string.Empty;

            var lineItem = cart.GetAllLineItems().FirstOrDefault(x => x.Code == code);

            if (lineItem == null)
            {
                lineItem = _orderFactory.CreateLineItem(code);
                lineItem.Quantity = 1;
                cart.AddLineItem(lineItem, _orderFactory);
            }
            else
            {
                var shipment = cart.GetFirstShipment();
                cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + 1);
            }

            var validationIssues = ValidateCart(cart);

            foreach (var validationIssue in validationIssues)
            {
                warningMessage += String.Format("Line Item with code {0} ", lineItem.Code);
                warningMessage = validationIssue.Value.Aggregate(warningMessage, (current, issue) => current + String.Format("{0}, ", issue));
                warningMessage = warningMessage.Substring(0, warningMessage.Length - 2);
            }

            if (validationIssues.HasItemBeenRemoved(lineItem))
            {
                return false;
            }

            return GetFirstLineItem(cart, code) != null;
        }
        public CheckoutControllerTests()
        {
            _controllerExceptionHandlerMock = new Mock <ControllerExceptionHandler>();
            _requestContextMock             = new Mock <RequestContext>();
            _httpRequestBaseMock            = new Mock <HttpRequestBase>();
            _httpContextBaseMock            = new Mock <HttpContextBase>();
            _contentRepositoryMock          = new Mock <IContentRepository>();
            _mailServiceMock                  = new Mock <IMailService>();
            _localizationService              = new MemoryLocalizationService();
            _currencyServiceMock              = new Mock <ICurrencyService>();
            _customerContextFacadeMock        = new Mock <CustomerContextFacade>();
            _orderRepositoryMock              = new Mock <IOrderRepository>();
            _orderGroupCalculatorMock         = new Mock <IOrderGroupCalculator>();
            _paymentProcessorMock             = new Mock <IPaymentProcessor>();
            _promotionEngineMock              = new Mock <IPromotionEngine>();
            _cartServiceMock                  = new Mock <ICartService>();
            _addressBookServiceMock           = new Mock <IAddressBookService>();
            _orderSummaryViewModelFactoryMock = new Mock <OrderSummaryViewModelFactory>(null, null, null, null);
            _checkoutViewModelFactoryMock     = new Mock <CheckoutViewModelFactory>(null, null, null, null, null, null, null, null);
            _orderFactoryMock                 = new Mock <IOrderFactory>();
            _cart             = new FakeCart(null, new Currency("USD"));
            _exceptionContext = new ExceptionContext
            {
                HttpContext    = _httpContextBaseMock.Object,
                RequestContext = _requestContextMock.Object
            };
            var customerId     = Guid.NewGuid();
            var orderReference = new OrderReference(1, "PurchaseOrder", customerId, typeof(InMemoryPurchaseOrder));
            var purchaseOrder  = new InMemoryPurchaseOrder {
                Name = orderReference.Name, Currency = _cart.Currency, CustomerId = customerId, OrderLink = orderReference
            };

            var paymentMock = new Mock <ICreditCardPayment>();

            paymentMock.SetupGet(x => x.CreditCardNumber).Returns("423465654");
            paymentMock.SetupGet(x => x.Status).Returns(PaymentStatus.Pending.ToString());

            _httpContextBaseMock.Setup(x => x.Request).Returns(_httpRequestBaseMock.Object);

            _subject = new CheckoutControllerForTest(_contentRepositoryMock.Object, _mailServiceMock.Object, _localizationService,
                                                     _currencyServiceMock.Object, _controllerExceptionHandlerMock.Object, _customerContextFacadeMock.Object, _orderRepositoryMock.Object,
                                                     _checkoutViewModelFactoryMock.Object, _orderGroupCalculatorMock.Object, _paymentProcessorMock.Object, _promotionEngineMock.Object,
                                                     _cartServiceMock.Object, _addressBookServiceMock.Object, _orderSummaryViewModelFactoryMock.Object, _orderFactoryMock.Object);

            _checkoutViewModelFactoryMock
            .Setup(x => x.CreateCheckoutViewModel(It.IsAny <ICart>(), It.IsAny <CheckoutPage>(), It.IsAny <PaymentMethodViewModel <PaymentMethodBase> >()))
            .Returns((ICart cart, CheckoutPage currentPage, PaymentMethodViewModel <PaymentMethodBase> paymentMethodViewModel) => CreateCheckoutViewModel(currentPage, paymentMethodViewModel));

            _orderFactoryMock.Setup(x => x.CreateCardPayment()).Returns(paymentMock.Object);
            _orderRepositoryMock.Setup(x => x.SaveAsPurchaseOrder(_cart)).Returns(orderReference);
            _orderRepositoryMock.Setup(x => x.Load <IPurchaseOrder>(orderReference.OrderGroupId)).Returns(purchaseOrder);

            _contentRepositoryMock.Setup(x => x.Get <StartPage>(It.IsAny <ContentReference>())).Returns(new StartPage());
            _contentRepositoryMock.Setup(x => x.GetChildren <OrderConfirmationPage>(It.IsAny <ContentReference>())).Returns(new[] { new OrderConfirmationPageForTest()
                                                                                                                                    {
                                                                                                                                        Language = new CultureInfo("en-US")
                                                                                                                                    } });

            _cartServiceMock.Setup(x => x.LoadCart(It.IsAny <string>())).Returns(_cart);
            _cartServiceMock.Setup(x => x.ValidateCart(It.IsAny <ICart>())).Returns(new Dictionary <ILineItem, List <ValidationIssue> >());
            _cartServiceMock.Setup(x => x.RequestInventory(It.IsAny <ICart>())).Returns(new Dictionary <ILineItem, List <ValidationIssue> >());

            _cart.AddLineItem(new InMemoryLineItem(), _orderFactoryMock.Object);
        }
Ejemplo n.º 26
0
        private void UpdateLineItemSku(ICart cart, int shipmentId, string oldCode, string newCode, decimal quantity)
        {
            RemoveLineItem(cart, shipmentId, oldCode);

            //merge same sku's
            var newLineItem = GetFirstLineItem(cart, newCode);
            if (newLineItem != null)
            {
                var shipment = cart.GetFirstForm().Shipments.First(s => s.ShipmentId == shipmentId || shipmentId <= 0);
                cart.UpdateLineItemQuantity(shipment, newLineItem, newLineItem.Quantity + quantity);
            }
            else
            {
                newLineItem = _orderFactory.CreateLineItem(newCode);
                newLineItem.Quantity = quantity;
                cart.AddLineItem(newLineItem, _orderFactory);

                var price = _pricingService.GetCurrentPrice(newCode);
                if (price.HasValue)
                {
                    newLineItem.PlacedPrice = price.Value.Amount;
                }
            }

            ValidateCart(cart);
        }
Ejemplo n.º 27
0
        public AddToCartResult AddToCart(ICart cart, EntryContentBase entryContent, decimal quantity, string deliveryMethod, string warehouseCode)
        {
            var result  = new AddToCartResult();
            var contact = PrincipalInfo.CurrentPrincipal.GetCustomerContact();

            if (contact?.OwnerId != null)
            {
                var org = cart.GetString("OwnerOrg");
                if (string.IsNullOrEmpty(org))
                {
                    cart.Properties["OwnerOrg"] = contact.OwnerId.Value.ToString().ToLower();
                }
            }
            IWarehouse warehouse = null;

            if (deliveryMethod.Equals("instore") && !string.IsNullOrEmpty(warehouseCode))
            {
                warehouse = _warehouseRepository.Get(warehouseCode);
            }

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

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

                return(result);
            }

            var form = cart.GetFirstForm();

            if (form == null)
            {
                form      = _orderGroupFactory.CreateOrderForm(cart);
                form.Name = cart.Name;
                cart.Forms.Add(form);
            }

            var shipment = cart.GetFirstShipment();

            if (warehouse != null)
            {
                if (shipment != null && !shipment.LineItems.Any())
                {
                    shipment.WarehouseCode    = warehouseCode;
                    shipment.ShippingMethodId = InStorePickupInfoModel.MethodId;
                    shipment.ShippingAddress  = GetOrderAddressFromWarehosue(cart, warehouse);
                }
                else
                {
                    shipment = form.Shipments.FirstOrDefault(x => !string.IsNullOrEmpty(x.WarehouseCode) && x.WarehouseCode.Equals(warehouse.Code));
                    if (shipment == null)
                    {
                        shipment = _orderGroupFactory.CreateShipment(cart);
                        shipment.WarehouseCode    = warehouseCode;
                        shipment.ShippingMethodId = InStorePickupInfoModel.MethodId;
                        cart.GetFirstForm().Shipments.Add(shipment);
                        shipment.ShippingAddress = GetOrderAddressFromWarehosue(cart, warehouse);
                    }
                }
            }

            if (shipment == null)
            {
                shipment = _orderGroupFactory.CreateShipment(cart);
                cart.GetFirstForm().Shipments.Add(shipment);
            }

            var lineItem = cart.GetAllLineItems().FirstOrDefault(x => x.Code == entryContent.Code);

            if (lineItem == null)
            {
                lineItem             = cart.CreateLineItem(entryContent.Code, _orderGroupFactory);
                lineItem.DisplayName = entryContent.DisplayName;
                lineItem.Quantity    = quantity;
                cart.AddLineItem(shipment, lineItem);
            }
            else
            {
                cart.UpdateLineItemQuantity(shipment, lineItem, lineItem.Quantity + quantity);
            }

            var validationIssues = ValidateCart(cart);

            AddValidationMessagesToResult(result, lineItem, validationIssues);

            return(result);
        }