150 lines
5.6 KiB
Python
150 lines
5.6 KiB
Python
# coding=utf-8
|
||
# 编译日期:2026-08-12 10:13:31
|
||
# 版权所有:www.i-search.com.cn
|
||
#
|
||
# ============================================================================
|
||
# 合同建单 (CONTRACT_CREATE) RPA 流程 - 全局函数
|
||
# ============================================================================
|
||
# 在 APA Studio 的 Python 脚本节点中调用:
|
||
# 1. 拖一个 "Python 脚本块" 到画布
|
||
# 2. 从 codes/rpa_yisaiqi 导入需要的函数
|
||
# 3. 示例见下方 __main__ 块
|
||
#
|
||
# 流程图节点清单 (15步 + 4异常分支):
|
||
# 参考: codes/rpa_yisaiqi/flows/contract_create_flow.json
|
||
# ============================================================================
|
||
|
||
import json
|
||
import os
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
# 确保 codes 目录在 sys.path 中,以便 APA Python 3.8 能导入 rpa_yisaiqi
|
||
_CODES_DIR = Path(__file__).resolve().parent # .../AutoRpa/codes/
|
||
if str(_CODES_DIR) not in sys.path:
|
||
sys.path.insert(0, str(_CODES_DIR))
|
||
|
||
|
||
def verify_module_imports():
|
||
"""验证所有模块可以正常导入(不依赖 APA ubpa SDK)。"""
|
||
results = {}
|
||
modules = [
|
||
("callback_client", "rpa_yisaiqi.callback_client", "StateCenterClient"),
|
||
("contract_flow_steps", "rpa_yisaiqi.contract_flow_steps", "ContractFlowContext"),
|
||
("contract_draft", "rpa_yisaiqi.contract_draft", "ContractDraft"),
|
||
("pdf_utils", "rpa_yisaiqi.pdf_utils", "ensure_merged_pdf"),
|
||
("contract_create_node", "rpa_yisaiqi.contract_create_node", "ContractCreateInput"),
|
||
]
|
||
for name, module_path, class_name in modules:
|
||
try:
|
||
mod = __import__(module_path, fromlist=[class_name])
|
||
getattr(mod, class_name)
|
||
results[name] = "OK"
|
||
except Exception as e:
|
||
results[name] = f"FAIL: {e}"
|
||
return results
|
||
|
||
|
||
def build_yyc_dry_run_context():
|
||
"""构建 YYC 来源的 dry-run 流程上下文(不提交合同)。"""
|
||
from rpa_yisaiqi.contract_flow_steps import ContractFlowContext, ContractGoodsLine
|
||
|
||
return ContractFlowContext(
|
||
contract_url="http://contract-test.gzmpc.com:8080/",
|
||
username="test002",
|
||
password="1",
|
||
supplier_name="测试供应商",
|
||
order_date="2026-08-12",
|
||
source_type="YYC",
|
||
template_type="NON_FORMAT",
|
||
template_name="非格式--采购协议",
|
||
customer_code="CUST-001",
|
||
customer_name="测试供应商",
|
||
contract_category="采购协议",
|
||
settlement_type="付款",
|
||
contract_background="自动化测试合同起草",
|
||
contract_nature="主合同",
|
||
amount_currency="人民币",
|
||
amount="50000.00",
|
||
contract_end_date="2026-12-31",
|
||
is_major_contract="否",
|
||
is_prepayment="否",
|
||
belong_year="2026",
|
||
payment_method="账期支付",
|
||
payment_days="30",
|
||
contract_status="正式合同",
|
||
contains_blood_products="否",
|
||
submit_mode="dry_run",
|
||
goods_lines=[
|
||
ContractGoodsLine(
|
||
operation_code="ADD", goods_code="YP-001",
|
||
goods_name="测试药品A", approval_no="国药准字H00000001",
|
||
spec="50mg*30片", unit="盒", quantity=10.0,
|
||
),
|
||
],
|
||
merged_pdf_path=None,
|
||
)
|
||
|
||
|
||
def load_draft_from_json(json_path):
|
||
"""从 JSON 文件加载合同起草数据。"""
|
||
from rpa_yisaiqi.contract_draft import load_contract_draft
|
||
return load_contract_draft(json_path)
|
||
|
||
|
||
def run_contract_flow_with_apa_browser(browser, ctx):
|
||
"""
|
||
在 APA Studio 中使用真实的 ubpa 浏览器适配器执行流程。
|
||
|
||
browser: 实现了 BrowserAdapter 协议的对象 (如 ubpa 的 iedge/ichrome_firefox)
|
||
ctx: ContractFlowContext 实例
|
||
|
||
返回合同编号字符串(dry_run 模式返回空字符串)。
|
||
"""
|
||
from rpa_yisaiqi.contract_flow_steps import run_contract_create_flow
|
||
return run_contract_create_flow(browser, ctx)
|
||
|
||
|
||
# ============================================================================
|
||
# APA Studio 集成说明:
|
||
#
|
||
# 方式一: Python 脚本块(推荐)
|
||
# 在 APA Studio 画布拖一个 "Python 脚本块",写入:
|
||
#
|
||
# import sys
|
||
# sys.path.insert(0, r"d:\RPA\AutoRpa\codes")
|
||
# from GlobalFun import build_yyc_dry_run_context, run_contract_flow_with_apa_browser
|
||
# from rpa_yisaiqi.contract_flow_steps import BrowserAdapter
|
||
#
|
||
# ctx = build_yyc_dry_run_context()
|
||
# # browser 由 APA Studio 提供 (如 ubpa 网页对象)
|
||
# contract_no = run_contract_flow_with_apa_browser(browser, ctx)
|
||
#
|
||
# 方式二: 原生活动拖拽 (15个节点)
|
||
# 按 codes/rpa_yisaiqi/flows/contract_create_flow.json 的顺序,
|
||
# 在画布上依次拖拽: 打开网页 → 输入文本 → 鼠标点击 → ...
|
||
# 每个节点对应 contract_flow_steps.py 中的一个函数。
|
||
# ============================================================================
|
||
|
||
|
||
# 测试运行或调试以上全局函数入口
|
||
if __name__ == '__main__':
|
||
print("GlobalFun 模块导入验证:")
|
||
results = verify_module_imports()
|
||
for name, status in results.items():
|
||
print(f" [{status}] {name}")
|
||
|
||
print("\n构建 YYC dry-run 上下文:")
|
||
ctx = build_yyc_dry_run_context()
|
||
print(f" supplier_name={ctx.supplier_name}")
|
||
print(f" source_type={ctx.source_type}")
|
||
print(f" template_name={ctx.template_name}")
|
||
print(f" submit_mode={ctx.submit_mode}")
|
||
print(f" goods_lines={len(ctx.goods_lines)} 条")
|
||
|
||
print("\n加载 YYC JSON 示例:")
|
||
draft = load_draft_from_json(
|
||
r"d:\RPA\AutoRpa\codes\rpa_yisaiqi\examples\contract_draft_yyc.json"
|
||
)
|
||
print(f" supplier={draft.supplier_name}, source={draft.source_type}")
|