Skip to content

Commit

Permalink
Merge branch 'dev' into fix/VCST-2466
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev authored Dec 19, 2024
2 parents c38b75d + 459456d commit a9310b0
Show file tree
Hide file tree
Showing 19 changed files with 256 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<!-- These properties will be shared for all projects -->
<PropertyGroup>
<VersionPrefix>3.820.0</VersionPrefix>
<VersionPrefix>3.822.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<VersionSuffix Condition=" '$(VersionSuffix)' != '' AND '$(BuildNumber)' != '' ">$(VersionSuffix)-$(BuildNumber)</VersionSuffix>
</PropertyGroup>
Expand Down
56 changes: 29 additions & 27 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())
if (availableGifts.Count > 0)
{
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
9 changes: 9 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,15 @@ public LineItemType(
Field<NonNullGraphType<MoneyType>>("listPriceWithTax",
"List price with tax",
resolve: context => context.Source.ListPriceWithTax.ToMoney(context.GetCart().Currency));
Field<NonNullGraphType<MoneyType>>("listTotal",
"List total",
resolve: context => context.Source.ListTotal.ToMoney(context.GetCart().Currency));
Field<NonNullGraphType<MoneyType>>("listTotalWithTax",
"List total with tax",
resolve: context => context.Source.ListTotalWithTax.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
12 changes: 6 additions & 6 deletions src/VirtoCommerce.XCart.Core/VirtoCommerce.XCart.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
<ItemGroup>
<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.815.0" />
<PackageReference Include="VirtoCommerce.XCatalog.Core" Version="3.818.0" />
<PackageReference Include="VirtoCommerce.CartModule.Core" Version="3.822.0" />
<PackageReference Include="VirtoCommerce.PaymentModule.Core" Version="3.804.0" />
<PackageReference Include="VirtoCommerce.ShippingModule.Core" Version="3.802.0" />
<PackageReference Include="VirtoCommerce.CartModule.Core" Version="3.823.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" />
<PackageReference Include="VirtoCommerce.PaymentModule.Core" Version="3.804.0" />
<PackageReference Include="VirtoCommerce.PricingModule.Core" Version="3.809.0" />
<PackageReference Include="VirtoCommerce.ShippingModule.Core" Version="3.802.0" />
<PackageReference Include="VirtoCommerce.Xapi.Core" Version="3.815.0" />
<PackageReference Include="VirtoCommerce.XCatalog.Core" Version="3.820.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
8 changes: 8 additions & 0 deletions src/VirtoCommerce.XCart.Web/Localizations/de.XCart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"XCart:create": "xCart-bezogene Daten erstellen",
"XCart:read": "xCart-bezogene Daten anzeigen",
"XCart:update": "xCart-bezogene Daten aktualisieren",
"XCart:delete": "xCart-bezogene Daten löschen"
}
}
8 changes: 4 additions & 4 deletions src/VirtoCommerce.XCart.Web/Localizations/en.XCart.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"permissions": {
"XCart:create": "Create XCart related data",
"XCart:read": "View XCart related data",
"XCart:update": "Update XCart related data",
"XCart:delete": "Delete XCart related data"
"XCart:create": "Create xCart related data",
"XCart:read": "View xCart related data",
"XCart:update": "Update xCart related data",
"XCart:delete": "Delete xCart related data"
}
}
8 changes: 8 additions & 0 deletions src/VirtoCommerce.XCart.Web/Localizations/es.XCart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"XCart:create": "Crear datos relacionados con xCart",
"XCart:read": "Ver datos relacionados con xCart",
"XCart:update": "Actualizar datos relacionados con xCart",
"XCart:delete": "Eliminar datos relacionados con xCart"
}
}
8 changes: 8 additions & 0 deletions src/VirtoCommerce.XCart.Web/Localizations/fr.XCart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"XCart:create": "Créer des données liées à xCart",
"XCart:read": "Afficher les données liées à xCart",
"XCart:update": "Mettre à jour les données liées à xCart",
"XCart:delete": "Supprimer les données liées à xCart"
}
}
8 changes: 8 additions & 0 deletions src/VirtoCommerce.XCart.Web/Localizations/it.XCart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"XCart:create": "Creare dati relativi a xCart",
"XCart:read": "Visualizzare dati relativi a xCart",
"XCart:update": "Aggiornare dati relativi a xCart",
"XCart:delete": "Eliminare dati relativi a xCart"
}
}
8 changes: 8 additions & 0 deletions src/VirtoCommerce.XCart.Web/Localizations/ja.XCart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"XCart:create": "xCart 関連データを作成",
"XCart:read": "xCart 関連データを表示",
"XCart:update": "xCart 関連データを更新",
"XCart:delete": "xCart 関連データを削除"
}
}
8 changes: 8 additions & 0 deletions src/VirtoCommerce.XCart.Web/Localizations/pl.XCart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"XCart:create": "Tworzenie danych powiązanych z xCart",
"XCart:read": "Wyświetlanie danych powiązanych z xCart",
"XCart:update": "Aktualizowanie danych powiązanych z xCart",
"XCart:delete": "Usuwanie danych powiązanych z xCart"
}
}
8 changes: 8 additions & 0 deletions src/VirtoCommerce.XCart.Web/Localizations/pt.XCart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"XCart:create": "Criar dados relacionados ao xCart",
"XCart:read": "Visualizar dados relacionados ao xCart",
"XCart:update": "Atualizar dados relacionados ao xCart",
"XCart:delete": "Excluir dados relacionados ao xCart"
}
}
8 changes: 8 additions & 0 deletions src/VirtoCommerce.XCart.Web/Localizations/ru.XCart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"XCart:create": "Создать данные, связанные с xCart",
"XCart:read": "Просмотреть данные, связанные с xCart",
"XCart:update": "Обновить данные, связанные с xCart",
"XCart:delete": "Удалить данные, связанные с xCart"
}
}
8 changes: 8 additions & 0 deletions src/VirtoCommerce.XCart.Web/Localizations/zh.XCart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"XCart:create": "创建与 xCart 相关的数据",
"XCart:read": "查看与 xCart 相关的数据",
"XCart:update": "更新与 xCart 相关的数据",
"XCart:delete": "删除与 xCart 相关的数据"
}
}
8 changes: 4 additions & 4 deletions src/VirtoCommerce.XCart.Web/module.manifest
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<module>
<id>VirtoCommerce.XCart</id>
<version>3.820.0</version>
<version>3.822.0</version>
<version-tag></version-tag>

<platformVersion>3.867.0</platformVersion>
<dependencies>
<dependency id="VirtoCommerce.Cart" version="3.822.0" />
<dependency id="VirtoCommerce.Cart" version="3.823.0" />
<dependency id="VirtoCommerce.Inventory" version="3.805.0" />
<dependency id="VirtoCommerce.Marketing" version="3.812.0" />
<dependency id="VirtoCommerce.Marketing" version="3.815.0" />
<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.815.0" />
<dependency id="VirtoCommerce.XCatalog" version="3.818.0" />
<dependency id="VirtoCommerce.XCatalog" version="3.820.0" />
</dependencies>

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

0 comments on commit a9310b0

Please sign in to comment.