"""Crane 收料报表导出适配器。""" from __future__ import annotations from typing import Any, Callable from urllib.parse import urlsplit, urlunsplit import crane_report_export import crane_session def _inbound_details_entry_url(login_url: str) -> str: """将复用的 Crane Profile 登录地址固定到收料 Inbound Details 路由。""" parsed = urlsplit(str(login_url or '').strip()) if not parsed.scheme or not parsed.netloc: raise RuntimeError('Crane receipt unit requires an absolute login_url') return urlunsplit((parsed.scheme, parsed.netloc, parsed.path or '/', parsed.query, '/inbound-details-query')) def export_receipt_report( page: Any, unit: dict[str, Any], *, env: Callable[[str, str], str], browser_timeout_ms: int, select_or_fill: Callable[..., None], click_text: Callable[..., None], first_in_profile: bool, ) -> Any: """创建并下载唯一 Description 对应的 Crane Inbound Details 文件,禁止按最新历史文件选择。""" report_path = dict(unit.get('report_path_json') or {}) if str(report_path.get('report_name') or '').strip() != 'Inbound Details': raise RuntimeError('Crane receipt report must be Inbound Details') entry_url = _inbound_details_entry_url(str(unit.get('login_url') or '').strip()) if first_in_profile: page.goto(entry_url, wait_until='domcontentloaded', timeout=browser_timeout_ms) crane_session.maybe_auto_login_crane(page, env=env, browser_timeout_ms=browser_timeout_ms) crane_session.wait_for_crane_app_ready(page, entry_url, env=env, browser_timeout_ms=browser_timeout_ms) # 旧 Crane driver 的 Scheduled Reports 匹配名来自 source;收料固定为 # Inbound Details,不能沿用库存的 OnHandDetails.xlsx 默认值。 source = { 'entry_url': entry_url, 'scheduled_report_name': 'Inbound Details', } return crane_report_export.export_crane_inventory_report( page, source, trigger={'callback_id': unit.get('callback_id')}, env=env, browser_timeout_ms=browser_timeout_ms, select_or_fill=select_or_fill, click_text=click_text, )