forked from DigiFi/digifi-BankToProductCore
[add]: Charges
This commit is contained in:
@@ -5,6 +5,7 @@ from .transaction import Transaction
|
||||
from .repayment import Repayment
|
||||
from .loan_charge import LoanCharge
|
||||
from .offer import Offer
|
||||
from .charge import Charge
|
||||
|
||||
|
||||
__all__ = ['Customer', 'Account', 'Loan', 'Transaction', 'Repayment', 'LoanCharge', 'Offer']
|
||||
__all__ = ['Customer', 'Account', 'Loan', 'Transaction', 'Repayment', 'LoanCharge', 'Offer', 'Charge']
|
||||
@@ -0,0 +1,85 @@
|
||||
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)
|
||||
percent = db.Column(db.Float, default=0.0)
|
||||
description = db.Column(db.Text, nullable=True)
|
||||
due = db.Column(db.Integer, nullable=False)
|
||||
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))
|
||||
|
||||
offer = relationship(
|
||||
"Offer",
|
||||
primaryjoin="Charge.offer_id == Offer.id",
|
||||
foreign_keys=[offer_id],
|
||||
back_populates="charges",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def create_charges_for_loan(cls, loan_id, transaction_id, charges, referenced_amount = 0.0):
|
||||
"""
|
||||
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 = []
|
||||
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)
|
||||
|
||||
if amount == 0.0:
|
||||
amount = (percent / 100.0) * referenced_amount
|
||||
|
||||
charge_obj = cls(
|
||||
loan_id = loan_id,
|
||||
transaction_id = transaction_id,
|
||||
code = charge.get("code"),
|
||||
amount = amount,
|
||||
percent = percent,
|
||||
description = charge.get("description", ""),
|
||||
due = due_days,
|
||||
due_date = now + timedelta(days=due_days)
|
||||
)
|
||||
|
||||
db.session.add(charge_obj)
|
||||
loan_charges.append(charge_obj)
|
||||
|
||||
return loan_charges
|
||||
|
||||
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'loanId': self.loan_id,
|
||||
'transactionId': self.transaction_id,
|
||||
'code': self.code,
|
||||
'amount': self.amount,
|
||||
'percent': self.percent,
|
||||
'description': self.description,
|
||||
'due': self.due,
|
||||
}
|
||||
|
||||
def __repr__(self):
|
||||
return f"<LoanCharge {self.id} - Loan {self.loan_id} - {self.code}>"
|
||||
@@ -1,5 +1,6 @@
|
||||
from datetime import datetime, timezone
|
||||
from app.extensions import db
|
||||
from app.models.charge import Charge
|
||||
|
||||
class Offer(db.Model):
|
||||
__tablename__ = 'offers'
|
||||
@@ -12,6 +13,13 @@ 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))
|
||||
|
||||
charges = relationship(
|
||||
"Charge",
|
||||
primaryjoin="Offer.id == Charge.offer_id",
|
||||
foreign_keys="Charge.offer_id",
|
||||
back_populates="offer",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def get_all_offers(cls):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user