feat(freight-rpa): 库存/收料改名在手/入站,报表归属RPA,类名统一FreightRpa

把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与
枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa
持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀,
上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-19 10:46:06 +08:00
1 parent ffc0d4a430
commit 39fe3c05f2
453 files changed
+17945 -8646

No files matched your search

+40 -31
View File
@@ -33,9 +33,9 @@ from app.models import (
ShippingExcelTemplate,
ShippingExportArtifact,
ShippingNoticeLine,
ShippingNoticeLineInventoryMatch,
ShippingInventoryField,
ShippingInventorySource,
ShippingNoticeLineOnhandMatch,
FreightRpaOnhandField,
FreightRpaSource,
ShippingOrderSnapshot,
ShippingOrderField,
ShippingNotice,
@@ -58,11 +58,11 @@ def _notice_source_code(task: ShippingNotice) -> str | None:
task_id = int(task_id)
codes = [
str(row[0]).strip().upper()
for row in 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),
for row in db.session.query(ShippingNoticeLineOnhandMatch.source_code).filter(
ShippingNoticeLineOnhandMatch.task_id == task_id,
ShippingNoticeLineOnhandMatch.is_current.is_(True),
ShippingNoticeLineOnhandMatch.match_result == 'matched',
ShippingNoticeLineOnhandMatch.source_code.isnot(None),
).distinct().all()
if row[0]
]
@@ -76,10 +76,10 @@ def _notice_chosen_odm(task: ShippingNotice) -> str | None:
return None
values = [
str(row[0]).strip()
for row in db.session.query(ShippingNoticeLineInventoryMatch.chosen_odm_id).filter(
ShippingNoticeLineInventoryMatch.task_id == int(task_id),
ShippingNoticeLineInventoryMatch.is_current.is_(True),
ShippingNoticeLineInventoryMatch.chosen_odm_id.isnot(None),
for row in db.session.query(ShippingNoticeLineOnhandMatch.chosen_odm_id).filter(
ShippingNoticeLineOnhandMatch.task_id == int(task_id),
ShippingNoticeLineOnhandMatch.is_current.is_(True),
ShippingNoticeLineOnhandMatch.chosen_odm_id.isnot(None),
).distinct().all()
if row[0]
]
@@ -93,11 +93,11 @@ def _notice_inventory_file_id(task: ShippingNotice) -> int | None:
return None
task_id = int(task_id)
ids = [
int(row[0]) for row in db.session.query(ShippingNoticeLineInventoryMatch.inventory_snapshot_id).filter(
ShippingNoticeLineInventoryMatch.task_id == task_id,
ShippingNoticeLineInventoryMatch.is_current.is_(True),
ShippingNoticeLineInventoryMatch.match_result == 'matched',
ShippingNoticeLineInventoryMatch.inventory_snapshot_id.isnot(None),
int(row[0]) for row in db.session.query(ShippingNoticeLineOnhandMatch.onhand_snapshot_id).filter(
ShippingNoticeLineOnhandMatch.task_id == task_id,
ShippingNoticeLineOnhandMatch.is_current.is_(True),
ShippingNoticeLineOnhandMatch.match_result == 'matched',
ShippingNoticeLineOnhandMatch.onhand_snapshot_id.isnot(None),
).distinct().all()
]
return ids[0] if len(ids) == 1 else None
@@ -395,8 +395,8 @@ def _inventory_fields_by_mode(mappings: list[dict[str, Any]], row_mode: str) ->
ids = {int(item) for item in raw_ids if item.isdigit()}
if len(ids) != len(raw_ids):
raise ValueError('发货模板引用的库存字段无效')
rows = ShippingInventoryField.query.filter(
ShippingInventoryField.id.in_(ids), ShippingInventoryField.status == 'active',
rows = FreightRpaOnhandField.query.filter(
FreightRpaOnhandField.id.in_(ids), FreightRpaOnhandField.status == 'active',
).all() if ids else []
if len(rows) != len(ids):
raise ValueError('发货模板引用的库存字段已不存在')
@@ -407,7 +407,7 @@ def _inventory_field_user_label(field_id: str) -> str:
"""把库存字段主键转换为用户可读中文名,禁止在业务错误中直接暴露「字段 6」。"""
normalized = str(field_id or '').strip()
if normalized.isdigit():
row = ShippingInventoryField.query.filter_by(id=int(normalized)).first()
row = FreightRpaOnhandField.query.filter_by(id=int(normalized)).first()
display_name = str(getattr(row, 'display_name', '') or '').strip() if row else ''
if display_name:
return display_name
@@ -572,17 +572,20 @@ def _filename(
def _input_checksum(
task: ShippingNotice, source: ShippingInventorySource, template: ShippingExcelTemplate,
task: ShippingNotice, source: FreightRpaSource, template: ShippingExcelTemplate,
snapshot: ShippingOrderSnapshot, header: dict[str, Any], lines: list[dict[str, Any]],
inventory_lines: list[dict[str, Any]],
notice_id: int | None = None,
) -> str:
"""以本次实际导出库存行计算输入指纹,保证 Artifact 内容与审计事实一致。"""
# s18 起解析方案绑定归 onhand_report_config 配置行,站点行不再承载该外键。
report_config = source.onhand_report_config
payload = {
'task_id': task.id, 'source_id': source.id, 'template_id': template.id,
'template_checksum': template.file_checksum, 'definition_checksum': template.definition_checksum,
'schema_version_id': snapshot.schema_version_id, 'snapshot_id': snapshot.id,
'parsing_profile_id': source.parsing_profile_id, 'inventory_snapshot_id': _notice_inventory_file_id(task),
'parsing_profile_id': report_config.parsing_profile_id if report_config is not None else None,
'inventory_snapshot_id': _notice_inventory_file_id(task),
'order_header': header, 'order_lines': lines, 'inventory_lines': inventory_lines,
'notice_id': notice_id,
}
@@ -592,7 +595,7 @@ def _input_checksum(
def _resolve_odm_inventory_field_id() -> int | None:
"""返回 odm_id 库存字段目录 ID;目录未配置或已停用时返回 None(整单 ODM 裁决整体跳过)。"""
field = ShippingInventoryField.query.filter_by(field_code='odm_id', status='active').first()
field = FreightRpaOnhandField.query.filter_by(field_code='odm_id', status='active').first()
return int(field.id) if field is not None else None
@@ -610,7 +613,7 @@ def _normalize_match_keys(match_keys: list[dict[str, Any]]) -> list[dict[str, st
}
catalog = {
str(item.id): str(item.id)
for item in ShippingInventoryField.query.filter(ShippingInventoryField.id.in_(ids)).all()
for item in FreightRpaOnhandField.query.filter(FreightRpaOnhandField.id.in_(ids)).all()
}
normalized_keys: list[dict[str, str]] = []
for item in match_keys:
@@ -762,7 +765,7 @@ def prepare_export_match_inputs(task: ShippingNotice) -> dict[str, Any]:
)
# [T6 E2] 滚动履约只导出当前可用行;短缺/冲突行由下方 Reservation 资格筛选排除。
source_code = str(_notice_source_code(task) or '').strip().upper()
source = ShippingInventorySource.query.filter_by(source_code=source_code, enabled=True).first()
source = FreightRpaSource.query.filter_by(source_code=source_code, enabled=True).first()
if source is None:
source = None
if source is None:
@@ -770,7 +773,10 @@ def prepare_export_match_inputs(task: ShippingNotice) -> dict[str, Any]:
template = source.excel_template
if template is None or template.status != 'published':
raise ValueError('库存源未绑定已发布的发货 Excel 模板')
if source.parsing_profile is None or source.parsing_profile.status != 'published':
# s18 起解析方案绑定归 onhand_report_config 配置行;配置行缺失即未绑定。
report_config = source.onhand_report_config
profile = report_config.parsing_profile if report_config is not None else None
if profile is None or profile.status != 'published':
raise ValueError('库存源未绑定已发布的库存解析模板')
definition = template.definition_json or {}
mappings = definition.get('mappings') if isinstance(definition.get('mappings'), list) else []
@@ -784,7 +790,7 @@ def prepare_export_match_inputs(task: ShippingNotice) -> dict[str, Any]:
raise ValueError('任务没有可用于导出的冻结订单快照')
from app.services.shipping_notice.effective_order_view import effective_order_field_values
header_values, all_line_values = effective_order_field_values(task)
profile_definition = source.parsing_profile.definition_json if isinstance(source.parsing_profile.definition_json, dict) else {}
profile_definition = profile.definition_json if isinstance(profile.definition_json, dict) else {}
match_keys = profile_definition.get('match_keys') if isinstance(profile_definition.get('match_keys'), list) else []
line_inventory_fields = _inventory_fields_by_mode(mappings, 'line')
header_inventory_fields = _inventory_fields_by_mode(mappings, 'header')
@@ -801,7 +807,7 @@ def prepare_export_match_inputs(task: ShippingNotice) -> dict[str, Any]:
.filter(
ShippingOrderReviewCase.confirmed_task_id == int(source_notice.id),
ShippingOrderReviewLineDecision.is_active.is_(True),
ShippingOrderReviewLineDecision.decision_code == 'release_inventory_shortage',
ShippingOrderReviewLineDecision.decision_code == 'release_onhand_shortage',
)
.all()
)
@@ -861,7 +867,7 @@ def prepare_export_match_inputs(task: ShippingNotice) -> dict[str, Any]:
row['__order_line_index'] = source_index
row['__snapshot_line_id'] = int(line.id)
line_values.append(row)
from app.services.shipping_notice.inventory_file_store import resolve_inventory_lines_for_execution
from app.services.shipping_notice.onhand_file_store import resolve_inventory_lines_for_execution
# [STEP 3] 活动库存 Link 为事实源;先按本单 match_keys(通常 reservation+part)
# 裁成候选子集,再做模板字段匹配——禁止上万行全量快照进入逐行匹配。
@@ -874,7 +880,7 @@ def prepare_export_match_inputs(task: ShippingNotice) -> dict[str, Any]:
}
catalog = {
str(item.id): str(item.id)
for item in ShippingInventoryField.query.filter(ShippingInventoryField.id.in_(match_field_ids)).all()
for item in FreightRpaOnhandField.query.filter(FreightRpaOnhandField.id.in_(match_field_ids)).all()
} if match_field_ids else {}
order_field_ids: list[str] = []
inventory_field_keys: list[str] = []
@@ -958,6 +964,8 @@ def generate_shipment_excel(
assert_notice_has_no_export_artifact(task)
inputs = prepare_export_match_inputs(task)
source = inputs['source']
# s18 起解析方案绑定归 onhand_report_config 配置行;Artifact 审计外键同样取自配置行。
report_config = source.onhand_report_config
template = inputs['template']
snapshot = inputs['snapshot']
mappings = inputs['mappings']
@@ -1090,7 +1098,8 @@ def generate_shipment_excel(
artifact_no=artifact_no, task_id=task.id,
template_id=template.id,
order_schema_version_id=snapshot.schema_version_id, order_extraction_id=snapshot.extraction_id,
inventory_parsing_profile_id=source.parsing_profile_id, inventory_snapshot_id=_notice_inventory_file_id(task),
inventory_parsing_profile_id=report_config.parsing_profile_id if report_config is not None else None,
inventory_snapshot_id=_notice_inventory_file_id(task),
input_checksum=_input_checksum(
task, source, template, snapshot, header_values, line_values, inventory_lines,
notice_id=int(task.id),