215 lines
7.9 KiB
C#
215 lines
7.9 KiB
C#
using System.Linq.Expressions;
|
|
using WmsLite.Api.Common;
|
|
using WmsLite.Api.Dtos.Common;
|
|
using WmsLite.Api.Dtos.Inventory;
|
|
using WmsLite.Api.Enums;
|
|
using WmsLite.Api.Infrastructure.Auth;
|
|
using WmsLite.Api.Models;
|
|
using WmsLite.Api.Repositories.Interfaces;
|
|
using WmsLite.Api.Services;
|
|
|
|
namespace WmsLite.Api.Tests;
|
|
|
|
public class InventoryMovementServiceTests
|
|
{
|
|
[Fact]
|
|
public async Task PendingOutboundAsync_ReservesAvailableQtyAndWritesTransaction()
|
|
{
|
|
var inventory = NewInventory(qty: 10);
|
|
var (_, transactionRepository, service) = CreateService();
|
|
|
|
await service.PendingOutboundAsync(inventory, 4, "SO-1", "pick");
|
|
|
|
Assert.Equal(10, inventory.Qty);
|
|
Assert.Equal(4, inventory.PendingOutboundQty);
|
|
Assert.Equal(InventoryStatus.PendingOutbound, inventory.InventoryStatus);
|
|
|
|
var tx = Assert.Single(transactionRepository.Entities);
|
|
Assert.Equal(InventoryMovementType.PendingOutbound, tx.MovementType);
|
|
Assert.Equal(10, tx.BeforeQty);
|
|
Assert.Equal(10, tx.AfterQty);
|
|
Assert.Equal(0, tx.BeforePendingOutboundQty);
|
|
Assert.Equal(4, tx.AfterPendingOutboundQty);
|
|
Assert.Equal(4, tx.ChangeQty);
|
|
Assert.Equal("SO-1", tx.BizNo);
|
|
Assert.Equal("pick", tx.Attrs?["remark"]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConfirmOutboundAsync_DecrementsPendingAndStockQty()
|
|
{
|
|
var inventory = NewInventory(qty: 10, pendingOutboundQty: 4);
|
|
var (_, transactionRepository, service) = CreateService();
|
|
|
|
await service.ConfirmOutboundAsync(inventory, 4, "SO-1");
|
|
|
|
Assert.Equal(6, inventory.Qty);
|
|
Assert.Equal(0, inventory.PendingOutboundQty);
|
|
Assert.Equal(InventoryStatus.Available, inventory.InventoryStatus);
|
|
|
|
var tx = Assert.Single(transactionRepository.Entities);
|
|
Assert.Equal(InventoryMovementType.OutboundConfirm, tx.MovementType);
|
|
Assert.Equal(10, tx.BeforeQty);
|
|
Assert.Equal(6, tx.AfterQty);
|
|
Assert.Equal(4, tx.BeforePendingOutboundQty);
|
|
Assert.Equal(0, tx.AfterPendingOutboundQty);
|
|
Assert.Equal(-4, tx.ChangeQty);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConfirmInboundAsync_MovesPendingInboundToStockQty()
|
|
{
|
|
var inventory = NewInventory(qty: 2, pendingInboundQty: 5);
|
|
var (_, transactionRepository, service) = CreateService();
|
|
|
|
await service.ConfirmInboundAsync(inventory, 3, "IN-1");
|
|
|
|
Assert.Equal(5, inventory.Qty);
|
|
Assert.Equal(2, inventory.PendingInboundQty);
|
|
Assert.Equal(InventoryStatus.PendingInbound, inventory.InventoryStatus);
|
|
|
|
var tx = Assert.Single(transactionRepository.Entities);
|
|
Assert.Equal(InventoryMovementType.InboundConfirm, tx.MovementType);
|
|
Assert.Equal(2, tx.BeforeQty);
|
|
Assert.Equal(5, tx.AfterQty);
|
|
Assert.Equal(5, tx.BeforePendingInboundQty);
|
|
Assert.Equal(2, tx.AfterPendingInboundQty);
|
|
Assert.Equal(3, tx.ChangeQty);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PendingOutboundAsync_ThrowsWhenAvailableQtyIsNotEnough()
|
|
{
|
|
var inventory = NewInventory(qty: 10, pendingOutboundQty: 7, frozenQty: 2);
|
|
var (inventoryRepository, transactionRepository, service) = CreateService();
|
|
|
|
await Assert.ThrowsAsync<BusinessException>(
|
|
() => service.PendingOutboundAsync(inventory, 2, "SO-2"));
|
|
|
|
Assert.Equal(7, inventory.PendingOutboundQty);
|
|
Assert.Empty(inventoryRepository.UpdatedEntities);
|
|
Assert.Empty(transactionRepository.Entities);
|
|
}
|
|
|
|
private static (FakeInventoryRepository InventoryRepository, FakeInventoryTransactionRepository TransactionRepository, InventoryMovementService Service) CreateService()
|
|
{
|
|
var inventoryRepository = new FakeInventoryRepository();
|
|
var transactionRepository = new FakeInventoryTransactionRepository();
|
|
var service = new InventoryMovementService(
|
|
inventoryRepository,
|
|
transactionRepository,
|
|
new FakeCurrentUserContext());
|
|
return (inventoryRepository, transactionRepository, service);
|
|
}
|
|
|
|
private static Inventory NewInventory(decimal qty, decimal pendingInboundQty = 0, decimal pendingOutboundQty = 0, decimal frozenQty = 0)
|
|
{
|
|
return new Inventory
|
|
{
|
|
Id = 1001,
|
|
TenantId = "default",
|
|
OwnerCode = "default",
|
|
InventoryKind = InventoryKind.Material,
|
|
MaterialCode = "MAT-001",
|
|
WarehouseCode = "WH-1",
|
|
AreaCode = "A-1",
|
|
ZoneCode = "Z-1",
|
|
LocationCode = "L-1",
|
|
Qty = qty,
|
|
PendingInboundQty = pendingInboundQty,
|
|
PendingOutboundQty = pendingOutboundQty,
|
|
FrozenQty = frozenQty,
|
|
InventoryStatus = InventoryStatus.Available,
|
|
Unit = "pcs",
|
|
Attrs = new Dictionary<string, object> { ["lot"] = "L202606" },
|
|
};
|
|
}
|
|
|
|
private sealed class FakeCurrentUserContext : ICurrentUserContext
|
|
{
|
|
public bool IsAuthenticated => true;
|
|
public string UserId => "tester";
|
|
public string TenantId => "default";
|
|
public string OwnerCode => "default";
|
|
public string? DisplayName => "Tester";
|
|
}
|
|
|
|
private sealed class FakeInventoryTransactionRepository : IInventoryTransactionRepository
|
|
{
|
|
public List<InventoryTransaction> Entities { get; } = [];
|
|
|
|
public Task<long> InsertAsync(InventoryTransaction entity)
|
|
{
|
|
entity.Id = Entities.Count + 1;
|
|
Entities.Add(entity);
|
|
return Task.FromResult(entity.Id);
|
|
}
|
|
|
|
public Task<PagedResult<InventoryTransaction>> GetPagedListAsync(InventoryTransactionPagedQuery query)
|
|
=> Task.FromResult(new PagedResult<InventoryTransaction>());
|
|
}
|
|
|
|
private sealed class FakeInventoryRepository : IInventoryRepository
|
|
{
|
|
public List<Inventory> InsertedEntities { get; } = [];
|
|
public List<Inventory> UpdatedEntities { get; } = [];
|
|
|
|
public Task<Inventory?> GetByStockKeyAsync(
|
|
string tenantId,
|
|
string ownerCode,
|
|
string warehouseCode,
|
|
string areaCode,
|
|
string zoneCode,
|
|
string locationCode,
|
|
InventoryKind inventoryKind,
|
|
string? materialCode,
|
|
string? containerCode)
|
|
=> Task.FromResult<Inventory?>(null);
|
|
|
|
public Task<List<Inventory>> GetAllocatableAsync(
|
|
string tenantId,
|
|
string ownerCode,
|
|
string warehouseCode,
|
|
string areaCode,
|
|
InventoryKind inventoryKind,
|
|
string? materialCode,
|
|
string? containerCode,
|
|
string? locationCode = null)
|
|
=> Task.FromResult(new List<Inventory>());
|
|
|
|
public Task<Inventory?> GetByIdAsync(long id)
|
|
=> Task.FromResult<Inventory?>(null);
|
|
|
|
public Task<Inventory?> GetByIdAsync(long id, string tenantId, string ownerCode)
|
|
=> Task.FromResult<Inventory?>(null);
|
|
|
|
public Task<List<Inventory>> GetListAsync(Expression<Func<Inventory, bool>> predicate)
|
|
=> Task.FromResult(new List<Inventory>());
|
|
|
|
public Task<PagedResult<Inventory>> GetPagedListAsync(BasePagedQuery query)
|
|
=> Task.FromResult(new PagedResult<Inventory>());
|
|
|
|
public Task<PagedResult<Inventory>> GetPagedListAsync(InventoryPagedQuery query)
|
|
=> Task.FromResult(new PagedResult<Inventory>());
|
|
|
|
public Task<long> InsertAsync(Inventory entity)
|
|
{
|
|
entity.Id = entity.Id == 0 ? InsertedEntities.Count + 1 : entity.Id;
|
|
InsertedEntities.Add(entity);
|
|
return Task.FromResult(entity.Id);
|
|
}
|
|
|
|
public Task<int> UpdateAsync(Inventory entity)
|
|
{
|
|
UpdatedEntities.Add(entity);
|
|
return Task.FromResult(1);
|
|
}
|
|
|
|
public Task<int> SoftDeleteAsync(long id)
|
|
=> Task.FromResult(0);
|
|
|
|
public Task<int> SoftDeleteAsync(long id, string tenantId, string ownerCode)
|
|
=> Task.FromResult(0);
|
|
}
|
|
}
|