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 productDtosById = await _dbContext.ProductDtos.GetAllByIds(orderProducts.Select(op => op.Id)).ToDictionaryAsync(p => p.Id, prodDto => prodDto);
var store = _storeContext.GetCurrentStore(); 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 _ => var transactionSuccess = await _dbContext.TransactionSafeAsync(async _ =>
{ {
@ -336,9 +336,10 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
foreach (var item in orderProducts) foreach (var item in orderProducts)
{ {
var product = await _productService.GetProductByIdAsync(item.Id); 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}"); _logger.Error($"{errorText}");
throw new Exception($"{errorText}"); throw new Exception($"{errorText}");
@ -347,14 +348,6 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
var productDto = productDtosByOrderItemId[item.Id]; var productDto = productDtosByOrderItemId[item.Id];
var isMeasurable = productDto.IsMeasurable; 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 var orderItem = new OrderItem
{ {
OrderId = order.Id, OrderId = order.Id,
@ -569,20 +562,15 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers
pageSize: maxResults); pageSize: maxResults);
var result = new List<object>(); 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) foreach (var product in products)
{ {
var productDto = productDtosById[product.Id];
result.Add(new 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, value = product.Id,
sku = product.Sku, sku = product.Sku,
price = product.Price, price = product.Price,
stockQuantity = product.StockQuantity, stockQuantity = product.StockQuantity
incomingQuantity = productDto.IncomingQuantity,
}); });
} }

View File

@ -8,16 +8,12 @@ using FruitBank.Common.SignalRs;
using Mango.Nop.Core.Loggers; using Mango.Nop.Core.Loggers;
using Nop.Core; using Nop.Core;
using Nop.Core.Domain.Orders; using Nop.Core.Domain.Orders;
using Nop.Core.Events;
using Nop.Plugin.Misc.FruitBankPlugin.Controllers; using Nop.Plugin.Misc.FruitBankPlugin.Controllers;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; 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; 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()); 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}"); _logger.Detail($"SetOrderStatusToComplete invoked; orderId: {orderId}; revisorId: {revisorId}");
//TODO: Ez egész nagyon kusza, átgondolni és refaktorálni! - J. if (!await ctx.SetOrderStatusToCompleteSafeAsync(orderId, revisorId)) return null;
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); 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() private async Task OrderTotalsFix()
{ {
//var orders = await _dbContext.Orders.Table.ToListAsync(); var orders = await _dbContext.Orders.Table.ToListAsync();
//foreach (var order in orders) foreach (var order in orders)
//{ {
// await _customPriceCalculationService.CheckAndUpdateOrderTotalPrice(order); await _customPriceCalculationService.CheckAndUpdateOrderTotalPrice(order);
//} }
} }
public async Task<IActionResult> Test() public async Task<IActionResult> Test()

View File

@ -763,7 +763,7 @@
<thead> <thead>
<tr> <tr>
<th>Product</th> <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: 120px;">Egységár</th>
<th style="width: 50px;"></th> <th style="width: 50px;"></th>
</tr> </tr>
@ -860,7 +860,6 @@
sku: product.sku || '', sku: product.sku || '',
quantity: 1, quantity: 1,
stockQuantity : product.stockQuantity, stockQuantity : product.stockQuantity,
incomingQuantity : product.incomingQuantity,
price: product.price || 0 price: product.price || 0
}; };
@ -890,7 +889,7 @@
); );
var quantityCell = $('<td>').html( 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( var priceCell = $('<td>').html(

View File

@ -22,9 +22,6 @@
var discountInclTax = parseFloat($('#pvDiscountInclTax'+itemId).val()) || 0; var discountInclTax = parseFloat($('#pvDiscountInclTax'+itemId).val()) || 0;
var discountExclTax = parseFloat($('#pvDiscountExclTax'+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 < 1) {
if (quantity > maxQuantity) quantity = maxQuantity; else quantity = 1; if (quantity > maxQuantity) quantity = maxQuantity; else quantity = 1;
$('#pvQuantity' + itemId).val(quantity); $('#pvQuantity' + itemId).val(quantity);
@ -104,7 +101,7 @@
<script> <script>
$(function() { $(function() {
toggleOrderItemEditGlobal(false, @(item.Id)); 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> </script>
} }
@ -294,7 +291,7 @@
<div id="pnlEditPvQuantity@(item.Id)"> <div id="pnlEditPvQuantity@(item.Id)">
<div class="form-group row"> <div class="form-group row">
<div class="col-md-8 offset-md-2"> <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" /> value="@item.Quantity" id="pvQuantity@(item.Id)" class="form-control input-sm" />
</div> </div>
</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}"); 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; orderDto.OrderStatus = OrderStatus.Complete;
await OrderDtos.UpdateAsync(orderDto); await OrderDtos.UpdateAsync(orderDto);
@ -433,6 +433,9 @@ public class FruitBankDbContext : MgDbContextBase,
if (!orderItemDto.IsMeasurable) continue; 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"); var gaNetWeight = CommonHelper.To<double>(orderItemDto.GenericAttributes.FirstOrDefault(x => x.Key == nameof(IMeasuringNetWeight.NetWeight))?.Value ?? "0");
await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<OrderItem, double> await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync<OrderItem, double>
@ -441,8 +444,10 @@ public class FruitBankDbContext : MgDbContextBase,
await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product> await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync<Product>
(orderItemDto.ProductId, -(orderItemDto.NetWeight-gaNetWeight), orderItemDto.IsMeasurable, true); (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; return orderDto;
} }

View File

@ -51,7 +51,7 @@ public class CustomPriceCalculationService : PriceCalculationService
productAttributeParser, productService, productAttributeParser, productService,
cacheManager) cacheManager)
{ {
_logger = new Logger<CustomPriceCalculationService>(logWriters.ToArray()); _logger = new Logger(logWriters.ToArray());
_dbContext = dbContext; _dbContext = dbContext;
// assign all base deps to local private vars if needed // assign all base deps to local private vars if needed
@ -60,49 +60,41 @@ public class CustomPriceCalculationService : PriceCalculationService
_localizationService = localizationService; _localizationService = localizationService;
} }
public static decimal CalculateOrderItemFinalPrice(bool isMeasurable, decimal unitPrice, int quantity, double netWeight) public decimal CalculateOrderItemFinalPrice(OrderItemDto orderItemDto, decimal unitPrice, int quantity)
=> decimal.Round(unitPrice * (isMeasurable ? (decimal)netWeight: quantity), 0); => decimal.Round(unitPrice * (orderItemDto.IsMeasurable ? (decimal)orderItemDto.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) public async Task<bool> CheckAndUpdateOrderItemFinalPricesAsync(OrderItem orderItem)
{ {
var orderItemDto = await _dbContext.OrderItemDtos.GetByIdAsync(orderItem.Id, true); 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>
/// ///
/// </summary> /// </summary>
/// <param name="orderItem"></param> /// <param name="orderItem"></param>
/// <param name="isMeasurable"></param> /// <param name="orderItemDto"></param>
/// /// <param name="netWeight"></param>
/// <returns>true if has changes</returns> /// <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}) || " + _logger.Error($"CustomPriceCalculationService->CheckAndUpdateOrderItemFinalPricesAsync; " +
$"orderItem.PriceExclTax({orderItem.PriceExclTax}) != finalPriceExclTax({finalPrices.finalPriceExclTax})"); $"orderItem.PriceInclTax({orderItem.PriceInclTax}) != finalPriceInclTax({finalPriceInclTax}) || " +
$"orderItem.PriceExclTax({orderItem.PriceExclTax}) != finalPriceExclTax({finalPriceExclTax})");
orderItem.PriceInclTax = finalPrices.finalPriceInclTax; orderItem.PriceInclTax = finalPriceInclTax;
orderItem.PriceExclTax = finalPrices.finalPriceExclTax; orderItem.PriceExclTax = finalPriceExclTax;
await _dbContext.OrderItems.UpdateAsync(orderItem, false); await _dbContext.OrderItems.UpdateAsync(orderItem, false);
return true; return true;
@ -116,30 +108,26 @@ public class CustomPriceCalculationService : PriceCalculationService
public async Task<bool> CheckAndUpdateOrderTotalPrice(Order order) public async Task<bool> CheckAndUpdateOrderTotalPrice(Order order)
{ {
var prevOrderTotal = order.OrderTotal; 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; order.OrderTotal = 0;
foreach (var orderItem in orderItems) foreach (var itemDto in orderItemDtos)
{ {
var orderItemDto = orderItemDtosById[orderItem.Id]; await CheckAndUpdateOrderItemFinalPricesAsync(itemDto);
order.OrderTotal += itemDto.PriceInclTax;
await CheckAndUpdateOrderItemFinalPricesAsync(orderItem, orderItemDto.IsMeasurable, orderItemDto.NetWeight);
order.OrderTotal += orderItem.PriceInclTax;
} }
if (order.OrderTotal == prevOrderTotal) return false; 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.OrderSubtotalInclTax = order.OrderTotal;
order.OrderSubtotalExclTax = order.OrderTotal; order.OrderSubtotalExclTax = order.OrderTotal;
order.OrderSubTotalDiscountInclTax = order.OrderTotal; order.OrderSubTotalDiscountInclTax = order.OrderTotal;
order.OrderSubTotalDiscountExclTax = order.OrderTotal; order.OrderSubTotalDiscountExclTax = order.OrderTotal;
await _dbContext.Orders.UpdateAsync(order, false); await _dbContext.Orders.UpdateAsync(order);
return true; return true;
} }