Files
openclaw-config/workspace-finances/verify_excluded.py
T

47 lines
2.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json, pymysql, collections
cfg = json.load(open('db_config.json'))
conn = pymysql.connect(host=cfg['host'], port=cfg['port'], user=cfg['user'],
password=cfg.get('password'), database=cfg['database'], charset='utf8mb4')
cur = conn.cursor()
cur.execute("""SELECT amount, recorded_date FROM variable_expenses
WHERE member_id=3 AND recorded_date BETWEEN '2026-01-01' AND '2026-06-30'
AND (note LIKE '%[支付宝]%' OR note LIKE '%[邮储卡]%')""")
imp = {(d.strftime('%Y-%m-%d'), round(float(a), 2)) for a, d in cur.fetchall()}
print(f"支付宝+邮储卡已计入: {len(imp)}笔")
d = json.load(open('/tmp/wx_juaner.json'))
def fp(s): return float(s)
REFUND = ('已全额退款', '对方已退还', '已退款')
excluded = []
for r in d['records']:
if r[4].strip() != '支出': continue
if any(x in r[7] for x in REFUND): continue
key = (r[0][:10], round(fp(r[5]), 2))
if key in imp:
excluded.append((key, r[2], r[6].strip()))
print(f"微信中被去重排除的: {len(excluded)}笔")
by_pay = collections.Counter(x[2] for x in excluded)
print(" 按微信支付方式:", dict(by_pay))
amt = sum(x[0][1] for x in excluded)
print(f" 被排除总额: ¥{amt:.2f}")
# 每个排除项在库中是否有邮储卡/支付宝对应
cnt_ext = 0; cnt_ali = 0; missing = []
for key, peer, pay in excluded:
cur.execute("""SELECT note FROM variable_expenses WHERE member_id=3 AND recorded_date=%s AND amount=%s
AND (note LIKE '%%[邮储卡]%%' OR note LIKE '%%[支付宝]%%')""", (key[0], key[1]))
tags = set()
for (n,) in cur.fetchall():
if '[邮储卡]' in n: tags.add('PSBC')
if '[支付宝]' in n: tags.add('ALI')
if 'PSBC' in tags: cnt_ext += 1
if 'ALI' in tags: cnt_ali += 1
if not tags:
missing.append((key, peer))
print(f" 排除项对应邮储卡记录: {cnt_ext}, 对应支付宝记录: {cnt_ali}")
print(f" 排除项但库中无对应(=可能漏计): {len(missing)}笔")
for m in missing[:10]:
print(" 缺:", m)
conn.close()