From ad5f27a77ce370bdb26558fe0bc644908a5b3ad8 Mon Sep 17 00:00:00 2001 From: Loretta Date: Fri, 14 Nov 2025 13:20:21 +0100 Subject: [PATCH] UpdateStockQuantityAndWeightAsync --- .../Domains/DataLayer/FruitBankDbContext.cs | 153 +++++++++++++----- .../EventConsumers/FruitBankEventConsumer.cs | 3 + 2 files changed, 119 insertions(+), 37 deletions(-) diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs index 1687da7..9a50d7e 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs @@ -6,15 +6,18 @@ using FruitBank.Common.Dtos; using FruitBank.Common.Entities; using FruitBank.Common.Interfaces; using FruitBank.Common.Models; +using Mango.Nop.Core.Entities; using Mango.Nop.Core.Extensions; using Mango.Nop.Core.Loggers; using Mango.Nop.Data.Repositories; +using Microsoft.EntityFrameworkCore; using Nop.Core; using Nop.Core.Caching; using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Common; using Nop.Core.Domain.Customers; using Nop.Core.Domain.Orders; +using Nop.Core.Domain.Shipping; using Nop.Core.Events; using Nop.Data; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces; @@ -65,6 +68,8 @@ public class FruitBankDbContext : MgDbContextBase, public IRepository CustomerAddressMappings { get; set; } public IRepository GenericAttributes { get; set; } + public IRepository StockQuantityHistories { get; set; } + public IRepository StockQuantityHistoriesExt { get; set; } public FruitBankDbContext(INopDataProvider dataProvider, ILockService lockService, FruitBankAttributeService fruitBankAttributeService, IStoreContext storeContext, PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, ShippingItemDbTable shippingItemDbTable, @@ -79,6 +84,8 @@ public class FruitBankDbContext : MgDbContextBase, IRepository customerAddressMappingRepository, IRepository customerRoleRepository, IRepository genericAttributes, + IRepository stockQuantityHistories, + IRepository stockQuantityHistoriesExt, IEventPublisher eventPublisher, IEnumerable logWriters) : base(productRepository, orderRepository, orderItemRepository, dataProvider, lockService, new Logger(logWriters.ToArray())) { @@ -110,6 +117,9 @@ public class FruitBankDbContext : MgDbContextBase, CustomerAddressMappings = customerAddressMappingRepository; GenericAttributes = genericAttributes; + + StockQuantityHistories = stockQuantityHistories; + StockQuantityHistoriesExt = stockQuantityHistoriesExt; } public IQueryable GetCustomersBySystemRoleName(string systemRoleName) @@ -282,38 +292,42 @@ public class FruitBankDbContext : MgDbContextBase, if (shippingItem.IsMeasured) { var quantityInc = productIdChanged ? shippingItem.MeasuredQuantity : shippingItem.MeasuredQuantity - dbShippingItem.MeasuredQuantity; - productDto!.StockQuantity += quantityInc; - if (!await UpdateProductDtoStockQuantityAsync(productDto, true)) - throw new Exception($"UpdateProductStockQuantity() == false; shippingItem! product.Id: {productDto.Id}"); + //productDto!.StockQuantity += quantityInc; + //if (!await UpdateProductDtoStockQuantityAsync(productDto, true)) + // throw new Exception($"UpdateProductStockQuantity() == false; shippingItem! product.Id: {productDto.Id}"); - var incomingQuantity = productDto.GenericAttributes.GetValueOrNull(nameof(IIncomingQuantity.IncomingQuantity)); + var incomingQuantity = productDto!.GenericAttributes.GetValueOrNull(nameof(IIncomingQuantity.IncomingQuantity)); if (incomingQuantity != null) { await _fruitBankAttributeService.UpdateGenericAttributeAsync (productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value - quantityInc); } + var weightToChange = 0d; if (productIsMeasurable) { - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(productDto.Id, - productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight, - shippingItem.IsMeasurable, true); + weightToChange = productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight; + + await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(productDto.Id, weightToChange, shippingItem.IsMeasurable, true); } + + await UpdateStockQuantityAndWeightAsync(productDto, quantityInc, $"Bejövő mérés, shippingItem: #{shippingItem.Id}", weightToChange); + productDto!.StockQuantity += quantityInc; } //if (productIdUnchanged || !dbShippingItem.IsMeasured) return true; if (!dbShippingItem.ProductId.HasValue || (!productIdChanged && (shippingItem.IsMeasured || !dbShippingItem.IsMeasured))) return true; productDto = await ProductDtos.GetByIdAsync(dbShippingItem.ProductId!.Value, true); - + if (productDto != null) { productIsMeasurable = productDto.IsMeasurable; - productDto.StockQuantity -= dbShippingItem.MeasuredQuantity; - if (!await UpdateProductDtoStockQuantityAsync(productDto, true)) - throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {productDto.Id}"); + //productDto.StockQuantity -= dbShippingItem.MeasuredQuantity; + //if (!await UpdateProductDtoStockQuantityAsync(productDto, true)) + // throw new Exception($"UpdateProductStockQuantity() == false; dbShippingItem! product.Id: {productDto.Id}"); var incomingQuantity = productDto.GenericAttributes.GetValueOrNull(nameof(IIncomingQuantity.IncomingQuantity)); if (incomingQuantity != null) @@ -322,10 +336,14 @@ public class FruitBankDbContext : MgDbContextBase, (productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value + dbShippingItem.MeasuredQuantity); } - if (!productIsMeasurable) return true; + if (productIsMeasurable) + { + var measuringValues = new MeasuringAttributeValues(productDto.Id, -dbShippingItem.MeasuredNetWeight, dbShippingItem.IsMeasurable); + await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(measuringValues, true); + } - var measuringValues = new MeasuringAttributeValues(productDto.Id, -dbShippingItem.MeasuredNetWeight, dbShippingItem.IsMeasurable); - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(measuringValues, true); + await UpdateStockQuantityAndWeightAsync(productDto, -dbShippingItem.MeasuredQuantity, $"Bejövő mérés, ShippingItem.Id: #{shippingItem.Id}. Product.Id megváltozott, #{productDto.Id}->#{shippingItem.ProductId}!", -dbShippingItem.MeasuredNetWeight); + productDto!.StockQuantity -= dbShippingItem.MeasuredQuantity; } else Logger.Warning($"product == null; dbShippingItem.ProductId: {dbShippingItem.ProductId}"); //else //TODO: productIdUnchanged-et lekezelni! - J. @@ -466,7 +484,7 @@ public class FruitBankDbContext : MgDbContextBase, orderDto.OrderStatus = OrderStatus.Complete; await OrderDtos.UpdateAsync(orderDto); - + await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(orderDto.Id, nameof(IOrderDto.RevisorId), revisorId); foreach (var orderItemDto in orderDto.OrderItemDtos) @@ -487,8 +505,12 @@ public class FruitBankDbContext : MgDbContextBase, await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync (orderItemDto.Id, nameof(IMeasuringNetWeight.NetWeight), orderItemDto.NetWeight); + var weightToChange = -(orderItemDto.NetWeight - prevNetWeightFromGa); + await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync - (orderItemDto.ProductId, -(orderItemDto.NetWeight - prevNetWeightFromGa), orderItemDto.IsMeasurable, true); + (orderItemDto.ProductId, weightToChange, orderItemDto.IsMeasurable, true); + + await UpdateStockQuantityAndWeightAsync(orderItemDto.ProductId, 0, $"Kimenő mérés, OrderStatus set to complete. Rendelés: #{orderDto.Id}, rendelés tétel: #{orderItemDto.Id}", weightToChange, prevNetWeightFromGa + weightToChange); } //await _eventPublisher.PublishAsync(new OrderStatusChangedEvent(order, prevOrderStatus)); @@ -552,7 +574,7 @@ public class FruitBankDbContext : MgDbContextBase, public async Task AddOrderItemPalletAsync(OrderItemPallet orderItemPallet) { if (!await SetupOrderItemPalletMeauringValues(orderItemPallet)) return null; - + await OrderItemPallets.InsertAsync(orderItemPallet); return orderItemPallet; } @@ -571,7 +593,7 @@ public class FruitBankDbContext : MgDbContextBase, public async Task AddOrUpdateOrderItemPalletAsync(OrderItemPallet orderItemPallet) { if (orderItemPallet.Id <= 0) return await AddOrderItemPalletAsync(orderItemPallet); - + return await UpdateOrderItemPalletAsync(orderItemPallet); } @@ -590,7 +612,7 @@ public class FruitBankDbContext : MgDbContextBase, if (orderItemDto == null || orderItemPallet.OrderItemId != orderItemDto.Id || //orderItemDto.IsOtherMeasuringInProgress(orderItemPallet.CreatorId) || orderItemPallet.TrayQuantity > orderItemDto.Quantity || !orderItemPallet.IsValidSafeMeasuringValues()) return false; - + orderItemDto.OrderItemPallets.UpdateCollection(orderItemPallet, false); if (orderItemDto.TrayQuantity > orderItemDto.Quantity) return false; @@ -598,32 +620,89 @@ public class FruitBankDbContext : MgDbContextBase, return true; } - private async Task UpdateProductDtoStockQuantityAsync(int productDtoId, bool publishEvent) - { - var productDto = await ProductDtos.GetByIdAsync(productDtoId, false); - if (productDto != null) return await UpdateProductDtoStockQuantityAsync(productDto, publishEvent); + //private async Task UpdateProductDtoStockQuantityAsync(int productDtoId, bool publishEvent) + //{ + // var productDto = await ProductDtos.GetByIdAsync(productDtoId, false); + // if (productDto != null) return await UpdateProductDtoStockQuantityAsync(productDto, publishEvent); - Logger.Error($"product == null; id: {productDtoId}"); - return await Task.FromResult(false); + // Logger.Error($"product == null; id: {productDtoId}"); + // return await Task.FromResult(false); + //} + + //private async Task UpdateProductDtoStockQuantityAsync(ProductDto productDto, bool publishEvent) + //{ + // //TODO: !!!!!!!!!!!!!!!! - J. + // //await _productService.AdjustInventoryAsync(product, quantityInc, string.Empty, ""); + + // await ProductDtos.UpdateAsync(productDto, publishEvent); + // return await Task.FromResult(true); + + // //var updatedRowsCount = await DataProvider.ExecuteNonQueryAsync($"update product set {nameof(Product.StockQuantity)} = {product.StockQuantity} where {nameof(Product.Id)} = {product.Id}"); + // //if (updatedRowsCount == 1) return await Task.FromResult(true); + + // //Logger.Error($"Product updatedRowsCount != 1; id: {product.Id}"); + // //return await Task.FromResult(false); + //} + + public async Task UpdateStockQuantityAndWeightAsync(int productId, int quantityToChange, string message, double weightToChange = 0, double stockWeight = 0) + { + var product = await Products.GetByIdAsync(productId); + if (weightToChange != 0 && stockWeight == 0) stockWeight = await _fruitBankAttributeService.GetGenericAttributeValueAsync(productId, nameof(IMeasuringNetWeight.NetWeight)); + + await UpdateStockQuantityAndWeightAsync(product, quantityToChange, message, weightToChange, stockWeight); } - private async Task UpdateProductDtoStockQuantityAsync(ProductDto productDto, bool publishEvent) + public Task UpdateStockQuantityAndWeightAsync(ProductDto productDto, int quantityToChange, string message, double weightToChange = 0) + => UpdateStockQuantityAndWeightAsync(productDto.Id, quantityToChange, message, weightToChange, productDto.NetWeight + weightToChange); + + public async Task UpdateStockQuantityAndWeightAsync(Product product, int quantityToChange, string message, double weightToChange = 0, double stockWeight = 0) { - //Itt mi legyen? RollBack? - J. - if (productDto.StockQuantity < 0) - Logger.Error($"productDto.StockQuantity < 0; Id: {productDto.Id}; StockQuantity: {productDto.StockQuantity}"); + var latStockQuantityHistoryId = 0; - //TODO: !!!!!!!!!!!!!!!! - J. - //await _productService.AdjustInventoryAsync(product, quantityInc, string.Empty, ""); + if (quantityToChange != 0) await _productService.AdjustInventoryAsync(product, quantityToChange, string.Empty, message); + else if (weightToChange != 0) + { + //Vizsgálja, h a quantityToChange != 0... - J. + //await _productService.AddStockQuantityHistoryEntryAsync(product, 0, product.StockQuantity, product.WarehouseId, message); - await ProductDtos.UpdateAsync(productDto, publishEvent); - return await Task.FromResult(true); + var historyEntry = new StockQuantityHistory + { + ProductId = product.Id, + CombinationId = null, + WarehouseId = product.WarehouseId > 0 ? (int?)product.WarehouseId : null, + QuantityAdjustment = 0, + StockQuantity = product.StockQuantity, + Message = message, + CreatedOnUtc = DateTime.UtcNow + }; - //var updatedRowsCount = await DataProvider.ExecuteNonQueryAsync($"update product set {nameof(Product.StockQuantity)} = {product.StockQuantity} where {nameof(Product.Id)} = {product.Id}"); - //if (updatedRowsCount == 1) return await Task.FromResult(true); + await StockQuantityHistories.InsertAsync(historyEntry); + latStockQuantityHistoryId = historyEntry.Id; + } + else return; - //Logger.Error($"Product updatedRowsCount != 1; id: {product.Id}"); - //return await Task.FromResult(false); + //await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight); + //if (weightToChange == 0 && stockWeight == 0) return; + + if (latStockQuantityHistoryId <= 0) + { + //A LastOrDefaultAsync elszáll! - J. + latStockQuantityHistoryId = await StockQuantityHistories.Table.Where(x => x.ProductId == product.Id).MaxAsync(x => x.Id); + if (latStockQuantityHistoryId == 0) + { + Logger.Error($"UpdateProductDtoStockQuantityAndWeightAsync (latStockQuantityHistory == 0). product.Id: {product.Id}"); + return; + } + } + + var stockQuantityHistoryExt = new StockQuantityHistoryExt + { + StockQuantityHistoryId = latStockQuantityHistoryId, + NetWeightAdjustment = double.Round(weightToChange, 1), + NetWeight = double.Round(stockWeight, 1), + }; + + await StockQuantityHistoriesExt.InsertAsync(stockQuantityHistoryExt, false); } public async Task> GetShippingDocumentsByShippingIdAsync(int shippingId) diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs index 61c2278..38a88e2 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs @@ -117,7 +117,10 @@ public class FruitBankEventConsumer : var productDtoNetWeight = productDto?.GenericAttributes.GetValueOrNull(nameof(IMeasuringNetWeight.NetWeight)); if (productDtoNetWeight == null || double.Round(productDtoNetWeight.Value, 1) != netWeight) + { await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight); + await _ctx.UpdateStockQuantityAndWeightAsync(productDto, 0, $"Manuális készlet súly változtatás az admin felületen.", netWeight - productDtoNetWeight.Value); + } //Tára var tare = double.Round(CommonHelper.To(form[nameof(ITare.Tare)].ToString()), 1);