mirror of
https://github.com/brmlab/brmbar.git
synced 2025-06-08 13:24:01 +02:00
All db operations now should go thru Database object
This commit is contained in:
parent
ffa119e7f0
commit
a4dbf3b77c
5 changed files with 128 additions and 100 deletions
|
@ -17,31 +17,25 @@ class Account:
|
|||
|
||||
@classmethod
|
||||
def load_by_barcode(cls, db, barcode):
|
||||
with closing(db.cursor()) as cur:
|
||||
cur.execute("SELECT account FROM barcodes WHERE barcode = %s", [barcode])
|
||||
res = cur.fetchone()
|
||||
if res is None:
|
||||
return None
|
||||
id = res[0]
|
||||
res = db.execute_and_fetch("SELECT account FROM barcodes WHERE barcode = %s", [barcode])
|
||||
if res is None:
|
||||
return None
|
||||
id = res[0]
|
||||
return cls.load(db, id = id)
|
||||
|
||||
@classmethod
|
||||
def load(cls, db, id = None, name = None):
|
||||
""" Constructor for existing account """
|
||||
if id is not None:
|
||||
with closing(db.cursor()) as cur:
|
||||
cur.execute("SELECT name FROM accounts WHERE id = %s", [id])
|
||||
name = cur.fetchone()[0]
|
||||
name = db.execute_and_fetch("SELECT name FROM accounts WHERE id = %s", [id])
|
||||
name = name[0]
|
||||
elif name is not None:
|
||||
with closing(db.cursor()) as cur:
|
||||
cur.execute("SELECT id FROM accounts WHERE name = %s", [name])
|
||||
id = cur.fetchone()[0]
|
||||
id = db.execute_and_fetch("SELECT id FROM accounts WHERE name = %s", [name])
|
||||
id = id[0]
|
||||
else:
|
||||
raise NameError("Account.load(): Specify either id or name")
|
||||
|
||||
with closing(db.cursor()) as cur:
|
||||
cur.execute("SELECT currency, acctype FROM accounts WHERE id = %s", [id])
|
||||
currid, acctype = cur.fetchone()
|
||||
currid, acctype = db.execute_and_fetch("SELECT currency, acctype FROM accounts WHERE id = %s", [id])
|
||||
currency = Currency.load(db, id = currid)
|
||||
|
||||
return cls(db, name = name, id = id, currency = currency, acctype = acctype)
|
||||
|
@ -49,17 +43,15 @@ class Account:
|
|||
@classmethod
|
||||
def create(cls, db, name, currency, acctype):
|
||||
""" Constructor for new account """
|
||||
with closing(db.cursor()) as cur:
|
||||
cur.execute("INSERT INTO accounts (name, currency, acctype) VALUES (%s, %s, %s) RETURNING id", [name, currency.id, acctype])
|
||||
id = cur.fetchone()[0]
|
||||
id = db.execute_and_fetch("INSERT INTO accounts (name, currency, acctype) VALUES (%s, %s, %s) RETURNING id", [name, currency.id, acctype])
|
||||
id = id[0]
|
||||
return cls(db, name = name, id = id, currency = currency, acctype = acctype)
|
||||
|
||||
def balance(self):
|
||||
with closing(self.db.cursor()) as cur:
|
||||
cur.execute("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'debit'])
|
||||
debit = cur.fetchone()[0] or 0
|
||||
cur.execute("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'credit'])
|
||||
credit = cur.fetchone()[0] or 0
|
||||
debit = self.db.execute_and_fetch("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'debit'])
|
||||
debit = debit[0] or 0
|
||||
credit = self.db.execute_and_fetch("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'credit'])
|
||||
credit = credit[0] or 0
|
||||
return debit - credit
|
||||
|
||||
def balance_str(self):
|
||||
|
@ -76,15 +68,12 @@ class Account:
|
|||
|
||||
def _transaction_split(self, transaction, side, amount, memo):
|
||||
""" Common part of credit() and debit(). """
|
||||
with closing(self.db.cursor()) as cur:
|
||||
cur.execute("INSERT INTO transaction_splits (transaction, side, account, amount, memo) VALUES (%s, %s, %s, %s, %s)", [transaction, side, self.id, amount, memo])
|
||||
self.db.execute("INSERT INTO transaction_splits (transaction, side, account, amount, memo) VALUES (%s, %s, %s, %s, %s)", [transaction, side, self.id, amount, memo])
|
||||
|
||||
def add_barcode(self, barcode):
|
||||
with closing(self.db.cursor()) as cur:
|
||||
cur.execute("INSERT INTO barcodes (account, barcode) VALUES (%s, %s)", [self.id, barcode])
|
||||
self.db.execute("INSERT INTO barcodes (account, barcode) VALUES (%s, %s)", [self.id, barcode])
|
||||
self.db.commit()
|
||||
|
||||
def rename(self, name):
|
||||
with closing(self.db.cursor()) as cur:
|
||||
cur.execute("UPDATE accounts SET name = %s WHERE id = %s", [name, self.id])
|
||||
self.db.execute("UPDATE accounts SET name = %s WHERE id = %s", [name, self.id])
|
||||
self.name = name
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue