public ActionResult UpdateCart(int PurchaseQuantity, string itemCode) { var itemRef = _referenceConverter.GetContentLink(itemCode); var itemVar = _contentLoader.Get <DefaultVariation>(itemRef); var cart = _orderRepository.LoadOrCreateCart <ICart>(CustomerContext.Current.CurrentContactId, "Default"); var lineItem = cart.GetAllLineItems().FirstOrDefault(x => x.Code == itemCode); if (lineItem == null) { lineItem = _orderGroupFactory.CreateLineItem(itemCode, cart); lineItem.Quantity = PurchaseQuantity; lineItem.PlacedPrice = itemVar.GetDefaultPrice().UnitPrice; if (itemVar.RequireSpecialShipping) { IShipment specialShip = _orderGroupFactory.CreateShipment(cart); specialShip.ShippingMethodId = GetShipMethodByParam(itemCode); specialShip.ShippingAddress = GetOrderAddress(cart); cart.AddShipment(specialShip); cart.AddLineItem(specialShip, lineItem); } else { var ship = cart.GetFirstShipment(); ship.ShippingAddress = GetOrderAddress(cart); cart.AddLineItem(lineItem); } } else { var shipment = cart.GetFirstForm().Shipments. Where(s => s.LineItems.Contains(lineItem) == true).FirstOrDefault(); cart.UpdateLineItemQuantity(shipment, lineItem, PurchaseQuantity); } _orderRepository.Save(cart); return(RedirectToAction("Index")); }
protected virtual IShipment CreateShipment( IPurchaseOrder purchaseOrder, IOrderForm orderForm, CustomerContact customerContact, SaleShippingViewModel kachingShipping, SaleViewModel kachingSale, SaleLineItemViewModel shippingLineItem) { IShipment shipment = _orderGroupFactory.CreateShipment(purchaseOrder); shipment.OrderShipmentStatus = kachingShipping != null ? OrderShipmentStatus.AwaitingInventory : OrderShipmentStatus.Shipped; if (kachingShipping != null) { // If this shipment is awaiting delivery, then the whole order is still in-progress. purchaseOrder.OrderStatus = OrderStatus.InProgress; } return(shipment); }
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 = _orderGroupFactory.CreateShipment(cart); form.Shipments.Add(shipment); shipment.ShippingAddress = _addressBookService.ConvertToAddress(cart, address); foreach (var item in items.Where(x => x.AddressId == address.AddressId)) { var lineItem = _orderGroupFactory.CreateLineItem(item.Code, cart); lineItem.IsGift = item.IsGift; lineItem.Quantity = item.Quantity; shipment.LineItems.Add(lineItem); } } ValidateCart(cart); }
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 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); }