<filename>app/dao/loan.py
from sqlalchemy import select
from app import db
from app.dao.database import commit_to_database
from app.dao.money import get_money_items_loan
from app.models import Loan, LoanIntRate, LoanTran


def get_loan_row(loan_id):
    return db.session.query(Loan).get(loan_id)


def get_loans(filtr):
    return db.session.execute(select(Loan).filter(*filtr)).scalars().all()


def get_loanstat_data(loan_id):
    money_items = get_money_items_loan(loan_id)
    transactions = db.session.query(LoanTran).filter_by(loan_id=loan_id).all()
    return money_items, transactions


def get_rates(loan_id):
    stmt = select(LoanIntRate.rate, LoanIntRate.start_date).filter_by(loan_id=loan_id)
    return db.session.execute(stmt).fetchall()


def post_loan(loan):
    db.session.add(loan)
    db.session.flush()
    loan_id = loan.id
    commit_to_database()
    return loan_id

<filename>app/dao/loan.py
from sqlalchemy import select
from app import db
from app.dao.database import commit_to_database
from app.dao.money import get_money_items_loan
from app.models import Loan, LoanIntRate, LoanTran


def get_loan_row(loan_id):
    return db.session.query(Loan).get(loan_id)


def get_loans(filtr):
    return db.session.execute(select(Loan).filter(*filtr)).scalars().all()


def get_loanstat_data(loan_id):
    money_items = get_money_items_loan(loan_id)
    transactions = db.session.query(LoanTran).filter_by(loan_id=loan_id).all()
    return money_items, transactions


def get_rates(loan_id):
    stmt = select(LoanIntRate.rate, LoanIntRate.start_date).filter_by(loan_id=loan_id)
    return db.session.execute(stmt).fetchall()


def post_loan(loan):
    db.session.add(loan)
    db.session.flush()
    loan_id = loan.id
    commit_to_database()
    return loan_id
