This commit is contained in:
admin
2026-02-20 15:47:27 +09:00
parent c60cbf2d9a
commit 584530937b
108 changed files with 12112 additions and 416 deletions

View File

@@ -14,19 +14,53 @@ def get_trial_balance(
):
print("### trial-balance OPTIONAL PARAMS ACTIVE ###")
sql = """
SELECT
a.account_id,
a.account_code,
a.account_name,
a.account_type,
COALESCE(SUM(l.debit), 0) AS debit,
COALESCE(SUM(l.credit), 0) AS credit
FROM journal_lines l
JOIN journal_entries e ON e.journal_entry_id = l.journal_entry_id
JOIN accounts a ON a.account_id = l.account_id
WHERE 1 = 1
"""
with get_connection() as conn:
# 检查 is_latest 字段是否存在(新的版本追踪系统)
with conn.cursor() as check_cur:
check_cur.execute("""
SELECT COUNT(*) as cnt
FROM information_schema.columns
WHERE table_name = 'journal_entries' AND column_name = 'is_latest'
""")
has_is_latest = check_cur.fetchone()[0] > 0
# 构建查询语句
if has_is_latest:
# 新系统只取最新版本is_latest = true
sql = """
SELECT
a.account_id,
a.account_code,
a.account_name,
a.account_type,
COALESCE(SUM(l.debit), 0) AS debit,
COALESCE(SUM(l.credit), 0) AS credit
FROM journal_lines l
JOIN journal_entries e ON e.journal_entry_id = l.journal_entry_id
JOIN accounts a ON a.account_id = l.account_id
WHERE e.is_deleted = false
AND e.is_latest = true
"""
else:
# 旧系统:排除已修正的记录(使用 parent_entry_id
sql = """
SELECT
a.account_id,
a.account_code,
a.account_name,
a.account_type,
COALESCE(SUM(l.debit), 0) AS debit,
COALESCE(SUM(l.credit), 0) AS credit
FROM journal_lines l
JOIN journal_entries e ON e.journal_entry_id = l.journal_entry_id
JOIN accounts a ON a.account_id = l.account_id
WHERE e.is_deleted = false
AND e.journal_entry_id NOT IN (
SELECT parent_entry_id
FROM journal_entries
WHERE parent_entry_id IS NOT NULL
)
"""
params = {}
@@ -35,11 +69,11 @@ def get_trial_balance(
params["fiscal_year"] = fiscal_year
if date_from is not None:
sql += " AND e.journal_date >= %(date_from)s"
sql += " AND e.entry_date >= %(date_from)s"
params["date_from"] = date_from
if date_to is not None:
sql += " AND e.journal_date <= %(date_to)s"
sql += " AND e.entry_date <= %(date_to)s"
params["date_to"] = date_to
sql += """