added collect loan
This commit is contained in:
@@ -74,6 +74,7 @@ class Loan(db.Model):
|
||||
'defaultPenaltyFee': self.default_penalty_fee,
|
||||
'continuousFee': self.continuous_fee,
|
||||
'collectionType': self.collection_type,
|
||||
'repaymentAmount':self.repayment_amount,
|
||||
'status': self.status,
|
||||
'productId': self.product_id,
|
||||
'disburseResult': self.disburse_result,
|
||||
|
||||
+151
-2
@@ -1,5 +1,6 @@
|
||||
from app.extensions import db
|
||||
from datetime import datetime, timezone
|
||||
from app.utils.logger import logger
|
||||
|
||||
class Repayment(db.Model):
|
||||
__tablename__ = "repayments"
|
||||
@@ -15,10 +16,158 @@ class Repayment(db.Model):
|
||||
transaction_id = db.Column(db.String(50), nullable=False)
|
||||
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))
|
||||
|
||||
repay_date = db.Column(db.DateTime, nullable=True)
|
||||
verify_date = db.Column(db.DateTime, nullable=True)
|
||||
repay_result = db.Column(db.String(10), nullable=True)
|
||||
repay_description = db.Column(db.String(100), nullable=True)
|
||||
verify_result = db.Column(db.String(10), nullable=True)
|
||||
verify_description = db.Column(db.String(100), nullable=True)
|
||||
def __repr__(self):
|
||||
return f'<Repayment {self.id}>'
|
||||
|
||||
def to_dict(self):
|
||||
"""
|
||||
Convert the Repayment object to a dictionary format for JSON serialization.
|
||||
"""
|
||||
return {
|
||||
'Id': self.id,
|
||||
"customerId": self.customer_id,
|
||||
'loanId': self.loan_id,
|
||||
'productId': self.product_id,
|
||||
'repayResult': self.repay_result,
|
||||
'repayDescription': self.repay_description,
|
||||
'verifyResult': self.verify_result,
|
||||
'verifyDescription': self.verify_description,
|
||||
'transactionId': self.transaction_id,
|
||||
'repayDate': self.repay_date.isoformat() if self.repay_date else None,
|
||||
'VerifyDate': self.verify_date.isoformat() if self.verify_date else None,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_repayment_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()
|
||||
|
||||
@classmethod
|
||||
def set_repay_date(cls, repayment_id, customer_id):
|
||||
"""
|
||||
Update the repay date of the loan with the given loan_id.
|
||||
"""
|
||||
# Retrieve repayment
|
||||
repayment = cls.query.get(repayment_id)
|
||||
|
||||
if not repayment:
|
||||
raise ValueError(f"repayment with ID {repayment_id} does not exist.")
|
||||
|
||||
# Check if customer_id matches
|
||||
if repayment.customer_id != customer_id:
|
||||
raise ValueError(f"Customer ID {customer_id} does not match the repayment's customer ID.")
|
||||
|
||||
current_time = datetime.now()
|
||||
logger.info(f"What is now ======= ==== ==> : {current_time}")
|
||||
# Update repayment date
|
||||
repayment.repay_date = current_time
|
||||
|
||||
# Commit changes to database
|
||||
try:
|
||||
logger.info(f"Updating repay date for repayment ID {repayment_id} to {current_time}")
|
||||
db.session.commit()
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.error(f"Failed to update repay date: {e}")
|
||||
raise
|
||||
@classmethod
|
||||
def set_repay_verify_date(cls, repayment_id, customer_id):
|
||||
"""
|
||||
Update the repayment verify date of the loan with the given repayment_id.
|
||||
"""
|
||||
# Retrieve repayment
|
||||
repayment = cls.query.get(repayment_id)
|
||||
|
||||
if not repayment:
|
||||
raise ValueError(f"repayment with ID {repayment_id} does not exist.")
|
||||
|
||||
# Check if customer_id matches
|
||||
if repayment.customer_id != customer_id:
|
||||
raise ValueError(f"Customer ID {customer_id} does not match the repayment's customer ID.")
|
||||
|
||||
current_time = datetime.now()
|
||||
logger.info(f"What is now ======= ==== ==> : {current_time}")
|
||||
# Update repayment verify_date
|
||||
repayment.verify_date = current_time
|
||||
|
||||
# Commit changes to database
|
||||
try:
|
||||
logger.info(f"Updating repay verify date for repayment ID {repayment_id} to {current_time}")
|
||||
db.session.commit()
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.error(f"Failed to update repay verify date: {e}")
|
||||
raise
|
||||
|
||||
|
||||
@classmethod
|
||||
def set_repay_result(cls, repayment_id, result, description):
|
||||
"""
|
||||
Update the repayment result and description of the repayment with the given repayment_id.
|
||||
"""
|
||||
# Retrieve loan
|
||||
repayment = cls.query.get(repayment_id)
|
||||
|
||||
if not repayment:
|
||||
raise ValueError(f"repayment with ID {repayment_id} does not exist.")
|
||||
|
||||
# Update repayment result and description
|
||||
repayment.repay_result = result
|
||||
repayment.repay_description = description
|
||||
|
||||
# Commit changes to database
|
||||
try:
|
||||
logger.info(f"Updating repayment result for repayment ID {repayment_id} to {result} with description {description}")
|
||||
db.session.commit()
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.error(f"Failed to update repayment result: {e}")
|
||||
raise
|
||||
@classmethod
|
||||
def set_verify_date_result(cls, repayment_id, result, description):
|
||||
"""
|
||||
Update the verify result and description of the repayment with the given repayment_id.
|
||||
"""
|
||||
# Retrieve repayment
|
||||
repayment = cls.query.get(repayment_id)
|
||||
|
||||
if not repayment:
|
||||
raise ValueError(f"repayment with ID {repayment_id} does not exist.")
|
||||
|
||||
# Update disburse result and description
|
||||
repayment.verify_result = result
|
||||
repayment.verify_description = description
|
||||
|
||||
# Commit changes to database
|
||||
try:
|
||||
logger.info(f"Updating verify result for repayment ID {repayment_id} to {result} with description {description}")
|
||||
db.session.commit()
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.error(f"Failed to update verify result: {e}")
|
||||
raise
|
||||
@classmethod
|
||||
def get_latest_repayment_without_repay_date(cls):
|
||||
"""
|
||||
Get the latest repayment without a repay date.
|
||||
"""
|
||||
return cls.query.filter(
|
||||
cls.repay_date.is_(None)
|
||||
).order_by(cls.created_at.desc()).first()
|
||||
|
||||
@classmethod
|
||||
def get_latest_loan_with_repay_date(cls):
|
||||
"""
|
||||
Get the latest repayment with a repay date and no verification date.
|
||||
"""
|
||||
return cls.query.filter(
|
||||
cls.repay_date.isnot(None),
|
||||
cls.verify_date.is_(None)
|
||||
).order_by(cls.created_at.desc()).first()
|
||||
Reference in New Issue
Block a user