完善出库闭环和前端作业体验

This commit is contained in:
zhougf committed 2026-06-03 23:25:41 +08:00
1 parent 3c6dfda5e5
commit ce0fd7ec52
21 files changed
+1495 -261

No files matched your search

@@ -16,6 +16,8 @@ public interface IInventoryMovementService
Task ConfirmOutboundAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null);
Task ReleasePendingOutboundAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null);
Task FreezeAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null);
Task UnfreezeAsync(Inventory inventory, decimal qty, string? bizNo = null, string? remark = null);
@@ -11,8 +11,12 @@ public interface IInboundOrderService
Task<List<InventoryOrderLineViewDto>?> GetLinesAsync(long orderId, string tenantId, string ownerCode);
Task<OrderViewDto> CreateHeaderAsync(OrderHeaderCreateRequest request);
Task<OrderViewDto> CreateAsync(InboundOrderCreateRequest request);
Task<InventoryOrderLineViewDto?> AddLineAsync(InboundOrderAddLineRequest request);
Task<InboundConfirmLineResultDto?> ConfirmLineAsync(InboundOrderLineConfirmRequest request);
}
@@ -24,11 +28,19 @@ public interface IOutboundOrderService
Task<List<OutboundPickTaskViewDto>?> GetPickTasksAsync(long orderId, string tenantId, string ownerCode);
Task<OrderViewDto> CreateHeaderAsync(OrderHeaderCreateRequest request);
Task<OrderViewDto> CreateAsync(OutboundOrderSaveRequest request);
Task<InventoryOrderLineViewDto?> AddLineAsync(OutboundOrderAddLineRequest request);
Task<OrderViewDto?> PendingAsync(OrderActionRequest request);
Task<OutboundPickTaskViewDto?> ConfirmPickTaskAsync(PickTaskActionRequest request);
Task<OrderViewDto?> ConfirmAsync(OrderActionRequest request);
Task<OrderViewDto?> CancelAsync(OrderActionRequest request);
}
public interface ITransferOrderService
@@ -95,6 +95,21 @@ public class InventoryMovementService : IInventoryMovementService
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);
+310 -77
View File
@@ -284,48 +284,49 @@ public class InboundOrderService : OrderServiceBase, IInboundOrderService
{
var tenantId = _userContext.TenantId.Trim();
var ownerCode = _userContext.OwnerCode.Trim();
var orderNo = await _orderNumberGenerator.NextInboundOrderNoAsync(tenantId, ownerCode);
var order = new InboundOrder
{
TenantId = tenantId,
OwnerCode = ownerCode,
OrderNo = orderNo,
BizType = request.BizType?.Trim(),
Remark = request.Remark?.Trim(),
Attrs = null,
OrderStatus = OrderStatus.Draft
};
await _orderRepository.InsertAsync(order);
var order = await CreateHeaderCoreAsync(tenantId, ownerCode, request);
for (var i = 0; i < request.Lines.Count; i++)
{
var line = request.Lines[i];
await ValidateInboundLineMasterDataAsync(tenantId, ownerCode, line);
await _lineRepository.InsertAsync(new InboundOrderLine
{
TenantId = order.TenantId,
OwnerCode = order.OwnerCode,
OrderId = order.Id,
LineNo = i + 1,
InventoryKind = line.InventoryKind,
MaterialCode = line.InventoryKind == InventoryKind.Material ? line.MaterialCode?.Trim() : null,
ContainerCode = null,
WarehouseCode = line.WarehouseCode.Trim(),
AreaCode = line.AreaCode.Trim(),
LocationCode = null,
Qty = line.Qty,
ReceivedQty = 0,
LineStatus = OrderLineStatus.Open,
Unit = null,
Attrs = line.Attrs
});
await InsertInboundLineAsync(order, request.Lines[i], i + 1);
}
return MapOrder(order, order.OrderNo, order.BizType, order.OrderStatus, order.Remark, order.Attrs);
});
}
public async Task<OrderViewDto> CreateHeaderAsync(OrderHeaderCreateRequest request)
{
return await _tx.ExecuteAsync(async () =>
{
var order = await CreateHeaderCoreAsync(
_userContext.TenantId.Trim(),
_userContext.OwnerCode.Trim(),
request);
return MapOrder(order, order.OrderNo, order.BizType, order.OrderStatus, order.Remark, order.Attrs);
});
}
public async Task<InventoryOrderLineViewDto?> AddLineAsync(InboundOrderAddLineRequest request)
{
var order = await _orderRepository.GetByIdAsync(
request.OrderId,
_userContext.TenantId.Trim(),
_userContext.OwnerCode.Trim());
if (order is null)
{
return null;
}
return await _tx.ExecuteAsync(async () =>
{
EnsureDraft(order.OrderStatus);
var lines = await _lineRepository.GetByOrderIdAsync(order.Id);
var line = await InsertInboundLineAsync(order, request.Line, lines.Count + 1);
return MapInventoryLine(line);
});
}
public async Task<InboundConfirmLineResultDto?> ConfirmLineAsync(InboundOrderLineConfirmRequest request)
{
var tenantId = _userContext.TenantId.Trim();
@@ -530,6 +531,47 @@ public class InboundOrderService : OrderServiceBase, IInboundOrderService
return string.Equals(aa, bb, StringComparison.Ordinal);
}
private async Task<InboundOrder> CreateHeaderCoreAsync(string tenantId, string ownerCode, OrderSaveRequestBase request)
{
var order = new InboundOrder
{
TenantId = tenantId,
OwnerCode = ownerCode,
OrderNo = await _orderNumberGenerator.NextInboundOrderNoAsync(tenantId, ownerCode),
BizType = request.BizType?.Trim(),
Remark = request.Remark?.Trim(),
Attrs = request.Attrs,
OrderStatus = OrderStatus.Draft
};
await _orderRepository.InsertAsync(order);
return order;
}
private async Task<InboundOrderLine> InsertInboundLineAsync(InboundOrder order, InboundOrderLineSaveRequest line, int lineNo)
{
await ValidateInboundLineMasterDataAsync(order.TenantId, order.OwnerCode, line);
var entity = new InboundOrderLine
{
TenantId = order.TenantId,
OwnerCode = order.OwnerCode,
OrderId = order.Id,
LineNo = lineNo,
InventoryKind = line.InventoryKind,
MaterialCode = line.InventoryKind == InventoryKind.Material ? line.MaterialCode?.Trim() : null,
ContainerCode = null,
WarehouseCode = line.WarehouseCode.Trim(),
AreaCode = line.AreaCode.Trim(),
LocationCode = null,
Qty = line.Qty,
ReceivedQty = 0,
LineStatus = OrderLineStatus.Open,
Unit = null,
Attrs = line.Attrs
};
await _lineRepository.InsertAsync(entity);
return entity;
}
private async Task ValidateInboundLineMasterDataAsync(string tenantId, string ownerCode, InboundOrderLineSaveRequest line)
{
var t = tenantId.Trim();
@@ -573,6 +615,7 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
private readonly IOutboundOrderLineRepository _lineRepository;
private readonly IOutboundPickTaskRepository _pickTaskRepository;
private readonly ILocationRepository _locationRepository;
private readonly IMaterialRepository _materialRepository;
private readonly IInventoryAllocationService _allocationService;
private readonly IOrderNumberGenerator _orderNumberGenerator;
private readonly ICurrentUserContext _userContext;
@@ -583,6 +626,7 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
IOutboundOrderLineRepository lineRepository,
IOutboundPickTaskRepository pickTaskRepository,
ILocationRepository locationRepository,
IMaterialRepository materialRepository,
IInventoryAllocationService allocationService,
IOrderNumberGenerator orderNumberGenerator,
IInventoryRepository inventoryRepository,
@@ -594,6 +638,7 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
_lineRepository = lineRepository;
_pickTaskRepository = pickTaskRepository;
_locationRepository = locationRepository;
_materialRepository = materialRepository;
_allocationService = allocationService;
_orderNumberGenerator = orderNumberGenerator;
_userContext = userContext;
@@ -643,54 +688,49 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
{
var tenantId = _userContext.TenantId.Trim();
var ownerCode = _userContext.OwnerCode.Trim();
var orderNo = string.IsNullOrWhiteSpace(request.OrderNo)
? await _orderNumberGenerator.NextOutboundOrderNoAsync(tenantId, ownerCode)
: request.OrderNo.Trim();
if (await _orderRepository.GetByOrderNoAsync(tenantId, ownerCode, orderNo) is not null)
{
throw new BusinessException($"Outbound order '{orderNo}' already exists.");
}
var order = new OutboundOrder
{
TenantId = tenantId,
OwnerCode = ownerCode,
OrderNo = orderNo,
BizType = request.BizType?.Trim(),
Remark = request.Remark?.Trim(),
Attrs = request.Attrs,
OrderStatus = OrderStatus.Draft
};
await _orderRepository.InsertAsync(order);
var order = await CreateHeaderCoreAsync(tenantId, ownerCode, request);
for (var i = 0; i < request.Lines.Count; i++)
{
var line = request.Lines[i];
await _lineRepository.InsertAsync(new OutboundOrderLine
{
TenantId = order.TenantId,
OwnerCode = order.OwnerCode,
OrderId = order.Id,
LineNo = i + 1,
InventoryKind = line.InventoryKind,
MaterialCode = line.InventoryKind == InventoryKind.Material ? line.MaterialCode?.Trim() : null,
ContainerCode = line.InventoryKind == InventoryKind.EmptyContainer ? line.ContainerCode?.Trim() : null,
WarehouseCode = line.WarehouseCode.Trim(),
AreaCode = line.AreaCode.Trim(),
LocationCode = line.LocationCode?.Trim(),
Qty = line.Qty,
ReceivedQty = 0,
LineStatus = OrderLineStatus.Open,
Unit = line.Unit.Trim(),
Attrs = line.Attrs
});
await InsertOutboundLineAsync(order, request.Lines[i], i + 1);
}
return MapOrder(order, order.OrderNo, order.BizType, order.OrderStatus, order.Remark, order.Attrs);
});
}
public async Task<OrderViewDto> CreateHeaderAsync(OrderHeaderCreateRequest request)
{
return await _tx.ExecuteAsync(async () =>
{
var order = await CreateHeaderCoreAsync(
_userContext.TenantId.Trim(),
_userContext.OwnerCode.Trim(),
request);
return MapOrder(order, order.OrderNo, order.BizType, order.OrderStatus, order.Remark, order.Attrs);
});
}
public async Task<InventoryOrderLineViewDto?> AddLineAsync(OutboundOrderAddLineRequest request)
{
var order = await _orderRepository.GetByIdAsync(
request.OrderId,
_userContext.TenantId.Trim(),
_userContext.OwnerCode.Trim());
if (order is null)
{
return null;
}
return await _tx.ExecuteAsync(async () =>
{
EnsureDraft(order.OrderStatus);
var lines = await _lineRepository.GetByOrderIdAsync(order.Id);
var line = await InsertOutboundLineAsync(order, request.Line, lines.Count + 1);
return MapInventoryLine(line);
});
}
public async Task<OrderViewDto?> PendingAsync(OrderActionRequest request)
{
var order = await _orderRepository.GetByIdAsync(request.Id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
@@ -703,6 +743,11 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
{
EnsureDraft(order.OrderStatus);
var lines = await _lineRepository.GetByOrderIdAsync(order.Id);
if (lines.Count == 0)
{
throw new BusinessException("Outbound order must have at least one line before picking.");
}
foreach (var line in lines)
{
var existingTasks = await _pickTaskRepository.GetByOrderLineIdAsync(line.Id);
@@ -796,17 +841,14 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
foreach (var task in tasks)
{
if (task.TaskStatus != PickTaskStatus.Open)
if (task.TaskStatus != PickTaskStatus.Picked)
{
throw new BusinessException("Only open pick tasks can be confirmed.");
throw new BusinessException("All outbound pick tasks must be picked before outbound confirmation.");
}
var inventory = await InventoryRepository.GetByIdAsync(task.InventoryId, order.TenantId, order.OwnerCode)
?? throw new BusinessException("Allocated inventory not found.");
await InventoryMovementService.ConfirmOutboundAsync(inventory, task.Qty, order.OrderNo);
task.PickedQty = task.Qty;
task.TaskStatus = PickTaskStatus.Picked;
await _pickTaskRepository.UpdateAsync(task);
}
var lines = await _lineRepository.GetByOrderIdAsync(order.Id);
@@ -822,6 +864,102 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
});
}
public async Task<OutboundPickTaskViewDto?> ConfirmPickTaskAsync(PickTaskActionRequest request)
{
var task = await _pickTaskRepository.GetByIdAsync(
request.Id,
_userContext.TenantId.Trim(),
_userContext.OwnerCode.Trim());
if (task is null)
{
return null;
}
return await _tx.ExecuteAsync(async () =>
{
if (task.TaskStatus != PickTaskStatus.Open)
{
throw new BusinessException("Only open pick tasks can be picked.");
}
var order = await _orderRepository.GetByIdAsync(task.OrderId, task.TenantId, task.OwnerCode)
?? throw new BusinessException("Outbound order not found.");
EnsurePending(order.OrderStatus);
var pickedQty = request.PickedQty ?? task.Qty;
if (pickedQty != task.Qty)
{
throw new BusinessException("Short picking is not supported yet. Please pick the full task quantity.");
}
task.PickedQty = pickedQty;
task.PickerUserId = _userContext.UserId;
task.PickedTime = DateTime.Now;
task.ExceptionReason = string.IsNullOrWhiteSpace(request.ExceptionReason)
? null
: request.ExceptionReason.Trim();
task.TaskStatus = PickTaskStatus.Picked;
await _pickTaskRepository.UpdateAsync(task);
return MapPickTask(task);
});
}
public async Task<OrderViewDto?> CancelAsync(OrderActionRequest request)
{
var order = await _orderRepository.GetByIdAsync(request.Id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
if (order is null)
{
return null;
}
return await _tx.ExecuteAsync(async () =>
{
if (order.OrderStatus == OrderStatus.Completed)
{
throw new BusinessException("Completed outbound orders cannot be cancelled.");
}
if (order.OrderStatus == OrderStatus.Cancelled)
{
return MapOrder(order, order.OrderNo, order.BizType, order.OrderStatus, order.Remark, order.Attrs);
}
var tasks = await _pickTaskRepository.GetByOrderIdAsync(order.Id);
foreach (var task in tasks)
{
if (task.TaskStatus == PickTaskStatus.Cancelled)
{
continue;
}
var inventory = await InventoryRepository.GetByIdAsync(task.InventoryId, order.TenantId, order.OwnerCode)
?? throw new BusinessException("Allocated inventory not found.");
await InventoryMovementService.ReleasePendingOutboundAsync(
inventory,
task.Qty,
order.OrderNo,
"Outbound order cancelled.");
task.TaskStatus = PickTaskStatus.Cancelled;
task.ExceptionReason = "Outbound order cancelled.";
await _pickTaskRepository.UpdateAsync(task);
}
var lines = await _lineRepository.GetByOrderIdAsync(order.Id);
foreach (var line in lines)
{
line.LineStatus = OrderLineStatus.Open;
line.ReceivedQty = 0;
await _lineRepository.UpdateAsync(line);
}
order.OrderStatus = OrderStatus.Cancelled;
await _orderRepository.UpdateAsync(order);
return MapOrder(order, order.OrderNo, order.BizType, order.OrderStatus, order.Remark, order.Attrs);
});
}
private static OutboundPickTaskViewDto MapPickTask(OutboundPickTask task)
{
return new OutboundPickTaskViewDto
@@ -840,11 +978,106 @@ public class OutboundOrderService : OrderServiceBase, IOutboundOrderService
LocationCode = task.LocationCode,
Qty = task.Qty,
PickedQty = task.PickedQty,
PickerUserId = task.PickerUserId,
PickedTime = task.PickedTime,
ExceptionReason = task.ExceptionReason,
Unit = task.Unit,
TaskStatus = task.TaskStatus,
Attrs = task.Attrs
};
}
private async Task<OutboundOrder> CreateHeaderCoreAsync(string tenantId, string ownerCode, OrderSaveRequestBase request)
{
var orderNo = string.IsNullOrWhiteSpace(request.OrderNo)
? await _orderNumberGenerator.NextOutboundOrderNoAsync(tenantId, ownerCode)
: request.OrderNo.Trim();
if (await _orderRepository.GetByOrderNoAsync(tenantId, ownerCode, orderNo) is not null)
{
throw new BusinessException($"Outbound order '{orderNo}' already exists.");
}
var order = new OutboundOrder
{
TenantId = tenantId,
OwnerCode = ownerCode,
OrderNo = orderNo,
BizType = request.BizType?.Trim(),
Remark = request.Remark?.Trim(),
Attrs = request.Attrs,
OrderStatus = OrderStatus.Draft
};
await _orderRepository.InsertAsync(order);
return order;
}
private async Task<OutboundOrderLine> InsertOutboundLineAsync(OutboundOrder order, OutboundOrderLineRequest line, int lineNo)
{
var unit = await ResolveOutboundLineUnitAsync(order.TenantId, order.OwnerCode, line);
var entity = new OutboundOrderLine
{
TenantId = order.TenantId,
OwnerCode = order.OwnerCode,
OrderId = order.Id,
LineNo = lineNo,
InventoryKind = line.InventoryKind,
MaterialCode = line.InventoryKind == InventoryKind.Material ? line.MaterialCode?.Trim() : null,
ContainerCode = line.InventoryKind == InventoryKind.EmptyContainer ? line.ContainerCode?.Trim() : null,
WarehouseCode = line.WarehouseCode.Trim(),
AreaCode = line.AreaCode.Trim(),
LocationCode = line.LocationCode?.Trim(),
Qty = line.Qty,
ReceivedQty = 0,
LineStatus = OrderLineStatus.Open,
Unit = unit,
Attrs = line.Attrs
};
await _lineRepository.InsertAsync(entity);
return entity;
}
private async Task<string> ResolveOutboundLineUnitAsync(string tenantId, string ownerCode, OutboundOrderLineRequest line)
{
switch (line.InventoryKind)
{
case InventoryKind.Material:
if (string.IsNullOrWhiteSpace(line.MaterialCode))
{
throw new BusinessException("MaterialCode is required for material lines.");
}
var material = await _materialRepository.GetByCodeAsync(tenantId, ownerCode, line.MaterialCode.Trim())
?? throw new BusinessException($"Material '{line.MaterialCode}' not found.");
var masterUnit = material.Unit.Trim();
if (string.IsNullOrWhiteSpace(masterUnit))
{
throw new BusinessException($"Material '{line.MaterialCode}' has no unit in master data.");
}
if (!string.IsNullOrWhiteSpace(line.Unit)
&& !string.Equals(line.Unit.Trim(), masterUnit, StringComparison.Ordinal))
{
throw new BusinessException("Line unit does not match material master unit.");
}
return masterUnit;
case InventoryKind.EmptyContainer:
if (string.IsNullOrWhiteSpace(line.ContainerCode))
{
throw new BusinessException("ContainerCode is required for empty container lines.");
}
if (string.IsNullOrWhiteSpace(line.Unit))
{
throw new BusinessException("Unit is required for empty container lines.");
}
return line.Unit.Trim();
default:
throw new BusinessException("Unsupported inventory kind.");
}
}
}
public class TransferOrderService : OrderServiceBase, ITransferOrderService