- 读 EHR 预支台账 → 按 ERP 付款申请单口径逐行建单,默认试算不写库 - 可选建单后自动提交、按套打样例生成 PDF 直连网络打印,打印完成钉钉提醒领取 - 含幂等查重与异常清单,业务口径与字段顺序按生产单核对 - 依赖 erpsdk / printkit / dingtalk_sdk(plugin.json requires.sdks 声明) - 需求单文档同步:开发/流程/评分/需求文档更新,新增交接文档
118 lines
4.2 KiB
Python
118 lines
4.2 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""台账数据层:读 EHR 导出的「预支台账」工作簿。
|
||
|
||
口径(2026-11-14 实测于 1030工资预支付款申请--已付.xlsx):
|
||
- sheet「预支报酬台账」:第 2 行是表头,第 3 行起是数据,后面大量空行/公式残留;
|
||
- 有效行:工号非空、预支金额 > 0、整行无 #N/A 类错误值;
|
||
- 同工作簿的「辅助2-ERP组织机构」sheet(FNumber/FName)用来做 组织名称 → 编码 映射。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import re
|
||
from datetime import datetime
|
||
|
||
import openpyxl
|
||
|
||
from .. import config
|
||
|
||
ERR_RE = re.compile(r'^#(N/A|REF!|VALUE!|DIV/0!|NAME\?|NULL!|NUM!|GETTING_DATA)$', re.I)
|
||
|
||
|
||
def _s(v) -> str:
|
||
if v is None:
|
||
return ''
|
||
if isinstance(v, datetime):
|
||
return v.strftime('%Y-%m-%d')
|
||
s = str(v).strip()
|
||
return '' if ERR_RE.match(s) else s
|
||
|
||
|
||
def _num(v):
|
||
try:
|
||
return float(str(v).replace(',', ''))
|
||
except (TypeError, ValueError):
|
||
return 0.0
|
||
|
||
|
||
def find_latest_ledger(work_dir: str = '') -> str:
|
||
"""取目录下最新的预支台账 xlsx(文件名含关键词,忽略临时文件)。"""
|
||
work_dir = work_dir or config.LEDGER_DIR_DEFAULT
|
||
cands = []
|
||
for name in os.listdir(work_dir):
|
||
if not name.lower().endswith(('.xlsx', '.xlsm')) or name.startswith('~$'):
|
||
continue
|
||
if any(k in name for k in config.LEDGER_KEYWORDS):
|
||
cands.append(os.path.join(work_dir, name))
|
||
if not cands:
|
||
raise FileNotFoundError(f'{work_dir} 下没有找到预支台账 xlsx(关键词:{config.LEDGER_KEYWORDS})')
|
||
cands.sort(key=lambda p: os.path.getmtime(p), reverse=True)
|
||
return cands[0]
|
||
|
||
|
||
def load_org_map(path: str) -> dict:
|
||
"""组织名称 → 编码(辅助2-ERP组织机构:FNumber / FName#2052)。"""
|
||
wb = openpyxl.load_workbook(path, data_only=True, read_only=True)
|
||
try:
|
||
if config.ORG_SHEET not in wb.sheetnames:
|
||
return {}
|
||
ws = wb[config.ORG_SHEET]
|
||
out = {}
|
||
for row in ws.iter_rows(min_row=3, values_only=True):
|
||
code, name = _s(row[0] if row else ''), _s(row[1] if len(row) > 1 else '')
|
||
if code and name:
|
||
out[name] = code
|
||
return out
|
||
finally:
|
||
wb.close()
|
||
|
||
|
||
def read_rows(path: str) -> list:
|
||
"""读台账有效数据行,返回行字典列表(键见 config.COL_*)。"""
|
||
wb = openpyxl.load_workbook(path, data_only=True)
|
||
if config.LEDGER_SHEET not in wb.sheetnames:
|
||
raise ValueError(f'{path} 缺少 sheet「{config.LEDGER_SHEET}」')
|
||
ws = wb[config.LEDGER_SHEET]
|
||
header = {}
|
||
for idx, cell in enumerate(next(ws.iter_rows(min_row=2, max_row=2, values_only=True)), 1):
|
||
name = _s(cell)
|
||
if name:
|
||
header[name] = idx - 1
|
||
rows = []
|
||
for raw in ws.iter_rows(min_row=3, values_only=True):
|
||
if not raw:
|
||
continue
|
||
def get(col):
|
||
i = header.get(col)
|
||
return raw[i] if i is not None and i < len(raw) else None
|
||
staff, amount = _s(get(config.COL_STAFF)), _num(get(config.COL_AMOUNT))
|
||
if not staff or amount <= 0:
|
||
continue
|
||
if any(ERR_RE.match(_s(v)) for v in raw):
|
||
continue
|
||
rows.append({
|
||
'seq': _s(get(config.COL_SEQ)),
|
||
'month': _s(get(config.COL_MONTH)),
|
||
'staff': staff,
|
||
'name': _s(get(config.COL_NAME)),
|
||
'date': _s(get(config.COL_DATE)),
|
||
'amount': amount,
|
||
'reason': _s(get(config.COL_REASON)),
|
||
'org_path': _s(get(config.COL_ORG_PATH)),
|
||
'dept': _s(get(config.COL_DEPT_PATH)),
|
||
'pay_status': _s(get(config.COL_PAY_STATUS)),
|
||
})
|
||
wb.close()
|
||
return rows
|
||
|
||
|
||
def guess_org_code(row: dict, org_map: dict) -> str:
|
||
"""从「预支组织全路径」推断付款组织编码(会馆级优先,其次公司级)。"""
|
||
path = str(row.get('org_path') or '')
|
||
parts = [p for p in re.split(r'[-/→>]', path) if p]
|
||
for p in reversed(parts): # 从末级向上找:会馆名优先
|
||
p = p.strip()
|
||
if p and p in org_map:
|
||
return org_map[p]
|
||
return config.APPLY_ORG_ID
|