- 明细出现模板 AH~AZ 没有的支付方式时,日志/核对报「券名 + 金额 + 笔数 + 日期」, 并列出「本月 0 笔、可回收的券列」(明细列 ↔ 汇总列对位) - 新增配置项 ledger_auto_slot(默认关):开启后把「本月 0 笔」的券列改名 (明细 AP~AZ 第 2 行 + 汇总 W~AG 第 3 行同步,位置不动、汇总公式不用改), 改名前校验汇总公式确实同列对位,对不上跳过并告警 - _selftest 新增空槽回收纯逻辑用例(列字母换算/未匹配识别/空槽判定/槽位不足/公式对位) - 同步 README 与 ui 产物
138 lines
5.9 KiB
Python
138 lines
5.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""台账文件定位:面板只能选「文件夹」(壳子只提供 `/api/dialog/directory`,没有选文件接口),
|
||
所以界面上填的是**台账所在文件夹**,具体用哪份 Excel 由本模块在运行时自动挑。
|
||
|
||
匹配规则(按优先级):
|
||
1. 填的是完整文件路径 → 直接用(兼容旧配置 / 直接粘贴路径);
|
||
2. 填的是文件夹 → 只认 `.xlsx` / `.xlsm`,跳过临时文件(`~$`)、插件备份(`_备份_`)、
|
||
同目录的导出表(`会员卡余额_*` / `会员卡明细_*`);
|
||
3. 优先「名字同时带 会员卡/储值卡 和 统计」的;其中又优先名字带**目标年月**
|
||
(`2026年11月` / `2026-11` / `202611` / `2026.11`)的那一份;
|
||
4. 目标月份的表不存在时(每月换新表、新表还没建或名字写法不同),按文件名里的年月
|
||
取**最新月份**那一份(切月后 10 月旧表还在也不影响);
|
||
5. 只剩一份候选 → 直接用它;同一个月多份、或全都没年月又分不清 → **报错,不猜**:
|
||
猜错会按「自动切月」清掉另一份台账的数据,代价太大,让人先在目录里清理/改名。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import re
|
||
from datetime import date, datetime
|
||
|
||
#: 台账扩展名(模板是 .xlsx;.xlsm 带宏也认)
|
||
LEDGER_EXTS = (".xlsx", ".xlsm")
|
||
#: 台账名字提示词(必须同时出现「提示词之一 + 统计」才算强匹配)
|
||
NAME_HINTS = ("会员卡", "储值卡")
|
||
#: 插件备份名片段:`<台账名>_备份_YYYYMMDD_HHMMSS.xlsx`
|
||
SKIP_HINTS = ("备份",)
|
||
#: 与台账同目录的导出表前缀(`会员卡余额_20261108.xlsx` / `会员卡明细_20261108.xlsx`)
|
||
EXPORT_PREFIXES = ("会员卡余额", "会员卡明细")
|
||
#: 文件名里的年月:2026年11月 / 2026-11 / 2026.11 / 202611
|
||
_MONTH_RE = re.compile(r"(20\d{2})\s*[年.\-_/]?\s*(\d{1,2})(?:\s*月)?")
|
||
|
||
|
||
class LedgerPathError(RuntimeError):
|
||
"""台账文件夹里有多个 Excel,分不清该维护哪一份。"""
|
||
|
||
|
||
def _month_keys(day) -> tuple:
|
||
"""目标营业日的年月写法(台账文件名里常见三种)。"""
|
||
d = day if isinstance(day, date) else datetime.now().date()
|
||
return ("%d年%d月" % (d.year, d.month),
|
||
"%d-%02d" % (d.year, d.month),
|
||
"%d%02d" % (d.year, d.month))
|
||
|
||
|
||
def _ym_of(name) -> tuple:
|
||
"""从文件名里取 (年, 月);取不到返回 None(用于「按最新月份换表」)。"""
|
||
m = _MONTH_RE.search(name)
|
||
if not m:
|
||
return None
|
||
year, month = int(m.group(1)), int(m.group(2))
|
||
return (year, month) if 1 <= month <= 12 else None
|
||
|
||
|
||
def _ambiguous(pool: list, folder: str, hint: str) -> "LedgerPathError":
|
||
"""分不清时统一的报错(宁可报错也不猜)。"""
|
||
return LedgerPathError(
|
||
"台账文件夹里有 %d 份 Excel,%s:%s\n%s\n"
|
||
"请把当月那一份单独放一个文件夹(名字里带年月,如「2026年11月」,最稳),"
|
||
"或在面板里直接填完整文件路径。"
|
||
% (len(pool), hint, folder, "\n".join(" · " + n for n in pool[:10])))
|
||
|
||
|
||
def _candidates(folder: str) -> list:
|
||
"""目录里可能是台账的 Excel(已排除备份 / 临时 / 导出表)。"""
|
||
out = []
|
||
try:
|
||
names = os.listdir(folder)
|
||
except OSError:
|
||
return out
|
||
for name in names:
|
||
if not name.lower().endswith(LEDGER_EXTS):
|
||
continue
|
||
if name.startswith("~$") or name.startswith("."):
|
||
continue
|
||
if any(h in name for h in SKIP_HINTS):
|
||
continue
|
||
if name.startswith(EXPORT_PREFIXES):
|
||
continue
|
||
if os.path.isfile(os.path.join(folder, name)):
|
||
out.append(name)
|
||
return sorted(out)
|
||
|
||
|
||
def resolve(raw, day=None, log=None) -> str:
|
||
"""把面板填的「台账文件夹 / 完整文件路径」解析成实际要维护的 Excel 路径。
|
||
|
||
返回 "" 表示不维护台账(未填 / 目录不存在 / 目录里没有 Excel),调用方跳过即可;
|
||
填了目录但分不清用哪份时抛 `LedgerPathError`(宁可报错也不猜)。
|
||
"""
|
||
log = log or (lambda *a, **k: None)
|
||
text = str(raw or "").strip().strip('"')
|
||
if not text:
|
||
return ""
|
||
|
||
if os.path.isfile(text):
|
||
return text
|
||
if not os.path.isdir(text):
|
||
log("台账文件夹不存在,跳过台账维护:%s" % text, "WARN")
|
||
return ""
|
||
|
||
names = _candidates(text)
|
||
if not names:
|
||
log("台账文件夹里没有 Excel(.xlsx/.xlsm),跳过台账维护:%s" % text, "WARN")
|
||
return ""
|
||
|
||
hinted = [n for n in names if "统计" in n and any(h in n for h in NAME_HINTS)]
|
||
if not hinted:
|
||
hinted = [n for n in names if any(h in n for h in NAME_HINTS)]
|
||
pool = hinted or names
|
||
|
||
keys = _month_keys(day)
|
||
matched = [n for n in pool if any(k in n for k in keys)]
|
||
if len(matched) == 1:
|
||
chosen = matched[0]
|
||
elif len(matched) > 1:
|
||
raise _ambiguous(matched, text, "名字都带「%s」" % keys[0])
|
||
elif len(pool) == 1:
|
||
chosen = pool[0]
|
||
else:
|
||
# 目标月份的表不在(每月换表/新表还没建):按文件名年月取最新月份那份
|
||
dated = [(n, _ym_of(n)) for n in pool]
|
||
dated = [(n, ym) for n, ym in dated if ym]
|
||
if dated:
|
||
newest = max(ym for _, ym in dated)
|
||
top = [n for n, ym in dated if ym == newest]
|
||
if len(top) == 1:
|
||
chosen = top[0]
|
||
log("台账文件夹里没有 %s,按最新月份取:%s(%d年%d月)"
|
||
% (keys[0], chosen, newest[0], newest[1]))
|
||
else:
|
||
raise _ambiguous(top, text, "都属于最新月份 %d年%d月" % newest)
|
||
else:
|
||
raise _ambiguous(pool, text, "都分不清月份")
|
||
|
||
log("台账自动匹配:%s(目录 %s,候选 %d 份)" % (chosen, text, len(pool)))
|
||
return os.path.join(text, chosen)
|