Compare commits

..

No commits in common. "1d19bd0e378a4e7b2b352ce924a4ab575c8b67fc" and "6735765821777de3a7191308434f24ab5ce7b514" have entirely different histories.

7 changed files with 51 additions and 88 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()).ToDictionaryAsync(k => k.Id, v => v);
var productDtosByOrderItemId = (await _dbContext.ProductDtos.GetAllByIds(orderProducts.Select(x => x.Id).ToArray()).ToListAsync()).ToDictionary(k => k.Id, v => v);
var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ =>
{
@ -336,9 +336,10 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
foreach (var item in orderProducts)
{
var product = await _productService.GetProductByIdAsync(item.Id);
if (product == null)
if (product == null || product.StockQuantity - item.Quantity < 0)
{
var errorText = $"product == null; productId: {item.Id};";
var errorText = $"product == null || product.StockQuantity - item.Quantity < 0; productId: {product?.Id}; product?.StockQuantity - item.Quantity: {product?.StockQuantity - item.Quantity}";
_logger.Error($"{errorText}");
throw new Exception($"{errorText}");
@ -347,14 +348,6 @@ 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,
@ -569,20 +562,15 @@ 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} [RENDELHETŐ: {(product.StockQuantity + productDto.IncomingQuantity)}] [ÁR: {product.Price}]",
label = $"{product.Name} [KÉSZLET: {product.StockQuantity}] [ÁR: {product.Price}]",
value = product.Id,
sku = product.Sku,
price = product.Price,
stockQuantity = product.StockQuantity,
incomingQuantity = productDto.IncomingQuantity,
stockQuantity = product.StockQuantity
});
}

View File

@ -8,16 +8,12 @@ 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, IPriceCalculationService customPriceCalculationService,IEventPublisher eventPublisher, IWorkContext workContext, IEnumerable<IAcLogWriterBase> logWriters) : ICustomOrderSignalREndpointServer
public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, IWorkContext workContext, IEnumerable<IAcLogWriterBase> logWriters) : ICustomOrderSignalREndpointServer
{
private readonly ILogger _logger = new Logger<CustomOrderSignalREndpoint>(logWriters.ToArray());
@ -65,17 +61,7 @@ public class CustomOrderSignalREndpoint(FruitBankDbContext ctx, IPriceCalculatio
{
_logger.Detail($"SetOrderStatusToComplete invoked; orderId: {orderId}; revisorId: {revisorId}");
//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));
if (!await ctx.SetOrderStatusToCompleteSafeAsync(orderId, revisorId)) return null;
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;">Mennyiség</th>
<th style="width: 100px;">Mennyisség</th>
<th style="width: 120px;">Egységár</th>
<th style="width: 50px;"></th>
</tr>
@ -860,7 +860,6 @@
sku: product.sku || '',
quantity: 1,
stockQuantity : product.stockQuantity,
incomingQuantity : product.incomingQuantity,
price: product.price || 0
};
@ -890,7 +889,7 @@
);
var quantityCell = $('<td>').html(
'<input type="number" class="form-control form-control-sm" min="1" max="' + (product.stockQuantity + product.incomingQuantity) + '" value="' + product.quantity + '" data-index="' + index + '" />'
'<input type="number" class="form-control form-control-sm" min="1" max="' + product.stockQuantity + '" value="' + product.quantity + '" data-index="' + index + '" />'
);
var priceCell = $('<td>').html(

View File

@ -22,9 +22,6 @@
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);
@ -104,7 +101,7 @@
<script>
$(function() {
toggleOrderItemEditGlobal(false, @(item.Id));
setupAutoCalculationGlobal(@(item.Id), @(Math.Max(item.ProductStockQuantity + item.ProductIncomingQuantity + item.Quantity, item.Quantity)));
setupAutoCalculationGlobal(@(item.Id), @(item.ProductStockQuantity + item.ProductIncomingQuantity + item.Quantity));
});
</script>
}
@ -294,7 +291,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="@(Math.Max(item.ProductStockQuantity + item.ProductIncomingQuantity + item.Quantity, item.Quantity))" min="1"
<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>

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,6 +433,9 @@ 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>
@ -442,7 +445,9 @@ public class FruitBankDbContext : MgDbContextBase,
(orderItemDto.ProductId, -(orderItemDto.NetWeight-gaNetWeight), orderItemDto.IsMeasurable, true);
}
//await _eventPublisher.PublishAsync(new OrderStatusChangedEvent(order, prevOrderStatus));
var order = Orders.GetById(orderDto.Id);
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<CustomPriceCalculationService>(logWriters.ToArray());
_logger = new Logger(logWriters.ToArray());
_dbContext = dbContext;
// assign all base deps to local private vars if needed
@ -60,49 +60,41 @@ public class CustomPriceCalculationService : PriceCalculationService
_localizationService = localizationService;
}
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 decimal CalculateOrderItemFinalPrice(OrderItemDto orderItemDto, decimal unitPrice, int quantity)
=> decimal.Round(unitPrice * (orderItemDto.IsMeasurable ? (decimal)orderItemDto.NetWeight : quantity), 0);
public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem)
{
var orderItemDto = await _dbContext.OrderItemDtos.GetByIdAsync(orderItem.Id, true);
return await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto.IsMeasurable, orderItemDto.NetWeight);
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);
}
//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="isMeasurable"></param>
/// /// <param name="netWeight"></param>
/// <param name="orderItemDto"></param>
/// <returns>true if has changes</returns>
public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem, bool isMeasurable, double netWeight)
public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem, OrderItemDto orderItemDto)
{
_logger.Info($"orderItem.Id: {orderItem.Id}");
_logger.Info($"CustomPriceCalculationService->CheckAndUpdateOrderItemFinalPricesAsync; orderItem.Id: {orderItem.Id}");
var finalPrices = CalculateOrderItemFinalPrices(orderItem.Quantity, orderItem.UnitPriceInclTax, orderItem.UnitPriceExclTax, isMeasurable, netWeight);
var finalPriceInclTax = CalculateOrderItemFinalPrice(orderItemDto, orderItem.UnitPriceInclTax, orderItem.Quantity);
var finalPriceExclTax = CalculateOrderItemFinalPrice(orderItemDto, orderItem.UnitPriceExclTax, orderItem.Quantity);
if (finalPrices.finalPriceInclTax == orderItem.PriceInclTax && finalPrices.finalPriceExclTax == orderItem.PriceExclTax) return false;
if (orderItem.PriceInclTax == finalPriceInclTax && orderItem.PriceExclTax == finalPriceExclTax) return false;
_logger.Info($"orderItem.PriceInclTax({orderItem.PriceInclTax}) != finalPriceInclTax({finalPrices.finalPriceInclTax}) || " +
$"orderItem.PriceExclTax({orderItem.PriceExclTax}) != finalPriceExclTax({finalPrices.finalPriceExclTax})");
_logger.Error($"CustomPriceCalculationService->CheckAndUpdateOrderItemFinalPricesAsync; " +
$"orderItem.PriceInclTax({orderItem.PriceInclTax}) != finalPriceInclTax({finalPriceInclTax}) || " +
$"orderItem.PriceExclTax({orderItem.PriceExclTax}) != finalPriceExclTax({finalPriceExclTax})");
orderItem.PriceInclTax = finalPrices.finalPriceInclTax;
orderItem.PriceExclTax = finalPrices.finalPriceExclTax;
orderItem.PriceInclTax = finalPriceInclTax;
orderItem.PriceExclTax = finalPriceExclTax;
await _dbContext.OrderItems.UpdateAsync(orderItem, false);
return true;
@ -116,30 +108,26 @@ public class CustomPriceCalculationService : PriceCalculationService
public async Task<bool> CheckAndUpdateOrderTotalPrice(Order order)
{
var prevOrderTotal = order.OrderTotal;
var orderItemDtosById = await _dbContext.OrderItemDtos.GetAllByOrderId(order.Id).ToDictionaryAsync(k => k.Id, v => v);
var orderItems = await _dbContext.OrderItems.GetByIdsAsync(orderItemDtosById.Keys.ToList());
var orderItemDtos = await _dbContext.OrderItemDtos.GetAllByOrderId(order.Id).ToListAsync();
order.OrderTotal = 0;
foreach (var orderItem in orderItems)
foreach (var itemDto in orderItemDtos)
{
var orderItemDto = orderItemDtosById[orderItem.Id];
await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto.IsMeasurable, orderItemDto.NetWeight);
order.OrderTotal += orderItem.PriceInclTax;
await CheckAndUpdateOrderItemFinalPricesAsync(itemDto);
order.OrderTotal += itemDto.PriceInclTax;
}
if (order.OrderTotal == prevOrderTotal) return false;
_logger.Error($"order.OrderTotal({order.OrderTotal}) == prevOrderTotal({prevOrderTotal})");
_logger.Error($"HandleEventAsync->CheckAndUpdateOrderItemFinalPrices; 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, false);
await _dbContext.Orders.UpdateAsync(order);
return true;
}