feat(shipping-notice): 增强发货通知候选管理功能,支持候选选择、重复检测与业务摘要修订
This commit is contained in:
1 parent
70f684b94a
commit
b595e157e9
83 files changed
+5857
-200
No files matched your search
@@ -2,7 +2,12 @@ from email.message import EmailMessage
|
||||
from pathlib import Path
|
||||
|
||||
from app.services.shipping_notice.extract import extract_notice_fields, _heuristic_extract
|
||||
from app.services.shipping_notice.order_candidate_builder import (
|
||||
build_email_order_index,
|
||||
merge_attachment_candidate,
|
||||
)
|
||||
from app.services.shipping_notice.notice_lines import (
|
||||
adjudicate_notice_lines,
|
||||
normalize_notice_line,
|
||||
parse_notice_table_rows,
|
||||
parse_notice_table_text,
|
||||
@@ -129,6 +134,71 @@ def test_extract_notice_text_reads_xfa_pdf_datasets():
|
||||
assert "EINDT=2026-06-23" in text
|
||||
|
||||
|
||||
def test_email_body_order_index_and_attachment_fusion_by_po():
|
||||
"""截图型邮件正文应解析三条订单,并按 PO 将正文分配字段融合到对应附件候选。"""
|
||||
body = """
|
||||
allocation_id\tReservation N.r\tsource type\tAllocated HHPN\tAllocated APN\tAllocated QTY\tMPN\tHub Code\tPO
|
||||
S-ENC-FOBU260702-286\t2000485916\tVMI-FRA01\tTEMP-S000218922\t504-006043-001\t30\tD4-A08AR12000001\tFRA01\t4510039227
|
||||
S-BRA-FOBU260702-187\t2000485915\tVMI-FRA01\tTEMP-S000196207\t504-006076-001\t60\tD4-AR1200115A1\tFRA01\t4510039228
|
||||
S-ENCFOX260705BG977W\t1000148820\tDBS DUB\tTEMP-S000218922\t504-006043-001\t1\tD4-A08AR12000001\tDUB\t4510039229
|
||||
"""
|
||||
rows = build_email_order_index(body)
|
||||
assert len(rows) == 3
|
||||
assert rows[0]['po_number'] == '4510039227'
|
||||
assert rows[0]['reservation_id'] == '2000485916'
|
||||
assert rows[0]['sku'] == '504-006043-001'
|
||||
assert rows[0]['qty'] == 30
|
||||
assert rows[0]['source_type'] == 'VMI-FRA01'
|
||||
assert rows[0]['customer_sku'] == 'TEMP-S000218922'
|
||||
assert rows[0]['vendor_part_number'] == 'D4-A08AR12000001'
|
||||
assert rows[0]['hub_code'] == 'FRA01'
|
||||
assert rows[2]['hub_code'] == 'DUB'
|
||||
|
||||
fused = merge_attachment_candidate(
|
||||
attachment_summary={
|
||||
'po_number': '4510039227',
|
||||
'reservation_ids': ['2000485916'],
|
||||
'lines': [{'reservation_id': '2000485916', 'sku': '504-006043-001', 'qty': 30, 'uom': 'EA'}],
|
||||
},
|
||||
filename='4510039227.pdf',
|
||||
body_rows=rows,
|
||||
)
|
||||
line = fused['business_summary']['lines'][0]
|
||||
assert fused['validation_status'] == 'ready'
|
||||
assert not fused['conflicts']
|
||||
assert line['source_type'] == 'VMI-FRA01'
|
||||
assert line['customer_sku'] == 'TEMP-S000218922'
|
||||
assert line['hub_code'] == 'FRA01'
|
||||
|
||||
conflict = merge_attachment_candidate(
|
||||
attachment_summary={
|
||||
'po_number': '4510039227',
|
||||
'reservation_ids': ['2000485916'],
|
||||
'lines': [{'reservation_id': '2000485916', 'sku': '504-006043-001', 'qty': 20, 'uom': 'EA'}],
|
||||
},
|
||||
filename='4510039227.pdf',
|
||||
body_rows=rows,
|
||||
)
|
||||
assert conflict['validation_status'] == 'needs_review'
|
||||
assert conflict['conflicts'][0]['field'] == 'qty'
|
||||
|
||||
multi_line = merge_attachment_candidate(
|
||||
attachment_summary={
|
||||
'po_number': '4510039227',
|
||||
'reservation_ids': ['OTHER', '2000485916'],
|
||||
'lines': [
|
||||
{'reservation_id': 'OTHER', 'sku': 'OTHER-SKU', 'qty': 5},
|
||||
{'reservation_id': '2000485916', 'sku': '504-006043-001', 'qty': 30},
|
||||
],
|
||||
},
|
||||
filename='4510039227.pdf',
|
||||
body_rows=rows,
|
||||
)
|
||||
assert multi_line['validation_status'] == 'ready'
|
||||
assert 'source_type' not in multi_line['business_summary']['lines'][0]
|
||||
assert multi_line['business_summary']['lines'][1]['source_type'] == 'VMI-FRA01'
|
||||
|
||||
|
||||
def test_extract_eml_with_three_pdf_attachments(tmp_path):
|
||||
"""带 3 个 PDF 附件的 EML:attachment_count=3,且各附件独立抽到文本。"""
|
||||
from app.services.shipping_notice.notice_upload import extract_notice_text
|
||||
@@ -634,6 +704,29 @@ def test_extract_eml_html_table_keeps_row_boundaries(tmp_path):
|
||||
assert "4510039227\t30" in body.replace(" ", "")
|
||||
|
||||
|
||||
def test_extract_eml_html_table_preserves_empty_cells(tmp_path):
|
||||
"""HTML 表格空单元格必须保留列占位,避免不同邮件模板的尾部业务字段整体错位。"""
|
||||
from email.message import EmailMessage
|
||||
|
||||
from app.services.shipping_notice.notice_upload import extract_notice_text
|
||||
|
||||
html = (
|
||||
"<table>"
|
||||
"<tr><th>SKU</th><th>Parent allocation ID</th><th>External reservation ID</th></tr>"
|
||||
"<tr><td>A001</td><td></td><td>AWS-311</td></tr>"
|
||||
"</table>"
|
||||
)
|
||||
msg = EmailMessage()
|
||||
msg.set_content(html, subtype="html")
|
||||
path = tmp_path / "html-empty-cell.eml"
|
||||
path.write_bytes(msg.as_bytes())
|
||||
|
||||
_, meta = extract_notice_text(path)
|
||||
body = meta["email"]["body_text"].replace(" ", "")
|
||||
|
||||
assert "A001\t\tAWS-311" in body
|
||||
|
||||
|
||||
def test_extract_eml_nested_eml_file_respects_depth(tmp_path):
|
||||
"""嵌套 .eml 文件附件须共享深度/限额,不得重置 depth=0 无限展开。"""
|
||||
from email.message import EmailMessage
|
||||
@@ -1134,6 +1227,76 @@ def test_extract_reservation_id_from_pr_number():
|
||||
assert find_reservation_id_in_document("PR Number: ADU_S-ENCWIW260528126265@1000140351") == "1000140351"
|
||||
|
||||
|
||||
def test_external_reservation_id_corrects_invalid_llm_candidate_with_column_evidence():
|
||||
"""明确 TSV 列关系与唯一 MPN 关联时,应把 32 位内部 ID 纠正为 External Reservation ID。"""
|
||||
raw_text = (
|
||||
"Allocated APN\tMPN\tAllocated QTY\tCommodity group ID\tExternal reservation ID\n"
|
||||
"504-006055-001\tD4-A08AR12000002\t10\t"
|
||||
"1f1a682dfdc446a393415dee93eb36ce\t3000092917"
|
||||
)
|
||||
lines = [
|
||||
{
|
||||
"sku": "TEMP-S000218929/D4-A08AR12000002",
|
||||
"vendor_part_number": "D4-A08AR12000002",
|
||||
"qty": 10,
|
||||
"uom": "EA",
|
||||
"reservation_id": "1f1a682dfdc446a393415dee93eb36ce",
|
||||
}
|
||||
]
|
||||
|
||||
adjudicated, issues, status = adjudicate_notice_lines(lines, raw_text=raw_text)
|
||||
|
||||
assert status == "ready"
|
||||
assert adjudicated[0]["reservation_id"] == "3000092917"
|
||||
assert issues == [
|
||||
{
|
||||
"line_index": 0,
|
||||
"sku": "TEMP-S000218929/D4-A08AR12000002",
|
||||
"field": "reservation_id",
|
||||
"reason": "value_corrected",
|
||||
"severity": "info",
|
||||
"raw_value": "1f1a682dfdc446a393415dee93eb36ce",
|
||||
"resolved_value": "3000092917",
|
||||
"resolution_basis": "email_table.External reservation ID",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_flattened_text_does_not_guess_external_reservation_id():
|
||||
"""纯文本缺少列边界时不得因附近存在数字而自动修正 Reservation ID。"""
|
||||
raw_text = (
|
||||
"Commodity group ID External reservation ID "
|
||||
"1f1a682dfdc446a393415dee93eb36ce 3000092917"
|
||||
)
|
||||
lines = [
|
||||
{
|
||||
"sku": "TEMP-S000218929/D4-A08AR12000002",
|
||||
"qty": 10,
|
||||
"uom": "EA",
|
||||
"reservation_id": "1f1a682dfdc446a393415dee93eb36ce",
|
||||
}
|
||||
]
|
||||
|
||||
adjudicated, issues, status = adjudicate_notice_lines(lines, raw_text=raw_text)
|
||||
|
||||
assert status == "needs_review"
|
||||
assert "reservation_id" not in adjudicated[0]
|
||||
assert issues[0]["reason"] == "invalid_format"
|
||||
assert issues[0]["severity"] == "blocking"
|
||||
|
||||
|
||||
def test_adjudication_supports_non_numeric_reservation_templates():
|
||||
"""不同来源模板可使用字母型 Reservation ID,后端只排除内部 ID,不绑定单一数字模板。"""
|
||||
raw_text = "AWS Sku\tReservation Order\tQty\nA001\tAWS-311\t2"
|
||||
lines = [{"sku": "A001", "reservation_id": "", "qty": 2, "uom": "EA"}]
|
||||
|
||||
adjudicated, issues, status = adjudicate_notice_lines(lines, raw_text=raw_text)
|
||||
|
||||
assert status == "ready"
|
||||
assert adjudicated[0]["reservation_id"] == "AWS-311"
|
||||
assert issues[0]["reason"] == "value_corrected"
|
||||
|
||||
|
||||
def test_wistron_po_table_with_pr_number_in_document():
|
||||
text = """
|
||||
Vendor: HOXIN
|
||||
|
||||
Reference in new issue
Block a user