debugging
This commit was merged in pull request #34.
This commit is contained in:
+28
-14
@@ -7,7 +7,9 @@ import logging
|
||||
from sqlalchemy import and_, or_, not_
|
||||
from sqlalchemy.sql import func
|
||||
from app.utils.logger import logger
|
||||
from app.extensions import db
|
||||
from app.extensions import db
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
from datetime import datetime, timezone
|
||||
|
||||
class Loan(db.Model):
|
||||
__tablename__ = "loans"
|
||||
@@ -243,21 +245,29 @@ class Loan(db.Model):
|
||||
|
||||
return customer_loans, total_amount
|
||||
@classmethod
|
||||
def get_customer_active_loans(cls, customer_id):
|
||||
def get_customer_active_loans(cls, customer_id):
|
||||
"""
|
||||
Get customer's active loans and sum by customer_id.
|
||||
"""
|
||||
customer_loans = cls.query.filter_by( customer_id = customer_id, status='active').all()
|
||||
customer_loans = cls.query.filter(
|
||||
cls.customer_id == customer_id,
|
||||
cls.status != 'repaid'
|
||||
).all()
|
||||
|
||||
if not customer_loans:
|
||||
raise ValueError(f"Customer with Id {customer_id} does not have any active loan.")
|
||||
|
||||
total_amount = (
|
||||
cls.query.with_entities(func.coalesce(func.sum(cls.balance), 0.0))
|
||||
.filter_by(customer_id=customer_id)
|
||||
.scalar()
|
||||
cls.query
|
||||
.with_entities(func.coalesce(func.sum(cls.balance), 0.0))
|
||||
.filter(
|
||||
cls.customer_id == customer_id,
|
||||
cls.status != 'repaid'
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id} with total amount: {total_amount}")
|
||||
logger.info(f"Found {len(customer_loans)} active loans for customer ID: {customer_id} with total amount: {total_amount}")
|
||||
|
||||
return customer_loans, total_amount
|
||||
|
||||
@@ -303,17 +313,21 @@ class Loan(db.Model):
|
||||
if not loan:
|
||||
raise ValueError(f"Loan with ID {loan_id} does not exist.")
|
||||
|
||||
# Ensure valid repayment amount
|
||||
if amount_collected <= 0:
|
||||
raise ValueError("Repayment amount must be greater than zero.")
|
||||
if loan.balance is None:
|
||||
raise ValueError("There is no balance for this loan")
|
||||
# Convert to Decimal and round to 2 decimal places
|
||||
amount_collected = Decimal(str(amount_collected)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
|
||||
balance = Decimal(str(loan.balance or 0)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
|
||||
|
||||
if amount_collected > loan.balance:
|
||||
# Ensure valid repayment amount
|
||||
if amount_collected <= Decimal("0.00"):
|
||||
raise ValueError("Repayment amount must be greater than zero.")
|
||||
if balance <= Decimal("0.00"):
|
||||
raise ValueError("There is no balance for this loan.")
|
||||
if amount_collected > balance:
|
||||
raise ValueError("Repayment amount exceeds current loan balance.")
|
||||
|
||||
# Deduct the amount from the current balance
|
||||
loan.balance -= float(amount_collected)
|
||||
new_balance = balance - amount_collected
|
||||
loan.balance = float(new_balance)
|
||||
loan.updated_at = datetime.now(timezone.utc)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@@ -42,9 +42,13 @@ class RepaymentsData(db.Model):
|
||||
Add a new repayment data entry.
|
||||
"""
|
||||
try:
|
||||
repaymentAmount = data.get('repaymentAmount', 0.0)
|
||||
amountCollected = data.get('amountCollected',0.0)
|
||||
accountBalance = repaymentAmount - amountCollected
|
||||
repayment_amount = float(data.get('repaymentAmount', 0.0))
|
||||
amount_collected = float(data.get('amountCollected', 0.0))
|
||||
|
||||
if amount_collected < 0 or repayment_amount < 0:
|
||||
raise ValueError("Amounts cannot be negative.")
|
||||
|
||||
account_balance = round(repayment_amount - amount_collected, 2)
|
||||
|
||||
new_data = cls(
|
||||
transaction_id=data.get('transactionId'),
|
||||
@@ -53,14 +57,19 @@ class RepaymentsData(db.Model):
|
||||
fbn_transaction_id=data.get('fbnTransactionId'),
|
||||
account_id=data.get('accountId'),
|
||||
customer_id=data.get('customerId'),
|
||||
amount_collected=amountCollected,
|
||||
repayment_amount=repaymentAmount,
|
||||
balance=round(float(accountBalance), 2)
|
||||
amount_collected=amount_collected,
|
||||
repayment_amount=repayment_amount,
|
||||
balance=account_balance,
|
||||
)
|
||||
|
||||
db.session.add(new_data)
|
||||
db.session.commit()
|
||||
logger.info(f"data has been commited ")
|
||||
|
||||
logger.info("Repayment data committed successfully")
|
||||
return new_data
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
raise Exception(f"Error adding repayment data: {str(e)}")
|
||||
logger.error(f"Error adding repayment data: {e}")
|
||||
raise Exception(f"Error adding repayment data: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user