Skip to content

Commit

Permalink
#1088: Bug with special product price when ordering (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
s04v authored Mar 7, 2024
1 parent 444c7a5 commit 484cb93
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public CheckoutItemVm(ICurrencyService currencyService)

public string ProductImage { get; set; }

public CalculatedProductPrice CalculatedProductPrice { get; set; }

public decimal ProductPrice { get; set; }

public string ProductPriceString => _currencyService.FormatCurrency(ProductPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Module.Catalog.Services;
using SimplCommerce.Module.Checkouts.Areas.Checkouts.ViewModels;
using SimplCommerce.Module.Checkouts.Models;
using SimplCommerce.Module.Core.Models;
Expand All @@ -26,6 +27,7 @@ public class CheckoutService : ICheckoutService
private readonly IMediaService _mediaService;
private readonly ICouponService _couponService;
private readonly bool _isProductPriceIncludeTax;
private readonly IProductPricingService _productPriceService;

public CheckoutService(
IRepositoryWithTypedId<Checkout, Guid> checkoutRepository,
Expand All @@ -36,7 +38,8 @@ public CheckoutService(
ICurrencyService currencyService,
IMediaService mediaService,
ICouponService couponService,
IConfiguration config)
IConfiguration config,
IProductPricingService productPriceService)
{
_checkoutRepository = checkoutRepository;
_userAddressRepository = userAddressRepository;
Expand All @@ -47,6 +50,7 @@ public CheckoutService(
_mediaService = mediaService;
_couponService = couponService;
_isProductPriceIncludeTax = config.GetValue<bool>("Catalog.IsProductPriceIncludeTax");
_productPriceService = productPriceService;
}

public async Task<Checkout> Create(long customerId, long createdById, IList<CartItemToCheckoutVm> cartItems, string couponCode)
Expand Down Expand Up @@ -182,6 +186,7 @@ public async Task<CheckoutVm> GetCheckoutDetails(Guid checkoutId)
Id = x.Id,
ProductId = x.ProductId,
ProductName = x.Product.Name,
CalculatedProductPrice = _productPriceService.CalculateProductPrice(x.Product),
ProductPrice = x.Product.Price,
ProductStockQuantity = x.Product.StockQuantity,
ProductStockTrackingIsEnabled = x.Product.StockTrackingIsEnabled,
Expand All @@ -191,7 +196,7 @@ public async Task<CheckoutVm> GetCheckoutDetails(Guid checkoutId)
VariationOptions = CheckoutItemVm.GetVariationOption(x.Product)
}).ToList();

checkoutVm.SubTotal = checkoutVm.Items.Sum(x => x.Quantity * x.ProductPrice);
checkoutVm.SubTotal = checkoutVm.Items.Sum(x => x.Quantity * (x.CalculatedProductPrice.OldPrice ?? x.ProductPrice));
if (!string.IsNullOrWhiteSpace(checkoutVm.CouponCode))
{
var cartInfoForCoupon = new CartInfoForCoupon
Expand All @@ -209,6 +214,10 @@ public async Task<CheckoutVm> GetCheckoutDetails(Guid checkoutId)
}
}

checkoutVm.Discount += checkoutVm.Items
.Where(x => x.CalculatedProductPrice.OldPrice.HasValue)
.Sum(x => x.Quantity * (x.CalculatedProductPrice.OldPrice.Value - x.CalculatedProductPrice.Price));

return checkoutVm;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@
}
</td>
<td class="text-center">@item.Quantity</td>
<td class="text-right">@item.ProductPriceString</td>
<td class="text-right">
@if(item.CalculatedProductPrice.OldPrice.HasValue)
{
<text>@item.CalculatedProductPrice.OldPriceString</text>
}
else
{
<text>@item.CalculatedProductPrice.PriceString</text>
}
</td>
</tr>
}
<tr>
Expand Down
18 changes: 15 additions & 3 deletions src/Modules/SimplCommerce.Module.Orders/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using SimplCommerce.Module.Checkouts.Services;
using SimplCommerce.Module.Checkouts.Models;
using SimplCommerce.Module.Checkouts.Areas.Checkouts.ViewModels;
using SimplCommerce.Module.Catalog.Services;

namespace SimplCommerce.Module.Orders.Services
{
Expand All @@ -33,6 +34,7 @@ public class OrderService : IOrderService
private readonly IShippingPriceService _shippingPriceService;
private readonly IRepository<UserAddress> _userAddressRepository;
private readonly IMediator _mediator;
private readonly IProductPricingService _productPricingService;

public OrderService(IRepository<Order> orderRepository,
ICouponService couponService,
Expand All @@ -43,7 +45,8 @@ public OrderService(IRepository<Order> orderRepository,
IRepositoryWithTypedId<Checkout, Guid> checkoutRepository,
IShippingPriceService shippingPriceService,
IRepository<UserAddress> userAddressRepository,
IMediator mediator)
IMediator mediator,
IProductPricingService productPricingService)
{
_orderRepository = orderRepository;
_couponService = couponService;
Expand All @@ -55,6 +58,7 @@ public OrderService(IRepository<Order> orderRepository,
_shippingPriceService = shippingPriceService;
_userAddressRepository = userAddressRepository;
_mediator = mediator;
_productPricingService = productPricingService;
}

public async Task<Result<Order>> CreateOrder(Guid checkoutId, string paymentMethod, decimal paymentFeeAmount, OrderStatus orderStatus = OrderStatus.New)
Expand Down Expand Up @@ -220,7 +224,10 @@ public async Task<Result<Order>> CreateOrder(Guid checkoutId, string paymentMeth
}

var taxPercent = await _taxService.GetTaxPercent(checkoutItem.Product.TaxClassId, shippingAddress.CountryId, shippingAddress.StateOrProvinceId, shippingAddress.ZipCode);
var productPrice = checkoutItem.Product.Price;

var calculatedProductPrice = _productPricingService.CalculateProductPrice(checkoutItem.Product);

var productPrice = calculatedProductPrice.OldPrice ?? calculatedProductPrice.Price;
if (checkout.IsProductPriceIncludeTax)
{
productPrice = productPrice / (1 + (taxPercent / 100));
Expand All @@ -241,6 +248,11 @@ public async Task<Result<Order>> CreateOrder(Guid checkoutId, string paymentMeth
orderItem.DiscountAmount = discountedItem.DiscountAmount;
}

if (calculatedProductPrice.OldPrice.HasValue)
{
orderItem.DiscountAmount += orderItem.Quantity * (calculatedProductPrice.OldPrice.Value - calculatedProductPrice.Price);
}

order.AddOrderItem(orderItem);
if (checkoutItem.Product.StockTrackingIsEnabled)
{
Expand All @@ -252,7 +264,7 @@ public async Task<Result<Order>> CreateOrder(Guid checkoutId, string paymentMeth
order.OrderNote = checkout.OrderNote;
order.CouponCode = checkingDiscountResult.CouponCode;
order.CouponRuleName = checkout.CouponRuleName;
order.DiscountAmount = checkingDiscountResult.DiscountAmount;
order.DiscountAmount = checkingDiscountResult.DiscountAmount + order.OrderItems.Sum(x => x.DiscountAmount);
order.ShippingFeeAmount = shippingMethod.Price;
order.ShippingMethod = shippingMethod.Name;
order.TaxAmount = order.OrderItems.Sum(x => x.TaxAmount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public async Task<IActionResult> AddToCartResult(long productId)
model.ProductName = addedProduct.ProductName;
model.ProductImage = addedProduct.ProductImage;
model.ProductPrice = addedProduct.ProductPrice;
model.CalculatedProductPrice = addedProduct.CalculatedProductPrice;
model.Quantity = addedProduct.Quantity;

return PartialView(model);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SimplCommerce.Module.Core.Services;
using SimplCommerce.Module.Catalog.Models;
using SimplCommerce.Module.Core.Services;

namespace SimplCommerce.Module.ShoppingCart.Areas.ShoppingCart.ViewModels
{
Expand All @@ -15,6 +16,8 @@ public AddToCartResultVm(ICurrencyService currencyService)

public string ProductImage { get; set; }

public CalculatedProductPrice CalculatedProductPrice { get; set; }

public decimal ProductPrice { get; set; }

public string VariationName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public CartItemVm(ICurrencyService currencyService)

public string ProductImage { get; set; }

public CalculatedProductPrice CalculatedProductPrice { get; set; }

public decimal ProductPrice { get; set; }

public string ProductPriceString => _currencyService.FormatCurrency(ProductPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<h6>@Model.VariationName</h6>
</div>
<div class="col-md-3">
@Model.Quantity x @Model.ProductPriceString
@Model.Quantity x @Model.CalculatedProductPrice.PriceString
</div>
</div>
<div class="row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
<span ng-if="!cartItem.isProductAvailabeToOrder" class="badge badge-pill badge-danger">@Localizer["Not availabe any more"]</span>
<span ng-if="cartItem.productStockTrackingIsEnabled && cartItem.productStockQuantity < cartItem.quantity" class="badge badge-pill badge-danger">@Localizer["Not enough stock. Available:"] {{cartItem.productStockQuantity}}</span>
</td>
<td class="text-right">{{cartItem.productPriceString}}</td>
<td class="text-right">
{{cartItem.calculatedProductPrice.priceString}}
<br />
<del ng-if="cartItem.calculatedProductPrice.oldPrice > cartItem.calculatedProductPrice.price">{{cartItem.calculatedProductPrice.oldPriceString}}</del>
</td>
<td class="text-center">
<button type="button" class="quantity-button" ng-click="vm.decreaseQuantity(cartItem)" name="subtract" value="-">-</button>
<input type="text" class="quantity-field" ng-model="cartItem.quantity" readonly="readonly" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using SimplCommerce.Module.Pricing.Services;
using SimplCommerce.Module.ShoppingCart.Areas.ShoppingCart.ViewModels;
using Microsoft.Extensions.Localization;
using SimplCommerce.Module.Catalog.Services;

namespace SimplCommerce.Module.ShoppingCart.Services
{
Expand All @@ -21,16 +22,18 @@ public class CartService : ICartService
private readonly bool _isProductPriceIncludeTax;
private readonly ICurrencyService _currencyService;
private readonly IStringLocalizer _localizer;
private readonly IProductPricingService _productPricingService;

public CartService(IRepository<CartItem> cartItemRepository, ICouponService couponService,
IMediaService mediaService, IConfiguration config, ICurrencyService currencyService, IStringLocalizerFactory stringLocalizerFactory)
IMediaService mediaService, IConfiguration config, ICurrencyService currencyService, IStringLocalizerFactory stringLocalizerFactory, IProductPricingService productPricingService)
{
_cartItemRepository = cartItemRepository;
_couponService = couponService;
_mediaService = mediaService;
_currencyService = currencyService;
_isProductPriceIncludeTax = config.GetValue<bool>("Catalog.IsProductPriceIncludeTax");
_localizer = stringLocalizerFactory.Create(null);
_productPricingService = productPricingService;
}

public async Task<AddToCartResult> AddToCart(long customerId, long productId, int quantity)
Expand Down Expand Up @@ -91,6 +94,7 @@ public async Task<CartVm> GetCartDetails(long customerId)
ProductId = x.ProductId,
ProductName = x.Product.Name,
ProductPrice = x.Product.Price,
CalculatedProductPrice = _productPricingService.CalculateProductPrice(x.Product),
ProductStockQuantity = x.Product.StockQuantity,
ProductStockTrackingIsEnabled = x.Product.StockTrackingIsEnabled,
IsProductAvailabeToOrder = x.Product.IsAllowToOrder && x.Product.IsPublished && !x.Product.IsDeleted,
Expand All @@ -99,7 +103,7 @@ public async Task<CartVm> GetCartDetails(long customerId)
VariationOptions = CartItemVm.GetVariationOption(x.Product)
}).ToList();

cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * x.ProductPrice);
cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * (x.CalculatedProductPrice.OldPrice ?? x.ProductPrice));
if (!string.IsNullOrWhiteSpace(cartVm.CouponCode))
{
var cartInfoForCoupon = new CartInfoForCoupon
Expand All @@ -117,6 +121,10 @@ public async Task<CartVm> GetCartDetails(long customerId)
}
}

cartVm.Discount += cartVm.Items
.Where(x => x.CalculatedProductPrice.OldPrice.HasValue)
.Sum(x => x.Quantity * (x.CalculatedProductPrice.OldPrice.Value - x.CalculatedProductPrice.Price));

return cartVm;
}

Expand Down

0 comments on commit 484cb93

Please sign in to comment.