105 lines
4.1 KiB
Python
105 lines
4.1 KiB
Python
from Schemas import AccountRecords, BuildDecisionBookPayments
|
|
from Controllers.Postgres.engine import get_session_factory
|
|
from sqlalchemy import func
|
|
from decimal import Decimal
|
|
|
|
def debug_remainder_balance():
|
|
session_factory = get_session_factory()
|
|
session = session_factory()
|
|
|
|
# Set sessions for models
|
|
AccountRecords.set_session(session)
|
|
BuildDecisionBookPayments.set_session(session)
|
|
|
|
print("\n" + "=" * 50)
|
|
print("DEBUGGING REMAINDER BALANCE ISSUES")
|
|
print("=" * 50)
|
|
|
|
# Get counts of accounts
|
|
total_accounts = session.query(AccountRecords).filter(
|
|
AccountRecords.currency_value > 0
|
|
).count()
|
|
|
|
zero_remainder = session.query(AccountRecords).filter(
|
|
AccountRecords.currency_value > 0,
|
|
AccountRecords.remainder_balance == 0
|
|
).count()
|
|
|
|
nonzero_remainder = session.query(AccountRecords).filter(
|
|
AccountRecords.currency_value > 0,
|
|
AccountRecords.remainder_balance != 0
|
|
).count()
|
|
|
|
print(f"Total accounts with positive currency_value: {total_accounts}")
|
|
print(f"Accounts with zero remainder_balance: {zero_remainder}")
|
|
print(f"Accounts with non-zero remainder_balance: {nonzero_remainder}")
|
|
|
|
# Get distinct account IDs with payments
|
|
distinct_account_ids = session.query(BuildDecisionBookPayments.account_records_id).filter(
|
|
BuildDecisionBookPayments.account_records_id.isnot(None),
|
|
BuildDecisionBookPayments.account_is_debit == False # Credit entries (payments)
|
|
).distinct().all()
|
|
|
|
print(f"\nDistinct account IDs with payments: {len(distinct_account_ids)}")
|
|
|
|
# Sample some accounts with zero remainder_balance but have payments
|
|
print("\nSampling accounts with zero remainder_balance:")
|
|
sample_count = 0
|
|
|
|
for account_id_tuple in distinct_account_ids[:10]: # Check first 10 accounts with payments
|
|
account_id = account_id_tuple[0]
|
|
|
|
# Get the account record
|
|
account = AccountRecords.query.get(account_id)
|
|
if not account or account.remainder_balance != 0:
|
|
continue
|
|
|
|
# Calculate the sum of payments made using this account
|
|
payment_sum = session.query(
|
|
func.sum(BuildDecisionBookPayments.payment_amount)
|
|
).filter(
|
|
BuildDecisionBookPayments.account_records_id == account_id,
|
|
BuildDecisionBookPayments.account_is_debit == False # Credit entries (payments)
|
|
).scalar() or 0
|
|
|
|
print(f" Account {account_id}: Currency Value={abs(account.currency_value):,.2f} TL, Payments={abs(payment_sum):,.2f} TL, Remainder={account.remainder_balance}")
|
|
sample_count += 1
|
|
|
|
if sample_count == 0:
|
|
print(" No accounts found with zero remainder_balance that have payments")
|
|
|
|
# Now let's fix a sample of accounts
|
|
print("\nFixing sample accounts with zero remainder_balance:")
|
|
fixed_count = 0
|
|
|
|
for account_id_tuple in distinct_account_ids[:5]: # Fix first 5 accounts with payments
|
|
account_id = account_id_tuple[0]
|
|
|
|
# Get the account record
|
|
account = AccountRecords.query.get(account_id)
|
|
if not account or not account.build_parts_id or account.currency_value <= 0:
|
|
continue
|
|
|
|
# Calculate the sum of payments made using this account
|
|
payment_sum = session.query(
|
|
func.sum(BuildDecisionBookPayments.payment_amount)
|
|
).filter(
|
|
BuildDecisionBookPayments.account_records_id == account_id,
|
|
BuildDecisionBookPayments.account_is_debit == False # Credit entries (payments)
|
|
).scalar() or 0
|
|
|
|
old_remainder = account.remainder_balance
|
|
|
|
# Update remainder_balance for this account
|
|
account.remainder_balance = payment_sum
|
|
account.save()
|
|
fixed_count += 1
|
|
|
|
print(f" Fixed Account {account_id}: Old remainder={old_remainder}, New remainder={account.remainder_balance:,.2f} TL")
|
|
|
|
print(f"\nTotal accounts fixed in this run: {fixed_count}")
|
|
print("=" * 50)
|
|
|
|
if __name__ == "__main__":
|
|
debug_remainder_balance()
|