[add]: loan charges and offers. Fix RACCheck

This commit is contained in:
VivianDee
2025-04-16 21:36:26 +01:00
parent 359621dc9d
commit 93ed8b3d17
12 changed files with 161 additions and 71 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ class Account(db.Model):
return False
if account.lien_amount > 0:
return False
return True
return account
def __repr__(self):
return f'<Account {self.id}>'
+1 -1
View File
@@ -32,7 +32,7 @@ class Customer(db.Model):
customer = cls.query.filter_by(id=customer_id).first()
if not customer:
return False
return True
return customer
@classmethod
def create_customer(cls, id, msisdn, country_code, account_id, account_type='savings'):
+3 -3
View File
@@ -48,8 +48,8 @@ class Loan(db.Model):
def create_loan(cls, customer_id, account_id, offer_id, product_id, initial_loan_amount, collection_type, transaction_id, status='pending'):
# Check if customer exists
is_valid = Customer.is_valid_customer(customer_id)
if not is_valid:
customer = Customer.is_valid_customer(customer_id)
if not customer:
raise ValueError("Customer does not exist")
now = datetime.now(timezone.utc)
@@ -67,7 +67,7 @@ class Loan(db.Model):
due_date=now,
status = status
)
try:
db.session.add(loan)
except IntegrityError as err:
+32 -2
View File
@@ -8,7 +8,6 @@ class LoanCharge(db.Model):
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)
@@ -24,7 +23,38 @@ class LoanCharge(db.Model):
back_populates="loan_charges",
)
@classmethod
def create_charges_for_loan(cls, loan_id, charges):
"""
Create loan charges for a given loan.
Args:
loan_id (int): ID of the loan to associate charges with.
charges (list): A list of dictionaries with keys:
code (str), amount (float), percent (float), description (str), due (int)
"""
if not charges or not isinstance(charges, list):
raise ValueError("Charges must be a non-empty list of dictionaries")
if loan_id is None:
raise ValueError("loan_id cannot be None")
loan_charges = []
for charge in charges:
charge_obj = cls(
loan_id=loan_id,
code=charge.get("code"),
amount=charge.get("amount", 0.0),
percent=charge.get("percent", 0.0),
description=charge.get("description", ""),
due=charge.get("due", 0)
)
db.session.add(charge_obj)
loan_charges.append(charge_obj)
return loan_charges
def to_dict(self):
return {
+20
View File
@@ -12,6 +12,26 @@ class Offer(db.Model):
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))
@classmethod
def get_all_offers(cls):
"""
Return all offers in dictionary format.
"""
offers = cls.query.all()
if not offers:
raise ValueError(f"No available offers")
return offers
@classmethod
def is_valid_offer(cls, offer_id):
offer = cls.query.filter_by(id=str(offer_id)).first()
if not offer:
return False
return offer
def to_dict(self):
return {
"offerId": self.id,