114 lines
3.2 KiB
Python
114 lines
3.2 KiB
Python
from uuid import UUID, uuid4
|
|
from enum import Enum
|
|
|
|
from sqlmodel import Field, SQLModel, select
|
|
|
|
|
|
class AccountBase(SQLModel):
|
|
name: str = Field(index=True)
|
|
type: str = Field(index=True)
|
|
|
|
class AccountBaseId(AccountBase):
|
|
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
|
|
|
|
class Account(AccountBaseId, table=True):
|
|
|
|
@classmethod
|
|
def create(cls, account, session):
|
|
account_db = cls.model_validate(account)
|
|
session.add(account_db)
|
|
session.commit()
|
|
session.refresh(account_db)
|
|
|
|
return account_db
|
|
|
|
@classmethod
|
|
def list(cls):
|
|
return select(Account)
|
|
|
|
@classmethod
|
|
def list_accounts(cls):
|
|
return cls.list().where(
|
|
Account.type.not_in_([v.value for v in CategoryType])
|
|
)
|
|
|
|
@classmethod
|
|
def list_categories(cls):
|
|
return cls.list().where(
|
|
Account.type.in_([v.value for v in CategoryType])
|
|
)
|
|
|
|
@classmethod
|
|
def get(cls, session, account_id):
|
|
return session.get(Account, account_id)
|
|
|
|
@classmethod
|
|
def update(cls, session, account_db, account_data):
|
|
account_db.sqlmodel_update(account_data)
|
|
session.add(account_db)
|
|
session.commit()
|
|
session.refresh(account_db)
|
|
return account_db
|
|
|
|
@classmethod
|
|
def delete(cls, session, account):
|
|
session.delete(account)
|
|
session.commit()
|
|
|
|
class AccountRead(AccountBaseId):
|
|
pass
|
|
|
|
class AccountType(Enum):
|
|
Asset = "Asset" # < Denotes a generic asset account.
|
|
Checkings = "Checkings" # < Standard checking account
|
|
Savings = "Savings" # < Typical savings account
|
|
Cash = "Cash" # < Denotes a shoe-box or pillowcase stuffed with cash
|
|
|
|
Liability = "Liability" # < Denotes a generic liability account.
|
|
CreditCard = "CreditCard" # < Credit card accounts
|
|
Loan = "Loan" # < Loan and mortgage accounts (liability)
|
|
CertificateDep = "CertificateDep" # < Certificates of Deposit
|
|
Investment = "Investment" # < Investment account
|
|
MoneyMarket = "MoneyMarket" # < Money Market Account
|
|
|
|
Currency = "Currency" # < Denotes a currency trading account.
|
|
AssetLoan = "AssetLoan" # < Denotes a loan (asset of the owner of this object)
|
|
Stock = "Stock" # < Denotes an security account as sub-account for an investment
|
|
Equity = "Equity" # < Denotes an equity account e.g. opening/closing balance
|
|
|
|
Income = "Income" # < Denotes an income account
|
|
Expense = "Expense" # < Denotes an expense account
|
|
|
|
class Asset(Enum):
|
|
Asset = "Asset"
|
|
Checkings = "Checkings"
|
|
Savings = "Savings"
|
|
Cash = "Cash"
|
|
|
|
class Liability(Enum):
|
|
Liability = "Liability"
|
|
CreditCard = "CreditCard"
|
|
Loan = "Loan"
|
|
Investment = "Investment"
|
|
|
|
class AccountWrite(AccountBase):
|
|
type: Asset | Liability = Field()
|
|
|
|
class AccountCreate(AccountWrite):
|
|
pass
|
|
|
|
class AccountUpdate(AccountWrite):
|
|
pass
|
|
|
|
class CategoryType(Enum):
|
|
Income = "Income"
|
|
Expense = "Expense"
|
|
|
|
class CategoryWrite(AccountBase):
|
|
type: CategoryType = Field()
|
|
|
|
class CategoryCreate(CategoryWrite):
|
|
pass
|
|
|
|
class CategoryUpdate(CategoryWrite):
|
|
pass |