Skip to content

Commit

Permalink
Update ApplyRewards()
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-dudarev committed Dec 5, 2024
1 parent d13f091 commit 437c42d
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 43 deletions.
54 changes: 28 additions & 26 deletions src/VirtoCommerce.XCart.Core/Extensions/RewardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using VirtoCommerce.CartModule.Core.Model;
using VirtoCommerce.CoreModule.Core.Common;
using VirtoCommerce.CoreModule.Core.Currency;
using VirtoCommerce.MarketingModule.Core.Model.Promotions;
using VirtoCommerce.PaymentModule.Core.Model;
using VirtoCommerce.Platform.Core.Common;
Expand All @@ -13,19 +14,19 @@ namespace VirtoCommerce.XCart.Core.Extensions
{
public static class RewardExtensions
{
public static void ApplyRewards(this PaymentMethod paymentMethod, ICollection<PromotionReward> rewards)
public static void ApplyRewards(this PaymentMethod paymentMethod, Currency currency, ICollection<PromotionReward> rewards)
=> paymentMethod.DiscountAmount = rewards
.Where(r => r.IsValid)
.OfType<PaymentReward>()
.Where(r => r.PaymentMethod.IsNullOrEmpty() || r.PaymentMethod.EqualsInvariant(paymentMethod.Code))
.Sum(reward => reward.GetRewardAmount(paymentMethod.Price - paymentMethod.DiscountAmount, 1));
.Sum(reward => reward.GetTotalAmount(paymentMethod.Price - paymentMethod.DiscountAmount, 1, currency));

public static void ApplyRewards(this ShippingRate shippingRate, ICollection<PromotionReward> rewards)
public static void ApplyRewards(this ShippingRate shippingRate, Currency currency, ICollection<PromotionReward> rewards)
=> shippingRate.DiscountAmount = rewards
.Where(r => r.IsValid)
.OfType<ShipmentReward>()
.Where(r => r.ShippingMethod.IsNullOrEmpty() || shippingRate.ShippingMethod != null && r.ShippingMethod.EqualsInvariant(shippingRate.ShippingMethod.Code))
.Sum(reward => reward.GetRewardAmount(shippingRate.Rate, 1));
.Sum(reward => reward.GetTotalAmount(shippingRate.Rate, 1, currency));

public static void ApplyRewards(this CartAggregate aggregate, ICollection<PromotionReward> rewards)
{
Expand All @@ -46,14 +47,15 @@ public static void ApplyRewards(this CartAggregate aggregate, ICollection<Promot
ApplyCartRewardsInternal(aggregate, rewards);
}

public static void ApplyRewards(this LineItem lineItem, string currency, IEnumerable<CatalogItemAmountReward> rewards)
public static void ApplyRewards(this LineItem lineItem, Currency currency, IEnumerable<CatalogItemAmountReward> rewards)
{
var lineItemRewards = rewards
.Where(r => r.IsValid)
.Where(r => r.ProductId.IsNullOrEmpty() || r.ProductId.EqualsInvariant(lineItem.ProductId));

lineItem.Discounts?.Clear();
lineItem.DiscountAmount = Math.Max(0, lineItem.ListPrice - lineItem.SalePrice);
lineItem.IsDiscountAmountRounded = true;

if (lineItem.Quantity == 0)
{
Expand All @@ -65,14 +67,14 @@ public static void ApplyRewards(this LineItem lineItem, string currency, IEnumer
var discount = new Discount
{
Coupon = reward.Coupon,
Currency = currency,
Currency = currency.Code,
PromotionId = reward.PromotionId ?? reward.Promotion?.Id,
Name = reward.Promotion?.Name,
Description = reward.Promotion?.Description,
DiscountAmount = reward.GetRewardAmount(lineItem.ListPrice - lineItem.DiscountAmount, lineItem.Quantity),
DiscountAmount = reward.GetAmountPerItem(lineItem.ListPrice - lineItem.DiscountAmount, lineItem.Quantity, currency),
};

// Pass invalid discounts
// Skip invalid discounts
if (discount.DiscountAmount <= 0)
{
continue;
Expand All @@ -81,10 +83,11 @@ public static void ApplyRewards(this LineItem lineItem, string currency, IEnumer
lineItem.Discounts ??= new List<Discount>();
lineItem.Discounts.Add(discount);
lineItem.DiscountAmount += discount.DiscountAmount;
lineItem.IsDiscountAmountRounded &= reward.RoundAmountPerItem;
}
}

public static void ApplyRewards(this Shipment shipment, string currency, IEnumerable<ShipmentReward> rewards)
public static void ApplyRewards(this Shipment shipment, Currency currency, IEnumerable<ShipmentReward> rewards)
{
var shipmentRewards = rewards
.Where(r => r.IsValid)
Expand All @@ -98,11 +101,11 @@ public static void ApplyRewards(this Shipment shipment, string currency, IEnumer
var discount = new Discount
{
Coupon = reward.Coupon,
Currency = currency,
Currency = currency.Code,
PromotionId = reward.PromotionId ?? reward.Promotion?.Id,
Name = reward.Promotion?.Name,
Description = reward.Promotion?.Description,
DiscountAmount = reward.GetRewardAmount(shipment.Price - shipment.DiscountAmount, 1),
DiscountAmount = reward.GetTotalAmount(shipment.Price - shipment.DiscountAmount, 1, currency),
};

// Pass invalid discounts
Expand All @@ -116,7 +119,7 @@ public static void ApplyRewards(this Shipment shipment, string currency, IEnumer
}
}

public static void ApplyRewards(this Payment payment, string currency, IEnumerable<PaymentReward> rewards)
public static void ApplyRewards(this Payment payment, Currency currency, IEnumerable<PaymentReward> rewards)
{
var paymentRewards = rewards
.Where(r => r.IsValid)
Expand All @@ -130,11 +133,11 @@ public static void ApplyRewards(this Payment payment, string currency, IEnumerab
var discount = new Discount
{
Coupon = reward.Coupon,
Currency = currency,
Currency = currency.Code,
PromotionId = reward.PromotionId ?? reward.Promotion?.Id,
Name = reward.Promotion?.Name,
Description = reward.Promotion?.Description,
DiscountAmount = reward.GetRewardAmount(payment.Price - payment.DiscountAmount, 1),
DiscountAmount = reward.GetTotalAmount(payment.Price - payment.DiscountAmount, 1, currency),
};

// Pass invalid discounts
Expand Down Expand Up @@ -167,13 +170,12 @@ public static async Task ApplyRewardsAsync(this CartAggregate aggregate, ICollec
// automatically add gift rewards to line items if the setting is enabled
if (aggregate.IsSelectedForCheckout)
{
var availableGifts = await aggregate.GetAvailableGiftsAsync(rewards);
var availableGifts = (await aggregate.GetAvailableGiftsAsync(rewards)).ToList();

if (availableGifts.Any())
{
var newGiftItems = availableGifts.Where(x => !x.HasLineItem).ToList(); //get new items
var newGiftItemIds = newGiftItems.Select(x => x.Id).ToList();
await aggregate.AddGiftItemsAsync(newGiftItemIds, availableGifts.ToList()); //add new items to cart
var newGiftItemIds = availableGifts.Where(x => !x.HasLineItem).Select(x => x.Id).ToList();
await aggregate.AddGiftItemsAsync(newGiftItemIds, availableGifts); //add new items to cart
}
}

Expand All @@ -184,22 +186,22 @@ private static void ApplyCartRewardsInternal(CartAggregate aggregate, ICollectio
{
var shoppingCart = aggregate.Cart;

var lineItemRewards = rewards.OfType<CatalogItemAmountReward>();
foreach (var lineItem in aggregate.LineItems ?? Enumerable.Empty<LineItem>())
var lineItemRewards = rewards.OfType<CatalogItemAmountReward>().ToList();
foreach (var lineItem in aggregate.LineItems ?? [])
{
lineItem.ApplyRewards(shoppingCart.Currency, lineItemRewards);
lineItem.ApplyRewards(aggregate.Currency, lineItemRewards);
}

var shipmentRewards = rewards.OfType<ShipmentReward>();
var shipmentRewards = rewards.OfType<ShipmentReward>().ToList();
foreach (var shipment in shoppingCart.Shipments ?? Enumerable.Empty<Shipment>())
{
shipment.ApplyRewards(shoppingCart.Currency, shipmentRewards);
shipment.ApplyRewards(aggregate.Currency, shipmentRewards);
}

var paymentRewards = rewards.OfType<PaymentReward>();
var paymentRewards = rewards.OfType<PaymentReward>().ToList();
foreach (var payment in shoppingCart.Payments ?? Enumerable.Empty<Payment>())
{
payment.ApplyRewards(shoppingCart.Currency, paymentRewards);
payment.ApplyRewards(aggregate.Currency, paymentRewards);
}

var subTotalExcludeDiscount = shoppingCart.Items.Where(li => li.SelectedForCheckout).Sum(li => (li.ListPrice - li.DiscountAmount) * li.Quantity);
Expand All @@ -217,7 +219,7 @@ private static void ApplyCartRewardsInternal(CartAggregate aggregate, ICollectio
PromotionId = reward.PromotionId ?? reward.Promotion?.Id,
Name = reward.Promotion?.Name,
Description = reward.Promotion?.Description,
DiscountAmount = reward.GetRewardAmount(subTotalExcludeDiscount, 1),
DiscountAmount = reward.GetTotalAmount(subTotalExcludeDiscount, 1, aggregate.Currency),
};

shoppingCart.Discounts ??= new List<Discount>();
Expand Down
3 changes: 3 additions & 0 deletions src/VirtoCommerce.XCart.Core/Schemas/LineItemType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ public LineItemType(
Field<NonNullGraphType<MoneyType>>("listPriceWithTax",
"List price with tax",
resolve: context => context.Source.ListPriceWithTax.ToMoney(context.GetCart().Currency));
Field<NonNullGraphType<BooleanGraphType>>("showPlacedPrice",
"Indicates whether the PlacedPrice should be visible to the customer",
resolve: context => context.Source.IsDiscountAmountRounded);
Field<NonNullGraphType<MoneyType>>("placedPrice",
"Placed price",
resolve: context => context.Source.PlacedPrice.ToMoney(context.GetCart().Currency));
Expand Down
6 changes: 3 additions & 3 deletions src/VirtoCommerce.XCart.Core/VirtoCommerce.XCart.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="VirtoCommerce.Xapi.Core" Version="3.809.0" />
<PackageReference Include="VirtoCommerce.XCatalog.Core" Version="3.818.0" />
<PackageReference Include="VirtoCommerce.CartModule.Core" Version="3.820.0" />
<PackageReference Include="VirtoCommerce.XCatalog.Core" Version="3.819.0-alpha.49-vcst-2305" />
<PackageReference Include="VirtoCommerce.CartModule.Core" Version="3.822.0-alpha.714-vcst-2305" />
<PackageReference Include="VirtoCommerce.PaymentModule.Core" Version="3.804.0" />
<PackageReference Include="VirtoCommerce.ShippingModule.Core" Version="3.802.0" />
<PackageReference Include="VirtoCommerce.InventoryModule.Core" Version="3.805.0" />
<PackageReference Include="VirtoCommerce.MarketingModule.Core" Version="3.812.0" />
<PackageReference Include="VirtoCommerce.MarketingModule.Core" Version="3.815.0-alpha.873-vcst-2305" />
<PackageReference Include="VirtoCommerce.PricingModule.Core" Version="3.809.0" />
</ItemGroup>
</Project>
12 changes: 6 additions & 6 deletions src/VirtoCommerce.XCart.Data/Services/CartAvailMethodsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task<IEnumerable<ShippingRate>> GetAvailableShippingRatesAsync(Cart
{
if (cartAggregate == null)
{
return Enumerable.Empty<ShippingRate>();
return [];
}

//Request available shipping rates
Expand All @@ -75,7 +75,7 @@ public async Task<IEnumerable<ShippingRate>> GetAvailableShippingRatesAsync(Cart

if (availableShippingRates.IsNullOrEmpty())
{
return Enumerable.Empty<ShippingRate>();
return [];
}

//Evaluate promotions cart and apply rewards for available shipping methods
Expand All @@ -90,7 +90,7 @@ public async Task<IEnumerable<ShippingRate>> GetAvailableShippingRatesAsync(Cart
var promoEvalResult = await cartAggregate.EvaluatePromotionsAsync(evalContextCartMap.PromotionEvaluationContext);
foreach (var shippingRate in availableShippingRates)
{
shippingRate.ApplyRewards(promoEvalResult.Rewards);
shippingRate.ApplyRewards(cartAggregate.Currency, promoEvalResult.Rewards);
}

var taxProvider = await GetActiveTaxProviderAsync(cartAggregate);
Expand All @@ -115,7 +115,7 @@ public async Task<IEnumerable<PaymentMethod>> GetAvailablePaymentMethodsAsync(Ca
{
if (cartAggregate == null)
{
return Enumerable.Empty<PaymentMethod>();
return [];
}

var criteria = new PaymentMethodsSearchCriteria
Expand All @@ -128,7 +128,7 @@ public async Task<IEnumerable<PaymentMethod>> GetAvailablePaymentMethodsAsync(Ca
var result = await _paymentMethodsSearchService.SearchAsync(criteria);
if (result.Results.IsNullOrEmpty())
{
return Enumerable.Empty<PaymentMethod>();
return [];
}

var evalContext = AbstractTypeFactory<PromotionEvaluationContext>.TryCreateInstance();
Expand All @@ -143,7 +143,7 @@ public async Task<IEnumerable<PaymentMethod>> GetAvailablePaymentMethodsAsync(Ca

foreach (var paymentMethod in result.Results)
{
paymentMethod.ApplyRewards(promoResult.Rewards);
paymentMethod.ApplyRewards(cartAggregate.Currency, promoResult.Rewards);
}

//Evaluate taxes for available payments
Expand Down
6 changes: 3 additions & 3 deletions src/VirtoCommerce.XCart.Web/module.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

<platformVersion>3.867.0</platformVersion>
<dependencies>
<dependency id="VirtoCommerce.Cart" version="3.820.0" />
<dependency id="VirtoCommerce.Cart" version="3.822.0-alpha.714-vcst-2305" />
<dependency id="VirtoCommerce.Inventory" version="3.805.0" />
<dependency id="VirtoCommerce.Marketing" version="3.812.0" />
<dependency id="VirtoCommerce.Marketing" version="3.815.0-alpha.873-vcst-2305" />
<dependency id="VirtoCommerce.Payment" version="3.804.0" />
<dependency id="VirtoCommerce.Pricing" version="3.809.0" />
<dependency id="VirtoCommerce.Shipping" version="3.802.0" />
<dependency id="VirtoCommerce.Xapi" version="3.809.0" />
<dependency id="VirtoCommerce.XCatalog" version="3.818.0" />
<dependency id="VirtoCommerce.XCatalog" version="3.819.0-alpha.49-vcst-2305" />
</dependencies>

<title>Cart Experience API</title>
Expand Down
Loading

0 comments on commit 437c42d

Please sign in to comment.