feat(shipping-notice): 完善发货通知自动推进与多库存源复核闭环

This commit is contained in:
hugo committed 2026-08-09 11:43:53 +08:00
1 parent cfa19a0b7b
commit 8e0bbf7a81
180 files changed
+31210 -1864

No files matched your search

@@ -21,6 +21,7 @@ from zoneinfo import ZoneInfo
from flask import current_app
from openpyxl import load_workbook
from sqlalchemy import func
from app import db
from app.models import (
ShippingExcelTemplate,
@@ -302,14 +303,70 @@ def generate_shipment_excel(task: ShippingNotice) -> tuple[str, str, ShippingExp
Raises:
ValueError: 库存源、模板、订单 Extraction 或解析 Profile 未形成完整发布链。
AppError: [T6 硬门禁] 行级库存匹配存在非 matched 行(shortage / not_found / conflict)。
"""
from app.services.shipping_notice.task_disposition_service import task_is_active
from app.services.shipping_notice.task_supersession_service import assert_task_not_supersession_blocked
from app.models.shipping_notice_line_inventory_match import ShippingNoticeLineInventoryMatch
from app.core.errors import AppError
if not task_is_active(task):
raise ValueError('已作废或归档的任务不能生成发货 Excel')
assert_task_not_supersession_blocked(task)
# [T6 E2 硬门禁] 生成 Excel 前强制断言所有当前行匹配结果均为 matched;
# 即使单源任务有明确 inventory_source_code,行级短缺/冲突仍须先处理,避免导出不完整数据。
if task.id is not None:
pending_rows = db.session.query(
ShippingNoticeLineInventoryMatch.line_key,
ShippingNoticeLineInventoryMatch.match_result,
ShippingNoticeLineInventoryMatch.part_number,
ShippingNoticeLineInventoryMatch.reservation_id,
ShippingNoticeLineInventoryMatch.shortage_qty,
).filter(
ShippingNoticeLineInventoryMatch.task_id == task.id,
ShippingNoticeLineInventoryMatch.is_current.is_(True),
ShippingNoticeLineInventoryMatch.match_result != 'matched',
).limit(20).all()
pending_count = db.session.query(func.count(ShippingNoticeLineInventoryMatch.id)).filter(
ShippingNoticeLineInventoryMatch.task_id == task.id,
ShippingNoticeLineInventoryMatch.is_current.is_(True),
ShippingNoticeLineInventoryMatch.match_result != 'matched',
).scalar() or 0
if pending_count > 0:
details = [
{
'line_key': r[0],
'match_result': r[1],
'part_number': r[2],
'reservation_id': r[3],
'shortage_qty': r[4],
}
for r in pending_rows
]
raise AppError(
error_code='excel_not_ready',
status_code=409,
blocking_reason_code='inventory_match_incomplete',
message=f'存在 {pending_count} 行库存未完全匹配,禁止生成发货 Excel(请先处理短缺或冲突)。',
details={
'pending_count': int(pending_count),
'pending_rows_preview': details,
'resume_suggestion': '调用 match_inventory 重匹配或人工逐行确认短缺/冲突后再导出。',
},
)
source_code = str(task.inventory_source_code or '').strip().upper()
source = ShippingInventorySource.query.filter_by(source_code=source_code, enabled=True).first()
if source is None:
# [T6 多候选兼容] 单源 resolved 走上方 inventory_source_code;多候选 candidates_frozen 时可能 source_code==None,
# 在严格全行 matched 前提下可退回到第一条 line_match 的 inventory_source_code(保证不绕过断言)。
first_match = db.session.query(ShippingNoticeLineInventoryMatch.inventory_source_code).filter(
ShippingNoticeLineInventoryMatch.task_id == task.id,
ShippingNoticeLineInventoryMatch.is_current.is_(True),
ShippingNoticeLineInventoryMatch.match_result == 'matched',
ShippingNoticeLineInventoryMatch.inventory_source_code.isnot(None),
).limit(1).first()
source_code_candidate = str(first_match[0]).strip().upper() if first_match and first_match[0] else ''
source = ShippingInventorySource.query.filter_by(source_code=source_code_candidate, enabled=True).first() if source_code_candidate else None
if source is None:
raise ValueError('库存源不存在或已停用,不能生成发货 Excel')
# Fixed Sources receive this value from configuration; dynamic Sources receive it