Files
37duAutomation/plugins/XQ0001-0100/XQ0013 社保费用申报导出并汇总/__init__.py
T
puddy a2774aa3a1 feat(xq0013): 社保申报全流程迁移完成(v0.3.0)
日常申报/特殊申报导出、申报记录 PDF 导出,合并职工未申报明细并汇总上下半月金额回写账户表副本;改为 Win32 消息驱动 + 图像/OCR 登录,移除旧 selectors/client 模块。
2026-11-26 09:28:32 +08:00

220 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""XQ0013 社保费用申报导出并汇总 插件入口(已迁移全流程)。
需求:重庆社保费管理客户端(EPPortal/EPEvenue_SB.exe)按账户列表批量
登录 → 日常申报导出 → 特殊申报导出 → 申报记录导出 PDF
→ 职工未申报明细合并 → PDF 上下半月金额汇总回写账户表副本。
重庆只有「日常申报 + 特殊申报」两步,**没有缴费工资申报**(那是成都/长沙的业务)。
执行模式:
check 只对第一家跑 启动 → add_unit(幂等)→ 登录 → settle → 关闭,验证环境
full 逐户完整跑,最后合并明细 + 汇总金额
前提:执行机必须**以管理员身份运行**(客户端是 High 完整性进程,普通权限发消息被 UIPI 拒绝)。
本插件不做 runas 自提权重启(会脱离执行机),非管理员直接报错。
"""
import time
import traceback
from webhost.backend.plugins.base import ScenePlugin, PLUGINS
from . import config
from .data import accounts
from .logic import batch, login, sbui
from .report import merge_detail, summarize_declare
def _as_bool(v):
if isinstance(v, str):
return v.strip().lower() in ("1", "true", "yes", "on", "是")
return bool(v)
class SocialDeclarePlugin(ScenePlugin):
"""社保费用申报导出并汇总(需求 XQ0013)。"""
id = "xq0013"
name = "社保费用申报导出并汇总"
version = "0.3.0"
description = ("按月批量登录重庆社保客户端,完成日常申报/特殊申报导出、"
"申报记录导出 PDF,并合并职工未申报明细、汇总上下半月金额。")
platform = "" # 客户端本地登录,不依赖底座平台登录态
uses_month = False
capabilities = ["xq0013"]
permissions = {"platforms": []}
fields = {
"mode": {
"type": "string", "title": "执行模式", "default": "check",
"enum": ["check", "full"],
"description": "check=只验证环境(启动→登录第一家→清弹窗→关闭);"
"full=逐户完整申报导出 + 合并明细 + 金额汇总",
},
"submit": {
"type": "boolean", "title": "真实提交申报", "default": False,
"description": "默认只导出不提交(演练);开启后日常/特殊申报会真正向税局提交,不可撤销",
},
"client_exe": {
"type": "string", "title": "社保客户端程序",
"default": r"D:\Program Files (x86)\EPPortal_SB1.0\EPEvenue_SB.exe",
"description": "EPEvenue_SB.exe 的完整路径(其所在目录即客户端安装目录)",
},
"save_dir": {
"type": "string", "title": "输出目录",
"default": r"D:\资料\人资\社保费用申报导出汇总",
"description": "账户列表、申报结果、职工未申报明细、汇总结果、截图等统一根目录",
},
"account_file": {
"type": "string", "title": "账户列表文件", "default": "",
"description": "留空 = 输出目录/账户列表.xlsx(按表头名读:门店/税号/密码/执行状态/日常申报状态/特殊申报状态/申报记录)",
},
}
# ---------------------------------------------------------------- 入口
def run(self, fields: dict, ctx):
config.set_log(ctx.log)
config.configure(
client_exe=fields.get("client_exe") or "",
save_dir=fields.get("save_dir") or "",
account_file=fields.get("account_file") or "",
)
mode = (fields.get("mode") or "check").strip().lower()
if mode not in ("check", "full"):
raise RuntimeError("未知执行模式:%r(只支持 check / full)" % mode)
submit = _as_bool(fields.get("submit"))
dirs = config.ensure_dirs()
ctx.progress(2)
ctx.log("===== 社保费用申报(%s,提交=%s):输出目录 %s ====="
% (mode, submit, dirs["base"]), "INFO")
# 环境前置:管理员 / 安全警告看门狗 / 不休眠 / 未锁屏
sbui.require_admin()
sbui.start_security_watchdog()
sbui.keep_awake(True)
try:
sbui.assert_unlocked()
ctx.progress(6)
acct_file = config.account_file()
sheet = accounts.AccountSheet(acct_file)
todo = sheet.accounts(redo=False)
if not todo:
raise RuntimeError("账户列表没有待执行门店(执行状态≠1):%s" % acct_file)
ctx.log("待执行 %d 家门店,账户列表:%s" % (len(todo), acct_file), "INFO")
if mode == "check":
return self._run_check(todo[0], acct_file, dirs, ctx)
return self._run_full(todo, sheet, acct_file, submit, dirs, ctx)
finally:
sbui.keep_awake(False)
# ---------------------------------------------------------------- check
def _run_check(self, acct, acct_file, dirs, ctx):
store = acct["store"]
ctx.log("验证门店:%s(税号 %s)" % (store, acct["tax_no"]), "INFO")
detail = {"模式": "check", "账户列表": acct_file, "验证门店": store}
try:
login.restart_client()
ctx.progress(35)
try:
login.add_unit(acct["tax_no"], acct["password"])
detail["添加单位"] = "已完成/已存在"
except Exception as e: # noqa: BLE001 单位通常在列表里,添加失败不阻断
ctx.log("添加单位没走通(继续尝试直接登录):%r" % e, "WARN")
detail["添加单位"] = "告警:%r" % (e,)
r = login.login(acct["tax_no"], acct["password"])
ctx.progress(75)
login.settle()
detail["登录"] = r.get("result")
detail["模板数"] = len(config.manifest())
summary = "环境验证通过:门店「%s」登录成功(结果=%s)" % (store, r.get("result"))
finally:
login.kill_client()
ctx.progress(100)
return {"summary": summary, "detail": detail, "path": dirs["base"]}
# ---------------------------------------------------------------- full
def _run_full(self, todo, sheet, acct_file, submit, dirs, ctx):
total = len(todo)
per = []
t_start = time.time()
for i, acc in enumerate(todo, 1):
ctx.progress(int(8 + 80 * (i - 1) / max(1, total)))
ctx.log("===== [%d/%d] %s =====" % (i, total, acc["store"]), "INFO")
t0 = time.time()
try:
res = batch.run_one(acc, submit=submit)
ok, daily_s, special_s, record_s = batch.mark(sheet, acc, res)
per.append({"门店": acc["store"], "税号": acc["tax_no"],
"完成": ok, "日常申报": daily_s,
"特殊申报": special_s, "申报记录": record_s,
"耗时秒": round(time.time() - t0, 1)})
ctx.log("%s 完成(日常=%s 特殊=%s 记录=%s),%.1f 秒"
% (acc["store"], daily_s, special_s, record_s, time.time() - t0), "INFO")
except Exception as e: # noqa: BLE001 单户失败不中断整批
ctx.log("%s 失败:%r" % (acc["store"], e), "ERROR")
for line in traceback.format_exc().splitlines():
ctx.log(" " + line, "ERROR")
try:
sheet.set(acc["row"], config.COL_DAILY, "失败")
sheet.set(acc["row"], config.COL_SPECIAL, "失败")
sheet.set(acc["row"], config.COL_RECORD, "失败")
sheet.save()
except Exception as e2: # noqa: BLE001
ctx.log("回写失败状态失败:%r" % e2, "WARN")
per.append({"门店": acc["store"], "税号": acc["tax_no"],
"完成": False, "错误": repr(e)[:300],
"耗时秒": round(time.time() - t0, 1)})
finally:
login.kill_client()
ctx.progress(90)
# 合并职工未申报明细(失败不影响已完成的申报)
ctx.log("合并职工未申报明细 …", "INFO")
try:
files, rows = merge_detail.merge_excel(config.dirs()["detail"], config.merged_file())
merge_info = {"文件数": files, "行数": rows, "输出": config.merged_file()}
ctx.log("合并完成:%d 个文件,%d 行 → %s" % (files, rows, config.merged_file()), "INFO")
except Exception as e: # noqa: BLE001
merge_info = {"错误": repr(e)[:300]}
ctx.log("合并失败(不影响已完成的申报):%r" % e, "WARN")
# 上下半月金额汇总 → 账户表副本
ctx.log("汇总上下半月金额 → 账户表副本 …", "INFO")
try:
code, payload = summarize_declare.run_summary(
root=config.dirs()["declare"],
xlsx=acct_file,
out_dir=config.summary_dir(),
region=None,
)
summary_info = {"code": code,
"门店数": len(payload.get("stores") or {}),
"写入户数": len(payload.get("written") or []),
"失败PDF": len(payload.get("failed") or []),
"文件": payload.get("file")}
ctx.log("金额汇总:code=%s 门店=%d 写入=%d 文件=%s"
% (code, summary_info["门店数"], summary_info["写入户数"],
summary_info["文件"]), "INFO")
except Exception as e: # noqa: BLE001
summary_info = {"错误": repr(e)[:300]}
ctx.log("金额汇总失败(不影响已完成的申报):%r" % e, "WARN")
ctx.progress(100)
done = sum(1 for p in per if p.get("完成"))
if summary_info.get("code") == 2 and not summary_info.get("门店数"):
summary_tail = "无当月申报"
else:
summary_tail = summary_info.get("文件") or "失败"
summary = ("批量完成:%d/%d 户成功,用时 %.0f 秒;合并明细 %s 行;金额汇总 %s"
% (done, total, time.time() - t_start,
merge_info.get("行数", "失败"), summary_tail))
detail = {"模式": "full", "提交": submit, "账户列表": acct_file,
"待执行户数": total, "每户结果": per,
"合并明细": merge_info, "金额汇总": summary_info}
return {"summary": summary, "detail": detail, "path": dirs["base"]}
PLUGINS.append(SocialDeclarePlugin())