Skip to content

Commit

Permalink
#5757 Fix reducing reward points of the order (#5885)
Browse files Browse the repository at this point in the history
  • Loading branch information
holydk committed Oct 8, 2021
1 parent d87e107 commit 672ccf5
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/Libraries/Nop.Services/Orders/OrderProcessingService.cs
Expand Up @@ -953,31 +953,26 @@ protected virtual async Task ReduceRewardPointsAsync(Order order)
if (order is null)
throw new ArgumentNullException(nameof(order));

var customer = await _customerService.GetCustomerByIdAsync(order.CustomerId);

var totalForRewardPoints = _orderTotalCalculationService
.CalculateApplicableOrderTotalForRewardPoints(order.OrderShippingInclTax, order.OrderTotal);
var points = totalForRewardPoints > decimal.Zero ?
await _orderTotalCalculationService.CalculateRewardPointsAsync(customer, totalForRewardPoints) : 0;
if (points == 0)
return;

//ensure that reward points were already earned for this order before
if (!order.RewardPointsHistoryEntryId.HasValue)
return;

//get appropriate history entry
var rewardPointsHistoryEntry = await _rewardPointService.GetRewardPointsHistoryEntryByIdAsync(order.RewardPointsHistoryEntryId.Value);
if (rewardPointsHistoryEntry != null && rewardPointsHistoryEntry.CreatedOnUtc > DateTime.UtcNow)
{
//just delete the upcoming entry (points were not granted yet)
await _rewardPointService.DeleteRewardPointsHistoryEntryAsync(rewardPointsHistoryEntry);
}
else
if (rewardPointsHistoryEntry != null)
{
//or reduce reward points if the entry already exists
await _rewardPointService.AddRewardPointsHistoryEntryAsync(customer, -points, order.StoreId,
string.Format(await _localizationService.GetResourceAsync("RewardPoints.Message.ReducedForOrder"), order.CustomOrderNumber));
if (rewardPointsHistoryEntry.CreatedOnUtc > DateTime.UtcNow)
{
//just delete the upcoming entry (points were not granted yet)
await _rewardPointService.DeleteRewardPointsHistoryEntryAsync(rewardPointsHistoryEntry);
}
{
var customer = await _customerService.GetCustomerByIdAsync(order.CustomerId);

//or reduce reward points if the entry already exists
await _rewardPointService.AddRewardPointsHistoryEntryAsync(customer, -rewardPointsHistoryEntry.Points, order.StoreId,
string.Format(await _localizationService.GetResourceAsync("RewardPoints.Message.ReducedForOrder"), order.CustomOrderNumber));
}
}
}

Expand Down

0 comments on commit 672ccf5

Please sign in to comment.