unis-oa/backend/scripts/check_form_change_noise.py

113 lines
5.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""审批人没动金额,留痕却写着"他改了四条金额":数字型字段要按数值比。
PYTHONPATH=. python3 scripts/check_form_change_noise.py
生产现场(费用报销单 FYBX202611090003,2026-11-09 17:06):部门主管点「同意」,
审批记录里跟出一张黄色的「改了表单」,四条全是 ``0.0 → 0``——审定其他费用、审定差旅
费用、审定通讯费用、审定市内交通费。而这四格在表单里是 ``money`` 且 ``readonly: True``,
他在这一环根本改不动;库里现在存的还是 ``0.0``,等于记了一次压根没落地的改动。
另一张 FYBX202611040001 的补充确认信息里那条 ``差旅费用 4075.0 → 4075`` 是同一条。
判据出在 ``engine.form_change_notes``:它拿 ``json.dumps`` 比改前改后,而 ``0.0`` 与 ``0``
字面不同。金额控件回传时尾零会掉(``0.0`` 送成 ``0``,``4075.0`` 送成 ``4075``),
于是每一单只要有人把整份表单原样点一遍同意,就凭空多出几条"人工改动"——审计记录
说这一环被人改过金额,改的人却连那一格都摸不到,事后追责追到谁头上都说不清。
修法只收紧一处:**数字型字段(number / money / rate / percent / year / formula)按数值比**,
其余字段照旧按字面比。四条分寸写在这里:
1. 只吞"同一个数的两种写法":``0.0``/``0``/``"0"``/``"1,000.00"`` 算没动;
2. 钱真变了照记:``0.0 → 0.01``、``4075 → 4075.5``、从(空)到有值,一条都不许漏;
3. 认不出是数就不吞:``None``、布尔、列表、``"abc"``、``NaN`` 一律按改动处理,
文本型字段(``text`` 那类)也不参与数值比较——``"7"`` 与 ``"7.0"`` 对文本框就是两串字;
4. 下划线开头的系统留痕照旧不进这里(那是原有的一条,本脚本一并钉住别改坏)。
这份判据是纯函数,不连库、不写数据,任何档位都能跑。
"""
from __future__ import annotations
import sys
from app.engine import engine
FAILS: list[str] = []
PASSED = 0
FIELDS = [
{"key": "checkOther", "label": "审定其他费用", "type": "money"},
{"key": "travel", "label": "差旅费用", "type": "money"},
{"key": "qty", "label": "数量", "type": "number"},
{"key": "total", "label": "合计", "type": "formula"},
{"key": "reason", "label": "事由", "type": "textarea"},
{"key": "urgent", "label": "加急", "type": "switch"},
{"key": "items", "label": "明细", "type": "table"},
]
def check(label: str, condition, detail: str = "") -> None:
global PASSED
if condition:
PASSED += 1
print(f"[通过] {label}")
else:
FAILS.append(label)
print(f"[FAIL] {label} -> {detail}")
def notes(before: dict, after: dict, incoming: dict | None = None) -> dict[str, dict]:
"""跑一遍留痕,按字段名收好,方便逐格断言。"""
rows = engine.form_change_notes(FIELDS, before, after, incoming if incoming is not None else after)
return {str(row["key"]): row for row in rows}
print("=== 一、同一个数的两种写法:不算改 ===")
# 生产上那四条:只读金额格原样点一遍同意
got = notes({"checkOther": 0.0}, {"checkOther": 0})
check("money 0.0 → 0 不留痕", "checkOther" not in got, str(got))
got = notes({"travel": 4075.0}, {"travel": 4075})
check("money 4075.0 → 4075 不留痕(FYBX202611040001 那条)", "travel" not in got, str(got))
got = notes({"travel": "4075.00"}, {"travel": 4075})
check("money 字符串写法 → 数值 不留痕", "travel" not in got, str(got))
got = notes({"travel": "1,000.00"}, {"travel": 1000})
check("money 带千分位 → 数值 不留痕", "travel" not in got, str(got))
got = notes({"qty": 2}, {"qty": "2.0"})
check("number 2 → \"2.0\" 不留痕", "qty" not in got, str(got))
got = notes({"total": 670.1}, {"total": 670.1})
check("formula 同值不留痕", "total" not in got, str(got))
print("\n=== 二、钱真变了:一条都不许漏 ===")
got = notes({"checkOther": 0.0}, {"checkOther": 0.01})
check("money 0.0 → 0.01 留痕", got.get("checkOther", {}).get("before") == "0.0"
and got.get("checkOther", {}).get("after") == "0.01", str(got))
got = notes({"travel": 4075}, {"travel": 4075.5})
check("money 4075 → 4075.5 留痕", "travel" in got, str(got))
got = notes({}, {"travel": 0.0})
check("money 从(空)到有值 留痕", got.get("travel", {}).get("before") == "(空)", str(got))
got = notes({"travel": 12.5}, {}, incoming={"travel": 12.5})
check("改后 data 里没这一格时不记,也不报错", "travel" not in got, str(got))
print("\n=== 三、认不出是数的一概不吞 ===")
got = notes({"reason": "7"}, {"reason": "7.0"})
check("文本字段 \"7\" → \"7.0\" 仍算改动(不参与数值比较)", "reason" in got, str(got))
got = notes({"urgent": True}, {"urgent": 1})
check("开关 True → 1 仍算改动(布尔不当数)", "urgent" in got, str(got))
got = notes({"travel": "abc"}, {"travel": 0})
check("money \"abc\" → 0 仍算改动", "travel" in got, str(got))
got = notes({"travel": float("nan")}, {"travel": 0})
check("money NaN → 0 仍算改动", "travel" in got, str(got))
got = notes({"items": [{"a": 1}]}, {"items": [{"a": 2}]})
check("明细表内容变了留痕", "items" in got, str(got))
got = notes({"items": [{"a": 1}]}, {"items": [{"a": 1}]})
check("明细表没变不留痕", "items" not in got, str(got))
print("\n=== 四、原有的两条分寸不能被改坏 ===")
got = notes({"_fetch": {"at": "2026-11-09 15:45:30"}}, {"_fetch": {"at": "2026-11-09 17:06:08"}})
check("下划线开头的系统留痕不进人工改动", "_fetch" not in got, str(got))
got = notes({"checkOther": 1.0}, {"checkOther": 2.0}, incoming={"reason": "原样"})
check("只比本人这次交上来的那几项:没点的格子不记", not got, str(got))
print(f"\n全部 {PASSED} 条对上。" if not FAILS else f"\n{PASSED} 条对上,{len(FAILS)} 条不对:{FAILS}")
sys.exit(1 if FAILS else 0)