82 lines
3.0 KiB
Python
82 lines
3.0 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import sys
|
|
sys.path.insert(0, 'backend')
|
|
|
|
os.environ['DB_HOST'] = '192.168.0.61'
|
|
os.environ['DB_NAME'] = 'njts_acct'
|
|
os.environ['DB_USER'] = 'njts_app'
|
|
os.environ['DB_PASSWORD'] = 'njts_app2025'
|
|
os.environ['DB_PORT'] = '55432'
|
|
|
|
from app.core.database import get_connection
|
|
from decimal import Decimal
|
|
|
|
def D(x):
|
|
return Decimal(str(x or 0))
|
|
|
|
with get_connection() as conn:
|
|
with conn.cursor() as cur:
|
|
print('\n【 验证试算表的SQL逻辑 】')
|
|
|
|
aid = 41 # account_code='701' 的真实 id
|
|
date_from = '2025-06-01'
|
|
date_to = '2025-06-30'
|
|
|
|
print(f"\n1. 当前代码使用的查询 (account_id = {aid}):")
|
|
sql = """
|
|
SELECT
|
|
COALESCE(SUM(l.debit), 0) AS debit,
|
|
COALESCE(SUM(l.credit), 0) AS credit
|
|
FROM journal_lines l
|
|
JOIN journal_entries j
|
|
ON j.journal_entry_id = l.journal_entry_id
|
|
LEFT JOIN journal_entries child
|
|
ON child.parent_entry_id = j.journal_entry_id
|
|
WHERE l.account_id = %s
|
|
AND j.entry_date BETWEEN %s AND %s
|
|
AND j.is_deleted = false
|
|
AND child.journal_entry_id IS NULL
|
|
"""
|
|
cur.execute(sql, (aid, date_from, date_to))
|
|
row = cur.fetchone()
|
|
print(f" 结果: debit={row['debit']}, credit={row['credit']}")
|
|
|
|
print(f"\n2. 不使用LEFT JOIN过滤的查询 (account_id = {aid}):")
|
|
sql2 = """
|
|
SELECT
|
|
COALESCE(SUM(l.debit), 0) AS debit,
|
|
COALESCE(SUM(l.credit), 0) AS credit
|
|
FROM journal_lines l
|
|
JOIN journal_entries j
|
|
ON j.journal_entry_id = l.journal_entry_id
|
|
WHERE l.account_id = %s
|
|
AND j.entry_date BETWEEN %s AND %s
|
|
AND j.is_deleted = false
|
|
"""
|
|
cur.execute(sql2, (aid, date_from, date_to))
|
|
row = cur.fetchone()
|
|
print(f" 结果: debit={row['debit']}, credit={row['credit']}")
|
|
|
|
print(f"\n3. 如果错误地用account_code'701'作为account_id会发生什么:")
|
|
sql3 = """
|
|
SELECT
|
|
COALESCE(SUM(l.debit), 0) AS debit,
|
|
COALESCE(SUM(l.credit), 0) AS credit
|
|
FROM journal_lines l
|
|
JOIN journal_entries j
|
|
ON j.journal_entry_id = l.journal_entry_id
|
|
WHERE l.account_id = %s
|
|
AND j.entry_date BETWEEN %s AND %s
|
|
AND j.is_deleted = false
|
|
"""
|
|
cur.execute(sql3, (701, date_from, date_to))
|
|
row = cur.fetchone()
|
|
print(f" 结果 (account_id=701): debit={row['debit']}, credit={row['credit']}")
|
|
|
|
print(f"\n4. 检查是否有人在代码中硬编码了'701'而不是从accounts表查询:")
|
|
# 在当前的trial_balance/service.py中查找硬编码的数字
|
|
print(" 需要检查代码中是否有 account_id IN (701, ...) 或类似的硬编码")
|