refactor(shipping-notice): 统一料号字段,移除过时的 SKU 相关字段,更新相关逻辑以支持新字段命名

This commit is contained in:
hugo committed 2026-07-16 20:02:33 +08:00
1 parent dde25344ca
commit 46eb63f5bb
111 files changed
+3116 -1250

No files matched your search

+90 -40
View File
@@ -1,6 +1,8 @@
from email.message import EmailMessage
from pathlib import Path
import pytest
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,
@@ -123,6 +125,8 @@ def test_extract_notice_text_reads_xfa_pdf_datasets():
from app.services.shipping_notice.notice_upload import extract_notice_text
pdf_path = Path(__file__).resolve().parents[2] / "docs" / "prototype" / "4510038672.pdf"
if not pdf_path.exists():
pytest.skip("XFA PDF fixture docs/prototype/4510038672.pdf is not present")
text, meta = extract_notice_text(pdf_path)
assert meta["file_type"] == "pdf"
@@ -146,19 +150,18 @@ S-ENCFOX260705BG977W\t1000148820\tDBS DUB\tTEMP-S000218922\t504-006043-001\t1\tD
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]['part_number'] == 'D4-A08AR12000001'
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'
assert rows[0]['inventory_source_hint'] == 'VMI-FRA01'
assert rows[0]['customer_part_number'] == 'TEMP-S000218922'
assert rows[0]['aws_part_number'] == '504-006043-001'
assert rows[2]['inventory_source_hint'] == 'DBS 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'}],
'lines': [{'reservation_id': '2000485916', 'part_number': 'D4-A08AR12000001', 'qty': 30, 'uom': 'EA'}],
},
filename='4510039227.pdf',
body_rows=rows,
@@ -166,15 +169,15 @@ S-ENCFOX260705BG977W\t1000148820\tDBS DUB\tTEMP-S000218922\t504-006043-001\t1\tD
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'
assert line['inventory_source_hint'] == 'VMI-FRA01'
assert line['customer_part_number'] == 'TEMP-S000218922'
assert line['aws_part_number'] == '504-006043-001'
conflict = merge_attachment_candidate(
attachment_summary={
'po_number': '4510039227',
'reservation_ids': ['2000485916'],
'lines': [{'reservation_id': '2000485916', 'sku': '504-006043-001', 'qty': 20, 'uom': 'EA'}],
'lines': [{'reservation_id': '2000485916', 'part_number': 'D4-A08AR12000001', 'qty': 20, 'uom': 'EA'}],
},
filename='4510039227.pdf',
body_rows=rows,
@@ -187,16 +190,16 @@ S-ENCFOX260705BG977W\t1000148820\tDBS DUB\tTEMP-S000218922\t504-006043-001\t1\tD
'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},
{'reservation_id': 'OTHER', 'part_number': 'OTHER-SKU', 'qty': 5},
{'reservation_id': '2000485916', 'part_number': 'D4-A08AR12000001', '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'
assert 'inventory_source_hint' not in multi_line['business_summary']['lines'][0]
assert multi_line['business_summary']['lines'][1]['inventory_source_hint'] == 'VMI-FRA01'
def test_extract_eml_with_three_pdf_attachments(tmp_path):
@@ -1236,8 +1239,8 @@ def test_external_reservation_id_corrects_invalid_llm_candidate_with_column_evid
)
lines = [
{
"sku": "TEMP-S000218929/D4-A08AR12000002",
"vendor_part_number": "D4-A08AR12000002",
"part_number": "D4-A08AR12000002",
"customer_part_number": "TEMP-S000218929",
"qty": 10,
"uom": "EA",
"reservation_id": "1f1a682dfdc446a393415dee93eb36ce",
@@ -1251,7 +1254,7 @@ def test_external_reservation_id_corrects_invalid_llm_candidate_with_column_evid
assert issues == [
{
"line_index": 0,
"sku": "TEMP-S000218929/D4-A08AR12000002",
"part_number": "D4-A08AR12000002",
"field": "reservation_id",
"reason": "value_corrected",
"severity": "info",
@@ -1270,7 +1273,7 @@ def test_flattened_text_does_not_guess_external_reservation_id():
)
lines = [
{
"sku": "TEMP-S000218929/D4-A08AR12000002",
"part_number": "D4-A08AR12000002",
"qty": 10,
"uom": "EA",
"reservation_id": "1f1a682dfdc446a393415dee93eb36ce",
@@ -1287,8 +1290,8 @@ def test_flattened_text_does_not_guess_external_reservation_id():
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"}]
raw_text = "Part Number\tReservation Order\tQty\nA001\tAWS-311\t2"
lines = [{"part_number": "A001", "reservation_id": "", "qty": 2, "uom": "EA"}]
adjudicated, issues, status = adjudicate_notice_lines(lines, raw_text=raw_text)
@@ -1306,14 +1309,14 @@ Line No\tMaterial\tVendor Part Number\tDescription\tQuantity\tETA\tUnit Price\tA
"""
lines = parse_notice_table_text(text)
assert len(lines) == 1
assert lines[0]["sku"] == "504-010213-001"
assert lines[0]["part_number"] == "B3-A08AR11000001"
assert lines[0]["line_no"] == "10"
assert lines[0]["qty"] == 9.0
assert lines[0]["unit_price"] == 1607.72
assert lines[0]["amount"] == 14469.48
assert lines[0]["reservation_id"] == "1000140351"
assert lines[0]["required_date"] == "2026-06-15"
assert lines[0]["vendor_part_number"] == "B3-A08AR11000001"
assert lines[0]["aws_part_number"] == "504-010213-001"
assert lines[0]["currency"] == "USD"
@@ -1325,7 +1328,7 @@ def test_normalize_reservation_id_from_excel_float():
def test_parse_notice_table_numeric_reservation_id():
text = """
AWS Sku\tReservation ID\tQty\tUnit Price\tAmount
Part Number\tReservation ID\tQty\tUnit Price\tAmount
10-40594-001\t1000140351\t2\t15.5\t31.0
"""
lines = parse_notice_table_text(text)
@@ -1333,7 +1336,7 @@ AWS Sku\tReservation ID\tQty\tUnit Price\tAmount
def test_infer_reservation_id_without_header():
headers = ["AWS Sku", "Ref", "Qty"]
headers = ["Part Number", "Ref", "Qty"]
rows = [["10-40594-001", 1000140351, 2]]
lines = parse_notice_table_rows(headers, rows)
assert lines[0]["reservation_id"] == "1000140351"
@@ -1341,13 +1344,13 @@ def test_infer_reservation_id_without_header():
def test_parse_notice_table_with_all_fields():
text = """
AWS Sku\tReservation ID\tQty\tUnit Price\tAmount
Part Number\tReservation ID\tQty\tUnit Price\tAmount
10-40594-001\tAWS-311\t2\t15.5\t31.0
10-40594-002\tORD-3002\t1\t20\t20
"""
lines = parse_notice_table_text(text)
assert len(lines) == 2
assert lines[0]["sku"] == "10-40594-001"
assert lines[0]["part_number"] == "10-40594-001"
assert lines[0]["reservation_id"] == "AWS-311"
assert lines[0]["qty"] == 2
assert lines[0]["unit_price"] == 15.5
@@ -1362,14 +1365,15 @@ def test_normalize_notice_line_aliases():
"com_id": "COM-99",
"price": "12.5",
"delivery_date": "2026/06/15",
"vendorPartNumber": "VND-001",
"customer_part_number": "CUST-001",
"aws_part_number": "AWS-001",
"currency": "USD",
"line_no": "10",
}
)
assert line == {
"line_no": "10",
"sku": "A-001",
"part_number": "A-001",
"qty": 3.0,
"uom": "EA",
"reservation_id": "COM-99",
@@ -1377,17 +1381,63 @@ def test_normalize_notice_line_aliases():
"amount": 37.5,
"currency": "USD",
"required_date": "2026-06-15",
"vendor_part_number": "VND-001",
"customer_part_number": "CUST-001",
"aws_part_number": "AWS-001",
}
def test_normalize_notice_line_splits_frozen_temp_composite():
"""TEMP 二段复合文本应拆为客户料号和内部料号,禁止把组合值持久化。"""
line = normalize_notice_line({
"part_number": "TEMP-S000218929/D4-A08AR12000002",
"qty": 10,
})
assert line is not None
assert line["part_number"] == "D4-A08AR12000002"
assert line["customer_part_number"] == "TEMP-S000218929"
@pytest.mark.parametrize(
"value",
["LEFT/RIGHT", "TEMP-A/D4-B/THIRD", "TEMP-A/TEMP-B"],
)
def test_normalize_notice_line_rejects_ambiguous_composite(value):
"""未命中冻结规则的复合料号不得猜测角色或生成有效订单行。"""
assert normalize_notice_line({"part_number": value, "qty": 1}) is None
def test_parse_notice_table_maps_three_part_number_roles():
"""MPN、Allocated HHPN 与 Allocated APN 必须分别进入内部、客户和 AWS 料号。"""
lines = parse_notice_table_rows(
["MPN", "Allocated HHPN", "Allocated APN", "Allocated QTY"],
[["D4-A08AR12000002", "TEMP-S000218929", "504-006055-001", 10]],
)
assert lines == [{
"part_number": "D4-A08AR12000002",
"customer_part_number": "TEMP-S000218929",
"aws_part_number": "504-006055-001",
"qty": 10.0,
"uom": "EA",
}]
def test_parse_notice_table_rejects_aws_only_as_internal_part_number():
"""只有 AWS Sku 的表格缺少内部料号,不能生成可建单订单行。"""
assert parse_notice_table_rows(
["AWS Sku", "Reservation ID", "Qty"],
[["504-006055-001", "3000092917", 10]],
) == []
def test_enrich_notice_with_document_currency():
from app.services.shipping_notice.notice_lines import enrich_notice_with_document_currency
parsed = enrich_notice_with_document_currency(
{
"currency": "USD",
"lines": [{"sku": "A-001", "qty": 1, "uom": "EA"}, {"sku": "B-002", "qty": 2, "currency": "EUR", "uom": "EA"}],
"lines": [{"part_number": "A-001", "qty": 1, "uom": "EA"}, {"part_number": "B-002", "qty": 2, "currency": "EUR", "uom": "EA"}],
}
)
assert parsed["lines"][0]["currency"] == "USD"
@@ -1408,7 +1458,7 @@ def test_enrich_notice_with_document_required_date():
parsed = enrich_notice_with_document_required_date(
{
"required_date": "2026-06-20",
"lines": [{"sku": "A-001", "qty": 1, "uom": "EA"}, {"sku": "B-002", "qty": 2, "required_date": "2026-06-18", "uom": "EA"}],
"lines": [{"part_number": "A-001", "qty": 1, "uom": "EA"}, {"part_number": "B-002", "qty": 2, "required_date": "2026-06-18", "uom": "EA"}],
}
)
assert parsed["lines"][0]["required_date"] == "2026-06-20"
@@ -1420,7 +1470,7 @@ def test_extract_notice_fields_structured():
"customer_name": "ACME",
"lines": [
{
"sku": "10-40594-001",
"part_number": "10-40594-001",
"qty": 2,
"reservation_id": "AWS-311",
"unit_price": 10,
@@ -1437,7 +1487,7 @@ def test_extract_notice_fields_structured():
def test_validate_notice_lines_missing_reservation():
warnings = validate_notice_lines([{"sku": "A001", "qty": 1, "uom": "EA"}])
warnings = validate_notice_lines([{"part_number": "A001", "qty": 1, "uom": "EA"}])
assert any("Reservation ID" in w for w in warnings)
assert any("阻断后续库存匹配" in w for w in warnings)
@@ -1447,12 +1497,12 @@ def test_heuristic_table_skips_title_row():
text = """
Shipping Notice
AWS Sku\tReservation Order\tOrder Qty\tUnit Price\tLine Amount
Part Number\tReservation Order\tOrder Qty\tUnit Price\tLine Amount
10-40594-001\tORD-3002\t4\t5\t20
"""
parsed, confidence, _ = _heuristic_extract(text)
assert len(parsed["lines"]) == 1
assert parsed["lines"][0]["sku"] == "10-40594-001"
assert parsed["lines"][0]["part_number"] == "10-40594-001"
assert parsed["lines"][0]["reservation_id"] == "ORD-3002"
assert parsed["lines"][0]["qty"] == 4
assert parsed["lines"][0]["unit_price"] == 5
@@ -1464,7 +1514,7 @@ def test_extract_notice_fields_structured_over_heuristic():
"""Dify 传入 structured 时应直接使用,不走启发式。"""
structured = {
"customer_name": "ACME",
"lines": [{"sku": "SKU-DIFY", "qty": 3, "uom": "EA"}],
"lines": [{"part_number": "SKU-DIFY", "qty": 3, "uom": "EA"}],
"confidence": 0.95,
}
parsed, confidence, warnings = extract_notice_fields(
@@ -1472,15 +1522,15 @@ def test_extract_notice_fields_structured_over_heuristic():
structured=structured,
)
assert parsed["parse_source"] == "dify"
assert parsed["lines"][0]["sku"] == "SKU-DIFY"
assert parsed["lines"][0]["part_number"] == "SKU-DIFY"
assert confidence > 0.8
assert "parse_fallback_heuristic" not in warnings
def test_extract_notice_fields_heuristic_fallback():
"""无 structured 时走后端启发式兜底。"""
text = "AWS Sku\tReservation Order\tOrder Qty\n10-40594-001\tORD-3002\t4"
text = "Part Number\tReservation Order\tOrder Qty\n10-40594-001\tORD-3002\t4"
parsed, _, warnings = extract_notice_fields(raw_text=text)
assert parsed["parse_source"] == "heuristic"
assert parsed["lines"][0]["sku"] == "10-40594-001"
assert parsed["lines"][0]["part_number"] == "10-40594-001"
assert "parse_fallback_heuristic" in warnings