private static Discount DBMapping(DBDiscount dbItem) { if (dbItem == null) return null; Discount item = new Discount(); item.DiscountID = dbItem.DiscountID; item.DiscountTypeID = dbItem.DiscountTypeID; item.DiscountRequirementID = dbItem.DiscountRequirementID; item.Name = dbItem.Name; item.UsePercentage = dbItem.UsePercentage; item.DiscountPercentage = dbItem.DiscountPercentage; item.DiscountAmount = dbItem.DiscountAmount; item.StartDate = dbItem.StartDate; item.EndDate = dbItem.EndDate; item.RequiresCouponCode = dbItem.RequiresCouponCode; item.CouponCode = dbItem.CouponCode; item.Deleted = dbItem.Deleted; return item; }
/// <summary> /// Gets shopping cart subtotal /// </summary> /// <param name="cart">Cart</param> /// <param name="customer">Customer</param> /// <param name="includingTax">A value indicating whether calculated price should include tax</param> /// <param name="discountAmount">Applied discount amount</param> /// <param name="appliedDiscount">Applied discount</param> /// <param name="subTotalWithoutDiscount">Sub total (without discount)</param> /// <param name="subTotalWithDiscount">Sub total (with discount)</param> /// <returns>Error</returns> public string GetShoppingCartSubTotal(ShoppingCart cart, Customer customer, bool includingTax, out decimal discountAmount, out Discount appliedDiscount, out decimal subTotalWithoutDiscount, out decimal subTotalWithDiscount) { SortedDictionary<decimal, decimal> taxRates = null; return GetShoppingCartSubTotal(cart, customer, includingTax, out discountAmount, out appliedDiscount, out subTotalWithoutDiscount, out subTotalWithDiscount, out taxRates); }
/// <summary> /// Gets shopping cart subtotal /// </summary> /// <param name="cart">Cart</param> /// <param name="customer">Customer</param> /// <param name="includingTax">A value indicating whether calculated price should include tax</param> /// <param name="discountAmount">Applied discount amount</param> /// <param name="appliedDiscount">Applied discount</param> /// <param name="subTotalWithoutDiscount">Sub total (without discount)</param> /// <param name="subTotalWithDiscount">Sub total (with discount)</param> /// <param name="taxRates">Tax rates (of order sub total)</param> /// <returns>Error</returns> public string GetShoppingCartSubTotal(ShoppingCart cart, Customer customer, bool includingTax, out decimal discountAmount, out Discount appliedDiscount, out decimal subTotalWithoutDiscount, out decimal subTotalWithDiscount, out SortedDictionary<decimal, decimal> taxRates) { discountAmount = decimal.Zero; appliedDiscount = null; subTotalWithoutDiscount = decimal.Zero; subTotalWithDiscount = decimal.Zero; taxRates = new SortedDictionary<decimal, decimal>(); string error = string.Empty; //sub totals decimal subTotalExclTaxWithoutDiscount = decimal.Zero; decimal subTotalInclTaxWithoutDiscount = decimal.Zero; foreach (var shoppingCartItem in cart) { decimal taxRate = decimal.Zero; string error2 = string.Empty; decimal sciSubTotal = PriceHelper.GetSubTotal(shoppingCartItem, customer, true); decimal sciExclTax = IoC.Resolve<ITaxService>().GetPrice(shoppingCartItem.ProductVariant, sciSubTotal, false, customer, out taxRate, ref error2); decimal sciInclTax = IoC.Resolve<ITaxService>().GetPrice(shoppingCartItem.ProductVariant, sciSubTotal, true, customer, out taxRate, ref error2); subTotalExclTaxWithoutDiscount += sciExclTax; subTotalInclTaxWithoutDiscount += sciInclTax; if (!String.IsNullOrEmpty(error2)) { error = error2; } //tax rates decimal sciTax = sciInclTax - sciExclTax; if (taxRate > decimal.Zero && sciTax > decimal.Zero) { if (!taxRates.ContainsKey(taxRate)) { taxRates.Add(taxRate, sciTax); } else { taxRates[taxRate] = taxRates[taxRate] + sciTax; } } } //checkout attributes if (customer != null) { var caValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(customer.CheckoutAttributes); foreach (var caValue in caValues) { decimal taxRate = decimal.Zero; string error2 = string.Empty; decimal caExclTax= IoC.Resolve<ITaxService>().GetCheckoutAttributePrice(caValue, false, customer, out taxRate, ref error2); decimal caInclTax = IoC.Resolve<ITaxService>().GetCheckoutAttributePrice(caValue, true, customer, out taxRate, ref error2); subTotalExclTaxWithoutDiscount += caExclTax; subTotalInclTaxWithoutDiscount += caInclTax; if (!String.IsNullOrEmpty(error2)) { error = error2; } //tax rates decimal caTax = caInclTax - caExclTax; if (taxRate > decimal.Zero && caTax > decimal.Zero) { if (!taxRates.ContainsKey(taxRate)) { taxRates.Add(taxRate, caTax); } else { taxRates[taxRate] = taxRates[taxRate] + caTax; } } } } //subtotal without discount if (includingTax) subTotalWithoutDiscount = subTotalInclTaxWithoutDiscount; else subTotalWithoutDiscount = subTotalExclTaxWithoutDiscount; if (subTotalWithoutDiscount < decimal.Zero) subTotalWithoutDiscount = decimal.Zero; subTotalWithoutDiscount = Math.Round(subTotalWithoutDiscount, 2); /*We calculate discount amount on order subtotal excl tax (discount first)*/ //calculate discount amount ('Applied to order subtotal' discount) decimal discountAmountExclTax = GetOrderSubtotalDiscount(customer, subTotalExclTaxWithoutDiscount, out appliedDiscount); if (subTotalExclTaxWithoutDiscount < discountAmountExclTax) discountAmountExclTax = subTotalExclTaxWithoutDiscount; decimal discountAmountInclTax = discountAmountExclTax; //subtotal with discount (excl tax) decimal subTotalExclTaxWithDiscount = subTotalExclTaxWithoutDiscount - discountAmountExclTax; decimal subTotalInclTaxWithDiscount = subTotalExclTaxWithDiscount; //add tax for shopping items & checkout attributes Dictionary<decimal, decimal> tempTaxRates = new Dictionary<decimal, decimal>(taxRates); foreach (KeyValuePair<decimal, decimal> kvp in tempTaxRates) { decimal taxRate = kvp.Key; decimal taxValue = kvp.Value; if (taxValue != decimal.Zero) { //discount the tax amount that applies to subtotal items if (subTotalExclTaxWithoutDiscount > decimal.Zero) { decimal discountTax = taxRates[taxRate] * (discountAmountExclTax / subTotalExclTaxWithoutDiscount); discountAmountInclTax += discountTax; taxValue = taxRates[taxRate] - discountTax; taxValue = Math.Round(taxValue, 2); taxRates[taxRate] = taxValue; } //subtotal with discount (incl tax) subTotalInclTaxWithDiscount += taxValue; } } discountAmountInclTax = Math.Round(discountAmountInclTax, 2); if (includingTax) { subTotalWithDiscount = subTotalInclTaxWithDiscount; discountAmount = discountAmountInclTax; } else { subTotalWithDiscount = subTotalExclTaxWithDiscount; discountAmount = discountAmountExclTax; } //round if (subTotalWithDiscount < decimal.Zero) subTotalWithDiscount = decimal.Zero; subTotalWithDiscount = Math.Round(subTotalWithDiscount, 2); return error; }
/// <summary> /// Gets an order discount (applied to order total) /// </summary> /// <param name="customer">Customer</param> /// <param name="orderTotal">Order total</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Order discount</returns> public decimal GetOrderTotalDiscount(Customer customer, decimal orderTotal, out Discount appliedDiscount) { decimal discountAmount = decimal.Zero; string customerCouponCode = string.Empty; if (customer != null) customerCouponCode = customer.LastAppliedCouponCode; var allDiscounts = IoC.Resolve<IDiscountService>().GetAllDiscounts(DiscountTypeEnum.AssignedToOrderTotal); var allowedDiscounts = new List<Discount>(); foreach (var discount in allDiscounts) { if (discount.IsActive(customerCouponCode) && discount.DiscountType == DiscountTypeEnum.AssignedToOrderTotal && !allowedDiscounts.ContainsDiscount(discount.Name)) { //discount requirements if (discount.CheckDiscountRequirements(customer) && discount.CheckDiscountLimitations(customer)) { allowedDiscounts.Add(discount); } } } appliedDiscount = IoC.Resolve<IDiscountService>().GetPreferredDiscount(allowedDiscounts, orderTotal); if (appliedDiscount != null) { discountAmount = appliedDiscount.GetDiscountAmount(orderTotal); } if (discountAmount < decimal.Zero) discountAmount = decimal.Zero; discountAmount = Math.Round(discountAmount, 2); return discountAmount; }
/// <summary> /// Gets shopping cart subtotal /// </summary> /// <param name="cart">Cart</param> /// <param name="customer">Customer</param> /// <param name="discountAmount">Applied discount amount</param> /// <param name="appliedDiscount">Applied discount</param> /// <param name="subTotalWithoutDiscount">Sub total (without discount)</param> /// <param name="subTotalWithDiscount">Sub total (with discount)</param> /// <returns>Error</returns> public string GetShoppingCartSubTotal(ShoppingCart cart, Customer customer, out decimal discountAmount, out Discount appliedDiscount, out decimal subTotalWithoutDiscount, out decimal subTotalWithDiscount) { bool includingTax = false; switch (NopContext.Current.TaxDisplayType) { case TaxDisplayTypeEnum.ExcludingTax: includingTax = false; break; case TaxDisplayTypeEnum.IncludingTax: includingTax = true; break; } return GetShoppingCartSubTotal(cart, customer, includingTax, out discountAmount, out appliedDiscount, out subTotalWithoutDiscount, out subTotalWithDiscount); }
public Discount SaveInfo() { //discou tn type DiscountTypeEnum discountType = (DiscountTypeEnum)int.Parse(this.ddlDiscountType.SelectedItem.Value); //requirements DiscountRequirementEnum discountRequirement = (DiscountRequirementEnum)int.Parse(this.ddlDiscountRequirement.SelectedItem.Value); int[] restrictedProductVariantIds = new int[0]; if (discountRequirement == DiscountRequirementEnum.HasAllOfTheseProductVariantsInTheCart || discountRequirement == DiscountRequirementEnum.HasOneOfTheseProductVariantsInTheCart|| discountRequirement == DiscountRequirementEnum.HadPurchasedAllOfTheseProductVariants || discountRequirement == DiscountRequirementEnum.HadPurchasedOneOfTheseProductVariants) restrictedProductVariantIds = ParseListOfRestrictedProductVariants(txtRestrictedProductVariants.Text); decimal requirementSpentAmount = txtRequirementSpentAmount.Value; int requirementBillingCountryIs= int.Parse(this.ddlRequirementBillingCountryIs.SelectedItem.Value); int requirementShippingCountryIs = int.Parse(this.ddlRequirementShippingCountryIs.SelectedItem.Value); //limitation DiscountLimitationEnum discountLimitation = (DiscountLimitationEnum)int.Parse(this.ddlDiscountLimitation.SelectedItem.Value); int limitationTimes = txtLimitationTimes.Value; string name = txtName.Text.Trim(); bool usePercentage= cbUsePercentage.Checked; decimal discountPercentage = txtDiscountPercentage.Value; decimal discountAmount = txtDiscountAmount.Value; bool requiresCouponCode = cbRequiresCouponCode.Checked; string couponCode = txtCouponCode.Text.Trim(); //dates if(!ctrlStartDatePicker.SelectedDate.HasValue) { throw new NopException("Start date is not set"); } DateTime discountStartDate = ctrlStartDatePicker.SelectedDate.Value; if(!ctrlEndDatePicker.SelectedDate.HasValue) { throw new NopException("End date is not set"); } DateTime discountEndDate = ctrlEndDatePicker.SelectedDate.Value; discountStartDate = DateTime.SpecifyKind(discountStartDate, DateTimeKind.Utc); discountEndDate = DateTime.SpecifyKind(discountEndDate, DateTimeKind.Utc); if (discountStartDate.CompareTo(discountEndDate) >= 0) throw new NopException("Start date should be less then expiration date"); if (requiresCouponCode && String.IsNullOrEmpty(couponCode)) { throw new NopException("Discount requires coupon code. Coupon code could not be empty."); } Discount discount = this.DiscountService.GetDiscountById(this.DiscountId); if (discount != null) { discount.DiscountTypeId = (int)discountType; discount.DiscountRequirementId = (int)discountRequirement; discount.RequirementSpentAmount = requirementSpentAmount; discount.RequirementBillingCountryIs = requirementBillingCountryIs; discount.RequirementShippingCountryIs = requirementShippingCountryIs; discount.DiscountLimitationId = (int)discountLimitation; discount.LimitationTimes = limitationTimes; discount.Name = name; discount.UsePercentage = usePercentage; discount.DiscountPercentage = discountPercentage; discount.DiscountAmount = discountAmount; discount.StartDate = discountStartDate; discount.EndDate = discountEndDate; discount.RequiresCouponCode = requiresCouponCode; discount.CouponCode = couponCode; this.DiscountService.UpdateDiscount(discount); //discount requirements foreach (CustomerRole customerRole in discount.CustomerRoles) this.CustomerService.RemoveDiscountFromCustomerRole(customerRole.CustomerRoleId, discount.DiscountId); foreach (int customerRoleId in CustomerRoleMappingControl.SelectedCustomerRoleIds) this.CustomerService.AddDiscountToCustomerRole(customerRoleId, discount.DiscountId); foreach (ProductVariant pv in this.ProductService.GetProductVariantsRestrictedByDiscountId(discount.DiscountId)) this.DiscountService.RemoveDiscountRestriction(pv.ProductVariantId, discount.DiscountId); foreach (int productVariantId in restrictedProductVariantIds) this.DiscountService.AddDiscountRestriction(productVariantId, discount.DiscountId); } else { discount = new Discount() { DiscountTypeId = (int)discountType, DiscountRequirementId = (int)discountRequirement, RequirementSpentAmount = requirementSpentAmount, RequirementBillingCountryIs = requirementBillingCountryIs, RequirementShippingCountryIs = requirementShippingCountryIs, DiscountLimitationId = (int)discountLimitation, LimitationTimes = limitationTimes, Name = name, UsePercentage = usePercentage, DiscountPercentage = discountPercentage, DiscountAmount = discountAmount, StartDate = discountStartDate, EndDate = discountEndDate, RequiresCouponCode = requiresCouponCode, CouponCode = couponCode }; this.DiscountService.InsertDiscount(discount); //discount requirements foreach (int customerRoleId in CustomerRoleMappingControl.SelectedCustomerRoleIds) this.CustomerService.AddDiscountToCustomerRole(customerRoleId, discount.DiscountId); foreach (int productVariantId in restrictedProductVariantIds) this.DiscountService.AddDiscountRestriction(productVariantId, discount.DiscountId); } return discount; }
/// <summary> /// Gets shopping cart total /// </summary> /// <param name="cart">Cart</param> /// <param name="paymentMethodId">Payment method identifier</param> /// <param name="customer">Customer</param> /// <param name="appliedGiftCards">Applied gift cards</param> /// <param name="discountAmount">Applied discount amount</param> /// <param name="appliedDiscount">Applied discount</param> /// <param name="useRewardPoints">A value indicating whether to use reward points</param> /// <param name="redeemedRewardPoints">Reward points to redeem</param> /// <param name="redeemedRewardPointsAmount">Reward points amount in primary store currency to redeem</param> /// <returns>Shopping cart total;Null if shopping cart total couldn't be calculated now</returns> public decimal? GetShoppingCartTotal(ShoppingCart cart, int paymentMethodId, Customer customer, out decimal discountAmount, out Discount appliedDiscount, out List<AppliedGiftCard> appliedGiftCards, bool useRewardPoints, out int redeemedRewardPoints, out decimal redeemedRewardPointsAmount) { string subTotalError = string.Empty; string shippingError = string.Empty; string paymentMethodAdditionalFeeError = string.Empty; string taxError = string.Empty; redeemedRewardPoints = 0; redeemedRewardPointsAmount = decimal.Zero; //subtotal without tax decimal subtotalBase = decimal.Zero; decimal orderSubTotalDiscountAmount = decimal.Zero; Discount orderSubTotalAppliedDiscount = null; decimal subTotalWithoutDiscountBase = decimal.Zero; decimal subTotalWithDiscountBase = decimal.Zero; subTotalError = this.GetShoppingCartSubTotal(cart, customer, false, out orderSubTotalDiscountAmount, out orderSubTotalAppliedDiscount, out subTotalWithoutDiscountBase, out subTotalWithDiscountBase); //subtotal with discount subtotalBase = subTotalWithDiscountBase; //shipping without tax decimal? shoppingCartShipping = IoC.Resolve<IShippingService>().GetShoppingCartShippingTotal(cart, customer, false, ref shippingError); //payment method additional fee without tax decimal paymentMethodAdditionalFeeWithoutTax = decimal.Zero; if (paymentMethodId > 0) { decimal paymentMethodAdditionalFee = IoC.Resolve<IPaymentService>().GetAdditionalHandlingFee(paymentMethodId); paymentMethodAdditionalFeeWithoutTax = IoC.Resolve<ITaxService>().GetPaymentMethodAdditionalFee(paymentMethodAdditionalFee, false, customer, ref paymentMethodAdditionalFeeError); } //tax decimal shoppingCartTax = IoC.Resolve<ITaxService>().GetTaxTotal(cart, paymentMethodId, customer, ref taxError); //order total decimal resultTemp = decimal.Zero; resultTemp += subtotalBase; if (shoppingCartShipping.HasValue) { resultTemp += shoppingCartShipping.Value; } resultTemp += paymentMethodAdditionalFeeWithoutTax; resultTemp += shoppingCartTax; resultTemp = Math.Round(resultTemp, 2); #region Discount discountAmount = GetOrderTotalDiscount(customer, resultTemp, out appliedDiscount); //sub totals with discount if (resultTemp < discountAmount) discountAmount = resultTemp; //reduce subtotal resultTemp -= discountAmount; if (resultTemp < decimal.Zero) resultTemp = decimal.Zero; resultTemp = Math.Round(resultTemp, 2); #endregion #region Gift Cards //let's apply gift cards now (gift cards that can be used) appliedGiftCards = new List<AppliedGiftCard>(); if (!cart.IsRecurring) { //we don't apply gift cards for recurring products var giftCards = GiftCardHelper.GetActiveGiftCards(customer); foreach (var gc in giftCards) { if (resultTemp > decimal.Zero) { decimal remainingAmount = GiftCardHelper.GetGiftCardRemainingAmount(gc); decimal amountCanBeUsed = decimal.Zero; if (resultTemp > remainingAmount) amountCanBeUsed = remainingAmount; else amountCanBeUsed = resultTemp; //reduce subtotal resultTemp -= amountCanBeUsed; AppliedGiftCard appliedGiftCard = new AppliedGiftCard(); appliedGiftCard.GiftCardId = gc.GiftCardId; appliedGiftCard.AmountCanBeUsed = amountCanBeUsed; appliedGiftCards.Add(appliedGiftCard); } } } #endregion if (resultTemp < decimal.Zero) resultTemp = decimal.Zero; resultTemp = Math.Round(resultTemp, 2); decimal? orderTotal = null; if (!String.IsNullOrEmpty(subTotalError) || !String.IsNullOrEmpty(shippingError) || !String.IsNullOrEmpty(paymentMethodAdditionalFeeError) || !String.IsNullOrEmpty(taxError)) { //return null if we have errors orderTotal = null; return orderTotal; } else { //return result if we have no errors orderTotal = resultTemp; } #region Reward points if (IoC.Resolve<IOrderService>().RewardPointsEnabled && useRewardPoints && customer != null) { int rewardPointsBalance = customer.RewardPointsBalance; decimal rewardPointsBalanceAmount = IoC.Resolve<IOrderService>().ConvertRewardPointsToAmount(rewardPointsBalance); if (orderTotal.HasValue && orderTotal.Value > decimal.Zero) { if (orderTotal.Value > rewardPointsBalanceAmount) { redeemedRewardPoints = rewardPointsBalance; redeemedRewardPointsAmount = rewardPointsBalanceAmount; } else { redeemedRewardPointsAmount = orderTotal.Value; redeemedRewardPoints = IoC.Resolve<IOrderService>().ConvertAmountToRewardPoints(redeemedRewardPointsAmount); } } } #endregion if (orderTotal.HasValue) { orderTotal = orderTotal.Value - redeemedRewardPointsAmount; orderTotal = Math.Round(orderTotal.Value, 2); return orderTotal; } else return null; }
/// <summary> /// Gets a shipping discount /// </summary> /// <param name="customer">Customer</param> /// <param name="shippingTotal">Shipping total</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Shipping discount</returns> public static decimal GetShippingDiscount(Customer customer, decimal shippingTotal, out Discount appliedDiscount) { decimal shippingDiscountAmount = decimal.Zero; string customerCouponCode = string.Empty; if (customer != null) customerCouponCode = customer.LastAppliedCouponCode; var allDiscounts = DiscountManager.GetAllDiscounts(DiscountTypeEnum.AssignedToShipping); var allowedDiscounts = new List<Discount>(); foreach (var _discount in allDiscounts) { if (_discount.IsActive(customerCouponCode) && _discount.DiscountType == DiscountTypeEnum.AssignedToShipping && !allowedDiscounts.ContainsDiscount(_discount.Name)) { //discount requirements if (_discount.CheckDiscountRequirements(customer) && _discount.CheckDiscountLimitations(customer)) { allowedDiscounts.Add(_discount); } } } appliedDiscount = DiscountManager.GetPreferredDiscount(allowedDiscounts, shippingTotal); if (appliedDiscount != null) { shippingDiscountAmount = appliedDiscount.GetDiscountAmount(shippingTotal); } if (shippingDiscountAmount < decimal.Zero) shippingDiscountAmount = decimal.Zero; shippingDiscountAmount = Math.Round(shippingDiscountAmount, 2); return shippingDiscountAmount; }
/// <summary> /// Gets shopping cart shipping total /// </summary> /// <param name="cart">Cart</param> /// <param name="customer">Customer</param> /// <param name="includingTax">A value indicating whether calculated price should include tax</param> /// <param name="taxRate">Applied tax rate</param> /// <param name="appliedDiscount">Applied discount</param> /// <param name="error">Error</param> /// <returns>Shipping total</returns> public static decimal? GetShoppingCartShippingTotal(ShoppingCart cart, Customer customer, bool includingTax, out decimal taxRate, out Discount appliedDiscount, ref string error) { decimal? shippingTotalWithoutDiscount = null; decimal? shippingTotalWithDiscount = null; decimal? shippingTotalWithDiscountTaxed = null; appliedDiscount = null; taxRate = decimal.Zero; bool isFreeShipping = IsFreeShipping(cart, customer); if (isFreeShipping) return decimal.Zero; ShippingOption lastShippingOption = null; if (customer != null) { lastShippingOption = customer.LastShippingOption; } if (lastShippingOption != null) { //use last shipping option (get from cache) //we have already discounted cache value shippingTotalWithoutDiscount = lastShippingOption.Rate; //discount decimal discountAmount = GetShippingDiscount(customer, shippingTotalWithoutDiscount.Value, out appliedDiscount); shippingTotalWithDiscount = shippingTotalWithoutDiscount - discountAmount; if (shippingTotalWithDiscount < decimal.Zero) shippingTotalWithDiscount = decimal.Zero; shippingTotalWithDiscount = Math.Round(shippingTotalWithDiscount.Value, 2); } else { //use fixed rate (if possible) Address shippingAddress = null; if (customer != null) { shippingAddress = customer.ShippingAddress; } var ShipmentPackage = CreateShipmentPackage(cart, customer, shippingAddress); var shippingRateComputationMethods = ShippingRateComputationMethodManager.GetAllShippingRateComputationMethods(false); if (shippingRateComputationMethods.Count == 0) throw new NopException("Shipping rate computation method could not be loaded"); if (shippingRateComputationMethods.Count == 1) { var shippingRateComputationMethod = shippingRateComputationMethods[0]; var iShippingRateComputationMethod = Activator.CreateInstance(Type.GetType(shippingRateComputationMethod.ClassName)) as IShippingRateComputationMethod; decimal? fixedRate = iShippingRateComputationMethod.GetFixedRate(ShipmentPackage); if (fixedRate.HasValue) { decimal additionalShippingCharge = GetShoppingCartAdditionalShippingCharge(cart, customer); shippingTotalWithoutDiscount = fixedRate.Value + additionalShippingCharge; shippingTotalWithoutDiscount = Math.Round(shippingTotalWithoutDiscount.Value, 2); decimal shippingTotalDiscount = GetShippingDiscount(customer, shippingTotalWithoutDiscount.Value, out appliedDiscount); shippingTotalWithDiscount = shippingTotalWithoutDiscount.Value - shippingTotalDiscount; if (shippingTotalWithDiscount.Value < decimal.Zero) shippingTotalWithDiscount = decimal.Zero; } } } if (!shippingTotalWithDiscount.HasValue) { error = "Shipping total could not be calculated"; } else { shippingTotalWithDiscountTaxed = TaxManager.GetShippingPrice(shippingTotalWithDiscount.Value, includingTax, customer, out taxRate, ref error); shippingTotalWithDiscountTaxed = Math.Round(shippingTotalWithDiscountTaxed.Value, 2); } return shippingTotalWithDiscountTaxed; }
/// <summary> /// Gets discount amount /// </summary> /// <param name="shoppingCartItem">The shopping cart item</param> /// <param name="customer">The customer</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Discount amount</returns> public static decimal GetDiscountAmount(ShoppingCartItem shoppingCartItem, Customer customer, out Discount appliedDiscount) { appliedDiscount = null; decimal discountAmount = decimal.Zero; var productVariant = shoppingCartItem.ProductVariant; if (productVariant != null) { decimal attributesTotalPrice = decimal.Zero; var pvaValues = ProductAttributeHelper.ParseProductVariantAttributeValues(shoppingCartItem.AttributesXml); foreach (var pvaValue in pvaValues) { attributesTotalPrice += pvaValue.PriceAdjustment; } decimal productVariantDiscountAmount = GetDiscountAmount(productVariant, customer, attributesTotalPrice, out appliedDiscount); discountAmount = productVariantDiscountAmount * shoppingCartItem.Quantity; } discountAmount = Math.Round(discountAmount, 2); return discountAmount; }
/// <summary> /// Gets discount amount /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="customer">The customer</param> /// <param name="additionalCharge">Additional charge</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Discount amount</returns> public static decimal GetDiscountAmount(ProductVariant productVariant, Customer customer, decimal additionalCharge, out Discount appliedDiscount) { decimal appliedDiscountAmount = decimal.Zero; //we don't apply discounts to products with price entered by a customer if (productVariant.CustomerEntersPrice) { appliedDiscount = null; return appliedDiscountAmount; } appliedDiscount = GetPreferredDiscount(productVariant, customer, additionalCharge); if (appliedDiscount != null) { decimal finalPriceWithoutDiscount = GetFinalPrice(productVariant, customer, additionalCharge, false); appliedDiscountAmount = appliedDiscount.GetDiscountAmount(finalPriceWithoutDiscount); } return appliedDiscountAmount; }
/// <summary> /// Gets discount amount /// </summary> /// <param name="productVariant">Product variant</param> /// <param name="customer">The customer</param> /// <param name="additionalCharge">Additional charge</param> /// <param name="appliedDiscount">Applied discount</param> /// <returns>Discount amount</returns> public static decimal GetDiscountAmount(ProductVariant productVariant, Customer customer, decimal additionalCharge, out Discount appliedDiscount) { return GetDiscountAmount(productVariant, customer, additionalCharge, 1, out appliedDiscount); }