Files
njts-accounting-core/identify_duplicate_records.py
2026-02-20 15:47:27 +09:00

79 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import psycopg2
conn = psycopg2.connect(
host="192.168.0.61",
port=55432,
database="njts_acct",
user="njts_app",
password="njts_app2025"
)
cur = conn.cursor()
print("=" * 120)
print("【分析】通过复制创建的重复数据识别")
print("=" * 120)
# 查询所有"遠達ビル 事務所費用"的记录,按月份分组
cur.execute("""
SELECT
EXTRACT(YEAR FROM je.entry_date)::int as year,
EXTRACT(MONTH FROM je.entry_date)::int as month,
COUNT(*) as cnt,
STRING_AGG(je.journal_entry_id::text, ', ' ORDER BY je.journal_entry_id) as ids
FROM journal_entries je
WHERE je.description LIKE '%遠達ビル 事務所費用%'
AND je.is_deleted = false
GROUP BY year, month
ORDER BY year, month
""")
results = cur.fetchall()
print("\n【遠達ビル 事務所費用】6月8月の件数未削除")
for year, month, cnt, ids in results:
print(f" {year}{month}月: {cnt}IDs: {ids}")
# 现在查询每个月份最多的金额组合(这应该是预期的、正确的记录)
print("\n【分析】6月8月各月の仕訳明細")
for month in [6, 7, 8]:
print(f"\n--- {month}月 ---")
cur.execute("""
SELECT
je.journal_entry_id,
je.description,
je.is_latest,
je.is_deleted,
COALESCE(SUM(jl.debit), 0) as debit_total,
COALESCE(SUM(jl.credit), 0) as credit_total
FROM journal_entries je
LEFT JOIN journal_lines jl ON je.journal_entry_id = jl.journal_entry_id
WHERE EXTRACT(MONTH FROM je.entry_date) = %s
AND EXTRACT(YEAR FROM je.entry_date) = 2025
AND je.description LIKE '%%遠達ビル 事務所費用%%'
AND je.is_deleted = false
GROUP BY je.journal_entry_id, je.description, je.is_latest, je.is_deleted
ORDER BY je.journal_entry_id
""", (month,))
month_results = cur.fetchall()
if month_results:
for row in month_results:
entry_id, description, is_latest, is_deleted, debit, credit = row
latest_mark = "" if is_latest else ""
print(f" ID={entry_id} [{latest_mark}] | 借={debit:>10,.0f} 貸={credit:>10,.0f} | {description}")
else:
print(" 該当データなし")
print("\n" + "=" * 120)
print("【推奨】删除对象")
print("=" * 120)
print("遠達ビル 事務所費用の重复数据:")
print(" - 原则上每月应该只有1笔合法的仕訳")
print(" - 如果某月有多笔除了最新的is_latest=true其他都应该删除")
print(" - 或者由用户确认哪些是通过复制创建的不需要的数据")
cur.close()
conn.close()