Add stock-taking enhancements and validation updates

Enhanced stock-taking functionality by introducing the `CloseStockTaking` method in `StockTakingDbContext` to manage session closures with validation. Added the `IsReadyForClose` method to `IMgStockTaking` and its implementation in `MgStockTaking`. Integrated `FruitBankDbContext` for stock updates.

Improved logging in `StockSignalREndpointServer` and added a new SignalR endpoint for closing stock-taking sessions. Updated `AddStockTaking` to initialize properties for new entries.

Simplified loading logic in `OrderItemDtoDbTable` by commenting out unnecessary `.ThenLoad` chains. Enhanced validation in `RefreshStockTakingItemMeasuredValuesFromPallets` and adjusted return statements in `AddOrUpdateMeasuredStockTakingItemPallet` and `UpdateStockTakingItemPallet` to fetch updated entities.

These changes improve maintainability, robustness, and functionality across the codebase.
This commit is contained in:
Loretta 2025-12-08 15:49:51 +01:00
parent bb553ed35d
commit e178c18f80
3 changed files with 52 additions and 6 deletions

View File

@ -23,6 +23,7 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC
[SignalR(SignalRTags.GetStockTakings)]
public async Task<List<StockTaking>> GetStockTakings(bool loadRelations)
{
_logger.Debug($"GetStockTakings invoke. loadRelations: {loadRelations}");
return await ctx.StockTakings.GetAll(loadRelations).ToListAsync();
}
@ -31,11 +32,27 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC
throw new NotImplementedException();
}
[SignalR(SignalRTags.CloseStockTaking)]
public async Task<StockTaking> 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<StockTaking> 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();

View File

@ -24,7 +24,7 @@ public class OrderItemDtoDbTable : MgDtoDbTableBase<OrderItemDto, OrderItem>
.LoadWith(oi => oi.OrderDto).ThenLoad(o => o.Customer)
.LoadWith(oi => oi.OrderDto).ThenLoad(o => o.OrderNotes)
.LoadWith(oi => oi.OrderDto).ThenLoad(o => o.GenericAttributes)
.LoadWith(oi => oi.OrderItemPallets).ThenLoad(oip => oip.OrderItemDto).ThenLoad(oi => oi.GenericAttributes)
.LoadWith(oi => oi.OrderItemPallets)//.ThenLoad(oip => oip.OrderItemDto).ThenLoad(oi => oi.GenericAttributes)
.LoadWith(oi => oi.ProductDto).ThenLoad(prod => prod.GenericAttributes);
}

View File

@ -25,6 +25,7 @@ public class StockTakingDbContext : MgDbContextBase,
IStockTakingItemDbSet<StockTakingItemDbTable>,
IStockTakingItemPalletDbSet<StockTakingItemPalletDbTable>
{
private FruitBankDbContext _fruitBankDbContext;
public ProductDtoDbTable ProductDtos { get; set; }
public OrderItemDtoDbTable OrderItemDtos { get; set; }
@ -42,7 +43,7 @@ public class StockTakingDbContext : MgDbContextBase,
private readonly IStaticCacheManager _staticCacheManager;
protected readonly IEventPublisher _eventPublisher;
public StockTakingDbContext(INopDataProvider dataProvider, ILockService lockService, IStoreContext storeContext,
public StockTakingDbContext(INopDataProvider dataProvider, ILockService lockService, IStoreContext storeContext, FruitBankDbContext fruitBankDbContext,
ProductDtoDbTable productDtoDbTable,
OrderItemDtoDbTable orderItemDtoDbTable,
StockQuantityHistoryDtoDbTable stockQuantityHistoryDtos,
@ -64,6 +65,8 @@ public class StockTakingDbContext : MgDbContextBase,
_productService = productService;
_staticCacheManager = staticCacheManager;
_fruitBankDbContext = fruitBankDbContext;
ProductDtos = productDtoDbTable;
OrderItemDtos = orderItemDtoDbTable;
@ -78,6 +81,28 @@ public class StockTakingDbContext : MgDbContextBase,
StockTakingItemPallets = stockTakingItemPalletDbTable;
}
public async Task CloseStockTaking(int stockTakingId)
{
var stockTaking = await StockTakings.GetByIdAsync(stockTakingId, true);
if (!stockTaking.IsReadyForClose()) throw new Exception($"Not all IsRequiredForMeasuring items are IsMeasured! IsReadyForClose: false;");
var count = 0;
foreach (var stockTakingItem in stockTaking.StockTakingItems!.Where(stockTakingItem => stockTakingItem is { IsMeasured: true, IsInvalid: false }))
{
count++;
//await _fruitBankDbContext.UpdateStockQuantityAndWeightAsync(stockTakingItem.Product!, stockTakingItem.QuantityDiff,
// $"Leltár által módosítva! stockTakingId: #{stockTaking.Id}, stockTakingItemId: #{stockTakingItem.Id}",
// stockTakingItem.NetWeightDiff);
}
stockTaking.IsClosed = true;
await StockTakings.UpdateAsync(stockTaking);
Logger.Info($"StockTaking closed! stockTakingId: {stockTaking.Id}; stockTakingItems count: {count}");
}
public async Task<StockTakingItemPallet> AddOrUpdateMeasuredStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
{
if (stockTakingItemPallet.Id == 0) return await AddStockTakingItemPallet(stockTakingItemPallet);
@ -94,7 +119,7 @@ public class StockTakingDbContext : MgDbContextBase,
return true;
});
return stockTakingItemPallet;
return await StockTakingItemPallets.GetByIdAsync(stockTakingItemPallet.Id);
}
public async Task<StockTakingItemPallet> UpdateStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
@ -107,16 +132,20 @@ public class StockTakingDbContext : MgDbContextBase,
return true;
});
return stockTakingItemPallet;
return await StockTakingItemPallets.GetByIdAsync(stockTakingItemPallet.Id);
}
private async Task RefreshStockTakingItemMeasuredValuesFromPallets(int stockTakingItemId)
{
var stockTakingItem = await StockTakingItems.GetByIdAsync(stockTakingItemId, true)!;
if (stockTakingItem.StockTaking!.IsClosed) throw new Exception($"stockTakingItem.StockTaking!.IsClosed");
if (stockTakingItem.IsInvalid) throw new Exception($"stockTakingItem.IsInvalid");
if (stockTakingItem.StockTaking!.IsClosed) throw new Exception($"stockTakingItem.StockTaking.IsClosed");
if (stockTakingItem.StockTakingItemPallets!.Count == 0) throw new Exception($"stockTakingItem.StockTakingItemPallets.Count == 0");
if (stockTakingItem.StockTakingItemPallets!.Any(x => !x.IsValidMeasuringValues(stockTakingItem.IsMeasurable))) throw new Exception($"IsValidMeasuringValues == false");
stockTakingItem.IsMeasured = stockTakingItem.StockTakingItemPallets!.Count > 0;
stockTakingItem.IsMeasured = true;
stockTakingItem.MeasuredStockQuantity = stockTakingItem.StockTakingItemPallets.Sum(x => x.TrayQuantity);
if (stockTakingItem.IsMeasurable) stockTakingItem.MeasuredNetWeight = double.Round(stockTakingItem.StockTakingItemPallets.Sum(x => x.NetWeight), 1);