mirror of
https://github.com/brmlab/brmbar.git
synced 2025-06-08 13:24:01 +02:00
Added caching of credit balance to accounts and replacing view account_balances.
This commit is contained in:
parent
4604cd36c3
commit
15bb03e5fe
3 changed files with 21 additions and 21 deletions
|
@ -45,11 +45,9 @@ class Account:
|
|||
return cls(db, name = name, id = id, currency = currency, acctype = acctype)
|
||||
|
||||
def balance(self):
|
||||
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
|
||||
crbalance = self.db.execute_and_fetch("SELECT crbalance FROM accounts WHERE account = %s", [self.id])
|
||||
crbalance = crbalance[0] or 0
|
||||
return crbalance
|
||||
|
||||
def balance_str(self):
|
||||
return self.currency.str(self.balance())
|
||||
|
@ -63,10 +61,13 @@ class Account:
|
|||
def credit(self, transaction, amount, memo):
|
||||
return self._transaction_split(transaction, 'credit', amount, memo)
|
||||
|
||||
# XXX atomicita
|
||||
def _transaction_split(self, transaction, side, amount, memo):
|
||||
""" Common part of credit() and debit(). """
|
||||
self.db.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("UPDATE accounts set crbalance = crbalance + (CASE WHEN %s = 'credit' THEN -amount ELSE amount END)", [side])
|
||||
|
||||
def add_barcode(self, barcode):
|
||||
self.db.execute("INSERT INTO barcodes (account, barcode) VALUES (%s, %s)", [self.id, barcode])
|
||||
self.db.commit()
|
||||
|
|
|
@ -105,16 +105,12 @@ class Shop:
|
|||
def credit_balance(self):
|
||||
# We assume all debt accounts share a currency
|
||||
sumselect = """
|
||||
SELECT SUM(ts.amount)
|
||||
FROM accounts AS a
|
||||
LEFT JOIN transaction_splits AS ts ON a.id = ts.account
|
||||
WHERE a.acctype = %s AND ts.side = %s
|
||||
SELECT SUM(crbalance)
|
||||
FROM accounts WHERE acctype = %s AND
|
||||
"""
|
||||
cur = self.db.execute_and_fetch(sumselect, ["debt", 'debit'])
|
||||
debit = cur[0] or 0
|
||||
credit = self.db.execute_and_fetch(sumselect, ["debt", 'credit'])
|
||||
credit = credit[0] or 0
|
||||
return debit - credit
|
||||
cur = self.db.execute_and_fetch(sumselect, ["debt"])
|
||||
cur = cur[0] or 0
|
||||
return cur
|
||||
def credit_negbalance_str(self):
|
||||
return self.currency.str(-self.credit_balance())
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue