This commit is contained in:
Azeez Muibi
2025-04-28 08:40:14 +01:00
parent 0052340843
commit 75e9a96ba3
11 changed files with 529 additions and 44 deletions
+39 -29
View File
@@ -42,16 +42,15 @@ class Loan(db.Model):
back_populates="loans",
)
@classmethod
def get_all_loans(cls, customer_id=None, account_id=None, status=None, offer_id=None,
def get_all_loans(cls, id=None, customer_id=None, account_id=None, status=None, offer_id=None,
product_id=None, start_date=None, end_date=None, due_before=None, due_after=None,
page=1, limit=20):
transaction_id=None, original_transaction=None, page=1, limit=20):
"""
Get all loans with optional filtering
Args:
id (int, optional): Filter by loan ID
customer_id (str, optional): Filter by customer ID
account_id (str, optional): Filter by account ID
status (str, optional): Filter by loan status
@@ -61,6 +60,8 @@ class Loan(db.Model):
end_date (datetime, optional): Filter by end date (created_at)
due_before (datetime, optional): Filter loans due before this date
due_after (datetime, optional): Filter loans due after this date
transaction_id (str, optional): Filter by transaction ID
original_transaction (str, optional): Filter by original transaction
Returns:
list: List of Loan objects
@@ -68,6 +69,9 @@ class Loan(db.Model):
query = cls.query
logger.info(f"Get all loan models from loans model cme back")
# Apply filters if provided
if id:
query = query.filter(cls.id == id)
if customer_id:
query = query.filter(cls.customer_id == customer_id)
@@ -83,6 +87,12 @@ class Loan(db.Model):
if product_id:
query = query.filter(cls.product_id == product_id)
if transaction_id:
query = query.filter(cls.transaction_id == transaction_id)
if original_transaction:
query = query.filter(cls.original_transaction == original_transaction)
if start_date:
query = query.filter(cls.created_at >= start_date)
@@ -106,31 +116,31 @@ class Loan(db.Model):
query = query.limit(limit).offset(offset)
return query.all(), total_count
#
# def to_dict(self):
# """
# Convert the Loan object to a dictionary format for JSON serialization.
# """
# return {
# 'id': self.id,
# 'customer_id': self.customer_id,
# 'account_id': self.account_id,
# 'transaction_id': self.transaction_id,
# 'original_transaction': self.original_transaction,
# 'offer_id': self.offer_id,
# 'initial_loan_amount': self.initial_loan_amount,
# 'current_loan_amount': self.current_loan_amount,
# 'status': self.status,
# 'product_id': self.product_id,
# 'default_penalty_fee': self.default_penalty_fee,
# 'continuous_fee': self.continuous_fee,
# 'upfront_fee': self.upfront_fee,
# 'repayment_amount': self.repayment_amount,
# 'installment_amount': self.installment_amount,
# 'due_date': self.due_date.isoformat() if self.due_date else None,
# 'created_at': self.created_at.isoformat() if self.created_at else None,
# 'updated_at': self.updated_at.isoformat() if self.updated_at else None
# }
def to_dict(self):
"""
Convert the Loan object to a dictionary format for JSON serialization.
"""
return {
'id': self.id,
'customer_id': self.customer_id,
'account_id': self.account_id,
'transaction_id': self.transaction_id,
'original_transaction': self.original_transaction,
'offer_id': self.offer_id,
'initial_loan_amount': self.initial_loan_amount,
'current_loan_amount': self.current_loan_amount,
'status': self.status,
'product_id': self.product_id,
'default_penalty_fee': self.default_penalty_fee,
'continuous_fee': self.continuous_fee,
'upfront_fee': self.upfront_fee,
'repayment_amount': self.repayment_amount,
'installment_amount': self.installment_amount,
'due_date': self.due_date.isoformat() if self.due_date else None,
'created_at': self.created_at.isoformat() if self.created_at else None,
'updated_at': self.updated_at.isoformat() if self.updated_at else None
}
def __repr__(self):
return f'<Loan {self.id}>'
+62 -9
View File
@@ -1,7 +1,7 @@
from datetime import datetime, timezone
from app.extensions import db
from sqlalchemy.orm import relationship
from dateutil.relativedelta import relativedelta
# from dateutil.relativedelta import relativedelta
class LoanRepaymentSchedule(db.Model):
__tablename__ = 'loan_repayment_schedules'
@@ -26,20 +26,73 @@ class LoanRepaymentSchedule(db.Model):
back_populates="loan_repayment_schedules",
)
@classmethod
def get_all_repayment_schedules(cls, loan_id=None, product_id=None, paid=None,
due_before=None, due_after=None, installment_number=None,
page=1, limit=20):
"""
Get all loan repayment schedules with optional filtering
Args:
loan_id (int, optional): Filter by loan ID
product_id (str, optional): Filter by product ID
paid (bool, optional): Filter by paid status
due_before (datetime, optional): Filter schedules due before this date
due_after (datetime, optional): Filter schedules due after this date
installment_number (int, optional): Filter by installment number
page (int, optional): Page number for pagination
limit (int, optional): Number of items per page
Returns:
tuple: (list of LoanRepaymentSchedule objects, total count)
"""
query = cls.query
# Apply filters if provided
if loan_id:
query = query.filter(cls.loan_id == loan_id)
if product_id:
query = query.filter(cls.product_id == product_id)
if paid is not None:
query = query.filter(cls.paid == paid)
if due_before:
query = query.filter(cls.due_date <= due_before)
if due_after:
query = query.filter(cls.due_date >= due_after)
if installment_number:
query = query.filter(cls.installment_number == installment_number)
# Order by due_date and installment_number
query = query.order_by(cls.due_date.asc(), cls.installment_number.asc())
# Get total count before pagination
total_count = query.count()
# Apply pagination
offset = (page - 1) * limit
query = query.limit(limit).offset(offset)
return query.all(), total_count
def to_dict(self):
return {
'id': self.id,
'loanId': self.loan_id,
'installmentNumber': self.installment_number,
'dueDate': self.due_date.isoformat(),
'principalAmount': self.principal_amount,
'interestAmount': self.interest_amount,
'totalInstallment': self.total_installment,
'loan_id': self.loan_id,
'product_id': self.product_id,
'installment_number': self.installment_number,
'due_date': self.due_date.isoformat() if self.due_date else None,
'installment_amount': self.installment_amount,
'total_repayment_amount': self.total_repayment_amount,
'paid': self.paid,
'paidAt': self.paid_at.isoformat() if self.paid_at else None
'paid_at': self.paid_at.isoformat() if self.paid_at else None,
'created_at': self.created_at.isoformat() if self.created_at else None,
'updated_at': self.updated_at.isoformat() if self.updated_at else None
}
def __repr__(self):
return f'<LoanRepaymentSchedule Loan:{self.loan_id} Installment:{self.installment_number}>'
return f'<LoanRepaymentSchedule Loan:{self.loan_id} Installment:{self.installment_number}>'