[add]: Loan table extention

This commit is contained in:
VivianDee
2025-04-24 18:29:38 +01:00
parent 7d691db7a5
commit c895cc36e0
5 changed files with 165 additions and 66 deletions
+72
View File
@@ -60,3 +60,75 @@ class BaseService:
def async_send_to_kafka(cls, loan_data, request_id, topic):
KafkaIntegration.send_loan_request(loan_data = loan_data, request_id = request_id, topic = topic)
KafkaIntegration.flush()
@classmethod
def calculate_charges(cls, offer, amount):
"""
Calculates and returns the charges for the given offer and amount.
Args:
offer (Offer): The offer object that contains the charges.
amount (float): The requested loan amount.
Returns:
dict: A dictionary containing the calculated charges.
"""
if not offer or not offer.charges:
logger.error(f"No charges found for offer ID {offer.id}")
return {"error": "No charges found for the offer"}
loan_charges = offer.charges
interest = cls.get_charge_detail(loan_charges, "INTEREST", amount)
management = cls.get_charge_detail(loan_charges, "MGTFEE", amount)
insurance = cls.get_charge_detail(loan_charges, "INSURANCE", amount)
vat = cls.get_charge_detail(loan_charges, "VAT", amount)
# Up-front payment: (principal + only those fees due immediately i.e due_days == 0)
upfront_payment = amount + sum(
amount * charge.percent / 100
for charge in loan_charges
if charge.due == 0
)
# Total amount: (principal + all fees)
total_amount = amount + sum(
amount * charge.percent / 100
for charge in loan_charges
)
# Calculate the installment amount
tenor = offer.tenor
installment_amount = total_amount / tenor
return {
"interest": interest,
"management": management,
"insurance": insurance,
"vat": vat,
"upfront_payment": upfront_payment,
"total_amount": total_amount,
"installment_amount": installment_amount
}
@classmethod
def get_charge_detail(cls, charges, code, amount):
"""
Get details for a specific charge code from a list of loan charges.
Returns default values if not found.
"""
for charge in charges:
if charge.code == code:
return {
"rate": charge.percent,
"fee": amount * charge.percent / 100,
"due_days": charge.due,
}
return {"rate": 0, "fee": 0, "due_days": 0}
+15 -2
View File
@@ -36,6 +36,8 @@ class ProvideLoanService(BaseService):
collection_type = validated_data.get('collectionType')
transaction_id = validated_data.get('transactionId')
offer_id = validated_data.get('offerId')
amount = validated_data.get("requestedAmount")
product_id = validated_data.get("productId")
customer = Customer.is_valid_customer(customer_id)
@@ -48,8 +50,7 @@ class ProvideLoanService(BaseService):
return jsonify({
"message": "Invalid Offer."
}), 400
# Log Transaction
transaction = ProvideLoanService.log_transaction(validated_data=validated_data)
@@ -58,6 +59,15 @@ class ProvideLoanService(BaseService):
return jsonify({
"message": "Failed to log transaction."
}), 400
db.session.flush()
charges = ProvideLoanService.calculate_charges(offer, amount)
upfront_fee = charges["upfront_payment"]
repayment_amount = charges["total_amount"]
installment_amount = charges["installment_amount"]
# Save the loan details
@@ -69,6 +79,9 @@ class ProvideLoanService(BaseService):
collection_type = collection_type,
transaction_id = validated_data.get('transactionId'),
initial_loan_amount = validated_data.get('requestedAmount'),
upfront_fee = upfront_fee,
repayment_amount = repayment_amount,
installment_amount = installment_amount,
status= LoanStatus.ACTIVE
)
+9 -54
View File
@@ -51,38 +51,16 @@ class SelectOfferService(BaseService):
# Get the offer by product ID
offer = Offer.get_offer_by_product_id(product_id)
if not offer:
logger.error(f"Offer with product ID {product_id} not found")
return jsonify({"message": "Offer not found"}), 404
# Get the loan charges for the offer
loan_charges = offer.charges
if not loan_charges:
logger.error(f"No charges found for offer ID {offer.id}")
return jsonify({"message": "No charges found for the offer"}), 404
logger.error(f"{loan_charges}")
db.session.flush()
interest = SelectOfferService.get_charge_detail(loan_charges, "INTEREST", amount)
management = SelectOfferService.get_charge_detail(loan_charges, "MGTFEE", amount)
insurance = SelectOfferService.get_charge_detail(loan_charges, "INSURANCE", amount)
vat = SelectOfferService.get_charge_detail(loan_charges, "VAT", amount)
# Up-front payment: (principal + only those fees due immediately i.e due_days == 0)
upfront_payment = amount + sum(
amount * charge.percent / 100
for charge in loan_charges
if charge.due == 0
)
# Total amount (principal + all fees)
total_amount = amount + sum(
amount * charge.percent / 100
for charge in loan_charges
)
charges = SelectOfferService.calculate_charges(offer, amount)
upfront_payment = charges["upfront_payment"]
total_amount = charges["total_amount"]
installment_amount = charges["installment_amount"]
interest = charges["interest"]
management = charges["management"]
insurance = charges["insurance"]
vat = charges["vat"]
# Calculate the repayment dates
@@ -97,8 +75,6 @@ class SelectOfferService(BaseService):
for i in range(months)
]
# Calculate the installment amount
installment_amount = total_amount / tenor
offers = [
@@ -150,25 +126,4 @@ class SelectOfferService(BaseService):
logger.error(f"An error occurred: {str(e)}", exc_info=True)
db.session.rollback()
return jsonify({"message": "Internal Server Error"}), 500
@staticmethod
def get_charge_detail(charges, code, amount):
"""
Get details for a specific charge code from a list of loan charges.
Returns default values if not found.
"""
for charge in charges:
if charge.code == code:
return {
"rate": charge.percent,
"fee": amount * charge.percent / 100,
"due_days": charge.due,
}
return {"rate": 0, "fee": 0, "due_days": 0}