StockTaking in progress...

This commit is contained in:
Loretta 2025-12-04 13:52:46 +01:00
parent 070d7ec3d2
commit bb553ed35d
6 changed files with 102 additions and 14 deletions

View File

@ -1,11 +1,14 @@
using AyCode.Core.Loggers; using AyCode.Core.Loggers;
using AyCode.Services.SignalRs; using AyCode.Services.SignalRs;
using DevExpress.Data.Helpers;
using FruitBank.Common.Entities; using FruitBank.Common.Entities;
using FruitBank.Common.Enums;
using FruitBank.Common.Server.Interfaces; using FruitBank.Common.Server.Interfaces;
using FruitBank.Common.Server.Services.SignalRs; using FruitBank.Common.Server.Services.SignalRs;
using FruitBank.Common.SignalRs; using FruitBank.Common.SignalRs;
using Mango.Nop.Core.Loggers; using Mango.Nop.Core.Loggers;
using Nop.Core; using Nop.Core;
using Nop.Core.Domain.Orders;
using Nop.Core.Events; using Nop.Core.Events;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
@ -18,9 +21,9 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC
private readonly ILogger _logger = new Logger<StockSignalREndpointServer>(logWriters.ToArray()); private readonly ILogger _logger = new Logger<StockSignalREndpointServer>(logWriters.ToArray());
[SignalR(SignalRTags.GetStockTakings)] [SignalR(SignalRTags.GetStockTakings)]
public async Task<List<StockTaking>> GetStockTakings() public async Task<List<StockTaking>> GetStockTakings(bool loadRelations)
{ {
return await ctx.StockTakings.GetAll(true).ToListAsync(); return await ctx.StockTakings.GetAll(loadRelations).ToListAsync();
} }
public async Task<List<StockTaking>> GetStockTakingsByProductId(int productId) public async Task<List<StockTaking>> GetStockTakingsByProductId(int productId)
@ -37,15 +40,21 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC
var productDtos = await ctx.ProductDtos.GetAll(true).ToListAsync(); 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) foreach (var productDto in productDtos)
{ {
var stockTakingItem = new StockTakingItem var stockTakingItem = new StockTakingItem
{ {
StockTakingId = stockTaking.Id, StockTakingId = stockTaking.Id,
ProductId = productDto.Id, ProductId = productDto.Id,
//IsMeasurable = productDto.IsMeasurable, IsMeasurable = productDto.IsMeasurable,
OriginalStockQuantity = productDto.StockQuantity, 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); await ctx.StockTakingItems.InsertAsync(stockTakingItem);
@ -59,9 +68,13 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC
} }
[SignalR(SignalRTags.UpdateStockTaking)]
public async Task<StockTaking> UpdateStockTaking(StockTaking stockTaking) public async Task<StockTaking> 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)] [SignalR(SignalRTags.GetStockTakingItems)]
@ -77,14 +90,16 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC
return result; return result;
} }
[SignalR(SignalRTags.GetStockTakingItemsByProductId)]
public async Task<List<StockTakingItem>> GetStockTakingItemsByProductId(int productId) public async Task<List<StockTakingItem>> GetStockTakingItemsByProductId(int productId)
{ {
throw new NotImplementedException(); return await ctx.StockTakingItems.GetAllByProductId(productId, true).ToListAsync();
} }
[SignalR(SignalRTags.GetStockTakingItemsByStockTakingId)]
public async Task<List<StockTakingItem>> GetStockTakingItemsByStockTakingId(int stockTakingId) public async Task<List<StockTakingItem>> GetStockTakingItemsByStockTakingId(int stockTakingId)
{ {
throw new NotImplementedException(); return await ctx.StockTakingItems.GetAllByStockTakingId(stockTakingId, true).ToListAsync();
} }
public async Task<StockTakingItem> AddStockTakingItem(StockTakingItem stockTakingItem) public async Task<StockTakingItem> AddStockTakingItem(StockTakingItem stockTakingItem)
@ -109,11 +124,17 @@ public class StockSignalREndpointServer(StockTakingDbContext ctx, SignalRSendToC
public async Task<StockTakingItemPallet> AddStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) public async Task<StockTakingItemPallet> AddStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
{ {
throw new NotImplementedException(); return await ctx.AddStockTakingItemPallet(stockTakingItemPallet);
} }
public async Task<StockTakingItemPallet> UpdateStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet) public async Task<StockTakingItemPallet> UpdateStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
{ {
throw new NotImplementedException(); return await ctx.UpdateStockTakingItemPallet(stockTakingItemPallet);
}
[SignalR(SignalRTags.AddOrUpdateMeasuredStockTakingItemPallet)]
public async Task<StockTakingItemPallet> AddOrUpdateMeasuredStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
{
return await ctx.AddOrUpdateMeasuredStockTakingItemPallet(stockTakingItemPallet);
} }
} }

View File

@ -1,14 +1,15 @@
using FruitBank.Common.Dtos; using FruitBank.Common.Dtos;
using FruitBank.Common.Entities; using FruitBank.Common.Entities;
using LinqToDB; using LinqToDB;
using Mango.Nop.Core.Loggers;
using Mango.Nop.Data.Repositories; using Mango.Nop.Data.Repositories;
using Nop.Core.Caching; using Nop.Core.Caching;
using Nop.Core.Configuration; using Nop.Core.Configuration;
using Nop.Core.Domain.Orders; using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Payments;
using Nop.Core.Events; using Nop.Core.Events;
using Nop.Data; using Nop.Data;
using Mango.Nop.Core.Loggers; using System.Linq;
using Nop.Core.Domain.Payments;
namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
@ -47,5 +48,8 @@ public class OrderDtoDbTable : MgDtoDbTableBase<OrderDto, Order>
public IQueryable<OrderDto> GetAllByProductId(int productId, bool loadRelations = true) => GetAll(loadRelations).Where(o => o.OrderItemDtos.Any(oi => oi.ProductId == productId)); public IQueryable<OrderDto> GetAllByProductId(int productId, bool loadRelations = true) => GetAll(loadRelations).Where(o => o.OrderItemDtos.Any(oi => oi.ProductId == productId));
public IQueryable<OrderDto> GetAllByProductIds(IEnumerable<int> productIds, bool loadRelations = true)
=> GetAll(loadRelations).Where(o => o.OrderItemDtos.Any(oi => productIds.Contains(oi.ProductId)));
public IQueryable<OrderDto> GetAllByIds(IEnumerable<int> orderIds, bool loadRelations = true) => GetAll(loadRelations).Where(o => orderIds.Contains(o.Id)); public IQueryable<OrderDto> GetAllByIds(IEnumerable<int> orderIds, bool loadRelations = true) => GetAll(loadRelations).Where(o => orderIds.Contains(o.Id));
} }

View File

@ -31,8 +31,12 @@ public class OrderItemDtoDbTable : MgDtoDbTableBase<OrderItemDto, OrderItem>
public Task<OrderItemDto> GetByIdAsync(int orderItemId, bool loadRelations) => GetAll(loadRelations).Where(oi => oi.Id == orderItemId).FirstOrDefaultAsync(null); public Task<OrderItemDto> GetByIdAsync(int orderItemId, bool loadRelations) => GetAll(loadRelations).Where(oi => oi.Id == orderItemId).FirstOrDefaultAsync(null);
public IQueryable<OrderItemDto> GetAllByOrderId(int orderId, bool loadRelations = true) => GetAll(loadRelations).Where(oi => oi.OrderId == orderId); public IQueryable<OrderItemDto> GetAllByOrderId(int orderId, bool loadRelations = true) => GetAll(loadRelations).Where(oi => oi.OrderId == orderId);
public IQueryable<OrderItemDto> GetAllByProductId(int productId, bool loadRelations = true) => GetAll(loadRelations).Where(oi => oi.ProductId == productId); public IQueryable<OrderItemDto> GetAllByProductId(int productId, bool loadRelations = true) => GetAll(loadRelations).Where(oi => oi.ProductId == productId);
public IQueryable<OrderItemDto> GetAllByProductIds(IEnumerable<int> productIds, bool loadRelations = true)
=> GetAll(loadRelations).Where(oi => productIds.Contains(oi.ProductId));
public IQueryable<OrderItemDto> GetAllByIds(IEnumerable<int> orderItemIds, bool loadRelations = true) => GetAll(loadRelations).Where(oi => orderItemIds.Contains(oi.Id)); public IQueryable<OrderItemDto> GetAllByIds(IEnumerable<int> orderItemIds, bool loadRelations = true) => GetAll(loadRelations).Where(oi => orderItemIds.Contains(oi.Id));
public IQueryable<OrderItemDto> GetAllByOrderIds(IEnumerable<int> orderIds, bool loadRelations = true) => GetAll(loadRelations).Where(oi => orderIds.Contains(oi.OrderId)); public IQueryable<OrderItemDto> GetAllByOrderIds(IEnumerable<int> orderIds, bool loadRelations = true) => GetAll(loadRelations).Where(oi => orderIds.Contains(oi.OrderId));
} }

View File

@ -1,6 +1,8 @@
#nullable enable #nullable enable
using System.Threading.Tasks;
using AyCode.Core.Loggers; using AyCode.Core.Loggers;
using FruitBank.Common.Dtos; using FruitBank.Common.Dtos;
using FruitBank.Common.Entities;
using Mango.Nop.Core.Entities; using Mango.Nop.Core.Entities;
using Mango.Nop.Core.Loggers; using Mango.Nop.Core.Loggers;
using Mango.Nop.Data.Repositories; using Mango.Nop.Data.Repositories;
@ -17,12 +19,14 @@ using Nop.Services.Catalog;
namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
public class StockTakingDbContext : MgDbContextBase, public class StockTakingDbContext : MgDbContextBase,
IStockTakingDbSet<StockTakingDbTable>, IStockTakingDbSet<StockTakingDbTable>,
IOrderItemDtoDbSet<OrderItemDtoDbTable>,
IStockTakingItemDbSet<StockTakingItemDbTable>, IStockTakingItemDbSet<StockTakingItemDbTable>,
IStockTakingItemPalletDbSet<StockTakingItemPalletDbTable> IStockTakingItemPalletDbSet<StockTakingItemPalletDbTable>
{ {
public ProductDtoDbTable ProductDtos { get; set; } public ProductDtoDbTable ProductDtos { get; set; }
public OrderItemDtoDbTable OrderItemDtos { get; set; }
public StockTakingDbTable StockTakings { get; set; } public StockTakingDbTable StockTakings { get; set; }
public StockTakingItemDbTable StockTakingItems { get; set; } public StockTakingItemDbTable StockTakingItems { get; set; }
@ -40,6 +44,7 @@ public class StockTakingDbContext : MgDbContextBase,
public StockTakingDbContext(INopDataProvider dataProvider, ILockService lockService, IStoreContext storeContext, public StockTakingDbContext(INopDataProvider dataProvider, ILockService lockService, IStoreContext storeContext,
ProductDtoDbTable productDtoDbTable, ProductDtoDbTable productDtoDbTable,
OrderItemDtoDbTable orderItemDtoDbTable,
StockQuantityHistoryDtoDbTable stockQuantityHistoryDtos, StockQuantityHistoryDtoDbTable stockQuantityHistoryDtos,
StockTakingDbTable stockTakingDbTable, StockTakingDbTable stockTakingDbTable,
StockTakingItemDbTable stockTakingItemDbTable, StockTakingItemDbTable stockTakingItemDbTable,
@ -60,6 +65,7 @@ public class StockTakingDbContext : MgDbContextBase,
_staticCacheManager = staticCacheManager; _staticCacheManager = staticCacheManager;
ProductDtos = productDtoDbTable; ProductDtos = productDtoDbTable;
OrderItemDtos = orderItemDtoDbTable;
GenericAttributes = genericAttributes; GenericAttributes = genericAttributes;
@ -71,4 +77,50 @@ public class StockTakingDbContext : MgDbContextBase,
StockTakingItems = stockTakingItemDbTable; StockTakingItems = stockTakingItemDbTable;
StockTakingItemPallets = stockTakingItemPalletDbTable; StockTakingItemPallets = stockTakingItemPalletDbTable;
} }
public async Task<StockTakingItemPallet> AddOrUpdateMeasuredStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
{
if (stockTakingItemPallet.Id == 0) return await AddStockTakingItemPallet(stockTakingItemPallet);
return await UpdateStockTakingItemPallet(stockTakingItemPallet);
}
public async Task<StockTakingItemPallet> AddStockTakingItemPallet(StockTakingItemPallet stockTakingItemPallet)
{
await TransactionSafeAsync(async _ =>
{
await StockTakingItemPallets.InsertAsync(stockTakingItemPallet);
await RefreshStockTakingItemMeasuredValuesFromPallets(stockTakingItemPallet.StockTakingItemId);
return true;
});
return stockTakingItemPallet;
}
public async Task<StockTakingItemPallet> 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);
}
} }

View File

@ -24,7 +24,7 @@ public class StockTakingDbTable : MgDbTableBase<StockTaking>
? GetAll() ? GetAll()
.LoadWith(st => st.StockTakingItems).ThenLoad(sti => sti.Product).ThenLoad(prod => prod.GenericAttributes) .LoadWith(st => st.StockTakingItems).ThenLoad(sti => sti.Product).ThenLoad(prod => prod.GenericAttributes)
//.LoadWith(st => st.StockTakingItems).ThenLoad(sti => sti.StockTakingItemPallets) //.LoadWith(st => st.StockTakingItems).ThenLoad(sti => sti.StockTakingItemPallets)
: GetAll().LoadWith(st => st.StockTakingItems); : GetAll();//.LoadWith(st => st.StockTakingItems);
} }
public Task<StockTaking> GetByIdAsync(int id, bool loadRelations) public Task<StockTaking> GetByIdAsync(int id, bool loadRelations)

View File

@ -22,10 +22,17 @@ public class StockTakingItemDbTable : MgDbTableBase<StockTakingItem>
return loadRelations return loadRelations
? GetAll() ? GetAll()
.LoadWith(sti => sti.StockTaking) .LoadWith(sti => sti.StockTaking)
//.LoadWith(sti => sti.StockTakingItemPallets) .LoadWith(sti => sti.StockTakingItemPallets)
.LoadWith(sti => sti.Product).ThenLoad(prod => prod.GenericAttributes) .LoadWith(sti => sti.Product).ThenLoad(prod => prod.GenericAttributes)
: GetAll(); : GetAll();
} }
public Task<StockTakingItem> GetByIdAsync(int stockTakingItemId, bool loadRelations) => GetAll(loadRelations).FirstOrDefaultAsync(sti => sti.Id == stockTakingItemId); public Task<StockTakingItem> GetByIdAsync(int stockTakingItemId, bool loadRelations) => GetAll(loadRelations).FirstOrDefaultAsync(sti => sti.Id == stockTakingItemId);
public IQueryable<StockTakingItem> GetAllByProductId(int productId, bool loadRelations)
=> GetAll(loadRelations).Where(x => x.ProductId == productId);
public IQueryable<StockTakingItem> GetAllByStockTakingId(int stockTakingId, bool loadRelations)
=> GetAll(loadRelations).Where(x => x.StockTakingId == stockTakingId);
} }