add loan and loan_charges model
This commit is contained in:
@@ -0,0 +1,59 @@
|
|||||||
|
from datetime import datetime, timezone
|
||||||
|
from app.extensions import db
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
|
class Loan(db.Model):
|
||||||
|
__tablename__ = "loans"
|
||||||
|
|
||||||
|
id = db.Column(
|
||||||
|
db.Integer,
|
||||||
|
primary_key=True,
|
||||||
|
autoincrement=True,
|
||||||
|
)
|
||||||
|
customer_id = db.Column(db.String(50), nullable=False)
|
||||||
|
transaction_id = db.Column(db.String(50), nullable=True)
|
||||||
|
account_id = db.Column(db.String(50), nullable=False)
|
||||||
|
offer_id = db.Column(db.String(20), nullable=False)
|
||||||
|
product_id = db.Column(db.String(20), nullable=True)
|
||||||
|
collection_type = db.Column(db.String(20), nullable=True)
|
||||||
|
current_loan_amount = db.Column(db.Float, nullable=True)
|
||||||
|
initial_loan_amount = db.Column(db.Float, nullable=False)
|
||||||
|
default_penalty_fee = db.Column(db.Float, default=0)
|
||||||
|
continuous_fee = db.Column(db.Float, default=0)
|
||||||
|
status = db.Column(db.String(20), default='pending')
|
||||||
|
due_date = db.Column(db.DateTime, nullable=True)
|
||||||
|
created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
|
||||||
|
updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc))
|
||||||
|
|
||||||
|
customer = relationship(
|
||||||
|
"Customer",
|
||||||
|
primaryjoin="Customer.id == Loan.customer_id",
|
||||||
|
foreign_keys=[customer_id],
|
||||||
|
back_populates="loans",
|
||||||
|
)
|
||||||
|
|
||||||
|
loan_charges = relationship(
|
||||||
|
"LoanCharge",
|
||||||
|
primaryjoin="Loan.id == LoanCharge.loan_id",
|
||||||
|
foreign_keys="LoanCharge.loan_id",
|
||||||
|
back_populates="loan",
|
||||||
|
)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Loan {self.id}>"
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
"""
|
||||||
|
Convert the Loan object to a dictionary format for JSON serialization.
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
'debtId': self.id,
|
||||||
|
'initialLoanAmount': self.initial_loan_amount,
|
||||||
|
'currentLoanAmount': self.current_loan_amount,
|
||||||
|
'defaultPenaltyFee': self.default_penalty_fee,
|
||||||
|
'continuousFee': self.continuous_fee,
|
||||||
|
'collectionType': self.collection_type,
|
||||||
|
'status': self.status,
|
||||||
|
'dueDate': self.due_date.isoformat() if self.due_date else None,
|
||||||
|
'loanDate': self.created_at.isoformat if self.created_at else None
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
from datetime import datetime, timezone, timedelta
|
||||||
|
from app.extensions import db
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
|
class LoanCharge(db.Model):
|
||||||
|
__tablename__ = 'loan_charges'
|
||||||
|
|
||||||
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
|
loan_id = db.Column(db.Integer, nullable=False)
|
||||||
|
transaction_id = db.Column(db.String(50), nullable=True)
|
||||||
|
code = db.Column(db.String(50), nullable=False)
|
||||||
|
amount = db.Column(db.Float, default=0.0)
|
||||||
|
percent = db.Column(db.Float, default=0.0)
|
||||||
|
description = db.Column(db.Text, nullable=True)
|
||||||
|
due = db.Column(db.Integer, nullable=False)
|
||||||
|
due_date = db.Column(db.DateTime, nullable=True)
|
||||||
|
created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
|
||||||
|
updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc))
|
||||||
|
|
||||||
|
loan = relationship(
|
||||||
|
"Loan",
|
||||||
|
primaryjoin="LoanCharge.loan_id == Loan.id",
|
||||||
|
foreign_keys=[loan_id],
|
||||||
|
back_populates="loan_charges",
|
||||||
|
)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<LoanCharge {self.id} - Loan {self.loan_id} - {self.code}>"
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
"""
|
||||||
|
Convert the Loan charge object to a dictionary format for JSON serialization.
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
'id': self.id,
|
||||||
|
'loanId': self.loan_id,
|
||||||
|
'transactionId': self.transaction_id,
|
||||||
|
'code': self.code,
|
||||||
|
'amount': self.amount,
|
||||||
|
'percent': self.percent,
|
||||||
|
'description': self.description,
|
||||||
|
'due': self.due
|
||||||
|
}
|
||||||
@@ -20,6 +20,19 @@ class Transaction(db.Model):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<Transaction {self.id}>'
|
return f'<Transaction {self.id}>'
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
"""
|
||||||
|
Convert the Transaction object to a dictionary format for JSON serialization.
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
'id': self.id,
|
||||||
|
'transaction_id': self.transaction_id,
|
||||||
|
'account_id': self.account_id,
|
||||||
|
'customer_id': self.customer_id,
|
||||||
|
'type': self.type,
|
||||||
|
'channel': self.channel,
|
||||||
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_transaction_by_transaction_id(cls, transaction_id):
|
def get_transaction_by_transaction_id(cls, transaction_id):
|
||||||
return cls.query.filter_by(transaction_id=transaction_id).first()
|
return cls.query.filter_by(transaction_id=transaction_id).first()
|
||||||
Reference in New Issue
Block a user