05/31 Phase 1: initial codes
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
from app.extensions import db
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class Goal(db.Model):
|
||||
__tablename__ = 'goals'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(100), nullable=False)
|
||||
description = db.Column(db.Text, nullable=True)
|
||||
target_amount = db.Column(db.Numeric(15, 2), nullable=False)
|
||||
current_amount = db.Column(db.Numeric(15, 2), default=0.00)
|
||||
target_date = db.Column(db.Date, nullable=True)
|
||||
linked_account_id = db.Column(db.Integer, db.ForeignKey('accounts.id'), nullable=True)
|
||||
color = db.Column(db.String(7), default='#10B981')
|
||||
icon = db.Column(db.String(50), default='bi-piggy-bank')
|
||||
is_completed = db.Column(db.Boolean, default=False)
|
||||
completed_at = db.Column(db.DateTime, nullable=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
linked_account = db.relationship('Account')
|
||||
contributions = db.relationship('GoalContribution', back_populates='goal',
|
||||
lazy='dynamic', cascade='all, delete-orphan')
|
||||
|
||||
@property
|
||||
def progress_percent(self):
|
||||
if self.target_amount <= 0:
|
||||
return 0
|
||||
pct = (float(self.current_amount) / float(self.target_amount)) * 100
|
||||
return min(round(pct, 1), 100.0)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Goal {self.name}>'
|
||||
|
||||
|
||||
class GoalContribution(db.Model):
|
||||
__tablename__ = 'goal_contributions'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
goal_id = db.Column(db.Integer, db.ForeignKey('goals.id'), nullable=False)
|
||||
amount = db.Column(db.Numeric(15, 2), nullable=False)
|
||||
notes = db.Column(db.String(255), nullable=True)
|
||||
date = db.Column(db.Date, nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
goal = db.relationship('Goal', back_populates='contributions')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<GoalContribution goal={self.goal_id} amount={self.amount}>'
|
||||
Reference in New Issue
Block a user