From f2e99f30506b3eb1f6cb176631920861a54dda29 Mon Sep 17 00:00:00 2001 From: Loretta Date: Fri, 14 Nov 2025 15:04:17 +0100 Subject: [PATCH] improvements, fixes... --- .../Domains/DataLayer/FruitBankDbContext.cs | 74 +++++++++++-------- .../EventConsumers/FruitBankEventConsumer.cs | 8 +- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs index 9a50d7e..c64dd95 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs @@ -309,11 +309,11 @@ public class FruitBankDbContext : MgDbContextBase, { weightToChange = productIdChanged ? shippingItem.MeasuredNetWeight : shippingItem.MeasuredNetWeight - dbShippingItem.MeasuredNetWeight; - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(productDto.Id, weightToChange, shippingItem.IsMeasurable, true); + //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; + //productDto!.StockQuantity += quantityInc; } //if (productIdUnchanged || !dbShippingItem.IsMeasured) return true; @@ -336,14 +336,17 @@ public class FruitBankDbContext : MgDbContextBase, (productDto.Id, nameof(IIncomingQuantity.IncomingQuantity), incomingQuantity.Value + dbShippingItem.MeasuredQuantity); } - if (productIsMeasurable) - { - var measuringValues = new MeasuringAttributeValues(productDto.Id, -dbShippingItem.MeasuredNetWeight, dbShippingItem.IsMeasurable); - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(measuringValues, true); - } + //if (productIsMeasurable) + //{ + // 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; + 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. @@ -499,18 +502,18 @@ public class FruitBankDbContext : MgDbContextBase, if (!orderItemDto.IsMeasurable) continue; - var prevNetWeightFromGa = orderItemDto.GenericAttributes.GetValueOrDefault(nameof(IMeasuringNetWeight.NetWeight), 0); + var prevOrderItemNetWeightFromGa = orderItemDto.GenericAttributes.GetValueOrDefault(nameof(IMeasuringNetWeight.NetWeight), 0); //var gaNetWeight = CommonHelper.To(orderItemDto.GenericAttributes.FirstOrDefault(x => x.Key == nameof(IMeasuringNetWeight.NetWeight))?.Value ?? "0"); - await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync - (orderItemDto.Id, nameof(IMeasuringNetWeight.NetWeight), orderItemDto.NetWeight); + await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(orderItemDto.Id, nameof(IMeasuringNetWeight.NetWeight), orderItemDto.NetWeight); - var weightToChange = -(orderItemDto.NetWeight - prevNetWeightFromGa); + var productWeightToChange = -(orderItemDto.NetWeight - prevOrderItemNetWeightFromGa); - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync - (orderItemDto.ProductId, weightToChange, orderItemDto.IsMeasurable, true); + //await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(orderItemDto.ProductId, productWeightToChange, 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 UpdateStockQuantityAndWeightAsync(orderItemDto.ProductId, 0, + $"Kimenő mérés, OrderStatus set to complete. Rendelés: #{orderDto.Id}, rendelés tétel: #{orderItemDto.Id}", + productWeightToChange); } //await _eventPublisher.PublishAsync(new OrderStatusChangedEvent(order, prevOrderStatus)); @@ -644,19 +647,30 @@ public class FruitBankDbContext : MgDbContextBase, // //return await Task.FromResult(false); //} - public async Task UpdateStockQuantityAndWeightAsync(int productId, int quantityToChange, string message, double weightToChange = 0, double stockWeight = 0) + public async Task UpdateStockQuantityAndWeightAsync(int productId, int quantityToChange, string message, double weightToChange = 0) { - var product = await Products.GetByIdAsync(productId); - if (weightToChange != 0 && stockWeight == 0) stockWeight = await _fruitBankAttributeService.GetGenericAttributeValueAsync(productId, nameof(IMeasuringNetWeight.NetWeight)); + if (quantityToChange == 0 && weightToChange == 0) return; - await UpdateStockQuantityAndWeightAsync(product, quantityToChange, message, weightToChange, stockWeight); + var product = await Products.GetByIdAsync(productId); + await UpdateStockQuantityAndWeightAsync(product, quantityToChange, message, weightToChange); } - 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) + public async Task UpdateStockQuantityAndWeightAsync(ProductDto productDto, int quantityToChange, string message, double weightToChange = 0) { + if (quantityToChange == 0 && weightToChange == 0) return; + + await UpdateStockQuantityAndWeightAsync(productDto.Id, quantityToChange, message, weightToChange); + productDto.StockQuantity += quantityToChange; + + if (weightToChange == 0) return; + productDto.GenericAttributes = await GenericAttributes.Table.Where(x => x.EntityId == productDto.Id && x.KeyGroup == nameof(Product) && x.StoreId == _storeContext.GetCurrentStore().Id).ToListAsync(); + } + + public async Task UpdateStockQuantityAndWeightAsync(Product product, int quantityToChange, string message, double weightToChange = 0) + { + weightToChange = double.Round(weightToChange, 1); + if (quantityToChange == 0 && weightToChange == 0) return; + var latStockQuantityHistoryId = 0; if (quantityToChange != 0) await _productService.AdjustInventoryAsync(product, quantityToChange, string.Empty, message); @@ -679,10 +693,10 @@ public class FruitBankDbContext : MgDbContextBase, await StockQuantityHistories.InsertAsync(historyEntry); latStockQuantityHistoryId = historyEntry.Id; } - else return; - //await _fruitBankAttributeService.InsertOrUpdateGenericAttributeAsync(product.Id, nameof(IMeasuringNetWeight.NetWeight), netWeight); - //if (weightToChange == 0 && stockWeight == 0) return; + if (weightToChange == 0) return; + + await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, weightToChange, true, true); if (latStockQuantityHistoryId <= 0) { @@ -695,11 +709,13 @@ public class FruitBankDbContext : MgDbContextBase, } } + var stockWeight = double.Round(await _fruitBankAttributeService.GetGenericAttributeValueAsync(product.Id, nameof(IMeasuringNetWeight.NetWeight)), 1); + var stockQuantityHistoryExt = new StockQuantityHistoryExt { StockQuantityHistoryId = latStockQuantityHistoryId, - NetWeightAdjustment = double.Round(weightToChange, 1), - NetWeight = double.Round(stockWeight, 1), + NetWeightAdjustment = weightToChange, + NetWeight = stockWeight, }; await StockQuantityHistoriesExt.InsertAsync(stockQuantityHistoryExt, false); diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs index 38a88e2..a751ce6 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs @@ -45,7 +45,7 @@ public class FruitBankEventConsumer : _measurementService = measurementService; _fruitBankAttributeService = fruitBankAttributeService; } - + public override async Task HandleEventAsync(EntityUpdatedEvent eventMessage) { var product = await CheckAndUpdateProductManageInventoryMethodToManageStock(eventMessage.Entity); @@ -118,8 +118,8 @@ public class FruitBankEventConsumer : 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); + //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.GetValueOrDefault(0)); } //Tára @@ -277,7 +277,7 @@ public class FruitBankEventConsumer : await _measurementService.OrderItemInsertedOrUpdatedPostProcess(eventMessage.Entity); } - + } #endregion Delete