feat(shipping-notice): 实现滚动履约功能,优化发货通知流程与代码清理
清理废弃的ShippingCase相关代码与依赖,统一使用ShippingNotice直接关联客户数据。调整库存网关依赖规则,支持仅对库存满足或已人工放行的订单行生成发货Excel,实现滚动履约逻辑。
This commit is contained in:
1 parent
7cfd5d167e
commit
1e5aedb9ed
235 files changed
+11394
-9613
No files matched your search
@@ -0,0 +1,160 @@
|
||||
"""
|
||||
Reservation ID 专用证据区域解析器。
|
||||
|
||||
用途:从管理员圈选的 Vendor Comments 区域提取 Reservation ID 和可选行号。
|
||||
依赖:reservation_id 的统一格式校验。
|
||||
逻辑:只消费区域文字,不依赖客户名称、固定坐标或旧的三来源映射;无行号仅标记为未归属,最终由 Schema scope 决定表头写入或明细复核。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from app.services.shipping_notice.document_layout.coordinate_region_text import visual_text_lines
|
||||
from app.services.shipping_notice.reservation_id import validated_reservation_id
|
||||
|
||||
|
||||
_ID_RE = re.compile(r"(?<!\d)(\d{5}|\d{10})(?!\d)")
|
||||
_LINE_RE = re.compile(r"\b(?:reservation\s+)?line\s*#?\s*(\d+)\b|\bfor\s+line\s*#?\s*(\d+)\b", re.I)
|
||||
_RESERVATION_LABEL_RE = re.compile(r"\breservation\b\s*(?:id)?\s*(?:#|:|-)?", re.I)
|
||||
_VENDOR_COMMENTS_HEADING_RE = re.compile(r'^\s*vendor\s+comments?\s*:?\s*$', re.I)
|
||||
_TERMS_AND_CONDITIONS_HEADING_RE = re.compile(r'^\s*terms?\s+and\s+conditions?\s*:?\s*$', re.I)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ReservationRegionCandidate:
|
||||
value: str
|
||||
line_no: int | None
|
||||
scope: str
|
||||
raw_text: str
|
||||
status: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ReservationRegionApplication:
|
||||
"""Reservation 区域候选的统一业务归属结果。"""
|
||||
|
||||
header_value: str | None
|
||||
line_values: dict[int, str]
|
||||
needs_review: bool
|
||||
review_reasons: tuple[str, ...]
|
||||
|
||||
|
||||
def extract_reservation_from_region(text: str) -> list[ReservationRegionCandidate]:
|
||||
"""从区域原文提取候选;有行号归明细,无行号归订单头。"""
|
||||
candidates: list[ReservationRegionCandidate] = []
|
||||
for raw_line in (str(text or '').splitlines() or [str(text or '')]):
|
||||
line = raw_line.strip()
|
||||
# A selected comments area can also contain customer numbers, quantities,
|
||||
# and dates. Only a value on a Reservation-labelled line is evidence.
|
||||
if not line or not _RESERVATION_LABEL_RE.search(line):
|
||||
continue
|
||||
ids = [validated_reservation_id(m.group(1)) for m in _ID_RE.finditer(line)]
|
||||
ids = [value for value in ids if value]
|
||||
if not ids:
|
||||
continue
|
||||
line_match = _LINE_RE.search(line)
|
||||
line_no = None
|
||||
if line_match:
|
||||
line_no = int(line_match.group(1) or line_match.group(2))
|
||||
scope = 'line' if line_no is not None else 'header'
|
||||
status = 'mapped' if line_no is not None else 'header'
|
||||
for value in dict.fromkeys(ids):
|
||||
candidates.append(ReservationRegionCandidate(value, line_no, scope, line, status))
|
||||
return candidates
|
||||
|
||||
|
||||
def extract_reservation_from_region_text(text: str) -> list[dict[str, object]]:
|
||||
"""返回 API/预览可直接序列化的统一候选 DTO。"""
|
||||
return [
|
||||
{
|
||||
'value': item.value,
|
||||
'line_no': item.line_no,
|
||||
'scope': item.scope,
|
||||
'raw_text': item.raw_text,
|
||||
'status': item.status,
|
||||
}
|
||||
for item in extract_reservation_from_region(text)
|
||||
]
|
||||
|
||||
|
||||
def extract_reservation_from_vendor_comments_pages(
|
||||
pages: list[tuple[int, list[dict[str, Any]]]],
|
||||
) -> tuple[list[dict[str, object]], str | None]:
|
||||
"""从跨页 Vendor Comments 区块提取 Reservation;仅首个 Terms 标题前的文字有效。
|
||||
|
||||
核心逻辑:Reservation 专区已启用时,页面坐标只是提供稳定阅读顺序,不能作为固定
|
||||
页码或矩形取值依据。缺少起止锚点时返回稳定原因,由调用方转人工复核。
|
||||
"""
|
||||
started = False
|
||||
window_lines: list[str] = []
|
||||
for _page_no, glyphs in sorted(pages, key=lambda item: item[0]):
|
||||
for line in visual_text_lines(glyphs):
|
||||
if not started:
|
||||
if _VENDOR_COMMENTS_HEADING_RE.fullmatch(line):
|
||||
started = True
|
||||
continue
|
||||
if _TERMS_AND_CONDITIONS_HEADING_RE.fullmatch(line):
|
||||
return extract_reservation_from_region_text('\n'.join(window_lines)), None
|
||||
window_lines.append(line)
|
||||
if not started:
|
||||
return [], 'vendor_comments_anchor_missing'
|
||||
return [], 'terms_and_conditions_anchor_missing'
|
||||
|
||||
|
||||
def apply_reservation_region_candidates(
|
||||
*, candidates: list[dict[str, object]], header_field_id: str | None,
|
||||
line_field_id: str | None, line_count: int,
|
||||
) -> ReservationRegionApplication:
|
||||
"""按固定契约将专用区域候选归属到订单头和逻辑明细行。
|
||||
|
||||
无行号 Reservation 是整单事实,最多接受一个不同值;带行号的值仅在
|
||||
Schema 启用明细 Reservation 字段时写入对应逻辑行。冲突、越界行号或
|
||||
多个不同整单值必须转人工复核,不能静默猜测。
|
||||
"""
|
||||
_ = header_field_id
|
||||
header_values: list[str] = []
|
||||
line_values_by_no: dict[int, list[str]] = {}
|
||||
reasons: list[str] = []
|
||||
for item in candidates:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
value = str(item.get('value') or '')
|
||||
if not value:
|
||||
continue
|
||||
if item.get('scope') == 'header':
|
||||
header_values.append(value)
|
||||
continue
|
||||
if item.get('scope') != 'line' or item.get('line_no') is None:
|
||||
continue
|
||||
try:
|
||||
line_no = int(item['line_no'])
|
||||
except (TypeError, ValueError):
|
||||
reasons.append('line_out_of_range')
|
||||
continue
|
||||
if line_no < 1 or line_no > line_count:
|
||||
reasons.append('line_out_of_range')
|
||||
continue
|
||||
if line_field_id:
|
||||
line_values_by_no.setdefault(line_no, []).append(value)
|
||||
else:
|
||||
reasons.append('line_field_unavailable')
|
||||
|
||||
unique_headers = list(dict.fromkeys(header_values))
|
||||
if len(unique_headers) > 1:
|
||||
reasons.append('conflicting_header_value')
|
||||
resolved_lines: dict[int, str] = {}
|
||||
for line_no, values in line_values_by_no.items():
|
||||
unique_values = list(dict.fromkeys(values))
|
||||
if len(unique_values) > 1:
|
||||
reasons.append('conflicting_line_value')
|
||||
continue
|
||||
resolved_lines[line_no] = unique_values[0]
|
||||
return ReservationRegionApplication(
|
||||
header_value=unique_headers[0] if len(unique_headers) == 1 else None,
|
||||
line_values=resolved_lines,
|
||||
needs_review=bool(reasons),
|
||||
review_reasons=tuple(dict.fromkeys(reasons)),
|
||||
)
|
||||
Reference in new issue
Block a user