forked from brmlab/brmbar-github
Compare commits
5 commits
master
...
test_cache
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5bc7b0e004 | ||
![]() |
96bf23662e | ||
![]() |
f922edf042 | ||
![]() |
0d2bb2eed7 | ||
![]() |
15bb03e5fe |
3 changed files with 22 additions and 22 deletions
17
brmbar3/SQL
17
brmbar3/SQL
|
@ -39,7 +39,8 @@ CREATE TABLE accounts (
|
||||||
|
|
||||||
acctype account_type NOT NULL,
|
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 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');
|
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
|
-- Note that currency information is currently not supplied; inventory items
|
||||||
-- have balances in stock amounts.
|
-- have balances in stock amounts.
|
||||||
CREATE VIEW account_balances AS
|
CREATE VIEW account_balances AS
|
||||||
SELECT ts.account AS id, accounts.name AS name, accounts.acctype AS acctype,
|
SELECT id, name, acctype, crbalance FROM accounts ORDER BY crbalance ASC;
|
||||||
-SUM(CASE WHEN ts.side = 'credit' THEN -ts.amount ELSE ts.amount END) AS crbalance
|
|
||||||
FROM transaction_splits AS ts
|
-- SELECT ts.account AS id, accounts.name AS name, accounts.acctype AS acctype,
|
||||||
LEFT JOIN accounts ON accounts.id = ts.account
|
-- -SUM(CASE WHEN ts.side = 'credit' THEN -ts.amount ELSE ts.amount END) AS crbalance
|
||||||
GROUP BY ts.account, accounts.name, accounts.acctype
|
-- FROM transaction_splits AS ts
|
||||||
ORDER BY crbalance ASC;
|
-- 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
|
-- Transaction splits in a form that's nicer to query during manual inspection
|
||||||
CREATE VIEW transaction_nicesplits AS
|
CREATE VIEW transaction_nicesplits AS
|
||||||
|
|
|
@ -45,11 +45,9 @@ class Account:
|
||||||
return cls(db, name = name, id = id, currency = currency, acctype = acctype)
|
return cls(db, name = name, id = id, currency = currency, acctype = acctype)
|
||||||
|
|
||||||
def balance(self):
|
def balance(self):
|
||||||
debit = self.db.execute_and_fetch("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'debit'])
|
crbalance = self.db.execute_and_fetch("SELECT crbalance FROM accounts WHERE id = %s", [self.id])
|
||||||
debit = debit[0] or 0
|
crbalance = crbalance[0] or 0
|
||||||
credit = self.db.execute_and_fetch("SELECT SUM(amount) FROM transaction_splits WHERE account = %s AND side = %s", [self.id, 'credit'])
|
return crbalance
|
||||||
credit = credit[0] or 0
|
|
||||||
return debit - credit
|
|
||||||
|
|
||||||
def balance_str(self):
|
def balance_str(self):
|
||||||
return self.currency.str(self.balance())
|
return self.currency.str(self.balance())
|
||||||
|
@ -63,10 +61,13 @@ class Account:
|
||||||
def credit(self, transaction, amount, memo):
|
def credit(self, transaction, amount, memo):
|
||||||
return self._transaction_split(transaction, 'credit', amount, memo)
|
return self._transaction_split(transaction, 'credit', amount, memo)
|
||||||
|
|
||||||
|
# XXX atomicita
|
||||||
def _transaction_split(self, transaction, side, amount, memo):
|
def _transaction_split(self, transaction, side, amount, memo):
|
||||||
""" Common part of credit() and debit(). """
|
""" 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("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 -%s ELSE %s END)", [side,amount,amount])
|
||||||
|
|
||||||
def add_barcode(self, barcode):
|
def add_barcode(self, barcode):
|
||||||
self.db.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()
|
self.db.commit()
|
||||||
|
|
|
@ -105,16 +105,12 @@ class Shop:
|
||||||
def credit_balance(self):
|
def credit_balance(self):
|
||||||
# We assume all debt accounts share a currency
|
# We assume all debt accounts share a currency
|
||||||
sumselect = """
|
sumselect = """
|
||||||
SELECT SUM(ts.amount)
|
SELECT SUM(crbalance)
|
||||||
FROM accounts AS a
|
FROM accounts WHERE acctype = %s
|
||||||
LEFT JOIN transaction_splits AS ts ON a.id = ts.account
|
|
||||||
WHERE a.acctype = %s AND ts.side = %s
|
|
||||||
"""
|
"""
|
||||||
cur = self.db.execute_and_fetch(sumselect, ["debt", 'debit'])
|
cur = self.db.execute_and_fetch(sumselect, ["debt"])
|
||||||
debit = cur[0] or 0
|
cur = cur[0] or 0
|
||||||
credit = self.db.execute_and_fetch(sumselect, ["debt", 'credit'])
|
return cur
|
||||||
credit = credit[0] or 0
|
|
||||||
return debit - credit
|
|
||||||
def credit_negbalance_str(self):
|
def credit_negbalance_str(self):
|
||||||
return self.currency.str(-self.credit_balance())
|
return self.currency.str(-self.credit_balance())
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue