[add]: Loan charge

This commit is contained in:
VivianDee
2025-04-17 15:52:14 +01:00
parent 75f71a807d
commit 57fa4d72d9
6 changed files with 93 additions and 90 deletions
+42 -32
View File
@@ -1,16 +1,14 @@
from datetime import datetime, timezone, timedelta
from app.extensions import db
from sqlalchemy.orm import relationship
from app.models.offer import Offer
class Charge(db.Model):
__tablename__ = 'charges'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
offer_id = db.Column(db.Integer, nullable=False)
code = db.Column(db.String(50), nullable=False)
amount = db.Column(db.Float, default=0.0)
offer_id = db.Column(db.String(50), nullable=False)
code = db.Column(db.String(50), nullable=False)
percent = db.Column(db.Float, default=0.0)
description = db.Column(db.Text, nullable=True)
due = db.Column(db.Integer, nullable=False)
@@ -25,61 +23,73 @@ class Charge(db.Model):
)
@classmethod
def create_charges_for_loan(cls, loan_id, transaction_id, charges, referenced_amount = 0.0):
def add_charges(cls, offer_id, charges):
"""
Create loan charges for a given loan.
Add charges to an offer.
Args:
loan_id (int): ID of the loan to associate charges with.
charges (list): A list of dictionaries with keys:
offer_id (int): ID of the offer 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 = []
now = datetime.now(timezone.utc)
if offer_id is None:
raise ValueError("offer_id cannot be None")
offer_charges = []
for charge in charges:
due_days = charge.get("due", 0)
amount = charge.get("amount", 0.0)
code = charge.get("code")
percent = charge.get("percent", 0.0)
description = charge.get("description", "")
due_days = charge.get("due", 0)
if amount == 0.0:
amount = (percent / 100.0) * referenced_amount
existing = cls.query.filter_by(offer_id=offer_id, code=code).first()
if existing:
continue
charge_obj = cls(
loan_id = loan_id,
transaction_id = transaction_id,
code = charge.get("code"),
amount = amount,
offer_id = offer_id,
code = code,
percent = percent,
description = charge.get("description", ""),
due = due_days,
due_date = now + timedelta(days=due_days)
description = description,
due = due_days
)
db.session.add(charge_obj)
loan_charges.append(charge_obj)
return loan_charges
offer_charges.append(charge_obj)
return offer_charges
@classmethod
def get_offer_charges(cls, offer_id):
"""
Get all charges for a particular offer as a dictionary
Args:
offer_id (str): The offer ID.
"""
if not offer_id:
raise ValueError("offer_id not found")
charges = cls.query.filter_by(offer_id=offer_id).all()
return charges
def to_dict(self):
return {
'id': self.id,
'loanId': self.loan_id,
'transactionId': self.transaction_id,
'offerId': self.offer_id,
'code': self.code,
'amount': self.amount,
'percent': self.percent,
'description': self.description,
'due': self.due,
'due': self.due
}
def __repr__(self):
return f"<LoanCharge {self.id} - Loan {self.loan_id} - {self.code}>"
return f"<Charge {self.id} - Offer {self.offer_id} - {self.code}>"
+5 -5
View File
@@ -45,9 +45,9 @@ class LoanCharge(db.Model):
now = datetime.now(timezone.utc)
for charge in charges:
due_days = charge.get("due", 0)
amount = charge.get("amount", 0.0)
percent = charge.get("percent", 0.0)
due_days = getattr(charge, "due", 0)
amount = getattr(charge, "amount", 0.0)
percent = getattr(charge, "percent", 0.0)
if amount == 0.0:
amount = (percent / 100.0) * referenced_amount
@@ -55,10 +55,10 @@ class LoanCharge(db.Model):
charge_obj = cls(
loan_id = loan_id,
transaction_id = transaction_id,
code = charge.get("code"),
code = getattr(charge, "code"),
amount = amount,
percent = percent,
description = charge.get("description", ""),
description = getattr(charge, "description", ""),
due = due_days,
due_date = now + timedelta(days=due_days)
)
+1
View File
@@ -1,6 +1,7 @@
from datetime import datetime, timezone
from app.extensions import db
from app.models.charge import Charge
from sqlalchemy.orm import relationship
class Offer(db.Model):
__tablename__ = 'offers'