private List <int> RepackageBasketItem(BasketItem originalBasketItem, List <int> parentItemIds, ShippableItem shippableItem, int quantityMultiplier, Product kitMasterProduct) { List <int> newBasketItemIds = new List <int>(); int i = 0; foreach (int addressId in shippableItem.Destinations.Keys) { int warehouseId; if (kitMasterProduct != null && !kitMasterProduct.Kit.ItemizeDisplay) { warehouseId = kitMasterProduct.Warehouse.Id; } else { warehouseId = originalBasketItem.Product.Warehouse.Id; } BasketShipment shipment = GetShipment(warehouseId, addressId, true); if (i == 0) { //ADJUST THE FIRST ITEM originalBasketItem.Shipment = shipment; originalBasketItem.Quantity = (short)(shippableItem.Destinations[addressId] * quantityMultiplier); if (parentItemIds != null) { originalBasketItem.ParentItemId = GetParentItemId(parentItemIds, addressId, originalBasketItem.ParentItemId); } originalBasketItem.Save(); newBasketItemIds.Add(originalBasketItem.Id); } else { //CREATE ADDITIONAL ITEMS TO SPLIT DESTINATIONS BasketItem splitBasketItem = originalBasketItem.Copy(); splitBasketItem.Basket = _Basket; splitBasketItem.Quantity = (short)(shippableItem.Destinations[addressId] * quantityMultiplier); splitBasketItem.Shipment = shipment; if (parentItemIds != null) { splitBasketItem.ParentItemId = GetParentItemId(parentItemIds, addressId, splitBasketItem.ParentItemId); } _Basket.Items.Add(splitBasketItem); splitBasketItem.Save(); if (splitBasketItem.ParentItemId == 0) { splitBasketItem.ParentItemId = splitBasketItem.Id; splitBasketItem.Save(); } newBasketItemIds.Add(splitBasketItem.Id); } i++; } return(newBasketItemIds); }
protected void CheckoutButton_Click(object sender, EventArgs e) { Basket basket = AbleContext.Current.User.Basket; foreach (RepeaterItem saveItem in BasketRepeater.Items) { int basketItemId = 0; HiddenField basketItemIdField = (HiddenField)saveItem.FindControl("BasketItemId"); if (basketItemIdField != null) { basketItemId = AlwaysConvert.ToInt(basketItemIdField.Value); } if (basketItemId > 0) { int itemIndex = basket.Items.IndexOf(basketItemId); if ((itemIndex > -1)) { BasketItem item = basket.Items[itemIndex]; TextBox quantity = (TextBox)saveItem.FindControl("Quantity"); if (!item.IsChildItem && item.OrderItemType == OrderItemType.Product && quantity != null) { item.Quantity = AlwaysConvert.ToInt16(quantity.Text); // Update for Minimum Maximum quantity of product if (item.Quantity < item.Product.MinQuantity) { item.Quantity = item.Product.MinQuantity; quantity.Text = item.Quantity.ToString(); } else if ((item.Product.MaxQuantity > 0) && (item.Quantity > item.Product.MaxQuantity)) { item.Quantity = item.Product.MaxQuantity; quantity.Text = item.Quantity.ToString(); } item.Save(); } } } } // IF THE ORDER AMOUNT DOES NOT FALL IN VALID RANGE SPECIFIED BY THE MERCHENT OrderItemType[] args = new OrderItemType[] { OrderItemType.Charge, OrderItemType.Coupon, OrderItemType.Credit, OrderItemType.Discount, OrderItemType.GiftCertificate, OrderItemType.GiftWrap, OrderItemType.Handling, OrderItemType.Product, OrderItemType.Shipping, OrderItemType.Tax }; decimal orderTotal = AbleContext.Current.User.Basket.Items.TotalPrice(args); var settings = AbleContext.Current.Store.Settings; decimal minOrderAmount = settings.OrderMinimumAmount; decimal maxOrderAmount = settings.OrderMaximumAmount; if ((minOrderAmount > orderTotal) || (maxOrderAmount > 0 && maxOrderAmount < orderTotal)) { Response.Redirect(AbleCommerce.Code.NavigationHelper.GetBasketUrl()); } Response.Redirect(AbleCommerce.Code.NavigationHelper.GetCheckoutUrl()); }
public static void UpdateBasketItem(Repeater BasketRepeater, int itemId) { if (itemId <= 0) { return; } Basket basket = AbleContext.Current.User.Basket; foreach (RepeaterItem saveItem in BasketRepeater.Items) { int basketItemId = 0; HiddenField basketItemIdField = (HiddenField)saveItem.FindControl("BasketItemId"); if (basketItemIdField != null) { basketItemId = AlwaysConvert.ToInt(basketItemIdField.Value); } if (basketItemId > 0 && basketItemId == itemId) { int itemIndex = basket.Items.IndexOf(basketItemId); if ((itemIndex > -1)) { BasketItem item = basket.Items[itemIndex]; TextBox quantity = (TextBox)saveItem.FindControl("Quantity"); if (quantity != null) { int qty = AlwaysConvert.ToInt(quantity.Text, item.Quantity); if (qty > System.Int16.MaxValue) { item.Quantity = System.Int16.MaxValue; } else { item.Quantity = (System.Int16)qty; } // Update for Minimum Maximum quantity of product if (item.Quantity < item.Product.MinQuantity) { item.Quantity = item.Product.MinQuantity; quantity.Text = item.Quantity.ToString(); } else if ((item.Product.MaxQuantity > 0) && (item.Quantity > item.Product.MaxQuantity)) { item.Quantity = item.Product.MaxQuantity; quantity.Text = item.Quantity.ToString(); } item.Save(); } } } } }
/// <summary> /// Recalculates shipping charges for the given basket. /// </summary> /// <param name="basket">The basket to calculate shipping charges for.</param> public static void Calculate(Basket basket) { ClearExistingShipping(basket); foreach (BasketShipment shipment in basket.Shipments) { if (shipment.Warehouse != null) { ShipMethod shipMethod = shipment.ShipMethod; if (shipMethod != null) { ShipRateQuote rate = shipMethod.GetShipRateQuote(shipment); if (rate != null) { BasketItem shipRateLineItem = new BasketItem(); shipRateLineItem.BasketId = basket.BasketId; shipRateLineItem.OrderItemType = OrderItemType.Shipping; shipRateLineItem.BasketShipmentId = shipment.BasketShipmentId; shipRateLineItem.Name = shipMethod.Name; shipRateLineItem.Price = rate.Rate; shipRateLineItem.Quantity = 1; shipRateLineItem.TaxCodeId = shipMethod.TaxCodeId; shipRateLineItem.Save(); basket.Items.Add(shipRateLineItem); if (rate.Surcharge > 0) { shipRateLineItem = new BasketItem(); shipRateLineItem.BasketId = basket.BasketId; shipRateLineItem.OrderItemType = OrderItemType.Handling; shipRateLineItem.BasketShipmentId = shipment.BasketShipmentId; shipRateLineItem.Name = shipMethod.Name; shipRateLineItem.Price = rate.Surcharge; shipRateLineItem.Quantity = 1; shipRateLineItem.TaxCodeId = shipMethod.SurchargeTaxCodeId; shipRateLineItem.Save(); basket.Items.Add(shipRateLineItem); } } else { //rate quote could not be obtained for some reason. Logger.Warn("Failed to obtain rate quote for the given ship method '" + shipMethod.Name + "'"); //here we need to communicate back to the caller that the selected ship method can't be used shipment.ShipMethodId = 0; shipment.Save(); } } } } }
private void CreateTaxLineItem(Basket basket, int shipmentId, string authorityName, string certiTaxTransactionId, LSDecimal amount) { BasketItem taxLineItem = new BasketItem(); taxLineItem.BasketId = basket.BasketId; taxLineItem.OrderItemType = OrderItemType.Tax; taxLineItem.BasketShipmentId = shipmentId; taxLineItem.Name = authorityName; taxLineItem.Sku = string.Format("CT:" + certiTaxTransactionId); taxLineItem.Price = amount; taxLineItem.Quantity = 1; taxLineItem.Save(); basket.Items.Add(taxLineItem); WebTrace.Write("Tax Line Item Added"); }
/// <summary> /// Calculates any product specific discounts for the basket /// </summary> /// <param name="basket">Basket to calculate discounts for</param> /// <param name="productsToExclude">Any products to exclude from discounting</param> /// <returns>The total discount applied to the basket</returns> private static LSDecimal CalculateProductDiscounts(Basket basket, List <int> productsToExclude) { LSDecimal totalDiscount = 0; //GET ALL PRODUCTS FROM THE BASKET THAT HAVE DISCOUNTS DIRECTLY ASSOCIATED //TODO: UPDATE THE METHOD BELOW TO FACTOR IN THE COMBINE VARIANTS IN LINE ITEM DISCOUNT MODE SETTING DiscountedBasketProduct[] discountedItems = DiscountCalculator.GetDiscountedBasketProducts(basket.BasketId, productsToExclude); if (discountedItems != null && discountedItems.Length > 0) { //EVALUATE EACH ITEM TO SEE WHETHER THE PRODUCT DISCOUNT APPLIES foreach (DiscountedBasketProduct discountedProduct in discountedItems) { VolumeDiscount appliedDiscount = null; LSDecimal discountAmount = -1; //LOOP ALL AVAILABLE DISCOUNTS AND FIND THE BEST ONE VolumeDiscountCollection availableDiscounts = VolumeDiscountDataSource.LoadForProduct(discountedProduct.ProductId); foreach (VolumeDiscount testDiscount in availableDiscounts) { if (testDiscount.IsValidForUser(basket.User)) { LSDecimal tempDiscountAmount = testDiscount.CalculateDiscount(discountedProduct.Quantity, discountedProduct.Quantity, discountedProduct.ExtendedPrice, discountedProduct.ExtendedPrice); if (tempDiscountAmount > discountAmount) { discountAmount = tempDiscountAmount; appliedDiscount = testDiscount; } } } //CHECK WHETHER A DISCOUNT APPLIES TO THIS PRODUCT FOR THIS USER if (appliedDiscount != null) { //productsToExclude.Add(discountedProduct.ProductId); List <BasketItem> newItems = new List <BasketItem>(); //LOOP ALL ITEMS IN BASKET, CALCULATE DISCOUNT FOR MATCHING PRODUCTS foreach (BasketItem basketItem in basket.Items) { if (basketItem.ProductId == discountedProduct.ProductId) { //THIS IS A DISCOUNTED PRODUCT, CREATE THE DISCOUNT LINE ITEM discountAmount = appliedDiscount.CalculateDiscount(basketItem.Quantity, discountedProduct.Quantity, basketItem.ExtendedPrice, discountedProduct.ExtendedPrice); if (discountAmount > 0) { //DISCOUNT AMOUNT SHOULD NOT BE GREATER THEN PARENT ITEM TOTAL if (discountAmount > basketItem.ExtendedPrice) { discountAmount = basketItem.ExtendedPrice; } //DISCOUNT MUST BE ADJUSTED FOR OPTIONS THAT ARE GROUPED BasketItem discountLineItem = new BasketItem(); discountLineItem.BasketId = basket.BasketId; discountLineItem.OrderItemType = OrderItemType.Discount; discountLineItem.ParentItemId = basketItem.BasketItemId; discountLineItem.BasketShipmentId = basketItem.BasketShipmentId; discountLineItem.Name = appliedDiscount.Name; discountLineItem.Sku = appliedDiscount.VolumeDiscountId.ToString(); discountLineItem.Price = (-1 * discountAmount); discountLineItem.Quantity = 1; // basketItem.Quantity; discountLineItem.TaxCodeId = basketItem.TaxCodeId; discountLineItem.Shippable = basketItem.Shippable; discountLineItem.Save(); newItems.Add(discountLineItem); totalDiscount += discountAmount; } } } //ADD ANY NEW ITEMS TO THE BASKET COLLECTION foreach (BasketItem basketItem in newItems) { basket.Items.Add(basketItem); } //AT LEAST ONE DISCOUNT WAS APPLICABLE TO THIS USER //ADD THIS PRODUCT TO EXCLUDE LIST if (productsToExclude.IndexOf(discountedProduct.ProductId) < 0) { productsToExclude.Add(discountedProduct.ProductId); } } } } return(totalDiscount); }
private static LSDecimal Calculate_GroupingMode(Basket basket) { //KEEP TRACK OF TOTAL DISCOUNT APPLIED LSDecimal totalDiscount = 0; //CLEAR EXISTING DISCOUNTS FROM THE BASKET ClearExistingDiscounts(basket); //BUILD A LIST OF PRODUCTS THAT HAVE DISCOUNTS SPECIFICALLY APPLIED //THESE PRODUCTS CANNOT RECEIVE ANY FURTHER DISCOUNTS List <int> productsToExclude = new List <int>(); //INITIALLY POPULATE THE LIST WITH ANY GIFT CERTIFICATE PRODUCT IS AS THESE //PRODUCTS ARE NOT ALLOWED TO BE DISCOUNTED productsToExclude.AddRange(DiscountCalculator.GetGiftCertificateProductIds(basket.BasketId)); //CALCULATE THE PRODUCT LEVEL DISCOUNTS totalDiscount += CalculateProductDiscounts(basket, productsToExclude); //GET POTENTIAL CATEGORY DISCOUNTS //THE DICTIONARY KEY WILL BE CATEGORY LEVEL //POTENTIAL DISCOUNTS ARE THOSE THAT COULD APPLY AT THE SAME SPECIFICITY //THE POTENTIAL DISCOUNTS ARE ORDERED FROM MOST SPECIFIC CATEGORY LEVEL TO LEAST Dictionary <int, List <PotentialDiscount> > potentialDiscounts = DiscountCalculator.GetPotentialDiscounts(basket, productsToExclude, GroupingMode.Category); //LOOP THE DISCOUNTED CATEGORIES //WE HAVE TO CHECK ALL POTENTIAL DISCOUNTS AT EACH CATEGORY LEVEL //TO DETERMINE WHICH ONE TO APPLY List <BasketItem> newItems = new List <BasketItem>(); foreach (int categoryLevel in potentialDiscounts.Keys) { //IF WE FIND A DISCOUNT AT THIS LEVEL, IT SHOULD BE APPLIED, THEN WE //SHOULD RECHECK DISCOUNTS AT THIS LEVEL TO SEE IF ANY OTHERS APPLY bool recheckThisLevel = false; List <int> appliedCategories = new List <int>(); do { LSDecimal appliedDiscountAmount = -1; VolumeDiscount appliedDiscount = null; int appliedCategoryId = 0; List <PotentialDiscount> discountGroup = potentialDiscounts[categoryLevel]; foreach (PotentialDiscount pd in discountGroup) { if (appliedCategories.IndexOf(pd.CategoryId) < 0) { //GET ALL BASKET ITEMS ELIGIBLE FOR DISCOUNT IN THIS CATEGORY BasketItemCollection eligibleItems = DiscountCalculator.GetCategoryItems(pd.CategoryId, basket, productsToExclude); //TOTAL UP ITEMS FOR GROUPING MODE int totalQuantity = eligibleItems.TotalQuantity(); if (totalQuantity > 0) { LSDecimal totalPrice = eligibleItems.TotalPrice(); VolumeDiscount tempDiscount = VolumeDiscountDataSource.Load(pd.VolumeDiscountId); //JUST USE TOTALS TO DETERMINE OVERALL DISCOUNT LSDecimal tempDiscountAmount = tempDiscount.CalculateDiscount(totalQuantity, totalQuantity, totalPrice, totalPrice); if (tempDiscountAmount > appliedDiscountAmount) { appliedDiscountAmount = tempDiscountAmount; appliedDiscount = tempDiscount; appliedCategoryId = pd.CategoryId; } } } } //SEE WHETHER WE FOUND A DISCOUNT AT THIS LEVEL if (appliedDiscount != null) { //GET ALL BASKET ITEMS ELIGIBLE FOR DISCOUNT IN THIS CATEGORY BasketItemCollection eligibleItems = DiscountCalculator.GetCategoryItems(appliedCategoryId, basket, productsToExclude); //TOTAL UP ITEMS FOR GROUPING MODE int totalQuantity = eligibleItems.TotalQuantity(); LSDecimal totalPrice = eligibleItems.TotalPrice(); //LOOP ALL BASKET ITEMS TO ADD DISCOUNTS TO BASKET foreach (BasketItem bi in eligibleItems) { LSDecimal discountAmount = appliedDiscount.CalculateDiscount(bi.Quantity, totalQuantity, bi.ExtendedPrice, totalPrice); if (discountAmount > 0) { //DISCOUNT AMOUNT SHOULD NOT BE GREATER THEN PARENT ITEM TOTAL if (discountAmount > bi.ExtendedPrice) { discountAmount = bi.ExtendedPrice; } //DISCOUNT MUST BE ADJUSTED FOR OPTIONS THAT ARE GROUPED BasketItem discountLineItem = new BasketItem(); discountLineItem.BasketId = basket.BasketId; discountLineItem.OrderItemType = OrderItemType.Discount; discountLineItem.ParentItemId = bi.BasketItemId; discountLineItem.BasketShipmentId = bi.BasketShipmentId; discountLineItem.Name = appliedDiscount.Name; discountLineItem.Sku = appliedDiscount.VolumeDiscountId.ToString(); discountLineItem.Price = (-1 * discountAmount); discountLineItem.Quantity = 1; //bi.Quantity; discountLineItem.TaxCodeId = bi.TaxCodeId; discountLineItem.Shippable = bi.Shippable; basket.Items.Add(discountLineItem); discountLineItem.Save(); totalDiscount += discountAmount; } if (productsToExclude.IndexOf(bi.ProductId) < 0) { productsToExclude.Add(bi.ProductId); } } } //IF A CATEGORY DISCOUNT WAS APPLIED //AND THERE IS MORE THAN ONE DISCOUNT AT THIS LEVEL, //WE MUST RECHECK THIS LEVEL recheckThisLevel = ((appliedCategoryId > 0) && (potentialDiscounts[categoryLevel].Count > 1)); } while (recheckThisLevel); } //ADD DISCOUNT ITEMS TO BASKET foreach (BasketItem bi in newItems) { basket.Items.Add(bi); bi.Save(); } //RETURN THE CALCULATED DISCOUNT return(totalDiscount); }