Compare commits

...

5 Commits

Author SHA1 Message Date
Loretta 1d19bd0e37 Merge branch '4.80' of https://git.aycode.com/Adam/Mango.Nop.Plugins into 4.80 2025-10-26 02:54:47 +02:00
Loretta 16cd9ccd73 new order fix 2025-10-26 02:47:53 +02:00
Loretta 11651e4946 fix 2025-10-25 19:57:09 +02:00
Loretta 05d9c97645 merge 2025-10-25 19:12:56 +02:00
Loretta 3b2af72d5e CalculatePrices improvements, fixes... 2025-10-25 19:11:17 +02:00
7 changed files with 88 additions and 51 deletions

View File

@ -325,7 +325,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 _ =>
{
@ -336,10 +336,9 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
foreach (var item in orderProducts)
{
var product = await _productService.GetProductByIdAsync(item.Id);
if (product == null || product.StockQuantity - item.Quantity < 0)
if (product == null)
{
var errorText = $"product == null || product.StockQuantity - item.Quantity < 0; productId: {product?.Id}; product?.StockQuantity - item.Quantity: {product?.StockQuantity - item.Quantity}";
var errorText = $"product == null; productId: {item.Id};";
_logger.Error($"{errorText}");
throw new Exception($"{errorText}");
@ -348,6 +347,14 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
var productDto = productDtosByOrderItemId[item.Id];
var isMeasurable = productDto.IsMeasurable;
if ((product.StockQuantity + productDto.IncomingQuantity) - item.Quantity < 0)
{
var errorText = $"((product.StockQuantity + productDto.IncomingQuantity) - item.Quantity < 0); productId: {product.Id}; (product.StockQuantity + productDto.IncomingQuantity) - item.Quantity: {(product.StockQuantity + productDto.IncomingQuantity) - item.Quantity}";
_logger.Error($"{errorText}");
throw new Exception($"{errorText}");
}
var orderItem = new OrderItem
{
OrderId = order.Id,
@ -562,15 +569,20 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
pageSize: maxResults);
var result = new List<object>();
var productDtosById = await _dbContext.ProductDtos.GetAllByIds(products.Select(p => p.Id)).ToDictionaryAsync(k => k.Id, v => v);
foreach (var product in products)
{
var productDto = productDtosById[product.Id];
result.Add(new
{
label = $"{product.Name} [KÉSZLET: {product.StockQuantity}] [ÁR: {product.Price}]",
label = $"{product.Name} [RENDELHETŐ: {(product.StockQuantity + productDto.IncomingQuantity)}] [ÁR: {product.Price}]",
value = product.Id,
sku = product.Sku,
price = product.Price,
stockQuantity = product.StockQuantity
stockQuantity = product.StockQuantity,
incomingQuantity = productDto.IncomingQuantity,
});
}

View File

@ -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);
}

View File

@ -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()

View File

@ -763,7 +763,7 @@
<thead>
<tr>
<th>Product</th>
<th style="width: 100px;">Mennyisség</th>
<th style="width: 100px;">Mennyiség</th>
<th style="width: 120px;">Egységár</th>
<th style="width: 50px;"></th>
</tr>
@ -860,6 +860,7 @@
sku: product.sku || '',
quantity: 1,
stockQuantity : product.stockQuantity,
incomingQuantity : product.incomingQuantity,
price: product.price || 0
};
@ -889,7 +890,7 @@
);
var quantityCell = $('<td>').html(
'<input type="number" class="form-control form-control-sm" min="1" max="' + product.stockQuantity + '" value="' + product.quantity + '" data-index="' + index + '" />'
'<input type="number" class="form-control form-control-sm" min="1" max="' + (product.stockQuantity + product.incomingQuantity) + '" value="' + product.quantity + '" data-index="' + index + '" />'
);
var priceCell = $('<td>').html(

View File

@ -22,6 +22,9 @@
var discountInclTax = parseFloat($('#pvDiscountInclTax'+itemId).val()) || 0;
var discountExclTax = parseFloat($('#pvDiscountExclTax'+itemId).val()) || 0;
//if (maxQuantity < currentQuantity) maxQuantity = currentQuantity;
//console.log(maxQuantity);
if (quantity > maxQuantity || quantity < 1) {
if (quantity > maxQuantity) quantity = maxQuantity; else quantity = 1;
$('#pvQuantity' + itemId).val(quantity);
@ -101,7 +104,7 @@
<script>
$(function() {
toggleOrderItemEditGlobal(false, @(item.Id));
setupAutoCalculationGlobal(@(item.Id), @(item.ProductStockQuantity + item.ProductIncomingQuantity + item.Quantity));
setupAutoCalculationGlobal(@(item.Id), @(Math.Max(item.ProductStockQuantity + item.ProductIncomingQuantity + item.Quantity, item.Quantity)));
});
</script>
}
@ -291,7 +294,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 + item.Quantity)" min="1"
<input name="pvQuantity@(item.Id)" type="number" max="@(Math.Max(item.ProductStockQuantity + item.ProductIncomingQuantity + item.Quantity, item.Quantity))" min="1"
value="@item.Quantity" id="pvQuantity@(item.Id)" class="form-control input-sm" />
</div>
</div>

View File

@ -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;
}

View File

@ -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.Info($"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;
}