import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'xygbfpsg.settings')
django.setup()

from accounting.models import GeneralLedger
from decimal import Decimal

print("=== ALL LEDGER BALANCES ===")
total_dr_in_ledger = Decimal('0')
total_cr_in_ledger = Decimal('0')

for entry in GeneralLedger.objects.all().select_related('account').order_by('account__code', 'transaction_date'):
    total_dr_in_ledger += entry.debit_amount
    total_cr_in_ledger += entry.credit_amount
    print(f"{entry.account.code} {entry.account.name} ({entry.account.account_type}): DR={entry.debit_amount}, CR={entry.credit_amount}, Balance={entry.balance}")

print(f"\n=== TOTALS FROM LEDGER ===")
print(f"Total Debits Posted: {total_dr_in_ledger}")
print(f"Total Credits Posted: {total_cr_in_ledger}")
print(f"Balanced: {total_dr_in_ledger == total_cr_in_ledger}")
