added salary table
This commit is contained in:
+11
-1
@@ -218,4 +218,14 @@ class Loan(db.Model):
|
||||
return cls.query.filter(
|
||||
cls.disburse_date.isnot(None),
|
||||
cls.disburse_verify.is_(None)
|
||||
).order_by(cls.created_at.desc()).first()
|
||||
).order_by(cls.created_at.desc()).first()
|
||||
|
||||
@classmethod
|
||||
def get_customer_loans(cls, customer_id):
|
||||
"""
|
||||
Get customer's active loans by customer_id.
|
||||
"""
|
||||
customer_loans = cls.query.filter_by( customer_id = customer_id).all()
|
||||
if not customer_loans:
|
||||
raise ValueError(f"Customer with Id {customer_id} does not have any loan.")
|
||||
return customer_loans
|
||||
@@ -14,6 +14,8 @@ class Repayment(db.Model):
|
||||
customer_id = db.Column(db.String(50), nullable=False)
|
||||
product_id = db.Column(db.String(20), nullable=True)
|
||||
transaction_id = db.Column(db.String(50), nullable=False)
|
||||
initiated_by = db.Column(db.String(50), nullable=True)
|
||||
salary_amount = db.Column(db.Float, nullable=True, default=0.0)
|
||||
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)
|
||||
@@ -39,12 +41,55 @@ class Repayment(db.Model):
|
||||
'verifyResult': self.verify_result,
|
||||
'verifyDescription': self.verify_description,
|
||||
'transactionId': self.transaction_id,
|
||||
'initiatedBy':self.initiated_by,
|
||||
'salaryAmount':self.salary_amount,
|
||||
'repayDate': self.repay_date.isoformat() if self.repay_date else None,
|
||||
'VerifyDate': self.verify_date.isoformat() if self.verify_date else None,
|
||||
}
|
||||
|
||||
|
||||
@classmethod
|
||||
def add_repayment(cls, data: dict):
|
||||
"""
|
||||
Create and persist a new repayment record.
|
||||
"""
|
||||
logger.info(f"Received repayment data: {data}")
|
||||
|
||||
try:
|
||||
new_repayment = cls(
|
||||
loan_id=data["loanId"],
|
||||
customer_id=data["customerId"],
|
||||
product_id=data.get("productId"),
|
||||
transaction_id=data["transactionId"],
|
||||
initiated_by=data.get("initiatedBy"),
|
||||
salary_amount=float(data.get("salaryAmount", 0.0)),
|
||||
repay_date=(
|
||||
datetime.strptime(data["repayDate"], "%Y-%m-%d")
|
||||
.replace(tzinfo=timezone.utc)
|
||||
if data.get("repayDate")
|
||||
else None
|
||||
),
|
||||
repay_result=data.get("repayResult"),
|
||||
repay_description=data.get("repayDescription"),
|
||||
verify_result=data.get("verifyResult"),
|
||||
verify_description=data.get("verifyDescription"),
|
||||
verify_date=(
|
||||
datetime.strptime(data["verifyDate"], "%Y-%m-%d")
|
||||
.replace(tzinfo=timezone.utc)
|
||||
if data.get("verifyDate")
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
db.session.add(new_repayment)
|
||||
db.session.commit()
|
||||
logger.info("Repayment record committed.")
|
||||
return new_repayment
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.error(f"Error adding repayment data: {e}")
|
||||
raise
|
||||
@classmethod
|
||||
def get_repayment_by_transaction_id(cls, transaction_id):
|
||||
return cls.query.filter_by(transaction_id=transaction_id).first()
|
||||
|
||||
+28
-1
@@ -45,7 +45,7 @@ class Salary(db.Model):
|
||||
try:
|
||||
new_data = cls(
|
||||
customer_id=data.get('customerId'),
|
||||
amount=data.get('salaryAmount', 0.0),
|
||||
amount=data.get('amount', 0.0),
|
||||
status='START',
|
||||
salary_date = datetime.strptime(data.get('salaryDate'), "%Y-%m-%d").date() if data.get('salaryDate') else None,
|
||||
account_id=data.get('accountId')
|
||||
@@ -69,3 +69,30 @@ class Salary(db.Model):
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching pending salaries: {str(e)}")
|
||||
return []
|
||||
@classmethod
|
||||
def update_status(cls, salary_id, status):
|
||||
"""
|
||||
Update the status of the salary record with the given salary_id.
|
||||
"""
|
||||
try:
|
||||
# Retrieve salary record
|
||||
salary = cls.query.get(salary_id)
|
||||
|
||||
if not salary:
|
||||
raise ValueError(f"Salary with ID {salary_id} does not exist.")
|
||||
|
||||
if salary.status == status:
|
||||
return salary.to_dict() # Still return the current state if no change
|
||||
|
||||
# Update status and timestamp
|
||||
salary.status = status
|
||||
salary.updated_at = datetime.now(timezone.utc) # Manually update timestamp if not auto-updating
|
||||
db.session.commit()
|
||||
|
||||
logger.info("Salary status updated and committed.")
|
||||
return salary.to_dict()
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
logger.error(f"Error updating salary status: {e}")
|
||||
raise Exception(f"Error updating salary status: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user