Files
budget-forecast/api/app/account/models.py

41 lines
1.3 KiB
Python

from typing import Optional
from sqlmodel import Relationship
from account.enums import CategoryFamily, Asset, Liability, AccountFamily
from account.schemas import AccountBaseId
class Account(AccountBaseId, table=True):
parent_account: Optional["Account"] = Relationship(
back_populates="children_accounts",
sa_relationship_kwargs=dict(remote_side='Account.id')
)
children_accounts: list["Account"] = Relationship(back_populates='parent_account')
transaction_splits: list["Split"] = Relationship(back_populates='account')
def is_category(self):
return self.family in [v.value for v in CategoryFamily]
def get_child_path(self, child):
return f"{self.path}{child.name}/"
def get_root_path(self):
root = "/Categories" if self.is_category() else "/Accounts"
return f"{root}/{self.family}/{self.name}/"
def compute_path(self):
if self.parent_account is None:
self.path = self.get_root_path()
else:
self.path = self.parent_account.get_child_path(self)
return self.path
def compute_family(self):
if self.type in Asset:
self.family = AccountFamily.Asset.value
elif self.type in Liability:
self.family = AccountFamily.Liability.value
else:
self.family = self.type
return self.family