CalculatePrices improvements, fixes...
This commit is contained in:
parent
dfec11584c
commit
3b2af72d5e
|
|
@ -285,7 +285,7 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
|
|||
|
||||
//var productDtosById = await _dbContext.ProductDtos.GetAllByIds(orderProducts.Select(op => op.Id)).ToDictionaryAsync(p => p.Id, prodDto => prodDto);
|
||||
var store = _storeContext.GetCurrentStore();
|
||||
var productDtosByOrderItemId = (await _dbContext.ProductDtos.GetAllByIds(orderProducts.Select(x => x.Id).ToArray()).ToListAsync()).ToDictionary(k => k.Id, v => v);
|
||||
var productDtosByOrderItemId = await _dbContext.ProductDtos.GetAllByIds(orderProducts.Select(x => x.Id).ToArray()).ToDictionaryAsync(k => k.Id, v => v);
|
||||
|
||||
var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -8,12 +8,16 @@ using FruitBank.Common.SignalRs;
|
|||
using Mango.Nop.Core.Loggers;
|
||||
using Nop.Core;
|
||||
using Nop.Core.Domain.Orders;
|
||||
using Nop.Core.Events;
|
||||
using Nop.Plugin.Misc.FruitBankPlugin.Controllers;
|
||||
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
|
||||
using Nop.Plugin.Misc.FruitBankPlugin.Services;
|
||||
using Nop.Services.Catalog;
|
||||
using static Nop.Services.Security.StandardPermission;
|
||||
|
||||
namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers;
|
||||
|
||||
public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, IWorkContext workContext, IEnumerable<IAcLogWriterBase> logWriters) : ICustomOrderSignalREndpointServer
|
||||
public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, IPriceCalculationService customPriceCalculationService,IEventPublisher eventPublisher, IWorkContext workContext, IEnumerable<IAcLogWriterBase> logWriters) : ICustomOrderSignalREndpointServer
|
||||
{
|
||||
private readonly ILogger _logger = new Logger<CustomOrderSignalREndpoint>(logWriters.ToArray());
|
||||
|
||||
|
|
@ -61,7 +65,17 @@ public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, IWorkContext wor
|
|||
{
|
||||
_logger.Detail($"SetOrderStatusToComplete invoked; orderId: {orderId}; revisorId: {revisorId}");
|
||||
|
||||
if (!await ctx.SetOrderStatusToCompleteSafeAsync(orderId, revisorId)) return null;
|
||||
//TODO: Ez egész nagyon kusza, átgondolni és refaktorálni! - J.
|
||||
var order = ctx.Orders.GetById(orderId);
|
||||
var prevOrderStatus = order.OrderStatus;
|
||||
|
||||
if (order.OrderStatus == OrderStatus.Complete || !await ctx.SetOrderStatusToCompleteSafeAsync(orderId, revisorId)) return null;
|
||||
|
||||
order.OrderStatus = OrderStatus.Complete;
|
||||
await ((CustomPriceCalculationService)customPriceCalculationService).CheckAndUpdateOrderTotalPrice(order);
|
||||
|
||||
if (prevOrderStatus != order.OrderStatus) await eventPublisher.PublishAsync(new OrderStatusChangedEvent(order, prevOrderStatus));
|
||||
|
||||
return await ctx.OrderDtos.GetByIdAsync(orderId, true);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,11 +44,11 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
|
|||
|
||||
private async Task OrderTotalsFix()
|
||||
{
|
||||
var orders = await _dbContext.Orders.Table.ToListAsync();
|
||||
foreach (var order in orders)
|
||||
{
|
||||
await _customPriceCalculationService.CheckAndUpdateOrderTotalPrice(order);
|
||||
}
|
||||
//var orders = await _dbContext.Orders.Table.ToListAsync();
|
||||
//foreach (var order in orders)
|
||||
//{
|
||||
// await _customPriceCalculationService.CheckAndUpdateOrderTotalPrice(order);
|
||||
//}
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Test()
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@
|
|||
var discountInclTax = parseFloat($('#pvDiscountInclTax'+itemId).val()) || 0;
|
||||
var discountExclTax = parseFloat($('#pvDiscountExclTax'+itemId).val()) || 0;
|
||||
|
||||
if (quantity > maxQuantity || quantity < 0) {
|
||||
if (quantity > maxQuantity) quantity = maxQuantity; else quantity = 0;
|
||||
if (quantity > maxQuantity || quantity < 1) {
|
||||
if (quantity > maxQuantity) quantity = maxQuantity; else quantity = 1;
|
||||
$('#pvQuantity' + itemId).val(quantity);
|
||||
}
|
||||
|
||||
|
|
@ -98,7 +98,7 @@
|
|||
<script>
|
||||
$(function() {
|
||||
toggleOrderItemEditGlobal(false, @(item.Id));
|
||||
setupAutoCalculationGlobal(@(item.Id), @(item.ProductStockQuantity + item.ProductIncomingQuantity));
|
||||
setupAutoCalculationGlobal(@(item.Id), @(item.ProductStockQuantity + item.ProductIncomingQuantity + item.Quantity));
|
||||
});
|
||||
</script>
|
||||
}
|
||||
|
|
@ -284,7 +284,7 @@
|
|||
<div id="pnlEditPvQuantity@(item.Id)">
|
||||
<div class="form-group row">
|
||||
<div class="col-md-8 offset-md-2">
|
||||
<input name="pvQuantity@(item.Id)" type="number" max="@(item.ProductStockQuantity + item.ProductIncomingQuantity)" min="0"
|
||||
<input name="pvQuantity@(item.Id)" type="number" max="@(item.ProductStockQuantity + item.ProductIncomingQuantity + item.Quantity)" min="1"
|
||||
value="@item.Quantity" id="pvQuantity@(item.Id)" class="form-control input-sm" />
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -417,7 +417,7 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
|
||||
if (!orderDto.IsMeasuredAndValid() || orderDto.OrderStatus == OrderStatus.Complete) return null; //throw new Exception($"SetOrderDtoToComplete; orderDto.IsMeasured == false; {orderDto}");
|
||||
|
||||
var prevOrderStatus = orderDto.OrderStatus;
|
||||
//var prevOrderStatus = orderDto.OrderStatus;
|
||||
orderDto.OrderStatus = OrderStatus.Complete;
|
||||
|
||||
await OrderDtos.UpdateAsync(orderDto);
|
||||
|
|
@ -433,9 +433,6 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
|
||||
if (!orderItemDto.IsMeasurable) continue;
|
||||
|
||||
var finalPriceInclTax = decimal.Round(orderItemDto.UnitPriceInclTax * orderItemDto.Quantity, 0);
|
||||
var finalPriceExclTax = decimal.Round(orderItemDto.UnitPriceExclTax * orderItemDto.Quantity, 0);
|
||||
|
||||
var gaNetWeight = CommonHelper.To<double>(orderItemDto.GenericAttributes.FirstOrDefault(x => x.Key == nameof(IMeasuringNetWeight.NetWeight))?.Value ?? "0");
|
||||
|
||||
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<OrderItem, double>
|
||||
|
|
@ -444,10 +441,8 @@ public class FruitBankDbContext : MgDbContextBase,
|
|||
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>
|
||||
(orderItemDto.ProductId, -(orderItemDto.NetWeight-gaNetWeight), orderItemDto.IsMeasurable, true);
|
||||
}
|
||||
|
||||
var order = Orders.GetById(orderDto.Id);
|
||||
await _eventPublisher.PublishAsync(new OrderStatusChangedEvent(order, prevOrderStatus));
|
||||
|
||||
|
||||
//await _eventPublisher.PublishAsync(new OrderStatusChangedEvent(order, prevOrderStatus));
|
||||
return orderDto;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ public class CustomPriceCalculationService : PriceCalculationService
|
|||
productAttributeParser, productService,
|
||||
cacheManager)
|
||||
{
|
||||
_logger = new Logger(logWriters.ToArray());
|
||||
_logger = new Logger<CustomPriceCalculationService>(logWriters.ToArray());
|
||||
|
||||
_dbContext = dbContext;
|
||||
// assign all base deps to local private vars if needed
|
||||
|
|
@ -60,41 +60,49 @@ public class CustomPriceCalculationService : PriceCalculationService
|
|||
_localizationService = localizationService;
|
||||
}
|
||||
|
||||
public decimal CalculateOrderItemFinalPrice(OrderItemDto orderItemDto, decimal unitPrice, int quantity)
|
||||
=> decimal.Round(unitPrice * (orderItemDto.IsMeasurable ? (decimal)orderItemDto.NetWeight : quantity), 0);
|
||||
public static decimal CalculateOrderItemFinalPrice(bool isMeasurable, decimal unitPrice, int quantity, double netWeight)
|
||||
=> decimal.Round(unitPrice * (isMeasurable ? (decimal)netWeight: quantity), 0);
|
||||
|
||||
private static (decimal finalPriceInclTax, decimal finalPriceExclTax) CalculateOrderItemFinalPrices(int quantity, decimal unitPriceInclTax, decimal unitPriceExclTax, bool isMeasurable, double netWeight)
|
||||
{
|
||||
var finalPriceInclTax = CalculateOrderItemFinalPrice(isMeasurable, unitPriceInclTax, quantity, netWeight);
|
||||
var finalPriceExclTax = CalculateOrderItemFinalPrice(isMeasurable, unitPriceExclTax, quantity, netWeight);
|
||||
|
||||
return (finalPriceInclTax, finalPriceExclTax);
|
||||
}
|
||||
|
||||
public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem)
|
||||
{
|
||||
var orderItemDto = await _dbContext.OrderItemDtos.GetByIdAsync(orderItem.Id, true);
|
||||
return await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto);
|
||||
}
|
||||
public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItemDto orderItemDto)
|
||||
{
|
||||
var orderItem = await _dbContext.OrderItems.GetByIdAsync(orderItemDto.Id);
|
||||
return await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto);
|
||||
return await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto.IsMeasurable, orderItemDto.NetWeight);
|
||||
}
|
||||
|
||||
//public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItemDto orderItemDto)
|
||||
//{
|
||||
// var orderItem = await _dbContext.OrderItems.GetByIdAsync(orderItemDto.Id);
|
||||
// return await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto.IsMeasurable, orderItemDto.NetWeight);
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="orderItem"></param>
|
||||
/// <param name="orderItemDto"></param>
|
||||
/// <param name="isMeasurable"></param>
|
||||
/// /// <param name="netWeight"></param>
|
||||
/// <returns>true if has changes</returns>
|
||||
public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem, OrderItemDto orderItemDto)
|
||||
public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem, bool isMeasurable, double netWeight)
|
||||
{
|
||||
_logger.Info($"CustomPriceCalculationService->CheckAndUpdateOrderItemFinalPricesAsync; orderItem.Id: {orderItem.Id}");
|
||||
_logger.Info($"orderItem.Id: {orderItem.Id}");
|
||||
|
||||
var finalPriceInclTax = CalculateOrderItemFinalPrice(orderItemDto, orderItem.UnitPriceInclTax, orderItem.Quantity);
|
||||
var finalPriceExclTax = CalculateOrderItemFinalPrice(orderItemDto, orderItem.UnitPriceExclTax, orderItem.Quantity);
|
||||
var finalPrices = CalculateOrderItemFinalPrices(orderItem.Quantity, orderItem.UnitPriceInclTax, orderItem.UnitPriceExclTax, isMeasurable, netWeight);
|
||||
|
||||
if (orderItem.PriceInclTax == finalPriceInclTax && orderItem.PriceExclTax == finalPriceExclTax) return false;
|
||||
if (finalPrices.finalPriceInclTax == orderItem.PriceInclTax && finalPrices.finalPriceExclTax == orderItem.PriceExclTax) return false;
|
||||
|
||||
_logger.Error($"CustomPriceCalculationService->CheckAndUpdateOrderItemFinalPricesAsync; " +
|
||||
$"orderItem.PriceInclTax({orderItem.PriceInclTax}) != finalPriceInclTax({finalPriceInclTax}) || " +
|
||||
$"orderItem.PriceExclTax({orderItem.PriceExclTax}) != finalPriceExclTax({finalPriceExclTax})");
|
||||
_logger.Error($"orderItem.PriceInclTax({orderItem.PriceInclTax}) != finalPriceInclTax({finalPrices.finalPriceInclTax}) || " +
|
||||
$"orderItem.PriceExclTax({orderItem.PriceExclTax}) != finalPriceExclTax({finalPrices.finalPriceExclTax})");
|
||||
|
||||
orderItem.PriceInclTax = finalPriceInclTax;
|
||||
orderItem.PriceExclTax = finalPriceExclTax;
|
||||
orderItem.PriceInclTax = finalPrices.finalPriceInclTax;
|
||||
orderItem.PriceExclTax = finalPrices.finalPriceExclTax;
|
||||
|
||||
await _dbContext.OrderItems.UpdateAsync(orderItem, false);
|
||||
return true;
|
||||
|
|
@ -108,26 +116,30 @@ public class CustomPriceCalculationService : PriceCalculationService
|
|||
public async Task<bool> CheckAndUpdateOrderTotalPrice(Order order)
|
||||
{
|
||||
var prevOrderTotal = order.OrderTotal;
|
||||
var orderItemDtos = await _dbContext.OrderItemDtos.GetAllByOrderId(order.Id).ToListAsync();
|
||||
|
||||
var orderItemDtosById = await _dbContext.OrderItemDtos.GetAllByOrderId(order.Id).ToDictionaryAsync(k => k.Id, v => v);
|
||||
var orderItems = await _dbContext.OrderItems.GetByIdsAsync(orderItemDtosById.Keys.ToList());
|
||||
|
||||
order.OrderTotal = 0;
|
||||
|
||||
foreach (var itemDto in orderItemDtos)
|
||||
foreach (var orderItem in orderItems)
|
||||
{
|
||||
await CheckAndUpdateOrderItemFinalPricesAsync(itemDto);
|
||||
order.OrderTotal += itemDto.PriceInclTax;
|
||||
var orderItemDto = orderItemDtosById[orderItem.Id];
|
||||
|
||||
await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto.IsMeasurable, orderItemDto.NetWeight);
|
||||
order.OrderTotal += orderItem.PriceInclTax;
|
||||
}
|
||||
|
||||
if (order.OrderTotal == prevOrderTotal) return false;
|
||||
|
||||
_logger.Error($"HandleEventAsync->CheckAndUpdateOrderItemFinalPrices; order.OrderTotal({order.OrderTotal}) == prevOrderTotal({prevOrderTotal})");
|
||||
_logger.Error($"order.OrderTotal({order.OrderTotal}) == prevOrderTotal({prevOrderTotal})");
|
||||
|
||||
order.OrderSubtotalInclTax = order.OrderTotal;
|
||||
order.OrderSubtotalExclTax = order.OrderTotal;
|
||||
order.OrderSubTotalDiscountInclTax = order.OrderTotal;
|
||||
order.OrderSubTotalDiscountExclTax = order.OrderTotal;
|
||||
|
||||
await _dbContext.Orders.UpdateAsync(order);
|
||||
await _dbContext.Orders.UpdateAsync(order, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue