feat(xq0012): 更新台账取数与付款申请流程(v0.3.17)
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""XQ0012 台账维护:撤掉"对账误判已付"的回填(把行清回未付,让它重跑能建单)。
|
||||
|
||||
背景:2026-11-26 对账只比「工号+金额」,把 10 月已付的同额旧单命中 11 月台账行,
|
||||
把 3 行写成了「付款状态=已付 + 备注 ERP:<旧单号>」。插件侧已加日期下限修复,
|
||||
但**已经写进台账的标记要手工撤**,否则重跑时这些行被 pending_only 过滤掉。
|
||||
|
||||
用法(默认只打印,不写文件)::
|
||||
|
||||
.venv\\Scripts\\python.exe tools\\xq0012_revert_paid.py # 预演
|
||||
.venv\\Scripts\\python.exe tools\\xq0012_revert_paid.py --apply # 真写(先备份)
|
||||
|
||||
安全:只改「工号 + 姓名 + 金额 + 提报日期 + 备注 == ERP:<单号>」全对得上的行;
|
||||
写之前自动备份成 ``<原名>_bak_before_revert_<时间戳>.xlsx``。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
import openpyxl
|
||||
|
||||
# ==================== 配置(人工核对后改这里) ====================
|
||||
#: 现用的台账工作簿(运行日志里的那份;如已复制新版,改成实际文件)
|
||||
WORKBOOK = r'D:\资料\人资\EHR预支及支付报酬自动发起ERP付款申请\1126工资预支付款申请_2.xlsx'
|
||||
SHEET = '预支报酬台账'
|
||||
COL_STAFF, COL_NAME, COL_AMOUNT, COL_DATE = '工号', '姓名', '预支金额', '提报日期'
|
||||
COL_STATUS, COL_REMARK = '付款状态', '备注'
|
||||
|
||||
#: 要撤销的误标行:备注里的 ERP 单号必须是**上个月**那笔(人工核对过)
|
||||
MARKS = [
|
||||
{'staff': '37029300', 'name': '陈静', 'amount': 1000, 'date': '2026-11-25',
|
||||
'bill_no': 'FKSQ20261001329'},
|
||||
{'staff': '37000358', 'name': '刘世川', 'amount': 2000, 'date': '2026-11-24',
|
||||
'bill_no': 'FKSQ20261000990'},
|
||||
{'staff': '37016383', 'name': '唐燕', 'amount': 1000, 'date': '2026-11-25',
|
||||
'bill_no': 'FKSQ20261001334'},
|
||||
]
|
||||
|
||||
|
||||
def _text(v) -> str:
|
||||
if v is None:
|
||||
return ''
|
||||
if isinstance(v, datetime):
|
||||
return v.strftime('%Y-%m-%d')
|
||||
return str(v).strip()
|
||||
|
||||
|
||||
def _same_date(cell, want: str) -> bool:
|
||||
return _text(cell) == want
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
argv = list(sys.argv[1:] if argv is None else argv)
|
||||
apply = '--apply' in argv
|
||||
|
||||
wb = openpyxl.load_workbook(WORKBOOK, data_only=False)
|
||||
if SHEET not in wb.sheetnames:
|
||||
print(f'工作簿缺 sheet「{SHEET}」:{WORKBOOK}')
|
||||
return 2
|
||||
ws = wb[SHEET]
|
||||
header = {}
|
||||
for idx, cell in enumerate(next(ws.iter_rows(min_row=2, max_row=2, values_only=True)), 1):
|
||||
name = _text(cell)
|
||||
if name:
|
||||
header[name] = idx
|
||||
|
||||
todo = []
|
||||
for mark in MARKS:
|
||||
hit = None
|
||||
for r in range(3, ws.max_row + 1):
|
||||
if _text(ws.cell(r, header[COL_STAFF]).value) != mark['staff']:
|
||||
continue
|
||||
if _text(ws.cell(r, header[COL_NAME]).value) != mark['name']:
|
||||
continue
|
||||
if abs(float(ws.cell(r, header[COL_AMOUNT]).value or 0)
|
||||
- float(mark['amount'])) > 0.01:
|
||||
continue
|
||||
if not _same_date(ws.cell(r, header[COL_DATE]).value, mark['date']):
|
||||
continue
|
||||
if _text(ws.cell(r, header[COL_REMARK]).value) != f"ERP:{mark['bill_no']}":
|
||||
continue
|
||||
hit = r
|
||||
break
|
||||
if hit is None:
|
||||
print(f"[跳过] {mark['name']}/{mark['staff']} {mark['amount']} 元 "
|
||||
f"{mark['date']}:找不到备注为 ERP:{mark['bill_no']} 的行(可能已撤/已改)")
|
||||
continue
|
||||
cur = _text(ws.cell(hit, header[COL_STATUS]).value)
|
||||
if cur not in ('已付', '已付款'):
|
||||
print(f"[跳过] 第 {hit} 行 {mark['name']}:当前状态「{cur}」不是已付")
|
||||
continue
|
||||
todo.append((hit, mark))
|
||||
print(f"[待撤] 第 {hit} 行 {mark['name']}/{mark['staff']} {mark['amount']} 元 "
|
||||
f"{mark['date']}:已付 → 空,备注 ERP:{mark['bill_no']} → 空")
|
||||
|
||||
if not todo:
|
||||
print('没有需要撤销的行。')
|
||||
wb.close()
|
||||
return 0
|
||||
if not apply:
|
||||
print(f'\n预演结束:{len(todo)} 行待撤;确认无误后加 --apply 真写(会先备份)。')
|
||||
wb.close()
|
||||
return 0
|
||||
|
||||
backup = f"{WORKBOOK.rsplit('.', 1)[0]}_bak_before_revert_" \
|
||||
f"{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
|
||||
shutil.copy2(WORKBOOK, backup)
|
||||
print(f'已备份:{backup}')
|
||||
|
||||
for row, _mark in todo:
|
||||
ws.cell(row, header[COL_STATUS]).value = ''
|
||||
ws.cell(row, header[COL_REMARK]).value = ''
|
||||
wb.save(WORKBOOK)
|
||||
wb.close()
|
||||
print(f'已撤销 {len(todo)} 行的「已付」标记:{WORKBOOK}')
|
||||
print('⚠️ openpyxl 保存后工作簿公式缓存会失效,请先用 Excel/WPS 打开重算保存一次,'
|
||||
'或直接重跑插件(插件会自动重算)。')
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user