feat(shipping-notice): 新增通用解析能力,优化性能并清理遗留废弃代码
This commit is contained in:
1 parent
935bf26fa6
commit
7cfd5d167e
230 files changed
+9337
-10989
No files matched your search
@@ -6,6 +6,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.shipping_notice.email_allocation_table_parser import (
|
||||
parse_email_allocation_tables,
|
||||
parse_email_allocation_text,
|
||||
@@ -87,6 +89,19 @@ def test_email_supplement_external_reservation_id_alias_merges_real_mail_header(
|
||||
assert merged[0]['_hxops_email_supplement_relation'] == 'po_mpn'
|
||||
|
||||
|
||||
def test_email_supplement_uses_schema_alias_order_for_duplicate_source_headers():
|
||||
"""同字段的两个邮件列按 Schema aliases 顺序取值,不能整表拒绝。"""
|
||||
supplement = parse_email_allocation_tables(
|
||||
content_hash='h' * 64,
|
||||
schema_definition=SCHEMA,
|
||||
html_tables=['''<table><tr><th>PO#</th><th>MPN</th><th>Source Type</th><th>hub code</th></tr><tr><td>4518738152</td><td>D4-A08AR12000002</td><td>FIRST-SOURCE</td><td>SECOND-HUB</td></tr></table>'''],
|
||||
)
|
||||
|
||||
assert supplement['issues'] == []
|
||||
assert supplement['rows'][0]['inventory_source_hint'] == 'FIRST-SOURCE'
|
||||
assert supplement['rows'][0]['evidence']['inventory_source_hint']['column_index'] == 2
|
||||
|
||||
|
||||
def test_email_supplement_ampn_alias_matches_customer_part_number():
|
||||
"""邮件 AMPN 应能与订单客户料号交叉关联并补入预约号。"""
|
||||
order = _order()
|
||||
@@ -139,7 +154,41 @@ def test_email_supplement_conflicting_schema_field_does_not_overwrite_pdf_or_pic
|
||||
orders=[order], supplements_by_intake={1: supplement}, schema_definition=SCHEMA,
|
||||
)[0]
|
||||
assert not any(item['field_id'] == 'reservation' for item in merged['lines'][0]['fields'])
|
||||
assert {'severity': 'blocking', 'reason': 'supplement_field_conflict', 'field': 'reservation_id'} in merged['conflicts']
|
||||
finding = next(item for item in merged['conflicts'] if item.get('field') == 'reservation_id')
|
||||
assert finding['resolution_status'] == 'unresolved'
|
||||
assert finding['scope'] == 'line'
|
||||
assert finding['line_index'] == 0
|
||||
assert finding['conflict_key'] == '0:reservation_id'
|
||||
assert finding['values'] == ['33874', '33875']
|
||||
assert [source['source_type'] for source in finding['sources']] == ['email_body', 'email_body']
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_email_supplement_does_not_cross_compare_values_from_different_part_lines():
|
||||
"""同 PO 的不同料号可有不同单价,补充合并只能比较同一目标行的关联正文行。"""
|
||||
schema = {
|
||||
'fields': [
|
||||
*SCHEMA['fields'],
|
||||
{'field_id': 'unit-price', 'field_code': 'unit_price', 'scope': 'line', 'value_type': 'decimal'},
|
||||
],
|
||||
}
|
||||
order = _order()
|
||||
order['lines'][0]['fields'].append({'field_id': 'unit-price', 'observations': [{'value': '10.00'}]})
|
||||
order['lines'].append({'fields': [
|
||||
{'field_id': 'part', 'observations': [{'value': 'D4-A08AR12000003'}]},
|
||||
{'field_id': 'qty', 'observations': [{'value': 1}]},
|
||||
{'field_id': 'unit-price', 'observations': [{'value': '20.00'}]},
|
||||
]})
|
||||
result = merge_email_supplements(
|
||||
orders=[order],
|
||||
supplements_by_intake={1: {'document_id': 304, 'rows': [
|
||||
{'customer_po_number': '4518738152', 'part_number': 'D4-A08AR12000002', 'unit_price': '10.00', 'evidence': {}},
|
||||
{'customer_po_number': '4518738152', 'part_number': 'D4-A08AR12000003', 'unit_price': '20.00', 'evidence': {}},
|
||||
]}},
|
||||
schema_definition=schema,
|
||||
)[0]
|
||||
|
||||
assert not any(item.get('field') == 'unit_price' for item in result['conflicts'])
|
||||
|
||||
|
||||
def test_email_supplement_pdf_value_conflict_keeps_pdf_and_requires_review():
|
||||
@@ -166,6 +215,110 @@ def test_email_supplement_pdf_value_conflict_keeps_pdf_and_requires_review():
|
||||
assert {'severity': 'blocking', 'reason': 'supplement_field_conflict', 'field': 'reservation_id'} in merged['conflicts']
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_email_supplement_treats_decimal_format_variants_as_the_same_value():
|
||||
"""PDF 与邮件的金额仅格式不同,必须按 Schema decimal 语义比较而非制造冲突。"""
|
||||
schema = {
|
||||
'fields': [
|
||||
*SCHEMA['fields'],
|
||||
{'field_id': 'unit-price', 'field_code': 'unit_price', 'scope': 'line', 'value_type': 'decimal'},
|
||||
],
|
||||
}
|
||||
order = _order()
|
||||
order['lines'][0]['fields'].append({
|
||||
'field_id': 'unit-price', 'observations': [{'value': '1731.6'}],
|
||||
})
|
||||
supplement = {
|
||||
'document_id': 44,
|
||||
'rows': [{
|
||||
'customer_po_number': '4518738152',
|
||||
'part_number': 'D4-A08AR12000002',
|
||||
'unit_price': '1,731.60',
|
||||
'evidence': {},
|
||||
}],
|
||||
}
|
||||
|
||||
merged = merge_email_supplements(
|
||||
orders=[order], supplements_by_intake={1: supplement}, schema_definition=schema,
|
||||
)[0]
|
||||
|
||||
assert _value(merged['lines'][0]['fields'], 'unit-price') == '1731.6'
|
||||
assert not any(item.get('field') == 'unit_price' for item in merged['conflicts'])
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_email_supplement_decimal_difference_creates_a_complete_unresolved_finding():
|
||||
"""真实金额不一致必须保留双方原值、规范值和邮件单元格证据,供复核直接裁决。"""
|
||||
schema = {
|
||||
'version': 'shipping-order-schema.v9',
|
||||
'fields': [
|
||||
*SCHEMA['fields'],
|
||||
{'field_id': 'unit-price', 'field_code': 'unit_price', 'scope': 'line', 'value_type': 'decimal'},
|
||||
],
|
||||
}
|
||||
order = _order()
|
||||
order['lines'][0]['fields'].append({
|
||||
'field_id': 'unit-price', 'observations': [{
|
||||
'value': '1731.60', 'document_id': 305, 'evidence_ref': {'page_no': 1, 'cell_id': 'pdf-unit-price'},
|
||||
}],
|
||||
})
|
||||
supplement = {
|
||||
'document_id': 304,
|
||||
'rows': [{
|
||||
'customer_po_number': '4518738152',
|
||||
'part_number': 'D4-A08AR12000002',
|
||||
'unit_price': '1732.00',
|
||||
'evidence': {'unit_price': {'table_index': 0, 'row_index': 5, 'column_index': 4, 'cell_id': 'email-unit-price'}},
|
||||
}],
|
||||
}
|
||||
|
||||
merged = merge_email_supplements(
|
||||
orders=[order], supplements_by_intake={1: supplement}, schema_definition=schema,
|
||||
)[0]
|
||||
|
||||
finding = next(item for item in merged['conflicts'] if item.get('field') == 'unit_price')
|
||||
assert finding['conflict_key'] == '0:unit_price'
|
||||
assert finding['severity'] == 'blocking'
|
||||
assert finding['resolution_status'] == 'unresolved'
|
||||
assert finding['scope'] == 'line'
|
||||
assert finding['line_index'] == 0
|
||||
assert finding['values'] == ['1731.60', '1732.00']
|
||||
assert finding['sources'] == [
|
||||
{
|
||||
'source_type': 'pdf', 'raw_value': '1731.60', 'canonical_value': '1731.60',
|
||||
'document_id': 305, 'evidence_ref': {'page_no': 1, 'cell_id': 'pdf-unit-price'},
|
||||
},
|
||||
{
|
||||
'source_type': 'email_body', 'raw_value': '1732.00', 'canonical_value': '1732.00',
|
||||
'document_id': 304,
|
||||
'evidence_ref': {'table_index': 0, 'row_index': 5, 'column_index': 4, 'cell_id': 'email-unit-price'},
|
||||
},
|
||||
]
|
||||
assert finding['rule_version'] == 'shipping-order-schema.v9'
|
||||
|
||||
|
||||
@pytest.mark.pure_unit
|
||||
def test_email_supplement_duplicate_email_decimal_formats_do_not_create_a_conflict():
|
||||
"""同一邮件补充行的等价金额格式必须按 decimal 归并,不能产生正文内部冲突。"""
|
||||
schema = {
|
||||
'fields': [
|
||||
*SCHEMA['fields'],
|
||||
{'field_id': 'unit-price', 'field_code': 'unit_price', 'scope': 'line', 'value_type': 'decimal'},
|
||||
],
|
||||
}
|
||||
result = merge_email_supplements(
|
||||
orders=[_order()],
|
||||
supplements_by_intake={1: {'document_id': 304, 'rows': [
|
||||
{'customer_po_number': '4518738152', 'part_number': 'D4-A08AR12000002', 'unit_price': '1731.6', 'evidence': {}},
|
||||
{'customer_po_number': '4518738152', 'part_number': 'D4-A08AR12000002', 'unit_price': '1,731.60', 'evidence': {}},
|
||||
]}},
|
||||
schema_definition=schema,
|
||||
)[0]
|
||||
|
||||
assert _value(result['lines'][0]['fields'], 'unit-price') == '1731.6'
|
||||
assert not any(item.get('field') == 'unit_price' for item in result['conflicts'])
|
||||
|
||||
|
||||
def test_email_supplement_cross_adjudicates_pn_and_ampn_against_both_order_part_numbers():
|
||||
"""邮件 PN/AMPN 均可与订单内部/客户料号交叉匹配,不依赖字段名称相同。"""
|
||||
scenarios = [
|
||||
|
||||
Reference in new issue
Block a user