- 读 EHR 预支台账 → 按 ERP 付款申请单口径逐行建单,默认试算不写库 - 可选建单后自动提交、按套打样例生成 PDF 直连网络打印,打印完成钉钉提醒领取 - 含幂等查重与异常清单,业务口径与字段顺序按生产单核对 - 依赖 erpsdk / printkit / dingtalk_sdk(plugin.json requires.sdks 声明) - 需求单文档同步:开发/流程/评分/需求文档更新,新增交接文档
164 lines
7.1 KiB
Python
164 lines
7.1 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""XQ0012 EHR 预支及支付报酬 → ERP 付款申请单(webhost 场景插件)。
|
||
|
||
流程:读 EHR 预支台账(xlsx)→ 逐行组装 Save Model →(幂等查重)→ 建单 →
|
||
(可选提交)→ 自绘套打 PDF →(可选)printkit 出纸。
|
||
|
||
默认 **试算模式**(dry_run=True):不写 ERP,只输出将要提交的报文与校验结果;
|
||
业务口径确认后关掉试算、按需打开「提交」「打印」。
|
||
"""
|
||
import os
|
||
from datetime import date
|
||
|
||
from webhost.backend.plugins.base import PLUGINS, ScenePlugin
|
||
|
||
from . import config
|
||
from .data import erp as erp_mod
|
||
from .data import ledger
|
||
from .logic import flow
|
||
|
||
|
||
def _parse_date(s: str):
|
||
s = (s or '').strip()
|
||
if not s:
|
||
return None
|
||
for fmt in ('%Y-%m-%d', '%Y/%m/%d', '%Y%m%d'):
|
||
try:
|
||
return date(*[int(x) for x in _split(s, fmt)])
|
||
except Exception: # noqa: BLE001
|
||
continue
|
||
return None
|
||
|
||
|
||
def _split(s: str, fmt: str):
|
||
import re
|
||
nums = [int(x) for x in re.findall(r'\d+', s)]
|
||
return nums[:3]
|
||
|
||
|
||
class PrepayPayApplyPlugin(ScenePlugin):
|
||
"""EHR 预支 → ERP 付款申请单(需求 XQ0012)。"""
|
||
|
||
id = 'xq0012'
|
||
name = 'EHR预支及支付报酬自动发起ERP付款申请'
|
||
version = '0.1.0'
|
||
description = ('读 EHR 预支台账 → 自动在 ERP 建付款申请单(默认试算不写库),'
|
||
'可选提交、自动套打打印;含幂等查重与异常清单')
|
||
platform = '' # 走 ERP WebAPI 账密登录,不用底座共享登录态
|
||
uses_month = False
|
||
capabilities = ['xq0012']
|
||
permissions = {'platforms': []}
|
||
fields = {
|
||
'ledger_dir': {
|
||
'type': 'string', 'title': '台账目录', 'default': config.LEDGER_DIR_DEFAULT,
|
||
'description': '预支台账 xlsx 所在目录;留空=默认目录,自动取最新一个',
|
||
},
|
||
'ledger_file': {
|
||
'type': 'string', 'title': '台账文件(可选)', 'default': '',
|
||
'description': '指定具体 xlsx 路径;留空=按目录取最新',
|
||
},
|
||
'apply_date': {
|
||
'type': 'string', 'title': '申请日期', 'default': '',
|
||
'description': 'YYYY-MM-DD;留空=今天',
|
||
},
|
||
'pay_purpose': {
|
||
'type': 'string', 'title': '付款用途', 'default': '个人借款',
|
||
'enum': ['个人借款', '工资预提', '工资发放'],
|
||
'description': '⚠️ 业务待确认:人工模板=个人借款,生产真实单=工资预提',
|
||
},
|
||
'apply_dept': {
|
||
'type': 'string', 'title': '申请部门编码', 'default': config.APPLY_DEPT_ID,
|
||
'description': '默认 BM000477 人力资源部(生产真实单口径)',
|
||
},
|
||
'expect_days': {
|
||
'type': 'number', 'title': '期望付款日=申请日+N 天', 'default': config.DEFAULT_EXPECT_DAYS,
|
||
},
|
||
'erp_system': {
|
||
'type': 'string', 'title': 'ERP系统', 'default': '新版', 'enum': ['新版', '旧版'],
|
||
},
|
||
'erp_username': {'type': 'string', 'title': 'ERP登录账号', 'default': ''},
|
||
'erp_password': {'type': 'string', 'title': 'ERP登录密码', 'default': '', 'format': 'password'},
|
||
'dry_run': {
|
||
'type': 'boolean', 'title': '试算模式(不写 ERP)', 'default': True,
|
||
'description': '开启后只组装+查重+输出结果,不建单;验证通过后再关',
|
||
},
|
||
'submit': {
|
||
'type': 'boolean', 'title': '建单后自动提交', 'default': False,
|
||
'description': '关闭时只建草稿(A),由 HR 在 ERP 确认后手动提交',
|
||
},
|
||
'print_enabled': {
|
||
'type': 'boolean', 'title': '建单后套打打印', 'default': False,
|
||
},
|
||
'printer': {
|
||
'type': 'string', 'title': '打印机', 'default': config.PRINTER_DEFAULT,
|
||
'enum': config.PRINTER_OPTIONS,
|
||
},
|
||
'copies': {'type': 'number', 'title': '打印份数', 'default': config.PRINT_COPIES_DEFAULT},
|
||
'dingtalk_enabled': {
|
||
'type': 'boolean', 'title': '打印后钉钉提醒领取',
|
||
'default': config.DINGTALK_ENABLED_DEFAULT,
|
||
'description': '打印成功后推送一条群提醒(单号/收款人/金额/打印机),提醒去领取签字',
|
||
},
|
||
'dingtalk_users': {
|
||
'type': 'string', 'title': '钉钉接收人(姓名/工号)',
|
||
'default': config.DINGTALK_USERS_DEFAULT,
|
||
'description': '多个用「、」「,」「;」或换行分隔;重名必须带工号;留空=不提醒',
|
||
},
|
||
'debug_mode': {'type': 'boolean', 'title': '调试日志', 'default': False},
|
||
}
|
||
|
||
def run(self, fields: dict, ctx) -> dict:
|
||
fields = fields or {}
|
||
debug = bool(fields.get('debug_mode', False))
|
||
|
||
def log(msg: str, level: str = 'INFO') -> None:
|
||
if level == 'DEBUG' and not debug:
|
||
return
|
||
try:
|
||
ctx.log(msg, level)
|
||
except TypeError:
|
||
ctx.log(msg)
|
||
config.set_log(log)
|
||
|
||
work_dir = str(fields.get('ledger_dir') or '').strip() or config.LEDGER_DIR_DEFAULT
|
||
ledger_file = str(fields.get('ledger_file') or '').strip()
|
||
path = ledger_file or ledger.find_latest_ledger(work_dir)
|
||
log(f'[台账] {path}')
|
||
|
||
rows = ledger.read_rows(path)
|
||
log(f'[台账] 有效数据 {len(rows)} 行')
|
||
if not rows:
|
||
return {'summary': '台账无有效数据(检查工号/金额是否为空或含 #N/A)', 'detail': [], 'path': ''}
|
||
|
||
org_map = ledger.load_org_map(path)
|
||
purpose = config.PAY_PURPOSE_OPTIONS.get(
|
||
str(fields.get('pay_purpose') or '个人借款').strip(), config.PAY_PURPOSE_ID)
|
||
dept_code = str(fields.get('apply_dept') or '').strip() or config.APPLY_DEPT_ID
|
||
apply_date = _parse_date(str(fields.get('apply_date') or ''))
|
||
|
||
dry_run = bool(fields.get('dry_run', True))
|
||
client = None
|
||
if not dry_run:
|
||
user = str(fields.get('erp_username') or '').strip()
|
||
pwd = str(fields.get('erp_password') or '')
|
||
if not (user and pwd):
|
||
return {'summary': '未填 ERP 账号密码(试算模式不需要)', 'detail': [], 'path': ''}
|
||
erp = 'old' if str(fields.get('erp_system') or '新版').strip() == '旧版' else 'new'
|
||
client = erp_mod.login(user, pwd, erp=erp)
|
||
|
||
out = flow.run_flow(
|
||
rows_data=rows, org_map=org_map, client=client,
|
||
apply_date=apply_date, purpose_id=purpose, dept_code=dept_code,
|
||
expect_days=int(fields.get('expect_days') or config.DEFAULT_EXPECT_DAYS),
|
||
submit=bool(fields.get('submit', False)),
|
||
print_enabled=bool(fields.get('print_enabled', False)),
|
||
printer=str(fields.get('printer') or config.PRINTER_DEFAULT),
|
||
copies=int(fields.get('copies') or 1),
|
||
dry_run=dry_run,
|
||
)
|
||
log(f'[完成] {out["summary"]}')
|
||
return out
|
||
|
||
|
||
PLUGINS.append(PrepayPayApplyPlugin())
|