using AyCode.Core.Extensions; 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; namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers; public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToClientService sendToClient, IEventPublisher eventPublisher, IWorkContext workContext, IEnumerable logWriters) : IStockSignalREndpointServer { private const int LastStockTakingDays = 15; private readonly ILogger _logger = new Logger(logWriters.ToArray()); [SignalR(SignalRTags.GetStockTakings)] public async Task> GetStockTakings(bool loadRelations) { _logger.Debug($"GetStockTakings invoke. loadRelations: {loadRelations}"); return await ctx.StockTakings.GetAll(loadRelations).ToListAsync(); } public async Task> GetStockTakingsByProductId(int productId) { throw new NotImplementedException(); } [SignalR(SignalRTags.CloseStockTaking)] public async Task CloseStockTaking(int stockTakingId) { var result = await ctx.TransactionSafeAsync(async _ => { await ctx.CloseStockTaking(stockTakingId); return true; }); if (result) return await ctx.StockTakings.GetByIdAsync(stockTakingId, false); return null; } [SignalR(SignalRTags.AddStockTaking)] public async Task AddStockTaking(StockTaking stockTaking) { var result = await ctx.TransactionSafeAsync(async _ => { stockTaking.IsClosed = false; stockTaking.StartDateTime = DateTime.Now; await ctx.StockTakings.InsertAsync(stockTaking); 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, OriginalStockQuantity = productDto.StockQuantity, 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); } return true; }); if (result) return await ctx.StockTakings.GetByIdAsync(stockTaking.Id, false); return null; } [SignalR(SignalRTags.UpdateStockTaking)] public async Task UpdateStockTaking(StockTaking stockTaking) { if(stockTaking == null) return null; await ctx.StockTakings.UpdateAsync(stockTaking); return await ctx.StockTakings.GetByIdAsync(stockTaking.Id, true); } [SignalR(SignalRTags.GetStockTakingItems)] public async Task> GetStockTakingItems() { return await ctx.StockTakingItems.GetAll(true).ToListAsync(); } [SignalR(SignalRTags.GetStockTakingItemsById)] public async Task GetStockTakingItemsById(int stockTakingItemId) { var result = await ctx.StockTakingItems.GetByIdAsync(stockTakingItemId, true); return result; } [SignalR(SignalRTags.GetStockTakingItemsByProductId)] public async Task> GetStockTakingItemsByProductId(int productId) { return await ctx.StockTakingItems.GetAllByProductId(productId, true).ToListAsync(); } [SignalR(SignalRTags.GetStockTakingItemsByStockTakingId)] public async Task> GetStockTakingItemsByStockTakingId(int stockTakingId) { return await ctx.StockTakingItems.GetAllByStockTakingId(stockTakingId, true).ToListAsync(); } public async Task AddStockTakingItem(StockTakingItem stockTakingItem) { throw new NotImplementedException(); } public async Task UpdateStockTakingItem(StockTakingItem stockTakingItem) { throw new NotImplementedException(); } public async Task> GetStockTakingItemPallets() { throw new NotImplementedException(); } public async Task> GetStockTakingItemPalletsByProductId(int productId) { throw new NotImplementedException(); } public async Task AddStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) { return await ctx.AddStockTakingItemPallet(stockTakingItemPallet); } public async Task UpdateStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) { return await ctx.UpdateStockTakingItemPallet(stockTakingItemPallet); } [SignalR(SignalRTags.AddOrUpdateMeasuredStockTakingItemPallet)] public async Task AddOrUpdateMeasuredStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) { return await ctx.AddOrUpdateMeasuredStockTakingItemPallet(stockTakingItemPallet); } }