diff --git a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/StockSignalREndpointServer.cs b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/StockSignalREndpointServer.cs index 2562c71..82ada7e 100644 --- a/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/StockSignalREndpointServer.cs +++ b/Nop.Plugin.Misc.AIPlugin/Areas/Admin/Controllers/StockSignalREndpointServer.cs @@ -1,11 +1,14 @@ using AyCode.Core.Loggers; using AyCode.Services.SignalRs; +using DevExpress.Data.Helpers; using FruitBank.Common.Entities; +using FruitBank.Common.Enums; using FruitBank.Common.Server.Interfaces; using FruitBank.Common.Server.Services.SignalRs; 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.Domains.DataLayer; @@ -18,9 +21,9 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC private readonly ILogger _logger = new Logger(logWriters.ToArray()); [SignalR(SignalRTags.GetStockTakings)] - public async Task> GetStockTakings() + public async Task> GetStockTakings(bool loadRelations) { - return await ctx.StockTakings.GetAll(true).ToListAsync(); + return await ctx.StockTakings.GetAll(loadRelations).ToListAsync(); } public async Task> GetStockTakingsByProductId(int productId) @@ -37,15 +40,21 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC var productDtos = await ctx.ProductDtos.GetAll(true).ToListAsync(); + var orderItemDtos = (await ctx.OrderItemDtos.GetAllByProductIds(productDtos.Select(p => p.Id)) + .Where(oi => oi.OrderDto.OrderStatusId != (int)OrderStatus.Complete).ToArrayAsync()) + .Where(x => x.MeasuringStatus == MeasuringStatus.NotStarted).ToLookup(k => k.ProductId, v => v); + foreach (var productDto in productDtos) { var stockTakingItem = new StockTakingItem { StockTakingId = stockTaking.Id, ProductId = productDto.Id, - //IsMeasurable = productDto.IsMeasurable, + IsMeasurable = productDto.IsMeasurable, OriginalStockQuantity = productDto.StockQuantity, - OriginalNetWeight = productDto.NetWeight + InProcessOrdersQuantity = orderItemDtos[productDto.Id].Sum(x => x.Quantity), + //A NetWeight-et nem növeljük meg, mert az nem vonódik le automatikusan a rendelés létrehozásakor! - J. + OriginalNetWeight = productDto.NetWeight //double.Round(productDto.NetWeight + orderItemDtos[productDto.Id].Sum(x => x.NetWeight), 1) }; await ctx.StockTakingItems.InsertAsync(stockTakingItem); @@ -59,9 +68,13 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC } + [SignalR(SignalRTags.UpdateStockTaking)] public async Task UpdateStockTaking(StockTaking stockTaking) { - throw new NotImplementedException(); + if(stockTaking == null) return null; + + await ctx.StockTakings.UpdateAsync(stockTaking); + return await ctx.StockTakings.GetByIdAsync(stockTaking.Id, true); } [SignalR(SignalRTags.GetStockTakingItems)] @@ -77,14 +90,16 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC return result; } + [SignalR(SignalRTags.GetStockTakingItemsByProductId)] public async Task> GetStockTakingItemsByProductId(int productId) { - throw new NotImplementedException(); + return await ctx.StockTakingItems.GetAllByProductId(productId, true).ToListAsync(); } + [SignalR(SignalRTags.GetStockTakingItemsByStockTakingId)] public async Task> GetStockTakingItemsByStockTakingId(int stockTakingId) { - throw new NotImplementedException(); + return await ctx.StockTakingItems.GetAllByStockTakingId(stockTakingId, true).ToListAsync(); } public async Task AddStockTakingItem(StockTakingItem stockTakingItem) @@ -109,11 +124,17 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC public async Task AddStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) { - throw new NotImplementedException(); + return await ctx.AddStockTakingItemPallet(stockTakingItemPallet); } public async Task UpdateStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) { - throw new NotImplementedException(); + return await ctx.UpdateStockTakingItemPallet(stockTakingItemPallet); + } + + [SignalR(SignalRTags.AddOrUpdateMeasuredStockTakingItemPallet)] + public async Task AddOrUpdateMeasuredStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) + { + return await ctx.AddOrUpdateMeasuredStockTakingItemPallet(stockTakingItemPallet); } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderDtoDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderDtoDbTable.cs index 3e19d00..c4583e0 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderDtoDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderDtoDbTable.cs @@ -1,14 +1,15 @@ using FruitBank.Common.Dtos; using FruitBank.Common.Entities; using LinqToDB; +using Mango.Nop.Core.Loggers; using Mango.Nop.Data.Repositories; using Nop.Core.Caching; using Nop.Core.Configuration; using Nop.Core.Domain.Orders; +using Nop.Core.Domain.Payments; using Nop.Core.Events; using Nop.Data; -using Mango.Nop.Core.Loggers; -using Nop.Core.Domain.Payments; +using System.Linq; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; @@ -47,5 +48,8 @@ public class OrderDtoDbTable : MgDtoDbTableBase public IQueryable GetAllByProductId(int productId, bool loadRelations = true) => GetAll(loadRelations).Where(o => o.OrderItemDtos.Any(oi => oi.ProductId == productId)); + public IQueryable GetAllByProductIds(IEnumerable productIds, bool loadRelations = true) + => GetAll(loadRelations).Where(o => o.OrderItemDtos.Any(oi => productIds.Contains(oi.ProductId))); + public IQueryable GetAllByIds(IEnumerable orderIds, bool loadRelations = true) => GetAll(loadRelations).Where(o => orderIds.Contains(o.Id)); } diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemDtoDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemDtoDbTable.cs index 55d8eb9..dca5e88 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemDtoDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/OrderItemDtoDbTable.cs @@ -31,8 +31,12 @@ public class OrderItemDtoDbTable : MgDtoDbTableBase public Task GetByIdAsync(int orderItemId, bool loadRelations) => GetAll(loadRelations).Where(oi => oi.Id == orderItemId).FirstOrDefaultAsync(null); public IQueryable GetAllByOrderId(int orderId, bool loadRelations = true) => GetAll(loadRelations).Where(oi => oi.OrderId == orderId); + public IQueryable GetAllByProductId(int productId, bool loadRelations = true) => GetAll(loadRelations).Where(oi => oi.ProductId == productId); + public IQueryable GetAllByProductIds(IEnumerable productIds, bool loadRelations = true) + => GetAll(loadRelations).Where(oi => productIds.Contains(oi.ProductId)); + public IQueryable GetAllByIds(IEnumerable orderItemIds, bool loadRelations = true) => GetAll(loadRelations).Where(oi => orderItemIds.Contains(oi.Id)); public IQueryable GetAllByOrderIds(IEnumerable orderIds, bool loadRelations = true) => GetAll(loadRelations).Where(oi => orderIds.Contains(oi.OrderId)); } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingDbContext.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingDbContext.cs index 7f77053..7cf2de4 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingDbContext.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingDbContext.cs @@ -1,6 +1,8 @@ #nullable enable +using System.Threading.Tasks; using AyCode.Core.Loggers; using FruitBank.Common.Dtos; +using FruitBank.Common.Entities; using Mango.Nop.Core.Entities; using Mango.Nop.Core.Loggers; using Mango.Nop.Data.Repositories; @@ -17,12 +19,14 @@ using Nop.Services.Catalog; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; -public class StockTakingDbContext : MgDbContextBase, +public class StockTakingDbContext : MgDbContextBase, IStockTakingDbSet, + IOrderItemDtoDbSet, IStockTakingItemDbSet, IStockTakingItemPalletDbSet { public ProductDtoDbTable ProductDtos { get; set; } + public OrderItemDtoDbTable OrderItemDtos { get; set; } public StockTakingDbTable StockTakings { get; set; } public StockTakingItemDbTable StockTakingItems { get; set; } @@ -40,6 +44,7 @@ public class StockTakingDbContext : MgDbContextBase, public StockTakingDbContext(INopDataProvider dataProvider, ILockService lockService, IStoreContext storeContext, ProductDtoDbTable productDtoDbTable, + OrderItemDtoDbTable orderItemDtoDbTable, StockQuantityHistoryDtoDbTable stockQuantityHistoryDtos, StockTakingDbTable stockTakingDbTable, StockTakingItemDbTable stockTakingItemDbTable, @@ -60,6 +65,7 @@ public class StockTakingDbContext : MgDbContextBase, _staticCacheManager = staticCacheManager; ProductDtos = productDtoDbTable; + OrderItemDtos = orderItemDtoDbTable; GenericAttributes = genericAttributes; @@ -71,4 +77,50 @@ public class StockTakingDbContext : MgDbContextBase, StockTakingItems = stockTakingItemDbTable; StockTakingItemPallets = stockTakingItemPalletDbTable; } + + public async Task AddOrUpdateMeasuredStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) + { + if (stockTakingItemPallet.Id == 0) return await AddStockTakingItemPallet(stockTakingItemPallet); + return await UpdateStockTakingItemPallet(stockTakingItemPallet); + } + + public async Task AddStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) + { + await TransactionSafeAsync(async _ => + { + await StockTakingItemPallets.InsertAsync(stockTakingItemPallet); + await RefreshStockTakingItemMeasuredValuesFromPallets(stockTakingItemPallet.StockTakingItemId); + + return true; + }); + + return stockTakingItemPallet; + } + + public async Task UpdateStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) + { + await TransactionSafeAsync(async _ => + { + await StockTakingItemPallets.UpdateAsync(stockTakingItemPallet); + await RefreshStockTakingItemMeasuredValuesFromPallets(stockTakingItemPallet.StockTakingItemId); + + return true; + }); + + return stockTakingItemPallet; + } + + private async Task RefreshStockTakingItemMeasuredValuesFromPallets(int stockTakingItemId) + { + var stockTakingItem = await StockTakingItems.GetByIdAsync(stockTakingItemId, true)!; + + if (stockTakingItem.StockTaking!.IsClosed) throw new Exception($"stockTakingItem.StockTaking!.IsClosed"); + + stockTakingItem.IsMeasured = stockTakingItem.StockTakingItemPallets!.Count > 0; + stockTakingItem.MeasuredStockQuantity = stockTakingItem.StockTakingItemPallets.Sum(x => x.TrayQuantity); + + if (stockTakingItem.IsMeasurable) stockTakingItem.MeasuredNetWeight = double.Round(stockTakingItem.StockTakingItemPallets.Sum(x => x.NetWeight), 1); + + await StockTakingItems.UpdateAsync(stockTakingItem); + } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingDbTable.cs index 7f187c7..be44211 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingDbTable.cs @@ -24,7 +24,7 @@ public class StockTakingDbTable : MgDbTableBase ? GetAll() .LoadWith(st => st.StockTakingItems).ThenLoad(sti => sti.Product).ThenLoad(prod => prod.GenericAttributes) //.LoadWith(st => st.StockTakingItems).ThenLoad(sti => sti.StockTakingItemPallets) - : GetAll().LoadWith(st => st.StockTakingItems); + : GetAll();//.LoadWith(st => st.StockTakingItems); } public Task GetByIdAsync(int id, bool loadRelations) diff --git a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingItemDbTable.cs b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingItemDbTable.cs index 324b819..65e98b2 100644 --- a/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingItemDbTable.cs +++ b/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/StockTakingItemDbTable.cs @@ -22,10 +22,17 @@ public class StockTakingItemDbTable : MgDbTableBase return loadRelations ? GetAll() .LoadWith(sti => sti.StockTaking) - //.LoadWith(sti => sti.StockTakingItemPallets) + .LoadWith(sti => sti.StockTakingItemPallets) .LoadWith(sti => sti.Product).ThenLoad(prod => prod.GenericAttributes) : GetAll(); } public Task GetByIdAsync(int stockTakingItemId, bool loadRelations) => GetAll(loadRelations).FirstOrDefaultAsync(sti => sti.Id == stockTakingItemId); + + public IQueryable GetAllByProductId(int productId, bool loadRelations) + => GetAll(loadRelations).Where(x => x.ProductId == productId); + + public IQueryable GetAllByStockTakingId(int stockTakingId, bool loadRelations) + => GetAll(loadRelations).Where(x => x.StockTakingId == stockTakingId); + } \ No newline at end of file