Refactor stock taking logic; add item refresh endpoint

Refactored StockSignalREndpointServer to use helper methods for creating and updating StockTakingItem objects. Added a new SignalR endpoint to refresh StockTakingItem values. Updated order item retrieval logic and switched the connection string to use the FruitBank_DEV database. Commented-out logic for handling virtual stock was moved to helper methods.
This commit is contained in:
Loretta 2026-03-04 15:22:30 +01:00
parent efebc96394
commit 126849d930
1 changed files with 62 additions and 19 deletions

View File

@ -1,18 +1,23 @@
using AyCode.Core.Extensions;
using System.Collections;
using AyCode.Core.Extensions;
using AyCode.Core.Loggers;
using AyCode.Services.SignalRs;
using DevExpress.Data.Helpers;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using FruitBank.Common.Dtos;
using FruitBank.Common.Entities;
using FruitBank.Common.Enums;
using FruitBank.Common.Interfaces;
using FruitBank.Common.Server.Interfaces;
using FruitBank.Common.Server.Services.SignalRs;
using FruitBank.Common.SignalRs;
using Mango.Nop.Core.Entities;
using Mango.Nop.Core.Loggers;
using Nop.Core;
using Nop.Core.Domain.Orders;
using Nop.Core.Events;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
using System.Linq;
namespace Nop.Plugin.Misc.FruitBankPlugin.Areas.Admin.Controllers;
@ -22,6 +27,30 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC
private const int LastStockTakingDays = 15;
private readonly ILogger _logger = new Logger<StockSignalREndpointServer>(logWriters.ToArray());
private static StockTakingItem CreateStockTakingItem(int stockTakingId, ProductDto productDto, IEnumerable<OrderItemDto> orderItemDtos)
{
//if (productDto.StockQuantity < 0 && productDto.IncomingQuantity > 0)
//{
// _logger.Info($"Beszállítás alatt lévő 'virtuális' készlet, nem leltározzuk! product: [#{productDto.Id}] {productDto.Name}; StockQuantity: {productDto.StockQuantity}; IncomingQuantity: {productDto.IncomingQuantity}");
// return null;
//}
var stockTakingItem = new StockTakingItem { StockTakingId = stockTakingId };
UpdateStockTakingItemValues(stockTakingItem, productDto, orderItemDtos);
return stockTakingItem;
}
private static void UpdateStockTakingItemValues(StockTakingItem stockTakingItem, ProductDto productDto, IEnumerable<OrderItemDto> orderItemDtos)
{
stockTakingItem.ProductId = productDto.Id;
stockTakingItem.IsMeasurable = productDto.IsMeasurable;
stockTakingItem.OriginalStockQuantity = productDto.StockQuantity;
stockTakingItem.InProcessOrdersQuantity = orderItemDtos.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.
stockTakingItem.OriginalNetWeight = productDto.NetWeight; //double.Round(productDto.NetWeight + orderItemDtos[productDto.Id].Sum(x => x.NetWeight), 1)
}
[SignalR(SignalRTags.GetStockTakings)]
public async Task<List<StockTaking>> GetStockTakings(bool loadRelations)
{
@ -61,28 +90,14 @@ 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))
var orderItemDtos = (await ctx.OrderItemDtos.GetAllByProductIds(productDtos.Select(p => p.Id), true)
.Where(oi => oi.OrderDto.OrderStatusId != (int)OrderStatus.Complete).ToArrayAsync())
.Where(x => x.MeasuringStatus == MeasuringStatus.NotStarted).ToLookup(k => k.ProductId, v => v);
.Where(oi => oi.MeasuringStatus == MeasuringStatus.NotStarted).ToLookup(k => k.ProductId, v => v);
foreach (var productDto in productDtos)
{
//if (productDto.StockQuantity < 0 && productDto.IncomingQuantity > 0)
//{
// _logger.Info($"Beszállítás alatt lévő 'virtuális' készlet, nem leltározzuk! product: [#{productDto.Id}] {productDto.Name}; StockQuantity: {productDto.StockQuantity}; IncomingQuantity: {productDto.IncomingQuantity}");
// continue;
//}
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)
};
var stockTakingItem = CreateStockTakingItem(stockTaking.Id, productDto, orderItemDtos[productDto.Id]);
if (stockTakingItem == null) continue;
await ctx.StockTakingItems.InsertAsync(stockTakingItem);
}
@ -92,7 +107,35 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC
if (result) return await ctx.StockTakings.GetByIdAsync(stockTaking.Id, false);
return null;
}
[SignalR(SignalRTags.RefreshStockTakingItem)]
public async Task<StockTakingItem> RefreshStockTakingItem(int stockTakingItemId)
{
var result = await ctx.TransactionSafeAsync(async _ =>
{
var stockTakingItem = await ctx.StockTakingItems.GetByIdAsync(stockTakingItemId, true);
if (stockTakingItem == null || stockTakingItem.StockTaking.IsClosed || stockTakingItem.IsMeasured)
{
_logger.Error($"StockTakingItem refresh ERROR! stockTakingItemId: {stockTakingItemId}; StockTaking is closed: {stockTakingItem?.StockTaking.IsClosed}; IsMeasured: {stockTakingItem?.IsMeasured}");
return false;
}
var productDto = await ctx.ProductDtos.GetByIdAsync(stockTakingItem.ProductId, true);
var orderItemDtos = (await ctx.OrderItemDtos.GetAllByProductId(productDto.Id, true)
.Where(oi => oi.OrderDto.OrderStatusId != (int)OrderStatus.Complete).ToArrayAsync())
.Where(oi => oi.MeasuringStatus == MeasuringStatus.NotStarted);
UpdateStockTakingItemValues(stockTakingItem, productDto, orderItemDtos);
await ctx.StockTakingItems.UpdateAsync(stockTakingItem);
return true;
});
if (result) return await ctx.StockTakingItems.GetByIdAsync(stockTakingItemId, false);
return null;
}
[SignalR(SignalRTags.UpdateStockTaking)]