using System.Collections.Generic; using WmsLite.Api.Common; using WmsLite.Api.Enums; using WmsLite.Api.Infrastructure.Auth; using WmsLite.Api.Models; using WmsLite.Api.Repositories.Interfaces; using WmsLite.Api.Services.Interfaces; namespace WmsLite.Api.Services; public class InventoryMovementService : IInventoryMovementService { private readonly IInventoryRepository _inventoryRepository; private readonly IInventoryTransactionRepository _transactionRepository; private readonly ICurrentUserContext _userContext; public InventoryMovementService( IInventoryRepository inventoryRepository, IInventoryTransactionRepository transactionRepository, ICurrentUserContext userContext) { _inventoryRepository = inventoryRepository; _transactionRepository = transactionRepository; _userContext = userContext; } public async Task ApplySnapshotAsync(Inventory inventory, Inventory? existing, InventoryMovementType movementType, string? bizNo = null) { ValidateQuantities(inventory); if (existing is null) { await _inventoryRepository.InsertAsync(inventory); await WriteTransactionAsync(null, inventory, movementType, inventory.Qty, bizNo, null); return; } await _inventoryRepository.UpdateAsync(inventory); await WriteTransactionAsync(existing, inventory, movementType, inventory.Qty - existing.Qty, bizNo, null); } public async Task PendingInboundAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null) { EnsurePositive(qty); var before = Clone(inventory); inventory.PendingInboundQty += qty; inventory.InventoryStatus = ResolveStatus(inventory); await _inventoryRepository.UpdateAsync(inventory); await WriteTransactionAsync(before, inventory, InventoryMovementType.PendingInbound, qty, bizNo, remark); } public async Task ConfirmInboundAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null) { EnsurePositive(qty); if (inventory.PendingInboundQty < qty) { throw new BusinessException("PendingInboundQty is not enough."); } var before = Clone(inventory); inventory.PendingInboundQty -= qty; inventory.Qty += qty; inventory.InventoryStatus = ResolveStatus(inventory); await _inventoryRepository.UpdateAsync(inventory); await WriteTransactionAsync(before, inventory, InventoryMovementType.InboundConfirm, qty, bizNo, remark); } public async Task PendingOutboundAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null) { EnsurePositive(qty); if (AvailableQty(inventory) < qty) { throw new BusinessException("Available quantity is not enough."); } var before = Clone(inventory); inventory.PendingOutboundQty += qty; inventory.InventoryStatus = ResolveStatus(inventory); await _inventoryRepository.UpdateAsync(inventory); await WriteTransactionAsync(before, inventory, InventoryMovementType.PendingOutbound, qty, bizNo, remark); } public async Task ConfirmOutboundAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null) { EnsurePositive(qty); if (inventory.PendingOutboundQty < qty || inventory.Qty < qty) { throw new BusinessException("PendingOutboundQty or Qty is not enough."); } var before = Clone(inventory); inventory.PendingOutboundQty -= qty; inventory.Qty -= qty; inventory.InventoryStatus = ResolveStatus(inventory); await _inventoryRepository.UpdateAsync(inventory); await WriteTransactionAsync(before, inventory, InventoryMovementType.OutboundConfirm, -qty, bizNo, remark); } public async Task ReleasePendingOutboundAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null) { EnsurePositive(qty); if (inventory.PendingOutboundQty < qty) { throw new BusinessException("PendingOutboundQty is not enough to release."); } var before = Clone(inventory); inventory.PendingOutboundQty -= qty; inventory.InventoryStatus = ResolveStatus(inventory); await _inventoryRepository.UpdateAsync(inventory); await WriteTransactionAsync(before, inventory, InventoryMovementType.PendingOutboundRelease, -qty, bizNo, remark); } public async Task FreezeAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null) { EnsurePositive(qty); if (AvailableQty(inventory) < qty) { throw new BusinessException("Available quantity is not enough."); } var before = Clone(inventory); inventory.FrozenQty += qty; inventory.InventoryStatus = ResolveStatus(inventory); await _inventoryRepository.UpdateAsync(inventory); await WriteTransactionAsync(before, inventory, InventoryMovementType.Freeze, qty, bizNo, remark); } public async Task UnfreezeAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null) { EnsurePositive(qty); if (inventory.FrozenQty < qty) { throw new BusinessException("FrozenQty is not enough."); } var before = Clone(inventory); inventory.FrozenQty -= qty; inventory.InventoryStatus = ResolveStatus(inventory); await _inventoryRepository.UpdateAsync(inventory); await WriteTransactionAsync(before, inventory, InventoryMovementType.Unfreeze, -qty, bizNo, remark); } private async Task WriteTransactionAsync(Inventory? before, Inventory after, InventoryMovementType movementType, decimal changeQty, string? bizNo, string? remark) { var transaction = new InventoryTransaction { TenantId = after.TenantId, OwnerCode = after.OwnerCode, InventoryId = after.Id, MovementType = movementType, InventoryStatus = after.InventoryStatus, BizNo = string.IsNullOrWhiteSpace(bizNo) ? null : bizNo.Trim(), BeforeQty = before?.Qty ?? 0, AfterQty = after.Qty, BeforePendingInboundQty = before?.PendingInboundQty ?? 0, AfterPendingInboundQty = after.PendingInboundQty, BeforePendingOutboundQty = before?.PendingOutboundQty ?? 0, AfterPendingOutboundQty = after.PendingOutboundQty, BeforeFrozenQty = before?.FrozenQty ?? 0, AfterFrozenQty = after.FrozenQty, ChangeQty = changeQty, MaterialCode = after.MaterialCode, ContainerCode = after.ContainerCode, WarehouseCode = after.WarehouseCode, AreaCode = after.AreaCode, ZoneCode = after.ZoneCode, LocationCode = after.LocationCode, Attrs = MergeTransactionAttrs(after.Attrs, remark) }; await _transactionRepository.InsertAsync(transaction); } private static Dictionary? MergeTransactionAttrs(Dictionary? inventoryAttrs, string? remark) { if (string.IsNullOrWhiteSpace(remark)) { return inventoryAttrs; } var dict = inventoryAttrs is null ? new Dictionary() : new Dictionary(inventoryAttrs); dict["remark"] = remark.Trim(); return dict; } private static Inventory Clone(Inventory inventory) { return new Inventory { Id = inventory.Id, TenantId = inventory.TenantId, OwnerCode = inventory.OwnerCode, InventoryKind = inventory.InventoryKind, MaterialCode = inventory.MaterialCode, ContainerCode = inventory.ContainerCode, WarehouseCode = inventory.WarehouseCode, AreaCode = inventory.AreaCode, ZoneCode = inventory.ZoneCode, LocationCode = inventory.LocationCode, Qty = inventory.Qty, PendingInboundQty = inventory.PendingInboundQty, PendingOutboundQty = inventory.PendingOutboundQty, FrozenQty = inventory.FrozenQty, InventoryStatus = inventory.InventoryStatus, Unit = inventory.Unit, Attrs = inventory.Attrs }; } private static InventoryStatus ResolveStatus(Inventory inventory) { if (inventory.FrozenQty > 0) { return InventoryStatus.Frozen; } if (inventory.PendingOutboundQty > 0) { return InventoryStatus.PendingOutbound; } if (inventory.PendingInboundQty > 0) { return InventoryStatus.PendingInbound; } return InventoryStatus.Available; } private static decimal AvailableQty(Inventory inventory) { return inventory.Qty - inventory.PendingOutboundQty - inventory.FrozenQty; } private static void EnsurePositive(decimal qty) { if (qty <= 0) { throw new BusinessException("Movement quantity must be greater than 0."); } } private static void ValidateQuantities(Inventory inventory) { if (inventory.Qty < 0 || inventory.PendingInboundQty < 0 || inventory.PendingOutboundQty < 0 || inventory.FrozenQty < 0) { throw new BusinessException("Inventory quantities cannot be negative."); } } }