From 15bb03e5feb8511b36d47d977e56fb95a716076c Mon Sep 17 00:00:00 2001 From: niekt0 Date: Sat, 31 Jan 2015 02:39:07 +0100 Subject: [PATCH 1/5] Added caching of credit balance to accounts and replacing view account_balances. --- brmbar3/SQL | 17 ++++++++++------- brmbar3/brmbar/Account.py | 11 ++++++----- brmbar3/brmbar/Shop.py | 14 +++++--------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/brmbar3/SQL b/brmbar3/SQL index 7e640a6..6fec1d2 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -39,7 +39,8 @@ CREATE TABLE accounts ( acctype account_type NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE + active BOOLEAN NOT NULL DEFAULT TRUE, + crbalance DECIMAL(12,2) NOT NULL ); INSERT INTO accounts (name, currency, acctype) VALUES ('BrmBar Cash', (SELECT id FROM currencies WHERE name='Kč'), 'cash'); INSERT INTO accounts (name, currency, acctype) VALUES ('BrmBar Profits', (SELECT id FROM currencies WHERE name='Kč'), 'income'); @@ -92,12 +93,14 @@ CREATE TABLE transaction_splits ( -- Note that currency information is currently not supplied; inventory items -- have balances in stock amounts. CREATE VIEW account_balances AS - SELECT ts.account AS id, accounts.name AS name, accounts.acctype AS acctype, - -SUM(CASE WHEN ts.side = 'credit' THEN -ts.amount ELSE ts.amount END) AS crbalance - FROM transaction_splits AS ts - LEFT JOIN accounts ON accounts.id = ts.account - GROUP BY ts.account, accounts.name, accounts.acctype - ORDER BY crbalance ASC; + SELECT id, name, acctype, crbalance FROM accounts ORDER BY crbalance ASC; + +-- SELECT ts.account AS id, accounts.name AS name, accounts.acctype AS acctype, +-- -SUM(CASE WHEN ts.side = 'credit' THEN -ts.amount ELSE ts.amount END) AS crbalance +-- FROM transaction_splits AS ts +-- LEFT JOIN accounts ON accounts.id = ts.account +-- GROUP BY ts.account, accounts.name, accounts.acctype +-- ORDER BY crbalance ASC; -- Transaction splits in a form that's nicer to query during manual inspection CREATE VIEW transaction_nicesplits AS diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index 29dd79e..df5dcd6 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -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() diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index bf74fef..1adcdbf 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -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()) From 0d2bb2eed70a760bfd8baa1504201f3cad87a6b4 Mon Sep 17 00:00:00 2001 From: niekt0 Date: Sat, 31 Jan 2015 03:38:08 +0100 Subject: [PATCH 2/5] sign fix (credit vs. debit) --- brmbar3/brmbar/Account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index df5dcd6..b22c041 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -66,7 +66,7 @@ class Account: """ 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]) + 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]) From f922edf04237b1e5d34d958831a19488edae881c Mon Sep 17 00:00:00 2001 From: niekt0 Date: Sat, 31 Jan 2015 03:45:50 +0100 Subject: [PATCH 3/5] tab vs. spaces unification (python:( --- brmbar3/brmbar/Account.py | 2 +- brmbar3/brmbar/Shop.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index b22c041..d474e6c 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -66,7 +66,7 @@ class Account: """ 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]) + 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]) diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 1adcdbf..a170fcd 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -135,7 +135,7 @@ class Shop: """list all accounts (people or items, as per acctype)""" accts = [] cur = self.db.execute_and_fetchall("SELECT id FROM accounts WHERE acctype = %s AND name ILIKE %s ORDER BY name ASC", [acctype, like_str]) - #FIXME: sanitize input like_str ^ + #FIXME: sanitize input like_str ^ for inventory in cur: accts += [ Account.load(self.db, id = inventory[0]) ] return accts From 96bf23662e9f4f5bd10e63235470d7200cef5bc3 Mon Sep 17 00:00:00 2001 From: niekt0 Date: Sat, 31 Jan 2015 04:11:28 +0100 Subject: [PATCH 4/5] fixes from runtime --- brmbar3/brmbar/Account.py | 4 ++-- brmbar3/brmbar/Shop.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index d474e6c..3186dcc 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -45,7 +45,7 @@ class Account: return cls(db, name = name, id = id, currency = currency, acctype = acctype) def balance(self): - crbalance = self.db.execute_and_fetch("SELECT crbalance FROM accounts WHERE account = %s", [self.id]) + crbalance = self.db.execute_and_fetch("SELECT crbalance FROM accounts WHERE id = %s", [self.id]) crbalance = crbalance[0] or 0 return crbalance @@ -66,7 +66,7 @@ class Account: """ 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]) + 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]) diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index a170fcd..b5c61dc 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -106,7 +106,7 @@ class Shop: # We assume all debt accounts share a currency sumselect = """ SELECT SUM(crbalance) - FROM accounts WHERE acctype = %s AND + FROM accounts WHERE acctype = %s """ cur = self.db.execute_and_fetch(sumselect, ["debt"]) cur = cur[0] or 0 From 5bc7b0e004fd3bff8ea5f3cd25f48e1ac783be26 Mon Sep 17 00:00:00 2001 From: niekt0 Date: Sat, 31 Jan 2015 04:24:05 +0100 Subject: [PATCH 5/5] runtime sql error fixed --- brmbar3/brmbar/Account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index 3186dcc..0c6448f 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -66,7 +66,7 @@ class Account: """ 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]) + self.db.execute("UPDATE accounts set crbalance = crbalance + (CASE WHEN %s = 'credit' THEN -%s ELSE %s END)", [side,amount,amount]) def add_barcode(self, barcode): self.db.execute("INSERT INTO barcodes (account, barcode) VALUES (%s, %s)", [self.id, barcode])