You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

53 lines
2.1 KiB

from finance.models import Account, Payee, Category, CardStatement, InstallmentPlan
from .base import DomainError, lock, money, save, write
def normalized_name(value):
if not isinstance(value, str) or not value.strip():
raise DomainError("invalid_name")
return value.strip()
@write("create_account")
def create_account(*, name, kind, currency="BRL"):
obj = save(Account(name=normalized_name(name), kind=kind, currency=currency))
return obj, ["name", "kind", "currency"]
@write("create_payee")
def create_payee(*, name):
return save(Payee(name=normalized_name(name))), ["name"]
@write("create_category")
def create_category(*, name, kind):
return save(Category(name=normalized_name(name), kind=kind)), ["name", "kind"]
@write("archive")
def archive(entity):
if type(entity) not in (Account, Payee, Category):
raise DomainError("invalid_reference")
obj = lock(type(entity), entity.pk)
obj.archived = True
return save(obj), ["archived"]
@write("create_statement")
def create_statement(*, account_id, period_start, period_end, due_date, statement_balance=None):
account = lock(Account, account_id, active=True)
if account.kind != "CREDIT_CARD":
raise DomainError("statement_requires_card")
balance = None if statement_balance is None else money(statement_balance, allow_zero=True)
obj = save(CardStatement(account=account, period_start=period_start, period_end=period_end,
due_date=due_date, statement_balance=balance))
return obj, ["account", "period_start", "period_end", "due_date", "statement_balance"]
@write("create_plan")
def create_plan(*, account_id, description, total_installments):
account = lock(Account, account_id, active=True)
if not isinstance(total_installments, int) or isinstance(total_installments, bool) or total_installments < 1:
raise DomainError("invalid_installment_count")
obj = save(InstallmentPlan(account=account, description=description, total_installments=total_installments))
return obj, ["account", "description", "total_installments"]