CaiWuXiTong/scripts/erp-daily-bills/dewu-alipay-summary.py

55 lines
1.9 KiB
Python

"""Validate a scoped Dewu Alipay CSV and emit its authoritative control totals."""
import argparse
import csv
import json
from decimal import Decimal
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("csv_path")
parser.add_argument("--accounts-json", required=True)
args = parser.parse_args()
with open(args.accounts_json, "r", encoding="utf-8") as handle:
mappings = json.load(handle)
allowed_accounts = {str(item["merchant_no"]) for item in mappings}
allowed_stores = {str(item["alipay_name"]) for item in mappings}
rows = 0
income = Decimal("0")
expense = Decimal("0")
seen_accounts = set()
seen_stores = set()
with open(args.csv_path, "r", encoding="utf-8-sig", newline="") as handle:
reader = csv.DictReader(handle)
for row in reader:
if not any((value or "").strip() for value in row.values()):
continue
store = (row.get("原始店铺") or row.get("店铺") or "").strip()
account = (row.get("支付宝账号") or "").strip()
if store not in allowed_stores:
raise ValueError(f"账单包含范围外店铺:{store or '<空>'}")
if account not in allowed_accounts:
raise ValueError(f"账单包含范围外支付宝账号:{account or '<空>'}")
rows += 1
seen_accounts.add(account)
seen_stores.add(store)
income += Decimal((row.get("收入") or "0").replace(",", ""))
expense += Decimal((row.get("支出") or "0").replace(",", ""))
print(json.dumps({
"rows": rows,
"income": f"{income:.2f}",
"expense": f"{expense:.2f}",
"store_count": len(seen_stores),
"account_count": len(seen_accounts),
}, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())