diff --git a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs index b59c5dc..5dde516 100644 --- a/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs +++ b/Nop.Plugin.Misc.AIPlugin/Controllers/FruitBankDataController.cs @@ -30,7 +30,6 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers public class FruitBankDataController( FruitBankDbContext ctx, MeasurementService measurementService, - StockQuantityHistoryDtoDbTable stockQuantityHistoryDtoDbTable, IWorkContext workContext, ICustomerService customerService, ICustomerRegistrationService customerRegistrationService, @@ -54,11 +53,11 @@ namespace Nop.Plugin.Misc.FruitBankPlugin.Controllers [SignalR(SignalRTags.GetStockQuantityHistoryDtos)] public async Task> GetStockQuantityHistoryDtos() - => await stockQuantityHistoryDtoDbTable.GetAll(true).ToListAsync(); + => await ctx.StockQuantityHistoryDtos.GetAll(true).ToListAsync(); [SignalR(SignalRTags.GetStockQuantityHistoryDtosByProductId)] public async Task> GetStockQuantityHistoryDtosByProductId(int productId) - => await stockQuantityHistoryDtoDbTable.GetByProductIdAsync(productId, true).ToListAsync(); + => await ctx.StockQuantityHistoryDtos.GetByProductIdAsync(productId, true).ToListAsync(); [SignalR(SignalRTags.GetPartners)] public async Task> GetPartners() diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs index 3b273f0..db9b6fe 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs @@ -61,6 +61,7 @@ public class FruitBankDbContext : MgDbContextBase, public FilesDbTable Files { get; set; } public ShippingDocumentToFilesDbTable ShippingDocumentToFiles { get; set; } + public StockQuantityHistoryDtoDbTable StockQuantityHistoryDtos { get; set; } public IRepository Customers { get; set; } public IRepository CustomerRoles { get; set; } @@ -75,6 +76,7 @@ public class FruitBankDbContext : MgDbContextBase, PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingDocumentDbTable shippingDocumentDbTable, ShippingItemDbTable shippingItemDbTable, ShippingItemPalletDbTable shippingItemPalletDbTable, FilesDbTable filesDbTable, ShippingDocumentToFilesDbTable shippingDocumentToFilesDbTable, ProductDtoDbTable productDtoDbTable, OrderDtoDbTable orderDtoDbTable, OrderItemDtoDbTable orderItemDtoDbTable, OrderItemPalletDbTable orderItemPalletDbTable, + StockQuantityHistoryDtoDbTable stockQuantityHistoryDtos, IProductService productService, IStaticCacheManager staticCacheManager, IRepository orderRepository, IRepository orderItemRepository, @@ -120,6 +122,7 @@ public class FruitBankDbContext : MgDbContextBase, StockQuantityHistories = stockQuantityHistories; StockQuantityHistoriesExt = stockQuantityHistoriesExt; + StockQuantityHistoryDtos = stockQuantityHistoryDtos; } public IQueryable GetCustomersBySystemRoleName(string systemRoleName) @@ -696,8 +699,9 @@ public class FruitBankDbContext : MgDbContextBase, if (weightToChange == 0) return; - await _fruitBankAttributeService.InsertOrUpdateMeasuringAttributeValuesAsync(product.Id, weightToChange, true, true); - + var storeId = (await _storeContext.GetCurrentStoreAsync()).Id; + var newStockWeight = await _fruitBankAttributeService.InsertOrUpdateNetWeightAsync(product.Id, weightToChange, true, storeId); + if (latStockQuantityHistoryId <= 0) { //A LastOrDefaultAsync elszáll! - J. @@ -709,13 +713,15 @@ public class FruitBankDbContext : MgDbContextBase, } } - var stockWeight = double.Round(await _fruitBankAttributeService.GetGenericAttributeValueAsync(product.Id, nameof(IMeasuringNetWeight.NetWeight)), 1); + //var stockWeight = double.Round(await _fruitBankAttributeService.GetGenericAttributeValueAsync(product.Id, nameof(IMeasuringNetWeight.NetWeight), storeId), 1); + var lastStockQuantityHistoryExtNetWeight = await StockQuantityHistoryDtos.Table.Where(x => x.ProductId == product.Id && x.StockQuantityHistoryExt != null).Select(x => x.StockQuantityHistoryExt!.NetWeight).FirstOrDefaultAsync(netWeight => netWeight != null); var stockQuantityHistoryExt = new StockQuantityHistoryExt { StockQuantityHistoryId = latStockQuantityHistoryId, NetWeightAdjustment = weightToChange, - NetWeight = stockWeight, + NetWeight = newStockWeight, + IsInconsistent = lastStockQuantityHistoryExtNetWeight != null && double.Round(lastStockQuantityHistoryExtNetWeight.Value + weightToChange, 1) != newStockWeight }; 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 a751ce6..b637b75 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/EventConsumers/FruitBankEventConsumer.cs @@ -119,7 +119,7 @@ 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.GetValueOrDefault(0)); + await _ctx.UpdateStockQuantityAndWeightAsync(product, 0, $"Manuális készlet súly változtatás az admin felületen.", netWeight - productDtoNetWeight.GetValueOrDefault(0)); } //Tára diff --git a/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs b/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs index 1458992..ee51313 100644 --- a/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs +++ b/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs @@ -99,9 +99,30 @@ public class FruitBankAttributeService(IGenericAttributeService genericAttribute //await UpdateGenericAttributeAsync(measuringAttributes.Single(ma => ma.Key == IS_MEASURABLE_KEY), measuringAttributeValues.IsMeasurable); } - private async Task UpdateMeasuringWeightAttributeValueAsync(GenericAttribute genericAttribute, double newWeightValue, bool cumulativeWeightUpdate) + /// Inserted NetWeight + private async Task UpdateMeasuringWeightAttributeValueAsync(GenericAttribute genericAttribute, double newWeightValue, bool cumulativeWeightUpdate) { - await UpdateGenericAttributeAsync(genericAttribute, double.Round((cumulativeWeightUpdate ? CommonHelper.To(genericAttribute.Value) + newWeightValue : newWeightValue), 1)); + var newNetWeight = double.Round((cumulativeWeightUpdate ? CommonHelper.To(genericAttribute.Value) + newWeightValue : newWeightValue), 1); + await UpdateGenericAttributeAsync(genericAttribute, newNetWeight); + + return newNetWeight; + } + + /// Inserted NetWeight + public async Task InsertOrUpdateNetWeightAsync(int entityId, double netWeight, bool cumulativeWeightUpdate) + => await InsertOrUpdateNetWeightAsync(entityId, netWeight, cumulativeWeightUpdate, (await storeContext.GetCurrentStoreAsync()).Id); + + /// Inserted NetWeight + public async Task InsertOrUpdateNetWeightAsync(int entityId, double netWeight, bool cumulativeWeightUpdate, int storeId) + { + var netWeightGa = await GetGenericAttributeAsync(entityId, NET_WEIGHT_KEY, storeId); + + if (netWeightGa != null) return await UpdateMeasuringWeightAttributeValueAsync(netWeightGa, netWeight, cumulativeWeightUpdate); + + netWeight = double.Round(netWeight, 1); + await InsertGenericAttributeAsync(entityId, NET_WEIGHT_KEY, double.Round(netWeight, 1), storeId); + + return netWeight; } public async Task DeleteAllMeasuringAttributesAsync(int entityId, int storeId)