forked from brmlab/brmbar-github
68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
from .Currency import Currency
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Account:
|
|
"""BrmBar Account
|
|
|
|
Both users and items are accounts. So is the money box, etc.
|
|
Each account has a currency."""
|
|
|
|
def __init__(self, db, id, name, currency, acctype):
|
|
self.db = db
|
|
self.id = id
|
|
self.name = name
|
|
self.currency = currency
|
|
self.acctype = acctype
|
|
|
|
@classmethod
|
|
def load_by_barcode(cls, db, barcode):
|
|
logger.debug("load_by_barcode: '%s'", barcode)
|
|
account_id, account_name, account_acctype, currency_id, currency_name = db_execute_and_fetch(
|
|
"SELECT account_id, account_name, account_acctype, currency_id, currency_name
|
|
FROM public.account_class_initialization_data('by_barcode', NULL, %s)",
|
|
[barcode])
|
|
currency = Currency(db, currency_id, currency_name)
|
|
return cls(db, account_id, account_name, currency, account_acctype)
|
|
|
|
@classmethod
|
|
def load(cls, db, id=None):
|
|
"""Constructor for existing account"""
|
|
account_id, account_name, account_acctype, currency_id, currency_name = db_execute_and_fetch(
|
|
"SELECT account_id, account_name, account_acctype, currency_id, currency_name
|
|
FROM public.account_class_initialization_data('by_id', %s, NULL)",
|
|
[id])
|
|
currency = Currency(db, currency_id, currency_name)
|
|
return cls(db, account_id, account_name, currency, account_acctype)
|
|
|
|
@classmethod
|
|
def create(cls, db, name, currency, acctype):
|
|
"""Constructor for new account"""
|
|
id = db.execute_and_fetch(
|
|
"SELECT public.create_account(%s, %s, %s)", [name, currency.id, acctype]
|
|
)
|
|
return cls(db, name=name, id=id, currency=currency, acctype=acctype)
|
|
|
|
def balance(self):
|
|
bal = self.db.execute_and_fetch(
|
|
"SELECT public.compute_account_balance(%s)", [self.id]
|
|
)[0]
|
|
return bal
|
|
|
|
def balance_str(self):
|
|
return self.currency.str(self.balance())
|
|
|
|
def negbalance_str(self):
|
|
return self.currency.str(-self.balance())
|
|
|
|
def add_barcode(self, barcode):
|
|
self.db.execute(
|
|
"SELECT public.add_barcode_to_account(%s, %s)", [self.id, barcode]
|
|
)
|
|
self.db.commit()
|
|
|
|
def rename(self, name):
|
|
self.db.execute("SELECT public.rename_account(%s, %s)", [self.id, name])
|
|
self.name = name
|