From ae3ad22d5920adc5f9653cbebb5167a5a1bf67ea Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Mon, 29 Oct 2012 15:09:15 +0100 Subject: [PATCH 001/112] StockMgmt: Rename button Add Item -> New Item --- brmbar3/brmbar-gui-qt4/StockMgmt.qml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brmbar3/brmbar-gui-qt4/StockMgmt.qml b/brmbar3/brmbar-gui-qt4/StockMgmt.qml index 68a6838..394c7eb 100644 --- a/brmbar3/brmbar-gui-qt4/StockMgmt.qml +++ b/brmbar3/brmbar-gui-qt4/StockMgmt.qml @@ -81,11 +81,11 @@ Item { } BarButton { - id: add_item + id: new_item x: 65 y: 582 width: 360 - text: "Add Item" + text: "New Item" fontSize: 0.768 * 60 onButtonClick: { loadPage("ItemEdit", { dbid: "" }) From 5f4c4cb5ca8341e1c35b283a0fba20c038bdbb2c Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 25 Nov 2012 04:01:57 +0100 Subject: [PATCH 002/112] brmbar-cli.py -> brmbar-tui.py, since it's not really commandline interface --- brmbar3/{brmbar-cli.py => brmbar-tui.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename brmbar3/{brmbar-cli.py => brmbar-tui.py} (100%) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-tui.py similarity index 100% rename from brmbar3/brmbar-cli.py rename to brmbar3/brmbar-tui.py From ff915072a0d5d4ab85639179c7f7d5b72e26e013 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 25 Nov 2012 16:30:01 +0100 Subject: [PATCH 003/112] brmbar-tui.py: Use brmbar.Database instead of psycopg2 directly --- brmbar3/brmbar-tui.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/brmbar3/brmbar-tui.py b/brmbar3/brmbar-tui.py index bf619b3..00bdf44 100755 --- a/brmbar3/brmbar-tui.py +++ b/brmbar3/brmbar-tui.py @@ -1,11 +1,12 @@ #!/usr/bin/python3 import sys -import psycopg2 + +from brmbar import Database import brmbar -db = psycopg2.connect("dbname=brmbar") +db = Database.Database("dbname=brmbar") shop = brmbar.Shop.new_with_defaults(db) currency = shop.currency From 1ba8099e8e3f0fd8f586cb6ba83833551306d5f7 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 25 Nov 2012 18:49:53 +0100 Subject: [PATCH 004/112] brmbar-cli.py: New command-line interface For remote inspection of brmbar status and to do things too complicated and unusual for the GUI. --- brmbar3/brmbar-cli.py | 151 +++++++++++++++++++++++++++++++++++++++++ brmbar3/brmbar/Shop.py | 14 ++++ 2 files changed, 165 insertions(+) create mode 100755 brmbar3/brmbar-cli.py diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py new file mode 100755 index 0000000..df6b078 --- /dev/null +++ b/brmbar3/brmbar-cli.py @@ -0,0 +1,151 @@ +#!/usr/bin/python3 + +import sys + +from brmbar import Database + +import brmbar + + +def help(): + print("""BrmBar v3 (c) Petr Baudis 2012 + +Usage: brmbar-cli.py COMMAND ARGS... + +1. Commands pertaining the standard operation + showcredit USER + changecredit USER +-AMT + sellitem USER ITEM +-AMT + You can use negative AMT to undo a sale. + userinfo USER + iteminfo ITEM + +2. Management commands + listusers + List all user accounts in the system. + listitems + List all item accounts in the system. + stats + A set of various balances as shown in the Management + screen of the GUI. +! changestock ITEM1 +-AMT_ITEM1 ITEM2 +-AMT_ITEM2 ... + Create a custom transaction that will change balance + of a variety of items at once, and either deduce + buy price (+amt) or add sell price (-amt) to the + cash balance for all of these items. This is useful + e.g. after events where stock is sold offline or when + doing an inventory recount. + If cash balance discrepancy is discovered as well + during the inventory recounting, first do changestock, + then compare actual and nominal cash balance again, + then issue changecash command if necessary. +! changecash +-AMT + Create a custom transaction that updates nominal cash + balance based on the actual cash balance counted + in the cash box. If you found more money than expected, + use +amt, if you found less money than expected, + use -amt. + +USER and ITEM may be barcodes or account ids. AMT may be +both positive and negative amount (big difference to other +user interfaces; you can e.g. undo a sale!). + +For users, you can use their name as USER as their username +is also the barcode. For items, use listitems command first +to find out the item id. + +Commands prefixed with ! are not implemented yet.""") + sys.exit(1) + + +def load_acct(inp): + acct = None + if inp.isdigit(): + acct = brmbar.Account.load(db, id = inp) + if acct is None: + acct = brmbar.Account.load_by_barcode(db, inp) + if acct is None: + print("Cannot map account " + inp, file=sys.stderr) + exit(1) + return acct + +def load_user(inp): + acct = load_acct(inp) + if acct.acctype != "debt": + print("Bad account " + inp + " type " + acct.acctype, file=sys.stderr) + exit(1) + return acct + +def load_item(inp): + acct = load_acct(inp) + if acct.acctype != "inventory": + print("Bad account " + inp + " type " + acct.acctype, file=sys.stderr) + exit(1) + return acct + + +db = Database.Database("dbname=brmbar") +shop = brmbar.Shop.new_with_defaults(db) +currency = shop.currency + +if len(sys.argv) <= 1: + help() + + +if sys.argv[1] == "showcredit": + acct = load_user(sys.argv[2]) + print("{}: {}".format(acct.name, acct.negbalance_str())) + +elif sys.argv[1] == "changecredit": + acct = load_user(sys.argv[2]) + amt = int(sys.argv[3]) + if amt > 0: + shop.add_credit(credit = amt, user = acct) + elif amt < 0: + shop.withdraw_credit(credit = -amt, user = acct) + print("{}: {}".format(acct.name, acct.negbalance_str())) + +elif sys.argv[1] == "sellitem": + uacct = load_user(sys.argv[2]) + iacct = load_item(sys.argv[3]) + amt = int(sys.argv[4]) + if amt > 0: + shop.sell(item = iacct, user = uacct, amount = amt) + elif amt < 0: + shop.undo_sale(item = iacct, user = uacct, amount = -amt) + print("{}: {}".format(uacct.name, uacct.negbalance_str())) + print("{}: {}".format(iacct.name, iacct.balance_str())) + +elif sys.argv[1] == "userinfo": + acct = load_user(sys.argv[2]) + print("{} (id {}): {}".format(acct.name, acct.id, acct.negbalance_str())) + + res = db.execute_and_fetchall("SELECT barcode FROM barcodes WHERE account = %s", [acct.id]) + print("Barcodes: " + ", ".join(map((lambda r: r[0]), res))) + +elif sys.argv[1] == "iteminfo": + acct = load_item(sys.argv[2]) + print("{} (id {}): {} pcs".format(acct.name, acct.id, acct.balance())) + + (buy, sell) = acct.currency.rates(currency) + print("Buy: " + currency.str(buy) + " Sell: " + currency.str(sell)); + + res = db.execute_and_fetchall("SELECT barcode FROM barcodes WHERE account = %s", [acct.id]) + print("Barcodes: " + ", ".join(map((lambda r: r[0]), res))) + +elif sys.argv[1] == "listusers": + for acct in shop.account_list("debt"): + print("{}\t{}\t{}".format(acct.name, acct.id, acct.negbalance_str())) + +elif sys.argv[1] == "listitems": + for acct in shop.account_list("inventory"): + print("{}\t{}\t{} pcs".format(acct.name, acct.id, acct.balance())) + +elif sys.argv[1] == "stats": + print("Cash: {}".format(shop.cash.balance_str())) + print("Profit: {}".format(shop.profits.balance_str())) + print("Credit: {}".format(shop.credit_negbalance_str())) + print("Inventory: {}".format(shop.inventory_balance_str())) + +else: + help() diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 1623cac..7f5c5a1 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -48,6 +48,20 @@ class Shop: return cost + def undo_sale(self, item, user, amount = 1): + # Undo sale; rarely needed + (buy, sell) = item.currency.rates(self.currency) + cost = amount * sell + profit = amount * (sell - buy) + + transaction = self._transaction(responsible = user, description = "BrmBar sale UNDO of {}x {} to {}".format(amount, item.name, user.name)) + item.debit(transaction, amount, user.name + " (sale undo)") + user.credit(transaction, cost, item.name + " (sale undo)") + self.profits.credit(transaction, profit, "Margin repaid on " + item.name) + self.db.commit() + + return cost + def add_credit(self, credit, user): transaction = self._transaction(responsible = user, description = "BrmBar credit replenishment for " + user.name) self.cash.debit(transaction, credit, user.name) From 3e8f44e5e073e0a12f0dfbc4683241d1aacaeec1 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 31 Jan 2013 18:12:14 +0100 Subject: [PATCH 005/112] UI cleanup --- brmbar3/brmbar-gui-qt4/BarButton.qml | 20 +++++- brmbar3/brmbar-gui-qt4/BarClock.qml | 5 +- brmbar3/brmbar-gui-qt4/BarScrollBar.qml | 6 +- brmbar3/brmbar-gui-qt4/BarTextHint.qml | 5 +- brmbar3/brmbar-gui-qt4/BarcodeInput.qml | 1 - brmbar3/brmbar-gui-qt4/BasePage.qml | 2 - brmbar3/brmbar-gui-qt4/ChargeCredit.qml | 10 ++- brmbar3/brmbar-gui-qt4/ItemEdit.qml | 66 +++++++++---------- brmbar3/brmbar-gui-qt4/ItemInfo.qml | 10 ++- brmbar3/brmbar-gui-qt4/MainPage.qml | 2 - brmbar3/brmbar-gui-qt4/Management.qml | 2 - brmbar3/brmbar-gui-qt4/Receipt.qml | 6 +- brmbar3/brmbar-gui-qt4/StockMgmt.qml | 2 - brmbar3/brmbar-gui-qt4/UserInfo.qml | 9 ++- brmbar3/brmbar-gui-qt4/UserMgmt.qml | 4 +- brmbar3/brmbar-gui-qt4/Withdraw.qml | 8 +-- .../brmbar-gui-qt4.qmlproject.user | 4 +- brmbar3/brmbar-gui-qt4/main.qml | 7 +- 18 files changed, 79 insertions(+), 90 deletions(-) diff --git a/brmbar3/brmbar-gui-qt4/BarButton.qml b/brmbar3/brmbar-gui-qt4/BarButton.qml index b5d8a30..48971ff 100644 --- a/brmbar3/brmbar-gui-qt4/BarButton.qml +++ b/brmbar3/brmbar-gui-qt4/BarButton.qml @@ -1,16 +1,30 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 Rectangle { id: rectangle1 width: 240 height: 83 - color: "#000000" + gradient: Gradient { + GradientStop { + position: 0 + color: "#888888" + } + + GradientStop { + position: 0.5 + color: "#000000" + } + + GradientStop { + position: 1 + color: "#888888" + } + } border.color: btnColor property string text: "Button" property int fontSize: 0.768 * 60 - property variant btnColor: "#aaaaaa" + property string btnColor: "#aaaaaa" signal buttonClick onButtonClick: { /* Supplied by component user. */ } diff --git a/brmbar3/brmbar-gui-qt4/BarClock.qml b/brmbar3/brmbar-gui-qt4/BarClock.qml index c348457..a444a2e 100644 --- a/brmbar3/brmbar-gui-qt4/BarClock.qml +++ b/brmbar3/brmbar-gui-qt4/BarClock.qml @@ -1,4 +1,3 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 Rectangle { @@ -6,8 +5,8 @@ Rectangle { width: 320 height: 65 property variant now: new Date() - property variant textColor: "#000000" - property variant textSize: 0.768 * 16 + property string textColor: "#000000" + property real textSize: 0.768 * 16 Timer { id: clockUpdater interval: 1000 // update clock every second diff --git a/brmbar3/brmbar-gui-qt4/BarScrollBar.qml b/brmbar3/brmbar-gui-qt4/BarScrollBar.qml index d93a9d1..ec9c5a7 100644 --- a/brmbar3/brmbar-gui-qt4/BarScrollBar.qml +++ b/brmbar3/brmbar-gui-qt4/BarScrollBar.qml @@ -10,9 +10,9 @@ Rectangle { property int scrollbarWidth: 20 - property variant color: "white" - property variant baseOpacityOff: 0.4 - property variant baseOpacityOn: 0.9 + property string color: "white" + property real baseOpacityOff: 0.4 + property real baseOpacityOn: 0.9 radius: vertical ? width/2 : height/2 diff --git a/brmbar3/brmbar-gui-qt4/BarTextHint.qml b/brmbar3/brmbar-gui-qt4/BarTextHint.qml index 777daf5..6720a90 100644 --- a/brmbar3/brmbar-gui-qt4/BarTextHint.qml +++ b/brmbar3/brmbar-gui-qt4/BarTextHint.qml @@ -1,4 +1,3 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 Item { @@ -6,8 +5,8 @@ Item { width: 894 height: 80 - property variant hint_goal: "" - property variant hint_action: "" + property string hint_goal: "" + property string hint_action: "" Text { id: text1 diff --git a/brmbar3/brmbar-gui-qt4/BarcodeInput.qml b/brmbar3/brmbar-gui-qt4/BarcodeInput.qml index 9aa069a..851f8c0 100644 --- a/brmbar3/brmbar-gui-qt4/BarcodeInput.qml +++ b/brmbar3/brmbar-gui-qt4/BarcodeInput.qml @@ -1,4 +1,3 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 TextInput { diff --git a/brmbar3/brmbar-gui-qt4/BasePage.qml b/brmbar3/brmbar-gui-qt4/BasePage.qml index 952a821..d5db8a4 100644 --- a/brmbar3/brmbar-gui-qt4/BasePage.qml +++ b/brmbar3/brmbar-gui-qt4/BasePage.qml @@ -1,6 +1,4 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Rectangle { width: 1024 diff --git a/brmbar3/brmbar-gui-qt4/ChargeCredit.qml b/brmbar3/brmbar-gui-qt4/ChargeCredit.qml index 07f2bdc..7dc3084 100644 --- a/brmbar3/brmbar-gui-qt4/ChargeCredit.qml +++ b/brmbar3/brmbar-gui-qt4/ChargeCredit.qml @@ -1,14 +1,12 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Item { id: page anchors.fill: parent - property variant username: "" - property variant userdbid: "" - property variant amount: credit_pad.enteredText + property string username: "" + property string userdbid: "" + property string amount: credit_pad.enteredText Text { id: item_name @@ -64,7 +62,7 @@ Item { status_text.setStatus("Unknown barcode", "#ff4444") return } - if (acct.acctype == "debt") { + if (acct.acctype === "debt") { username = acct.name userdbid = acct.id } else { diff --git a/brmbar3/brmbar-gui-qt4/ItemEdit.qml b/brmbar3/brmbar-gui-qt4/ItemEdit.qml index d5ecb27..759417e 100644 --- a/brmbar3/brmbar-gui-qt4/ItemEdit.qml +++ b/brmbar3/brmbar-gui-qt4/ItemEdit.qml @@ -1,16 +1,14 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Item { id: page anchors.fill: parent - property variant item_name: item_name_pad.enteredText - property variant dbid: "" - property variant info: "" - property variant buy_price: item_buyprice_pad.enteredText - property variant price: item_sellprice_pad.enteredText + property string item_name: item_name_pad.enteredText + property string dbid: "" + property string info: "" + property string buy_price: item_buyprice_pad.enteredText + property string price: item_sellprice_pad.enteredText property string barcode: "" state: "normal" @@ -28,7 +26,7 @@ Item { /* TODO: Allow override. */ return } - if (info.dbid == "") { + if (info.dbid === "") { status_text.setStatus("Press [Create] first", "#ff4444") return } @@ -310,35 +308,35 @@ Item { width: 360 text: dbid == "" ? "Create" : "Save" onButtonClick: { - var xi = info; - xi["name"] = page.item_name; - xi["buy_price"] = page.buy_price; - xi["price"] = page.price; - info = xi + var xi = info; + xi["name"] = page.item_name; + xi["buy_price"] = page.buy_price; + xi["price"] = page.price; + info = xi - var res; - if (dbid == "") { - res = shop.newItem(info) - if (!res) { - status_text.setStatus("Please fill all values first.", "#ff4444") - return - } - } else { - res = shop.saveItem(dbid, info) - } + var res; + if (dbid == "") { + res = shop.newItem(info) + if (!res) { + status_text.setStatus("Please fill all values first.", "#ff4444") + return + } + } else { + res = shop.saveItem(dbid, info) + } - if (res.cost) { - status_text.setStatus((dbid == "" ? "Stocked!" : "Restocked!") + " Take " + res.cost + " from the money box.", "#ffff7c") - } else { - status_text.setStatus(dbid == "" ? "Item created" : "Changes saved", "#ffff7c") - } + if (res.cost) { + status_text.setStatus((dbid == "" ? "Stocked!" : "Restocked!") + " Take " + res.cost + " from the money box.", "#ffff7c") + } else { + status_text.setStatus(dbid == "" ? "Item created" : "Changes saved", "#ffff7c") + } - if (dbid == "") { - dbid = res.dbid - var xi = info; xi["dbid"] = page.dbid; info = xi - } else { - loadPage("StockMgmt") - } + if (dbid == "") { + dbid = res.dbid + xi = info; xi["dbid"] = page.dbid; info = xi + } else { + loadPage("StockMgmt") + } } } diff --git a/brmbar3/brmbar-gui-qt4/ItemInfo.qml b/brmbar3/brmbar-gui-qt4/ItemInfo.qml index 8d765b0..b7d9fb5 100644 --- a/brmbar3/brmbar-gui-qt4/ItemInfo.qml +++ b/brmbar3/brmbar-gui-qt4/ItemInfo.qml @@ -1,14 +1,12 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Item { id: page anchors.fill: parent - property variant name: "" - property variant dbid: "" - property variant price: "" + property string name: "" + property string dbid: "" + property string price: "" Text { id: item_name @@ -52,7 +50,7 @@ Item { status_text.setStatus("Unknown barcode", "#ff4444") return } - if (acct.acctype != "debt") { + if (acct.acctype !== "debt") { loadPageByAcct(acct) return } diff --git a/brmbar3/brmbar-gui-qt4/MainPage.qml b/brmbar3/brmbar-gui-qt4/MainPage.qml index 5c6b5b1..18ff58b 100644 --- a/brmbar3/brmbar-gui-qt4/MainPage.qml +++ b/brmbar3/brmbar-gui-qt4/MainPage.qml @@ -1,6 +1,4 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Item { id: page diff --git a/brmbar3/brmbar-gui-qt4/Management.qml b/brmbar3/brmbar-gui-qt4/Management.qml index e316ca0..c547976 100644 --- a/brmbar3/brmbar-gui-qt4/Management.qml +++ b/brmbar3/brmbar-gui-qt4/Management.qml @@ -1,6 +1,4 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Item { id: page diff --git a/brmbar3/brmbar-gui-qt4/Receipt.qml b/brmbar3/brmbar-gui-qt4/Receipt.qml index 5e7288b..573986a 100644 --- a/brmbar3/brmbar-gui-qt4/Receipt.qml +++ b/brmbar3/brmbar-gui-qt4/Receipt.qml @@ -1,14 +1,12 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Item { id: page anchors.fill: parent property variant user - property variant description: item_name_pad.enteredText - property variant amount: amount_pad.enteredText + property string description: item_name_pad.enteredText + property string amount: amount_pad.enteredText state: "normal" diff --git a/brmbar3/brmbar-gui-qt4/StockMgmt.qml b/brmbar3/brmbar-gui-qt4/StockMgmt.qml index 394c7eb..0be284c 100644 --- a/brmbar3/brmbar-gui-qt4/StockMgmt.qml +++ b/brmbar3/brmbar-gui-qt4/StockMgmt.qml @@ -1,6 +1,4 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Item { id: page diff --git a/brmbar3/brmbar-gui-qt4/UserInfo.qml b/brmbar3/brmbar-gui-qt4/UserInfo.qml index ab4b0c2..3a76dea 100644 --- a/brmbar3/brmbar-gui-qt4/UserInfo.qml +++ b/brmbar3/brmbar-gui-qt4/UserInfo.qml @@ -1,13 +1,12 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 Item { id: page anchors.fill: parent - property variant name: "" - property variant dbid: "" - property variant negbalance: "" + property string name: "" + property string dbid: "" + property string negbalance: "" Text { id: item_name @@ -43,7 +42,7 @@ Item { status_text.setStatus("Unknown barcode", "#ff4444") return } - if (acct.acctype == "recharge") { + if (acct.acctype === "recharge") { loadPage("ChargeCredit", { "username": name, "userdbid": dbid, "amount": acct.amount }) return } diff --git a/brmbar3/brmbar-gui-qt4/UserMgmt.qml b/brmbar3/brmbar-gui-qt4/UserMgmt.qml index b77e840..b8ee7c8 100644 --- a/brmbar3/brmbar-gui-qt4/UserMgmt.qml +++ b/brmbar3/brmbar-gui-qt4/UserMgmt.qml @@ -1,6 +1,4 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Item { id: page @@ -17,7 +15,7 @@ Item { status_text.setStatus("Unknown barcode", "#ff4444") return } - if (acct.acctype != "debt") { + if (acct.acctype !== "debt") { loadPageByAcct(acct) return } diff --git a/brmbar3/brmbar-gui-qt4/Withdraw.qml b/brmbar3/brmbar-gui-qt4/Withdraw.qml index a936f7e..463fb9c 100644 --- a/brmbar3/brmbar-gui-qt4/Withdraw.qml +++ b/brmbar3/brmbar-gui-qt4/Withdraw.qml @@ -1,14 +1,12 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 -import QtQuick 1.0 Item { id: page anchors.fill: parent - property variant username: "" - property variant userdbid: "" - property variant amount: withdraw_pad.enteredText + property string username: "" + property string userdbid: "" + property string amount: withdraw_pad.enteredText Text { id: item_name diff --git a/brmbar3/brmbar-gui-qt4/brmbar-gui-qt4.qmlproject.user b/brmbar3/brmbar-gui-qt4/brmbar-gui-qt4.qmlproject.user index eeee7ab..0a23204 100644 --- a/brmbar3/brmbar-gui-qt4/brmbar-gui-qt4.qmlproject.user +++ b/brmbar3/brmbar-gui-qt4/brmbar-gui-qt4.qmlproject.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget @@ -112,7 +112,7 @@ ProjectExplorer.Project.Updater.EnvironmentId - {a277f310-b549-4ad7-87ca-cd03f76f19ff} + {524378aa-09e0-4345-892b-1bd47313bcaf} ProjectExplorer.Project.Updater.FileVersion diff --git a/brmbar3/brmbar-gui-qt4/main.qml b/brmbar3/brmbar-gui-qt4/main.qml index 5c43528..9eefbc6 100644 --- a/brmbar3/brmbar-gui-qt4/main.qml +++ b/brmbar3/brmbar-gui-qt4/main.qml @@ -1,4 +1,3 @@ -// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5 import QtQuick 1.1 BasePage { @@ -17,11 +16,11 @@ BasePage { } function loadPageByAcct(acct) { - if (acct.acctype == "inventory") { + if (acct.acctype === "inventory") { loadPage("ItemInfo", { name: acct["name"], dbid: acct["id"], price: acct["price"] }) - } else if (acct.acctype == "debt") { + } else if (acct.acctype === "debt") { loadPage("UserInfo", { name: acct["name"], dbid: acct["id"], negbalance: acct["negbalance"] }) - } else if (acct.acctype == "recharge") { + } else if (acct.acctype === "recharge") { loadPage("ChargeCredit", { amount: acct["amount"] }) } } From f5ffeeae971cdf100d3a87ac6174373842dd91d5 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 10 Feb 2013 14:47:57 +0100 Subject: [PATCH 006/112] brmbar-gui-qt4/ItemEdit: Fix bug in UI cleanup --- brmbar3/brmbar-gui-qt4/ItemEdit.qml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brmbar3/brmbar-gui-qt4/ItemEdit.qml b/brmbar3/brmbar-gui-qt4/ItemEdit.qml index 759417e..180973e 100644 --- a/brmbar3/brmbar-gui-qt4/ItemEdit.qml +++ b/brmbar3/brmbar-gui-qt4/ItemEdit.qml @@ -6,7 +6,7 @@ Item { property string item_name: item_name_pad.enteredText property string dbid: "" - property string info: "" + property variant info: "" property string buy_price: item_buyprice_pad.enteredText property string price: item_sellprice_pad.enteredText From be74f723d20c2eea52fcfc75ad9fe74cdeb39866 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 10 Feb 2013 15:01:37 +0100 Subject: [PATCH 007/112] Revert button gradients, it looks rather ugly, was just an experiment --- brmbar3/brmbar-gui-qt4/BarButton.qml | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/brmbar3/brmbar-gui-qt4/BarButton.qml b/brmbar3/brmbar-gui-qt4/BarButton.qml index 48971ff..8718312 100644 --- a/brmbar3/brmbar-gui-qt4/BarButton.qml +++ b/brmbar3/brmbar-gui-qt4/BarButton.qml @@ -4,22 +4,7 @@ Rectangle { id: rectangle1 width: 240 height: 83 - gradient: Gradient { - GradientStop { - position: 0 - color: "#888888" - } - - GradientStop { - position: 0.5 - color: "#000000" - } - - GradientStop { - position: 1 - color: "#888888" - } - } + color: "#000000" border.color: btnColor property string text: "Button" From 76cb5ba4c7b257d1f4e672488c02a23cc89fdf93 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 10 Feb 2013 16:18:34 +0100 Subject: [PATCH 008/112] Add basic documentation: README, INSTALL, Architectural overview --- brmbar3/INSTALL.md | 67 +++++++++++++++++++++++ brmbar3/README.md | 71 ++++++++++++++++++++++++ brmbar3/doc/architecture.md | 106 ++++++++++++++++++++++++++++++++++++ 3 files changed, 244 insertions(+) create mode 100644 brmbar3/INSTALL.md create mode 100644 brmbar3/README.md create mode 100644 brmbar3/doc/architecture.md diff --git a/brmbar3/INSTALL.md b/brmbar3/INSTALL.md new file mode 100644 index 0000000..cdff5c4 --- /dev/null +++ b/brmbar3/INSTALL.md @@ -0,0 +1,67 @@ +BrmBar v3 Installation +====================== + +This is woefully incomplete; if you are deploying BrmBar, at this +point you will likely need to learn at least something aobut its +structure and internals. Some code modifications might even be +required too. Patches enhancing user configurability are welcome! + +Maybe some things are missing. Ask the developers if you get in trouble, +e.g. in #brmlab on FreeNode. + +Hardware Requirements +--------------------- + +* Display. Current UI is optimized for 4:3, 1024x768 display. +* Touchscreen. In emergency, you can use a mouse too, but it's + clumsy and scrolling is not too intuitive. +* Barcode reader. We want the kind that will behave as a HID device + and on scanning a barcode, it will send a CR-terminated scanned string. +* Physical keyboard stashed in vicinity will help. It is possible + to enter text (inventory names, receipt reasons) on the touchscreen, + but it's a bit frustrating. +* You will want to print a sheet of barcodes with names of all user + accounts; these will be then used by people to buy stuff using their + accounts - first scan barcode of the item, then scan your barcode, + voila. Scanning your barcode directly can bring the user to a screen + where they can see their credit and charge it too. + +Software Requirements +--------------------- + +* Developed and tested on Debian, but should work on other systems too. +* Python 3. +* QT4 with Python bindings: + * QT4 with the "Declarative" module, e.g. libqt4-declarative package. + * The PySide Qt4 bindings, e.g. python3-pyside.qtdeclarative package. + * Installing the qtcreator program may be helpful for QML testing + and development. +* PostgreSQL with Python pindings: + * The database server itself, e.g. postgresql package. + * PsyCoPg2, e.g. python3-psycopg2 package. + +Software Setup +-------------- + +* Create psql user and `brmbar` database. +* The SQL schema in file `SQL` contains the required SQL tables, + but also INSERTs that add some rows essential for proper operation; + base currency and two base accounts. You *will* want to tweak the + currency name; default is `Kč` (the Czech crown), replace it with + your currency symbol. Then do `git grep 'Kč'` and replace all other + occurences of `Kč` in brmbar source with your currency name. +* Load the SQL schema stored in file `SQL` in the database (redirect + it to psql command standard input). +* You should be able to fire up the GUI now and start entering data. + If you want to make sure all works as expected, execute the SQL + statements in file `SQL.test` (revisit for currency names too) which + will populate the database with a bit of sample data for testing. + +TODO: Mention the actual commands to execute. + +Troubleshooting +--------------- + +Assuming that you run brmbar from a terminal, if something gets +stuck, you can switch to the terminal by Alt-TAB, then kill brmbar +by the Ctrl-\ shortcut (sends SIGQUIT) and restart it. diff --git a/brmbar3/README.md b/brmbar3/README.md new file mode 100644 index 0000000..4972c42 --- /dev/null +++ b/brmbar3/README.md @@ -0,0 +1,71 @@ +BrmBar v3 +========= + +BrmBar is a management system for running a tiny hackerspace shop +with self-service usage based on trust and support for user accounts +and inventory tracking. + +BrmBar offers a touchscreen-based user interface, identifies items +and users by barcodes scanned by a barcode reader, should run on any +decent Linux machine and stores its data in PostgreSQL database. + +Features +-------- + +* Very simple user interface (using big touchscreen buttons and + barcode reader) that should enable even non-technical users to + do basic shopping with little to no training. +* Users may have their accounts they can load with money by + depositing larger sum of money in advance, then charging their + account when buying stuff. Of course, paying direct for cash + is also supported. +* Inventory and cash accounts are tracked so that you can make sure + there is no Club Mate mysteriously disappearing or if the amount + of cash in the cash box is not less than expected by the system. +* You can enter receipts for duct tapes and other necessities to be + financed by cash surplus generated by brmbar. +* Simple management operations (depositing and withdrawing money + from user accounts, entering receipts, stocking in new inventory) + can be also performed in the user interface even by non-technical + users with basic training. +* The database is based on the classical accounting paradigm. + This means no information is needlessly lost, you could even + make a GNUCash export and your accounting geeks will feel warm + and fuzzy. +* Multiple user interfaces available (and possible). The primary + user interface is based on QtQuick (Qt4 QML QtDeclarative). + +User Interfaces +--------------- + +These UIs are provided: + +* **brmbar-gui-qt4**: The default touchscreen-based UI. The Python side + provides an adapter object whose methods can be executed by the QML + code; ad-hoc directionary objects are used to exchange complex data + like account information. +* **brmbar-tui**: A trivial text-based "shell" UI that mimics a historic + interface used in the Brmlab hackerspace in the past. It supports only + selling items, querying item price and user account balance and + depositing money for the user accounts. +* **brmbar-cli**: A command-line interface intended for use in scripts + and remote usage when fixing problems. It is also meant to provide + advanced functionality like inventory revision that is too tedious + to implement in the Qt4 GUI and only the brmbar admins are expected + to do these tasks. + +TODO +---- + +* The user interface needs some improvements, mainly regarding + scrolling in large lists. +* The brmbar-cli.py admin script for advanced/remote management + operations is largely unfinished. In the meantime, you need to use + SQL statements, sorry. Or finish it yourself. :-) +* It is common to have two stashes of cash, one in a cash box + in the shop, another in a vault (sometimes called "overflow") + where extra cash is stored. The brmbar model supports this, + but UI support needs to be added. +* Bitcoin support, somehow... + +Some more TODO items may be listed in the GitHub issue tracker. diff --git a/brmbar3/doc/architecture.md b/brmbar3/doc/architecture.md new file mode 100644 index 0000000..791517f --- /dev/null +++ b/brmbar3/doc/architecture.md @@ -0,0 +1,106 @@ +BrmBar v3 - Architectural Overview +================================== + +BrmBar v3 is written in Python, with the database stored in PostgreSQL +and the primary user interface modelled in QtQuick. All user interfaces +share a common *brmbar* package that provides few Python classes for +manipulation with the base objects. + +Objects and Database Schema +--------------------------- + +### Account ### + +The most essential brmbar object is an Account, which can track +balances of various kinds (described by *acctype* column) in the +classical accounting paradigm: + +* **Cash**: A physical stash of cash. One cash account is created + by default, corresponding to the cash box where people put money + when buying stuff (or depositing money in their user accounts). + Often, that's the only cash account you need. +* **Debt**: Represents brmbar's debt to some person. These accounts + are actually the "user accounts" where people deposit money. When + a deposit of 100 is made, 100 is *subtracted* from the balance, + the balance is -100 and brmbar is in debt of 100 to the user. + When the user buys something for 200, 200 is *added* to the balance, + the balance is 100 and the user is in debt of 100 to the brmbar. + This is correct notation from accounting point of view, but somewhat + confusing for the users, so in the user interface (and crbalance + column of some views), this balance is *negated*! +* **Inventory**: Represents inventory items (e.g. Club Mate bottles). + The account balance represents the quantity of items. +* **Income**: Represents pure income of brmbar, i.e. the profit; + there is usually just a single account of this type where all the + profit (sell price of an item minus the buy price of an item) + is accumulated. +* **Expense**: This type is currently not used. +* **Starting balance** and **ending balance**: This may be used + in the future when transaction book needs to be compressed. + +As you can see, the amount of cash, user accounts, inventory items +etc. are all represented as **Account** objects that are of various +**types**, are **named** and have a certain balance (calculated +from a transaction book). That balance is a number represented +in certain **currency**. It also has a set of **barcodes** associated. + +### Currency, Exchange rate ### + +Usually, all accounts that deal with cash (the cash, debt, income, ... +accounts) share a single currency that corresponds to the physical +currency locally in use (the default is `Kč`). However, inventory +items have balances corresponding to item quantities - to deal with +this correctly, each inventory item *has its own currency*; i.e. +`Club Mate` bottle is a currency associated with the `Club Mate` +account. + +Currencies have defined (uni-directional) exchange rates. The exchange +rate of "Kč to Club Mate bottles" is the buy price of Club Mate, how +much you pay for one bottle of Club Mate from the cash box when you +are stocking in Club Mate. The exchange rate of "Club Mate bottle to Kč" +is the sell price of Club Mate, how much you pay for one bottle of Club +Mate to the cash box when you are buying it from brmbar (sell price +should be higher than buy price if you want to make a profit). + +Exchange rate is valid since some defined time; historical exchange +rates are therefore kept and this allows to account for changing prices +of inventory items. (Unfortunately, at the time of writing this, the +profit calculation actually didn't make use of that yet.) + +### Transactions, Transaction splits ### + +A transaction book is used to determine current account balances and +stores all operations related to accounts - depositing or withdrawing +money, stocking in items, and most importantly buying stuff (either for +cash or from a debt account). A transaction happenned at some **time** +and was performed by certain **responsible** person. + +The actual accounts involved in a transaction are specified by a list of +transaction splits that either put balance into the transaction (*credit* +side) or grab balance from it (*debit* side). For example, a typical +transaction representing a sale of Club Mate bottle to user "pasky" +would be split like this: + +* *credit* of 1 Club Mate on Club Mate account with memo "pasky". +* *debit* of 35 Kč on "pasky" account with memo "Club Mate" + (indeed we _add_ 35Kč to the debt account for pasky buying + the Club Mate; if this seems weird, refer to the "debt" account + type description). +* *debit* of 5 Kč on income account Profits with memo "Margin + on Club Mate" (this represents the sale price - buy price delta, + i.e. the profit we made in brmbar by selling this Club Mate). + +The brmbar Python Package +------------------------- + +The **brmbar** package (in brmbar/ subdirectory) provides common brmbar +functionality for the various user interfaces: + +* **Database**: Layer for performing SQL queries with some error handling. +* **Currency**: Class for querying and manipulating currency objects and + converting between them based on stored exchange rates. +* **Account**: Class for querying and manipulating the account objects + and their current balance. +* **Shop**: Class providing the "business logic" of all the actual user + operations: selling stuff, depositing and withdrawing moeny, adding + stock, retrieving list of accounts of given type, etc. From c8a04834015c5a2077972e6f54695faab2361813 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 10 Feb 2013 16:25:49 +0100 Subject: [PATCH 009/112] TODO: Remove done items, add description --- brmbar3/TODO | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/brmbar3/TODO b/brmbar3/TODO index 5afc2de..edfcc27 100644 --- a/brmbar3/TODO +++ b/brmbar3/TODO @@ -1,26 +1,5 @@ -+ Management view - + Stock management link - + User management link - + Bilance overview (Cash, Profit, Credit total) -+ Item picker from list -+ User management - + List of users - + Withdrawal of user credit -+ Numerical manual entry support - + Use for credit charge - + Use for withdrawal -+ Restocking view (Stock management) - + Item picker with edit button - + Item editor (name, buy price, sale price, quantity) - + Item-barcode assignment - + Support for adding new items -+ Alphanumeric manual entry support - + Use in item editor -+ Withdrawal for brmbar receipts - 1. User responsible - 2. Amount and description - -** At this point it should be good enough to deploy ** +This reprensents some generic features that would need to be implemented +in brmbar-gui-qt4 to have a fully fledged user interface. . User management - Add user From dab9388703a5d6510437feefe035f15b88f7ee1a Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 10 Feb 2013 16:44:42 +0100 Subject: [PATCH 010/112] USAGE.md: Usage instructions Produced mainly by Google Translate of http://brmlab.cz/pipermail/brmlab/2012-October/004130.html --- brmbar3/USAGE.md | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 brmbar3/USAGE.md diff --git a/brmbar3/USAGE.md b/brmbar3/USAGE.md new file mode 100644 index 0000000..0bb6369 --- /dev/null +++ b/brmbar3/USAGE.md @@ -0,0 +1,49 @@ +Quick guide: + +* I want to buy for cash: I scan item's barcode, press "Pay by Cash" and pour + money into the cash box. + +* I want to buy from credit: I scan item's barcode, then my barcode. + +* I want to put money on credit: press "Charge", I scan my barcode, +type some amount, press "Charge" and put money in the cash box. + + +Advanced operations: + +* I want to withdraw funds from my (positive) credit: +Press "Management", choose "User Mgmt", scan your barcode, +press the Withdraw Amount and type the amount. Then take the money +from the cash box. + +* I want to stock in some inventory (that's been in brmbar before): +Press "Management", "Stock Mgmt", scan barcode of the item, edit +the purchase price (or also the selling price and label), press +"Restock" and enter the quantity of stocked in piece. Press "Save". +Toss the bill (if possible with the current written date, to allow +pairing) to brmbar. + +* I want to stock in some new inventory: Press "Management", "Stock +Mgmt", press "Add new item", enter the name, purchase and selling price, +press "Create". Then press "Restock", enter the quantity stocked in. +Scan the item's barcode and press "Save". Toss the bill in brmbar. + +* I want to bill the brmbar with some small expenses like duct tape: +Press "Management" and "Receipt". Press "Description" and write +a brief description of the bill. Press "Edit" near the "Money Amount" +and enter the amount. Scan *your* barcode. The operation is finished +by pressing "Create". Toss bill (inscribed with the current date +to ease pairing) to brmbar. + + +General notes: + +The system expects that we take money from the cash box right away. +If you don't want to (or there is e.g. not enough money), put money +on your credit account instead (see above). Please always do that +(never "I'll remember and I'll take money later") so that there is +a record that the cash box and system records are not in sync and +there are no irregularities. + +To enter text (or numbers too), you can use both the on-screen keyboard +and the physical keyboard nearby. From 1bb05e6935e19d0bedcaff42deb11a40621499b2 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 10 Feb 2013 16:44:58 +0100 Subject: [PATCH 011/112] README: Link to other documentation --- brmbar3/README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/brmbar3/README.md b/brmbar3/README.md index 4972c42..0bf93ca 100644 --- a/brmbar3/README.md +++ b/brmbar3/README.md @@ -68,4 +68,13 @@ TODO but UI support needs to be added. * Bitcoin support, somehow... -Some more TODO items may be listed in the GitHub issue tracker. +Some more TODO items may be listed in the GitHub issue tracker; +missing brmbar-gui-qt4 features are listed in the `TODO` file. + +Other Resources +--------------- + +See the INSTALL file for setup instructions and USAGE file for +basic usage instructions. The doc/architecture file describes +the brmbar object model and briefly explains the brmbar Python +package. From bacb9b1791eba00ff5eb65b7840ec36055d2fe83 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 10 Feb 2013 17:29:10 +0100 Subject: [PATCH 012/112] USAGE.md: Properly markdown --- brmbar3/USAGE.md | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/brmbar3/USAGE.md b/brmbar3/USAGE.md index 0bb6369..b20d428 100644 --- a/brmbar3/USAGE.md +++ b/brmbar3/USAGE.md @@ -1,47 +1,50 @@ -Quick guide: +Quick Guide +----------- -* I want to buy for cash: I scan item's barcode, press "Pay by Cash" and pour +* I want to buy for cash: I scan item's barcode, press **Pay by Cash** and pour money into the cash box. * I want to buy from credit: I scan item's barcode, then my barcode. -* I want to put money on credit: press "Charge", I scan my barcode, -type some amount, press "Charge" and put money in the cash box. +* I want to put money on credit: press **Charge**, I scan my barcode, +type some amount, press **Charge** and put money in the cash box. -Advanced operations: +Advanced Operations +------------------- * I want to withdraw funds from my (positive) credit: -Press "Management", choose "User Mgmt", scan your barcode, +Press **Management**, choose **User Mgmt**, scan your barcode, press the Withdraw Amount and type the amount. Then take the money from the cash box. * I want to stock in some inventory (that's been in brmbar before): -Press "Management", "Stock Mgmt", scan barcode of the item, edit +Press **Management**, **Stock Mgmt**, scan barcode of the item, edit the purchase price (or also the selling price and label), press -"Restock" and enter the quantity of stocked in piece. Press "Save". +**Restock** and enter the quantity of stocked in piece. Press **Save**. Toss the bill (if possible with the current written date, to allow pairing) to brmbar. -* I want to stock in some new inventory: Press "Management", "Stock -Mgmt", press "Add new item", enter the name, purchase and selling price, -press "Create". Then press "Restock", enter the quantity stocked in. -Scan the item's barcode and press "Save". Toss the bill in brmbar. +* I want to stock in some new inventory: Press **Management**, **Stock +Mgmt**, press **Add new item**, enter the name, purchase and selling price, +press **Create**. Then press **Restock**, enter the quantity stocked in. +Scan the item's barcode and press **Save**. Toss the bill in brmbar. * I want to bill the brmbar with some small expenses like duct tape: -Press "Management" and "Receipt". Press "Description" and write -a brief description of the bill. Press "Edit" near the "Money Amount" +Press **Management** and **Receipt**. Press **Description** and write +a brief description of the bill. Press **Edit** near the **Money Amount** and enter the amount. Scan *your* barcode. The operation is finished -by pressing "Create". Toss bill (inscribed with the current date +by pressing **Create**. Toss bill (inscribed with the current date to ease pairing) to brmbar. -General notes: +General Notes +------------- The system expects that we take money from the cash box right away. If you don't want to (or there is e.g. not enough money), put money on your credit account instead (see above). Please always do that -(never "I'll remember and I'll take money later") so that there is +(never *I'll remember and I'll take money later*) so that there is a record that the cash box and system records are not in sync and there are no irregularities. From 13bc4d6f60385fc5515e12e368e14b222467f0f9 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 10 Feb 2013 17:41:25 +0100 Subject: [PATCH 013/112] brmbar-cli: Add support for adduser action --- brmbar3/SQL | 1 + brmbar3/brmbar-cli.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/brmbar3/SQL b/brmbar3/SQL index 2865c02..1d1d32f 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -4,6 +4,7 @@ CREATE TABLE currencies ( name VARCHAR(128) NOT NULL, UNIQUE(name) ); +-- Some code depends on the primary physical currency to have id 1. INSERT INTO currencies (name) VALUES ('Kč'); CREATE TYPE exchange_rate_direction AS ENUM ('source_to_target', 'target_to_source'); diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index df6b078..b6c94dd 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -28,6 +28,8 @@ Usage: brmbar-cli.py COMMAND ARGS... stats A set of various balances as shown in the Management screen of the GUI. + adduser USER + Add user (debt) account with given username. ! changestock ITEM1 +-AMT_ITEM1 ITEM2 +-AMT_ITEM2 ... Create a custom transaction that will change balance of a variety of items at once, and either deduce @@ -147,5 +149,10 @@ elif sys.argv[1] == "stats": print("Credit: {}".format(shop.credit_negbalance_str())) print("Inventory: {}".format(shop.inventory_balance_str())) +elif sys.argv[1] == "adduser": + acct = brmbar.Account.create(db, sys.argv[2], brmbar.Currency.load(db, id = 1), 'debt') + acct.add_barcode(sys.argv[2]) # will commit + print("{}: id {}".format(acct.name, acct.id)); + else: help() From 55e6a5226c506ac465db6c0f341b6c1927d8f492 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 10 Feb 2013 17:56:01 +0100 Subject: [PATCH 014/112] USAGE: Adding users, what about reinventorization? --- brmbar3/INSTALL.md | 4 +++- brmbar3/USAGE.md | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/brmbar3/INSTALL.md b/brmbar3/INSTALL.md index cdff5c4..f7207c6 100644 --- a/brmbar3/INSTALL.md +++ b/brmbar3/INSTALL.md @@ -24,7 +24,7 @@ Hardware Requirements accounts; these will be then used by people to buy stuff using their accounts - first scan barcode of the item, then scan your barcode, voila. Scanning your barcode directly can bring the user to a screen - where they can see their credit and charge it too. + where they can see their credit and charge it too. See also USAGE. Software Requirements --------------------- @@ -56,6 +56,8 @@ Software Setup If you want to make sure all works as expected, execute the SQL statements in file `SQL.test` (revisit for currency names too) which will populate the database with a bit of sample data for testing. +* Regarding adding users at this point and for other usage instructions, + refer to the USAGE file. TODO: Mention the actual commands to execute. diff --git a/brmbar3/USAGE.md b/brmbar3/USAGE.md index b20d428..b2526c7 100644 --- a/brmbar3/USAGE.md +++ b/brmbar3/USAGE.md @@ -5,6 +5,8 @@ Quick Guide money into the cash box. * I want to buy from credit: I scan item's barcode, then my barcode. +(If you don't have your barcode printed out, you can also type your +username on a physical keyboard.) * I want to put money on credit: press **Charge**, I scan my barcode, type some amount, press **Charge** and put money in the cash box. @@ -50,3 +52,25 @@ there are no irregularities. To enter text (or numbers too), you can use both the on-screen keyboard and the physical keyboard nearby. + + +Administrative Usage +-------------------- + +* The most common administrative action you will need to do is adding + new user (also called debt or credit) accounts. The GUI support for + this is not implemented yet, but the `brmbar-cli.py` UI allows it: + + ./brmbar-cli.py adduser joehacker + + Afterwards, print out a barcode saying "joehacker" and stick that + somewhere nearby; scanning that barcode will allow access to this + account (and so will typing "joehacker" on a physical keyboard). + +* If your inventory stock count or cash box amount does not match +the in-system data, you will need to make a corrective transaction. +In the future, brmbar-cli.py will support this, but there is no +implementation yet; it's not entirely clear yet what is the proper +way to do this from the accounting standpoint. In the meantime, you +can use SQL INSERTs to manually create a transaction with appropriate +transaction splits (see doc/architecture for details on splits). From 35c353457510cfd1be7ea2547a474898318b40b4 Mon Sep 17 00:00:00 2001 From: Nephirus Date: Fri, 22 Feb 2013 21:24:45 +0100 Subject: [PATCH 015/112] INSTALL.md: psql command examples --- brmbar3/INSTALL.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/brmbar3/INSTALL.md b/brmbar3/INSTALL.md index f7207c6..d7976e1 100644 --- a/brmbar3/INSTALL.md +++ b/brmbar3/INSTALL.md @@ -44,14 +44,26 @@ Software Setup -------------- * Create psql user and `brmbar` database. + + brmuser@host:~> su postgres + postgres@host:/home/user> createuser -D brmuser + postgres@host:/home/user> su brmuser + brmuser@host:~> createdb brmbar + * The SQL schema in file `SQL` contains the required SQL tables, but also INSERTs that add some rows essential for proper operation; base currency and two base accounts. You *will* want to tweak the currency name; default is `Kč` (the Czech crown), replace it with your currency symbol. Then do `git grep 'Kč'` and replace all other occurences of `Kč` in brmbar source with your currency name. -* Load the SQL schema stored in file `SQL` in the database (redirect - it to psql command standard input). +* Load the SQL schema stored in file `SQL` in the database. + + brmuser@host:~/brmbar/brmbar3> psql brmbar + psql (9.1.8) + Type "help" for help. + + brmbar=# \i SQL + * You should be able to fire up the GUI now and start entering data. If you want to make sure all works as expected, execute the SQL statements in file `SQL.test` (revisit for currency names too) which From fb6eadf031e349a23c5d7884526d82d0bf023be9 Mon Sep 17 00:00:00 2001 From: Nephirus Date: Fri, 22 Feb 2013 22:37:14 +0100 Subject: [PATCH 016/112] INSTALL.md: Fix formatting --- brmbar3/INSTALL.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/brmbar3/INSTALL.md b/brmbar3/INSTALL.md index d7976e1..2dc5312 100644 --- a/brmbar3/INSTALL.md +++ b/brmbar3/INSTALL.md @@ -45,10 +45,10 @@ Software Setup * Create psql user and `brmbar` database. - brmuser@host:~> su postgres - postgres@host:/home/user> createuser -D brmuser - postgres@host:/home/user> su brmuser - brmuser@host:~> createdb brmbar + brmuser@host:~> su postgres + postgres@host:/home/user> createuser -D brmuser + postgres@host:/home/user> su brmuser + brmuser@host:~> createdb brmbar * The SQL schema in file `SQL` contains the required SQL tables, but also INSERTs that add some rows essential for proper operation; @@ -58,11 +58,11 @@ Software Setup occurences of `Kč` in brmbar source with your currency name. * Load the SQL schema stored in file `SQL` in the database. - brmuser@host:~/brmbar/brmbar3> psql brmbar - psql (9.1.8) - Type "help" for help. + brmuser@host:~/brmbar/brmbar3> psql brmbar + psql (9.1.8) + Type "help" for help. - brmbar=# \i SQL + brmbar=# \i SQL * You should be able to fire up the GUI now and start entering data. If you want to make sure all works as expected, execute the SQL From ba4d8686f63616fdd7e403b34d7527bd9aae9b09 Mon Sep 17 00:00:00 2001 From: Nephirus Date: Sat, 23 Feb 2013 15:18:20 +0100 Subject: [PATCH 017/112] Search functionality added to Stock Management. --- brmbar3/brmbar-gui-qt4.py | 8 +-- brmbar3/brmbar-gui-qt4/StockMgmt.qml | 90 +++++++++++++++++++++++++++- brmbar3/brmbar/Shop.py | 5 +- 3 files changed, 94 insertions(+), 9 deletions(-) diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index 878f04b..405084f 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -120,9 +120,9 @@ class ShopAdapter(QtCore.QObject): db.commit() return alist - @QtCore.Slot(result='QVariant') - def itemList(self): - alist = [ self.acct_inventory_map(a) for a in shop.account_list("inventory") ] + @QtCore.Slot('QVariant', result='QVariant') + def itemList(self, query): + alist = [ self.acct_inventory_map(a) for a in shop.account_list("inventory", like_str="%%"+query+"%%") ] db.commit() return alist @@ -188,5 +188,5 @@ ctx.setContextProperty('shop', ShopAdapter()) view.setSource('brmbar-gui-qt4/main.qml') -view.showFullScreen() +view.show() app.exec_() diff --git a/brmbar3/brmbar-gui-qt4/StockMgmt.qml b/brmbar3/brmbar-gui-qt4/StockMgmt.qml index 0be284c..b52e7fb 100644 --- a/brmbar3/brmbar-gui-qt4/StockMgmt.qml +++ b/brmbar3/brmbar-gui-qt4/StockMgmt.qml @@ -6,6 +6,8 @@ Item { property variant item_list_model + state: "normal" + BarcodeInput { color: "#00ff00" /* just for debugging */ onAccepted: { @@ -82,9 +84,11 @@ Item { id: new_item x: 65 y: 582 - width: 360 + width: 281 + height: 83 text: "New Item" fontSize: 0.768 * 60 + visible: page.state == "normal" onButtonClick: { loadPage("ItemEdit", { dbid: "" }) } @@ -97,11 +101,91 @@ Item { width: 360 text: "Main Screen" onButtonClick: { - loadPage("MainPage") + if (page.state == "search") + page.state = "normal" + else + loadPage("MainPage") } } + BarButton { + id: search_button + x: 353 + y: 582 + text: "Search" + visible: page.state == "normal" + onButtonClick: { page.state = "search" } + } + + BarKeyPad { + id: search_pad + x: 65 + y: 298 + opacity: 0 + } + + Text { + id: search_text + x: 65 + y: 602 + color: "#ffff7c" + text: search_pad.enteredText + visible: page.state == "search" + font.pixelSize: 0.768 * 46 + opacity: 0 + } + + BarButton { + id: query_button + x: 353 + y: 582 + text: "Search" + visible: page.state == "search" + onButtonClick: { + page.item_list_model = shop.itemList(search_pad.enteredText) + item_list.model = page.item_list_model + } + } + + states: [ + State { + name: "normal" + }, + State { + name: "search" + + PropertyChanges { + target: item_list_container + x: 66 + y: 166 + width: 899 + height: 132 + } + + PropertyChanges { + target: search_pad + x: 65 + y: 298 + opacity: 1 + } + + PropertyChanges { + target: cancel + text: "Back" + } + + PropertyChanges { + target: search_text + x: 65 + y: 582 + width: 528 + height: 83 + opacity: 1 + } + } + ] + Component.onCompleted: { - item_list_model = shop.itemList() + item_list_model = shop.itemList("") } } diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 7f5c5a1..cbeb455 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -131,10 +131,11 @@ class Shop: def inventory_balance_str(self): return self.currency.str(self.inventory_balance()) - def account_list(self, acctype): + def account_list(self, acctype, like_str="%%"): """list all accounts (people or items, as per acctype)""" accts = [] - cur = self.db.execute_and_fetchall("SELECT id FROM accounts WHERE acctype = %s ORDER BY name ASC", [acctype]) + 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 ^ for inventory in cur: accts += [ Account.load(self.db, id = inventory[0]) ] return accts From d44dbad313f3090bf4f22b5e69ba7993f9e7d217 Mon Sep 17 00:00:00 2001 From: Nephirus Date: Sat, 23 Feb 2013 15:21:19 +0100 Subject: [PATCH 018/112] Fullscreen fix --- brmbar3/brmbar-gui-qt4.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index 405084f..67773be 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -188,5 +188,5 @@ ctx.setContextProperty('shop', ShopAdapter()) view.setSource('brmbar-gui-qt4/main.qml') -view.show() +view.showFullScreen() app.exec_() From 3ecbf192e2828536470a9e7214b0b78c30b1bbd6 Mon Sep 17 00:00:00 2001 From: Radek Pilar Date: Mon, 4 Mar 2013 22:02:11 +0100 Subject: [PATCH 019/112] Updated for 1280x1024 resolution --- brmbar3/brmbar-gui-qt4/BarKeyPad.qml | 4 ++-- brmbar3/brmbar-gui-qt4/BarKeyboard.qml | 4 ++-- brmbar3/brmbar-gui-qt4/BarTextHint.qml | 2 +- brmbar3/brmbar-gui-qt4/BasePage.qml | 10 ++++---- brmbar3/brmbar-gui-qt4/ChargeCredit.qml | 6 ++--- brmbar3/brmbar-gui-qt4/ItemEdit.qml | 14 +++++------ brmbar3/brmbar-gui-qt4/ItemInfo.qml | 8 +++---- brmbar3/brmbar-gui-qt4/MainPage.qml | 6 ++--- brmbar3/brmbar-gui-qt4/Management.qml | 20 ++++++++-------- brmbar3/brmbar-gui-qt4/Receipt.qml | 10 ++++---- brmbar3/brmbar-gui-qt4/StockMgmt.qml | 32 ++++++++++++------------- brmbar3/brmbar-gui-qt4/UserInfo.qml | 6 ++--- brmbar3/brmbar-gui-qt4/UserMgmt.qml | 14 +++++------ brmbar3/brmbar-gui-qt4/Withdraw.qml | 8 +++---- 14 files changed, 72 insertions(+), 72 deletions(-) diff --git a/brmbar3/brmbar-gui-qt4/BarKeyPad.qml b/brmbar3/brmbar-gui-qt4/BarKeyPad.qml index bed9f02..c613f4f 100644 --- a/brmbar3/brmbar-gui-qt4/BarKeyPad.qml +++ b/brmbar3/brmbar-gui-qt4/BarKeyPad.qml @@ -4,6 +4,6 @@ BarKeyboard { keys: "0123456789 Date: Mon, 1 Jul 2013 14:55:54 +0200 Subject: [PATCH 020/112] Catching exception when sql query gets invalid data like too large number --- brmbar3/brmbar/Database.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/brmbar3/brmbar/Database.py b/brmbar3/brmbar/Database.py index e5962ab..d0202d4 100644 --- a/brmbar3/brmbar/Database.py +++ b/brmbar3/brmbar/Database.py @@ -36,6 +36,12 @@ class Database: else: cur.execute(query, attrs) return cur + except psycopg2.DataError as error: # when biitr comes and enters '99999999999999999999' for amount + print("We have invalid input data (SQLi?): level %s (%s) @%s" % ( + level, error, time.strftime("%Y%m%d %a %I:%m %p") + )) + self.db_conn.rollback() + raise RuntimeError("Unsanitized data entered again... BOBBY TABLES") except psycopg2.OperationalError as error: print("Sleeping: level %s (%s) @%s" % ( level, error, time.strftime("%Y%m%d %a %I:%m %p") From 4ae814179d8e04b94a85d8577d08389c2b2e2828 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Mon, 1 Jul 2013 14:51:59 +0200 Subject: [PATCH 021/112] brmbar-cli.py: changestock -> inventory, few notes from a conversation with TMA --- brmbar3/brmbar-cli.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index b6c94dd..155b19f 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -30,7 +30,12 @@ Usage: brmbar-cli.py COMMAND ARGS... screen of the GUI. adduser USER Add user (debt) account with given username. -! changestock ITEM1 +-AMT_ITEM1 ITEM2 +-AMT_ITEM2 ... +! inventory ITEM1 NEW_AMOUNT1 ITEM2 NEW_AMOUNT2 ... + Create a special inventory recounting + + mensi pocet [item] == prevedu na ucet manko (deficiency) BEZ marginu + vetsi pocet [item] == prevedu z uctu primnozky (excess) + stejny pocet [item] == prazdny prevod mezi item a item Create a custom transaction that will change balance of a variety of items at once, and either deduce buy price (+amt) or add sell price (-amt) to the From 32f76b290c3eadd87186437974dc8731f15b0332 Mon Sep 17 00:00:00 2001 From: b00lean Date: Sat, 6 Jul 2013 14:17:52 +0200 Subject: [PATCH 022/112] Added ubuntu packages --- brmbar3/INSTALL.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/brmbar3/INSTALL.md b/brmbar3/INSTALL.md index 2dc5312..b9cc75c 100644 --- a/brmbar3/INSTALL.md +++ b/brmbar3/INSTALL.md @@ -40,6 +40,10 @@ Software Requirements * The database server itself, e.g. postgresql package. * PsyCoPg2, e.g. python3-psycopg2 package. +Ubuntu packages installation instructions +----------------------------------------- +* sudo apt-get install postgresql libqt4-declarative python3 python3-pyside.qtdeclarative python3-psycopg2 python3-pyqt4 + Software Setup -------------- From 0706054e81a8bfd5555648441a4cd307971d5a05 Mon Sep 17 00:00:00 2001 From: b00lean Date: Mon, 8 Jul 2013 17:31:17 +0200 Subject: [PATCH 023/112] Added logic for fixing inventory status --- brmbar3/SQL | 2 ++ brmbar3/brmbar-cli.py | 30 +++++++++++++----------------- brmbar3/brmbar/Shop.py | 26 ++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/brmbar3/SQL b/brmbar3/SQL index 1d1d32f..bae5a15 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -41,6 +41,8 @@ CREATE TABLE accounts ( ); 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 Excess', (SELECT id FROM currencies WHERE name='Kč'), 'income'); +INSERT INTO accounts (name, currency, acctype) VALUES ('BrmBar Deficit', (SELECT id FROM currencies WHERE name='Kč'), 'expense'); CREATE SEQUENCE barcodes_id_seq START WITH 1 INCREMENT BY 1; diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 155b19f..7695986 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -8,7 +8,7 @@ import brmbar def help(): - print("""BrmBar v3 (c) Petr Baudis 2012 + print("""BrmBar v3 (c) Petr Baudis 2012-2013 Usage: brmbar-cli.py COMMAND ARGS... @@ -30,22 +30,8 @@ Usage: brmbar-cli.py COMMAND ARGS... screen of the GUI. adduser USER Add user (debt) account with given username. -! inventory ITEM1 NEW_AMOUNT1 ITEM2 NEW_AMOUNT2 ... - Create a special inventory recounting - - mensi pocet [item] == prevedu na ucet manko (deficiency) BEZ marginu - vetsi pocet [item] == prevedu z uctu primnozky (excess) - stejny pocet [item] == prazdny prevod mezi item a item - Create a custom transaction that will change balance - of a variety of items at once, and either deduce - buy price (+amt) or add sell price (-amt) to the - cash balance for all of these items. This is useful - e.g. after events where stock is sold offline or when - doing an inventory recount. - If cash balance discrepancy is discovered as well - during the inventory recounting, first do changestock, - then compare actual and nominal cash balance again, - then issue changecash command if necessary. + inventory NEW_AMOUNT1 ITEM1 NEW_AMOUNT2 ITEM2 + Inventory recounting (fixing the number of items) ! changecash +-AMT Create a custom transaction that updates nominal cash balance based on the actual cash balance counted @@ -159,5 +145,15 @@ elif sys.argv[1] == "adduser": acct.add_barcode(sys.argv[2]) # will commit print("{}: id {}".format(acct.name, acct.id)); +elif sys.argv[1] == "inventory": + if (len(sys.argv) % 2 != 0 or len(sys.argv) < 4): + print ("Invalid number of parameters, count your parameters.") + else: + iamt = int(sys.argv[2]) + iacct = load_item(sys.argv[3]) #TODO:use barcodes later + print("Current state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) + shop.fix_inventory(item = iacct, amount = iamt) + print("New state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) + else: help() diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index cbeb455..f872328 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -7,18 +7,22 @@ class Shop: Business logic so that only interaction is left in the hands of the frontend scripts. """ - def __init__(self, db, currency, profits, cash): + def __init__(self, db, currency, profits, cash, excess, deficit): self.db = db self.currency = currency # brmbar.Currency self.profits = profits # income brmbar.Account for brmbar profit margins on items self.cash = cash # our operational ("wallet") cash account + self.excess = excess # account from which is deducted cash during inventory item fixing (when system contains less items than is the reality) + self.deficit = deficit # account where is put cash during inventory item fixing (when system contains more items than is the reality) @classmethod def new_with_defaults(cls, db): return cls(db, currency = Currency.default(db), profits = Account.load(db, name = "BrmBar Profits"), - cash = Account.load(db, name = "BrmBar Cash")) + cash = Account.load(db, name = "BrmBar Cash"), + excess = Account.load(db, name = "BrmBar Excess"), + deficit = Account.load(db, name = "BrmBar Deficit")) def sell(self, item, user, amount = 1): # Sale: Currency conversion from item currency to shop currency @@ -139,3 +143,21 @@ class Shop: for inventory in cur: accts += [ Account.load(self.db, id = inventory[0]) ] return accts + + def fix_inventory(self, item, amount): + amount_in_reality = amount + amount_in_system = item.balance() + (buy, sell) = item.currency.rates(self.currency) + + diff = abs(amount_in_reality - amount_in_system) + buy_total = buy * diff + if amount_in_reality > amount_in_system: + transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) + item.debit(transaction, diff, "Inventory fix excess") + self.excess.credit(transaction, buy_total, "Inventory fix excess " + item.name) + self.db.commit() + elif amount_in_reality < amount_in_system: + transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) + item.credit(transaction, diff, "Inventory fix deficit") + self.deficit.credit(transaction, buy_total, "Inventory fix deficit " + item.name) + self.db.commit() From 7ac0031a8d9c35e741806d6ab22d2fadf516d498 Mon Sep 17 00:00:00 2001 From: b00lean Date: Mon, 8 Jul 2013 17:36:36 +0200 Subject: [PATCH 024/112] Fixes after tests --- brmbar3/brmbar-cli.py | 7 ++++--- brmbar3/brmbar/Shop.py | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 7695986..0661819 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -152,8 +152,9 @@ elif sys.argv[1] == "inventory": iamt = int(sys.argv[2]) iacct = load_item(sys.argv[3]) #TODO:use barcodes later print("Current state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) - shop.fix_inventory(item = iacct, amount = iamt) - print("New state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) - + if shop.fix_inventory(item = iacct, amount = iamt): + print("New state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) + else: + print ("No action needed amount is correct.") else: help() diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index f872328..ad24563 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -156,8 +156,12 @@ class Shop: item.debit(transaction, diff, "Inventory fix excess") self.excess.credit(transaction, buy_total, "Inventory fix excess " + item.name) self.db.commit() + return True elif amount_in_reality < amount_in_system: transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) item.credit(transaction, diff, "Inventory fix deficit") - self.deficit.credit(transaction, buy_total, "Inventory fix deficit " + item.name) + self.deficit.debit(transaction, buy_total, "Inventory fix deficit " + item.name) self.db.commit() + return True + else: + return False \ No newline at end of file From 06e1e15b7e0a90b0c8337b4831f66aefcf7b6ccf Mon Sep 17 00:00:00 2001 From: b00lean Date: Tue, 9 Jul 2013 09:29:08 +0200 Subject: [PATCH 025/112] Added excess and deficit into stats and added inventory interactive mode --- brmbar3/brmbar-cli.py | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 0661819..abf979f 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -30,8 +30,10 @@ Usage: brmbar-cli.py COMMAND ARGS... screen of the GUI. adduser USER Add user (debt) account with given username. - inventory NEW_AMOUNT1 ITEM1 NEW_AMOUNT2 ITEM2 + inventory ITEM1 NEW_AMOUNT1 ITEM2 NEW_AMOUNT2 Inventory recounting (fixing the number of items) + inventory-interactive + Launches interactive mode for performing inventory with barcode reader ! changecash +-AMT Create a custom transaction that updates nominal cash balance based on the actual cash balance counted @@ -139,6 +141,8 @@ elif sys.argv[1] == "stats": print("Profit: {}".format(shop.profits.balance_str())) print("Credit: {}".format(shop.credit_negbalance_str())) print("Inventory: {}".format(shop.inventory_balance_str())) + print("Excess: {}".format(shop.excess.negbalance_str())) + print("Deficit: {}".format(shop.deficit.balance_str())) elif sys.argv[1] == "adduser": acct = brmbar.Account.create(db, sys.argv[2], brmbar.Currency.load(db, id = 1), 'debt') @@ -149,12 +153,37 @@ elif sys.argv[1] == "inventory": if (len(sys.argv) % 2 != 0 or len(sys.argv) < 4): print ("Invalid number of parameters, count your parameters.") else: - iamt = int(sys.argv[2]) - iacct = load_item(sys.argv[3]) #TODO:use barcodes later - print("Current state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) - if shop.fix_inventory(item = iacct, amount = iamt): - print("New state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) + for i in range(2, len(sys.argv), 2): + iacct = load_item(sys.argv[i]) + iamt = int(sys.argv[i+1]) + print("Current state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) + if shop.fix_inventory(item = iacct, amount = iamt): + print("New state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) + else: + print ("No action needed amount is correct.") + + +elif sys.argv[1] == "inventory-interactive": + print("Inventory interactive mode. To exit interactive mode just enter empty barcode") + + keep_entering = True + while keep_entering: + barcode = str(input("Enter barcode:")) + if barcode == "": + break else: - print ("No action needed amount is correct.") + iacct = brmbar.Account.load_by_barcode(db, barcode) + amount = str(input("What is the amount of {} in reality current is {}:".format(iacct.name, iacct.balance()))) + if amount == "": + break + else: + iamt = int(amount) + print("Current state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) + if shop.fix_inventory(item = iacct, amount = iamt): + print("New state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) + else: + print ("No action needed amount is correct.") + print("End of processing. Bye") + else: help() From 0528a507696e9c92b821c6a34c92e5be97ce1e56 Mon Sep 17 00:00:00 2001 From: b00lean Date: Tue, 9 Jul 2013 09:50:58 +0200 Subject: [PATCH 026/112] Added support for cashchange command --- brmbar3/brmbar-cli.py | 19 +++++++++++++------ brmbar3/brmbar/Shop.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index abf979f..6fce513 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -34,12 +34,8 @@ Usage: brmbar-cli.py COMMAND ARGS... Inventory recounting (fixing the number of items) inventory-interactive Launches interactive mode for performing inventory with barcode reader -! changecash +-AMT - Create a custom transaction that updates nominal cash - balance based on the actual cash balance counted - in the cash box. If you found more money than expected, - use +amt, if you found less money than expected, - use -amt. + changecash AMT + Fixes the cash and puts money difference into excess or deficit account USER and ITEM may be barcodes or account ids. AMT may be both positive and negative amount (big difference to other @@ -184,6 +180,17 @@ elif sys.argv[1] == "inventory-interactive": else: print ("No action needed amount is correct.") print("End of processing. Bye") +elif sys.argv[1] == "changecash": + if (len(sys.argv) != 3): + print ("Invalid number of parameters, check your parameters.") + else: + print("Current Cash is : {}".format(shop.cash.balance_str())) + iamt = int(sys.argv[2]) + if shop.fix_cash(amount = iamt): + print("New Cash is : {}".format(shop.cash.balance_str())) + else: + print ("No action needed amount is the same.") + else: help() diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index ad24563..2578d43 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -163,5 +163,24 @@ class Shop: self.deficit.debit(transaction, buy_total, "Inventory fix deficit " + item.name) self.db.commit() return True + else: + return False + def fix_cash(self, amount): + amount_in_reality = amount + amount_in_system = self.cash.balance() + + diff = abs(amount_in_reality - amount_in_system) + if amount_in_reality > amount_in_system: + transaction = self._transaction(description = "BrmBar cash inventory fix of {} in system to {} in reality".format(amount_in_system, amount_in_reality)) + self.cash.debit(transaction, diff, "Inventory fix excess") + self.excess.credit(transaction, diff, "Inventory cash fix excess.") + self.db.commit() + return True + elif amount_in_reality < amount_in_system: + transaction = self._transaction(description = "BrmBar cash inventory fix of {} in system to {} in reality".format(amount_in_system, amount_in_reality)) + self.cash.credit(transaction, diff, "Inventory fix deficit") + self.deficit.debit(transaction, diff, "Inventory fix deficit.") + self.db.commit() + return True else: return False \ No newline at end of file From ac4f2f2c621bf2500652923046ef0ac1abfb828c Mon Sep 17 00:00:00 2001 From: brmbar Date: Tue, 6 Aug 2013 21:51:41 +0200 Subject: [PATCH 027/112] brmbar-cli inventory-interactive: fuckyou --- brmbar3/brmbar-cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 6fce513..19b3a6d 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -165,6 +165,7 @@ elif sys.argv[1] == "inventory-interactive": keep_entering = True while keep_entering: barcode = str(input("Enter barcode:")) + fuckyou = input("fuckyou") if barcode == "": break else: From d74ca2b6c09c68fc1c8244795fa8ff640653f0fb Mon Sep 17 00:00:00 2001 From: brmbar Date: Tue, 6 Aug 2013 21:51:58 +0200 Subject: [PATCH 028/112] Shop: Create null inventory fix transaction in case amount is correct --- brmbar3/brmbar/Shop.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 2578d43..5cb378c 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -164,6 +164,10 @@ class Shop: self.db.commit() return True else: + transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) + item.debit(transaction, 0, "Inventory fix - amount was correct") + item.credit(transaction, 0, "Inventory fix - amount was correct") + self.db.commit() return False def fix_cash(self, amount): amount_in_reality = amount @@ -183,4 +187,4 @@ class Shop: self.db.commit() return True else: - return False \ No newline at end of file + return False From e567d36e7761d7b3044aae2206434ef7e68c13b9 Mon Sep 17 00:00:00 2001 From: brmbar Date: Tue, 6 Aug 2013 22:18:23 +0200 Subject: [PATCH 029/112] USAGE: Add some useful SQL queries --- brmbar3/USAGE.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/brmbar3/USAGE.md b/brmbar3/USAGE.md index b2526c7..11eeec2 100644 --- a/brmbar3/USAGE.md +++ b/brmbar3/USAGE.md @@ -74,3 +74,22 @@ implementation yet; it's not entirely clear yet what is the proper way to do this from the accounting standpoint. In the meantime, you can use SQL INSERTs to manually create a transaction with appropriate transaction splits (see doc/architecture for details on splits). + + +Useful SQL queries +------------------ + +* Compute sum of sold stock: + + select sum(amount) from transactions + left join transaction_splits on transaction_splits.transaction = transactions.id + where description like '% sale %' and side = 'debit';` + +* List of items not covered by inventory check: + + select * from account_balances + where id not in (select account from transactions + left join transaction_splits on transaction_splits.transaction = transactions.id + where description like '% inventory %') + and acctype = 'inventory' + From 4f3bc87733bf229f05efd0fa1d93445190bfbfa7 Mon Sep 17 00:00:00 2001 From: brmbar Date: Tue, 6 Aug 2013 22:30:27 +0200 Subject: [PATCH 030/112] Shop.inventory_balance(): Use buy price, not sell price --- brmbar3/brmbar/Shop.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 5cb378c..0ec4282 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -130,7 +130,7 @@ class Shop: # might have been bought for a different price! Therefore, # we need to replace the command below with a complex SQL # statement that will... ugh, accounting is hard! - balance += inv.currency.convert(inv.balance(), self.currency) + balance += inv.balance() * inv.currency.rates(self.currency)[0] return balance def inventory_balance_str(self): return self.currency.str(self.inventory_balance()) From 617b466c774c37a31eced12db0888cab83c1930e Mon Sep 17 00:00:00 2001 From: brmbar Date: Sun, 11 Aug 2013 22:53:53 +0200 Subject: [PATCH 031/112] brmbar-cli: New command `consolidate` to finalize inventory check --- brmbar3/brmbar-cli.py | 8 ++++++++ brmbar3/brmbar/Shop.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 19b3a6d..d91a24b 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -36,6 +36,9 @@ Usage: brmbar-cli.py COMMAND ARGS... Launches interactive mode for performing inventory with barcode reader changecash AMT Fixes the cash and puts money difference into excess or deficit account + consolidate + Wraps up inventory + cash recounting, transferring the excess and + deficit accounts balance to the profits account and resetting them USER and ITEM may be barcodes or account ids. AMT may be both positive and negative amount (big difference to other @@ -191,6 +194,11 @@ elif sys.argv[1] == "changecash": print("New Cash is : {}".format(shop.cash.balance_str())) else: print ("No action needed amount is the same.") +elif sys.argv[1] == "consolidate": + if (len(sys.argv) != 2): + print ("Invalid number of parameters, check your parameters.") + else: + shop.consolidate() else: diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 0ec4282..bf74fef 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -188,3 +188,17 @@ class Shop: return True else: return False + def consolidate(self): + transaction = self._transaction(description = "BrmBar inventory consolidation") + + excess_balance = self.excess.balance() + if excess_balance != 0: + print("Excess balance {} debited to profit".format(-excess_balance)) + self.excess.debit(transaction, -excess_balance, "Excess balance added to profit.") + self.profits.debit(transaction, -excess_balance, "Excess balance added to profit.") + deficit_balance = self.deficit.balance() + if deficit_balance != 0: + print("Deficit balance {} credited to profit".format(deficit_balance)) + self.deficit.credit(transaction, deficit_balance, "Deficit balance removed from profit.") + self.profits.credit(transaction, deficit_balance, "Deficit balance removed from profit.") + self.db.commit() From 22503c242fe04ba6da8fcb49fc6c435d7eb01378 Mon Sep 17 00:00:00 2001 From: brmbar Date: Sun, 11 Aug 2013 23:07:26 +0200 Subject: [PATCH 032/112] USAGE: + recipe to list all cash transactions --- brmbar3/USAGE.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/brmbar3/USAGE.md b/brmbar3/USAGE.md index 11eeec2..7076bc9 100644 --- a/brmbar3/USAGE.md +++ b/brmbar3/USAGE.md @@ -83,7 +83,7 @@ Useful SQL queries select sum(amount) from transactions left join transaction_splits on transaction_splits.transaction = transactions.id - where description like '% sale %' and side = 'debit';` + where description like '% sale %' and side = 'debit'; * List of items not covered by inventory check: @@ -91,5 +91,10 @@ Useful SQL queries where id not in (select account from transactions left join transaction_splits on transaction_splits.transaction = transactions.id where description like '% inventory %') - and acctype = 'inventory' + and acctype = 'inventory'; +* List all cash transactions: + + select time, transactions.id, description, responsible, amount from transactions + left join transaction_splits on transaction_splits.transaction = transactions.id + where transaction_splits.account = 1; From 10d8a177239a5c179da343d20044ff8b7042bb30 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sat, 31 Aug 2013 19:11:14 +0200 Subject: [PATCH 033/112] SQL: Two new convenience views transaction_nicesplits and transaction_cashsums --- brmbar3/SQL | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/brmbar3/SQL b/brmbar3/SQL index bae5a15..82c22e4 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -93,3 +93,22 @@ CREATE VIEW account_balances AS 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 + SELECT ts.id AS id, ts.transaction AS transaction, ts.account AS account, + (CASE WHEN ts.side = 'credit' THEN -ts.amount ELSE ts.amount END) AS amount, + a.currency AS currency, ts.memo AS memo + FROM transaction_splits AS ts LEFT JOIN accounts AS a ON a.id = ts.account + ORDER BY ts.id; + +-- List transactions with summary information regarding their cash element +-- (except in case of transfers between cash and debt accounts, which will cancel out). +CREATE VIEW transaction_cashsums AS + SELECT t.id AS id, t.time AS time, SUM(ts.amount) AS cash, t.description AS description + FROM transactions AS t + LEFT JOIN transaction_nicesplits AS ts ON ts.transaction = t.id + LEFT JOIN accounts AS a ON a.id = ts.account + WHERE a.currency = (SELECT currency FROM accounts WHERE name = 'BrmBar Cash') + AND a.acctype IN ('cash', 'debt') + GROUP BY t.id ORDER BY t.id; From e6870f8b2a8cccb15d1ea21c8f03da371eb34b5d Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 6 Apr 2014 20:41:28 +0200 Subject: [PATCH 034/112] brmbar-web: New trivial read-only UI --- brmbar3/README.md | 1 + brmbar3/brmbar-web.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100755 brmbar3/brmbar-web.py diff --git a/brmbar3/README.md b/brmbar3/README.md index 0bf93ca..b113083 100644 --- a/brmbar3/README.md +++ b/brmbar3/README.md @@ -53,6 +53,7 @@ These UIs are provided: advanced functionality like inventory revision that is too tedious to implement in the Qt4 GUI and only the brmbar admins are expected to do these tasks. +* **brmbar-web**: A simple read-only web interface to the stock list. TODO ---- diff --git a/brmbar3/brmbar-web.py b/brmbar3/brmbar-web.py new file mode 100755 index 0000000..dd54a5e --- /dev/null +++ b/brmbar3/brmbar-web.py @@ -0,0 +1,29 @@ +#!/usr/bin/python3 + +import sys + +from brmbar import Database + +import brmbar + +from flask import * +app = Flask(__name__) +#app.debug = True + +@app.route('/stock') +def stock(): + # TODO: Use a fancy template. + # FIXME: XSS protection. + response = '' + for a in shop.account_list("inventory"): + response += '' % (a.id, a.name, a.balance()) + response += '
IdItem NameBal.
%d%s%d
' + return response + + +db = Database.Database("dbname=brmbar") +shop = brmbar.Shop.new_with_defaults(db) +currency = shop.currency + +if __name__ == '__main__': + app.run() From 400ef9e969b6a318f76ab34718be1f49fde1b746 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 6 Apr 2014 21:10:53 +0200 Subject: [PATCH 035/112] brmbar.Currency: Include utf8 fenc declaration, e.g. for python2 compatibility --- brmbar3/brmbar/Currency.py | 1 + 1 file changed, 1 insertion(+) diff --git a/brmbar3/brmbar/Currency.py b/brmbar3/brmbar/Currency.py index b382d77..d2216ca 100644 --- a/brmbar3/brmbar/Currency.py +++ b/brmbar3/brmbar/Currency.py @@ -1,3 +1,4 @@ +# vim: set fileencoding=utf8 class Currency: """ Currency From ae82675a60c89a04519e28a54c1a80d8840616be Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 6 Apr 2014 21:18:36 +0200 Subject: [PATCH 036/112] brmbar-web: Color rows by balance --- brmbar3/brmbar-web.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/brmbar3/brmbar-web.py b/brmbar3/brmbar-web.py index dd54a5e..11e496c 100755 --- a/brmbar3/brmbar-web.py +++ b/brmbar3/brmbar-web.py @@ -16,7 +16,13 @@ def stock(): # FIXME: XSS protection. response = '' for a in shop.account_list("inventory"): - response += '' % (a.id, a.name, a.balance()) + style = '' + balance = a.balance() + if balance == 0: + style = 'color: grey; font-style: italic' + elif balance < 0: + style = 'color: red' + response += '' % (style, a.id, a.name, balance) response += '
IdItem NameBal.
%d%s%d
%d%s%d
' return response From bf3fd3e842a6cc63d61c63245161f08803d798cd Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 6 Apr 2014 21:18:52 +0200 Subject: [PATCH 037/112] brmbar-web: Listen publicly --- brmbar3/brmbar-web.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brmbar3/brmbar-web.py b/brmbar3/brmbar-web.py index 11e496c..457a628 100755 --- a/brmbar3/brmbar-web.py +++ b/brmbar3/brmbar-web.py @@ -32,4 +32,4 @@ shop = brmbar.Shop.new_with_defaults(db) currency = shop.currency if __name__ == '__main__': - app.run() + app.run(host='0.0.0.0') From 65741c31f060d0cecf5475c7b8f771043e8f1108 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 6 Apr 2014 21:29:04 +0200 Subject: [PATCH 038/112] brmbar-web: By default hide out-of-stock items --- brmbar3/brmbar-web.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/brmbar3/brmbar-web.py b/brmbar3/brmbar-web.py index 457a628..0ec70dc 100755 --- a/brmbar3/brmbar-web.py +++ b/brmbar3/brmbar-web.py @@ -10,8 +10,8 @@ from flask import * app = Flask(__name__) #app.debug = True -@app.route('/stock') -def stock(): +@app.route('/stock/') +def stock(show_all=False): # TODO: Use a fancy template. # FIXME: XSS protection. response = '' @@ -19,13 +19,23 @@ def stock(): style = '' balance = a.balance() if balance == 0: + if not show_all: + continue style = 'color: grey; font-style: italic' elif balance < 0: style = 'color: red' response += '' % (style, a.id, a.name, balance) response += '
IdItem NameBal.
%d%s%d
' + if show_all: + response += '

(hide out-of-stock items)

' + else: + response += '

(show all items)

' return response +@app.route('/stock/all') +def stockall(): + return stock(show_all=True) + db = Database.Database("dbname=brmbar") shop = brmbar.Shop.new_with_defaults(db) From 6ed53b92aafa53d7a0ecd2244112dc6da6823a4c Mon Sep 17 00:00:00 2001 From: Stevko Date: Sun, 11 May 2014 02:38:53 +0200 Subject: [PATCH 039/112] Added Barcode for cash payment. Barcode is '_cash_'. --- brmbar3/SQL | 3 +++ brmbar3/brmbar-gui-qt4.py | 6 ++++++ brmbar3/brmbar-gui-qt4/ItemInfo.qml | 12 +++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/brmbar3/SQL b/brmbar3/SQL index 82c22e4..8623ed5 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -52,6 +52,9 @@ CREATE TABLE barcodes ( account INTEGER NOT NULL, FOREIGN KEY (account) REFERENCES accounts (id) ); +-- Barcode for cash +-- XXX Silently assume there is only one. +INSERT INTO barcodes (barcode, account) VALUES ('_cash_', (SELECT id FROM accounts WHERE acctype = 'cash')); CREATE SEQUENCE transactions_id_seq START WITH 1 INCREMENT BY 1; diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index 67773be..23e6d37 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -29,6 +29,10 @@ class ShopAdapter(QtCore.QObject): map["price"] = str(sell) return map + def acct_cash_map(self, acct): + map = acct.__dict__.copy() + return map + def acct_map(self, acct): if acct is None: return None @@ -36,6 +40,8 @@ class ShopAdapter(QtCore.QObject): return self.acct_debt_map(acct) elif acct.acctype == "inventory": return self.acct_inventory_map(acct) + elif acct.acctype == "cash": + return self.acct_cash_map(acct) else: return None diff --git a/brmbar3/brmbar-gui-qt4/ItemInfo.qml b/brmbar3/brmbar-gui-qt4/ItemInfo.qml index b15596f..a39e322 100644 --- a/brmbar3/brmbar-gui-qt4/ItemInfo.qml +++ b/brmbar3/brmbar-gui-qt4/ItemInfo.qml @@ -50,12 +50,18 @@ Item { status_text.setStatus("Unknown barcode", "#ff4444") return } - if (acct.acctype !== "debt") { + if (acct.acctype !== "debt" && acct.acctype !== "cash") { loadPageByAcct(acct) return } - var balance = shop.sellItem(dbid, acct.id) - status_text.setStatus("Sold! "+acct.name+"'s credit is "+balance+".", "#ffff7c") + + if (acct.acctype == "cash") { //Copied from BarButton.onButtonClick + shop.sellItemCash(dbid) + status_text.setStatus("Sold! Put " + price + " Kč in the money box.", "#ffff7c") + } else { + var balance = shop.sellItem(dbid, acct.id) + status_text.setStatus("Sold! "+acct.name+"'s credit is "+balance+".", "#ffff7c") + } loadPage("MainPage") } } From 8229b44774fc02f20130acc1405b63f82c62b710 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Fri, 6 Jun 2014 20:39:58 +0200 Subject: [PATCH 040/112] USAGE.md: Select for listing inventory items by their worth --- brmbar3/USAGE.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/brmbar3/USAGE.md b/brmbar3/USAGE.md index 7076bc9..5821232 100644 --- a/brmbar3/USAGE.md +++ b/brmbar3/USAGE.md @@ -98,3 +98,15 @@ Useful SQL queries select time, transactions.id, description, responsible, amount from transactions left join transaction_splits on transaction_splits.transaction = transactions.id where transaction_splits.account = 1; + +* List all inventory items ordered by their cummulative worth: + + select foo.*, foo.rate * -foo.crbalance as worth from + (select account_balances.*, + (select exchange_rates.rate from exchange_rates, accounts + where exchange_rates.target = accounts.currency + and accounts.id = account_balances.id + order by exchange_rates.valid_since limit 1) as rate + from account_balances where account_balances.acctype = 'inventory') + as foo order by worth; + From 664e7a5b3208537f25e95b6d6d2369cacdc0d810 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 22 Oct 2014 22:22:07 +0200 Subject: [PATCH 041/112] daily-summary.sh: New script --- brmbar3/daily-summary.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100755 brmbar3/daily-summary.sh diff --git a/brmbar3/daily-summary.sh b/brmbar3/daily-summary.sh new file mode 100755 index 0000000..d4817ed --- /dev/null +++ b/brmbar3/daily-summary.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# Add a crontab entry like: +# 5 4 * * * ~/brmbar/brmbar3/daily-summary.sh | mail -s "daily brmbar summary" rada@brmlab.cz +cd ~/brmbar/brmbar3 +./brmbar-cli.py stats +echo +echo "Time since last full inventory check: $(echo "select now()-time from transactions where description = 'BrmBar inventory consolidation' order by time desc limit 1;" | psql brmbar | tail -n +3 | head -n 1 | tr -s " ")" +echo +echo "Overflows: $(echo "SELECT name, -crbalance FROM account_balances WHERE name LIKE '%overflow%' AND crbalance != 0 ORDER BY name" | psql brmbar | tail -n +3 | grep '|' | tr -s " " | sed -e "s/ |/:/g" -e "s/$/;/" | tr -d "\n") TOTAL: $(echo "SELECT -SUM(crbalance) FROM account_balances WHERE name LIKE '%overflow%' AND crbalance != 0" | psql brmbar | tail -n +3 | head -n 1 | tr -s " ")" +echo +echo "Club Mate sold in last 24 hours: $(echo "select count(*) from transaction_cashsums where time > now() - '1 day'::INTERVAL and description like '%Club Mate%'" | psql brmbar | tail -n +3 | head -n 1 | tr -s " ") bottles" From 4c6558015fc9bbadfc525329fb7c1908bd2e4088 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Fri, 2 Jan 2015 19:35:00 +0100 Subject: [PATCH 042/112] daily-summary.sh: Add granatove mate to the count (Done in reality much earlier than now.) --- brmbar3/daily-summary.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brmbar3/daily-summary.sh b/brmbar3/daily-summary.sh index d4817ed..2fe58bd 100755 --- a/brmbar3/daily-summary.sh +++ b/brmbar3/daily-summary.sh @@ -8,4 +8,4 @@ echo "Time since last full inventory check: $(echo "select now()-time from trans echo echo "Overflows: $(echo "SELECT name, -crbalance FROM account_balances WHERE name LIKE '%overflow%' AND crbalance != 0 ORDER BY name" | psql brmbar | tail -n +3 | grep '|' | tr -s " " | sed -e "s/ |/:/g" -e "s/$/;/" | tr -d "\n") TOTAL: $(echo "SELECT -SUM(crbalance) FROM account_balances WHERE name LIKE '%overflow%' AND crbalance != 0" | psql brmbar | tail -n +3 | head -n 1 | tr -s " ")" echo -echo "Club Mate sold in last 24 hours: $(echo "select count(*) from transaction_cashsums where time > now() - '1 day'::INTERVAL and description like '%Club Mate%'" | psql brmbar | tail -n +3 | head -n 1 | tr -s " ") bottles" +echo "Club Mate sold in last 24 hours: $(echo "select count(*) from transaction_cashsums where time > now() - '1 day'::INTERVAL and (description like '%Club Mate%' or description like '%granatove mate%')" | psql brmbar | tail -n +3 | head -n 1 | tr -s " ") bottles" From d79dcb5a5c3f5c4c0f287f00c9b6d9c727fdf24a Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Fri, 2 Jan 2015 19:35:39 +0100 Subject: [PATCH 043/112] SQL transaction_cashsums: Update view to differentiate cash_credit, cash_debit This allows looking at credit changes as well. --- brmbar3/SQL | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/brmbar3/SQL b/brmbar3/SQL index 8623ed5..32a3cb1 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -108,10 +108,18 @@ CREATE VIEW transaction_nicesplits AS -- List transactions with summary information regarding their cash element -- (except in case of transfers between cash and debt accounts, which will cancel out). CREATE VIEW transaction_cashsums AS - SELECT t.id AS id, t.time AS time, SUM(ts.amount) AS cash, t.description AS description + SELECT t.id AS id, t.time AS time, SUM(credit_cash) AS cash_credit, SUM(debit_cash) AS cash_debit, t.description AS description FROM transactions AS t - LEFT JOIN transaction_nicesplits AS ts ON ts.transaction = t.id - LEFT JOIN accounts AS a ON a.id = ts.account - WHERE a.currency = (SELECT currency FROM accounts WHERE name = 'BrmBar Cash') - AND a.acctype IN ('cash', 'debt') + LEFT JOIN (SELECT cts.amount AS credit_cash, cts.transaction AS cts_t + FROM transaction_nicesplits AS cts + LEFT JOIN accounts AS a ON a.id = cts.account OR a.id = cts.account + WHERE a.currency = (SELECT currency FROM accounts WHERE name = 'BrmBar Cash') + AND a.acctype IN ('cash', 'debt') + AND cts.amount < 0) credit ON cts_t = t.id + LEFT JOIN (SELECT dts.amount AS debit_cash, dts.transaction AS dts_t + FROM transaction_nicesplits AS dts + LEFT JOIN accounts AS a ON a.id = dts.account OR a.id = dts.account + WHERE a.currency = (SELECT currency FROM accounts WHERE name = 'BrmBar Cash') + AND a.acctype IN ('cash', 'debt') + AND dts.amount > 0) debit ON dts_t = t.id GROUP BY t.id ORDER BY t.id; From 4604cd36c3f2bbe9e581cc186a372425f6ff1cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pinkava?= Date: Tue, 6 Jan 2015 17:41:16 +0100 Subject: [PATCH 044/112] SQL: add column for disabling unused accounts --- brmbar3/SQL | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/brmbar3/SQL b/brmbar3/SQL index 32a3cb1..7e640a6 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -37,7 +37,9 @@ CREATE TABLE accounts ( currency INTEGER NOT NULL, FOREIGN KEY (currency) REFERENCES currencies (id), - acctype account_type NOT NULL + acctype account_type NOT NULL, + + active BOOLEAN NOT NULL DEFAULT TRUE ); 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'); From 5de7bf7458d06b9db8bdcf0a0492e0404826e8ef Mon Sep 17 00:00:00 2001 From: niekt0 Date: Sat, 31 Jan 2015 21:09:52 +0100 Subject: [PATCH 045/112] Disabled default and inefective computation of the stock sumary --- brmbar3/brmbar/Shop.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index bf74fef..1c4ffb0 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -118,6 +118,7 @@ class Shop: def credit_negbalance_str(self): return self.currency.str(-self.credit_balance()) +# XXX causing extra heavy delay ( thousands of extra SQL queries ), disabled def inventory_balance(self): balance = 0 # Each inventory account has its own currency, @@ -132,8 +133,11 @@ class Shop: # statement that will... ugh, accounting is hard! balance += inv.balance() * inv.currency.rates(self.currency)[0] return balance + +# XXX bypass hack def inventory_balance_str(self): - return self.currency.str(self.inventory_balance()) + return "XXX" +# return self.currency.str(self.inventory_balance()) def account_list(self, acctype, like_str="%%"): """list all accounts (people or items, as per acctype)""" From 84881dece382c26446054a77d190d1fe5e0f9b68 Mon Sep 17 00:00:00 2001 From: Mrkva Date: Mon, 11 May 2015 02:04:11 +0200 Subject: [PATCH 046/112] * merge barcode-generator to master * quick howto on how current barcodes are generated --- barcode-generator/.gitignore | 2 + barcode-generator/barcode-generator.py | 51 ++++++++++++++++++++++++++ barcode-generator/howto.txt | 9 +++++ 3 files changed, 62 insertions(+) create mode 100644 barcode-generator/.gitignore create mode 100755 barcode-generator/barcode-generator.py create mode 100644 barcode-generator/howto.txt diff --git a/barcode-generator/.gitignore b/barcode-generator/.gitignore new file mode 100644 index 0000000..e6936f1 --- /dev/null +++ b/barcode-generator/.gitignore @@ -0,0 +1,2 @@ +barcodes*.svg +barcode-generator.txt diff --git a/barcode-generator/barcode-generator.py b/barcode-generator/barcode-generator.py new file mode 100755 index 0000000..f2d30a3 --- /dev/null +++ b/barcode-generator/barcode-generator.py @@ -0,0 +1,51 @@ +#!/usr/bin/python +# +# requires zint binary from zint package +# + +from subprocess import Popen, PIPE +import sys + +svghead = """ + + +""" + +svgfoot = """ +""" + +width = 5 +scalex = 0.8 +scaley = 0.8 + +p = 0 +i = 0 +j = 0 +f = None + +lines = sys.stdin.readlines() + +for idx in xrange(len(lines)): + items = lines[idx].strip().split(';') + if idx % 30 == 0: + if f and not f.closed: + f.write(svgfoot) + f.close() + f = open('barcodes' + str(p) + '.svg','w') + p += 1 + i = 0 + j = 0 + f.write(svghead) + elem = Popen(('./zint','--directsvg','--notext', '-d', items[1]), stdout = PIPE).communicate()[0].split('\n') + elem = elem[8:-2] + elem[0] = elem[0].replace('id="barcode"', 'transform="matrix(%f,0,0,%f,%f,%f)"' % (scalex, scaley, 50+i*140 , 180+j*140) ) + elem.insert(-1, ' %s' % items[0]) + f.write('\n'.join(elem)+'\n\n') + i += 1 + if i >= width: + i = 0 + j += 1 + +if not f.closed: + f.write(svgfoot) + f.close() diff --git a/barcode-generator/howto.txt b/barcode-generator/howto.txt new file mode 100644 index 0000000..4dfba63 --- /dev/null +++ b/barcode-generator/howto.txt @@ -0,0 +1,9 @@ +on brmbar: +select distinct barcode from barcodes b, transactions t, accounts a where t.responsible=a.id and time>'2015-01-01' and b.account=a.id order by barcode asc; + +run this locally and paste output of previous command: + +while read tmp; do echo "$tmp;$tmp";done|grep -v overflow|python2 ./barcode-generator.py + +print resulting SVG files + From 1c0e51a246065934177a33d297079ee23e48a74b Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 4 Jun 2015 22:57:20 +0200 Subject: [PATCH 047/112] brmbar-qt4: Add LIMIT_BALANCE(=-200) as a lower limit of user credit --- brmbar3/brmbar-gui-qt4.py | 13 +++++++++++++ brmbar3/brmbar-gui-qt4/ItemInfo.qml | 2 ++ 2 files changed, 15 insertions(+) diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index 23e6d37..429aeb6 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -8,6 +8,9 @@ from brmbar import Database import brmbar +# User credit balance limit; sale will fail when balance is below this limit. +LIMIT_BALANCE = -200 + class ShopAdapter(QtCore.QObject): """ Interface between QML and the brmbar package """ @@ -70,6 +73,11 @@ class ShopAdapter(QtCore.QObject): db.commit() return acct + @QtCore.Slot('QVariant', 'QVariant', result='QVariant') + def canSellItem(self, itemid, userid): + user = brmbar.Account.load(db, id = userid) + return -user.balance() > LIMIT_BALANCE + @QtCore.Slot('QVariant', 'QVariant', result='QVariant') def sellItem(self, itemid, userid): user = brmbar.Account.load(db, id = userid) @@ -99,6 +107,11 @@ class ShopAdapter(QtCore.QObject): db.commit() return balance + @QtCore.Slot('QVariant', result='QVariant') + def balance_user(self, userid): + user = brmbar.Account.load(db, id=userid) + return user.negbalance_str() + @QtCore.Slot(result='QVariant') def balance_cash(self): balance = shop.cash.balance_str() diff --git a/brmbar3/brmbar-gui-qt4/ItemInfo.qml b/brmbar3/brmbar-gui-qt4/ItemInfo.qml index a39e322..11e959d 100644 --- a/brmbar3/brmbar-gui-qt4/ItemInfo.qml +++ b/brmbar3/brmbar-gui-qt4/ItemInfo.qml @@ -58,6 +58,8 @@ Item { if (acct.acctype == "cash") { //Copied from BarButton.onButtonClick shop.sellItemCash(dbid) status_text.setStatus("Sold! Put " + price + " Kč in the money box.", "#ffff7c") + } else if (!shop.canSellItem(dbid, acct.id)) { + status_text.setStatus("NOT SOLD! "+acct.name+"'s credit is TOO LOW: "+shop.balance_user(acct.id), "#ff4444") } else { var balance = shop.sellItem(dbid, acct.id) status_text.setStatus("Sold! "+acct.name+"'s credit is "+balance+".", "#ffff7c") From 6cc95ab6808760f57a7716abccf8c71b52b36e9e Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 4 Jun 2015 23:06:57 +0200 Subject: [PATCH 048/112] brmbar-qt4: Add ALERT_SCRIPT support to call external hook on low balance --- brmbar3/brmbar-gui-qt4.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index 429aeb6..a351bee 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 import sys +import subprocess from PySide import QtCore, QtGui, QtDeclarative @@ -10,6 +11,12 @@ import brmbar # User credit balance limit; sale will fail when balance is below this limit. LIMIT_BALANCE = -200 +# When below this credit balance, an alert hook script (see below) is run. +ALERT_BALANCE = 0 +# This script is executed when a user is buying things and their balance is +# below LIMIT_BALANCE (with argument "limit") or below ALERT_BALANCE +# (with argument "alert"). +ALERT_SCRIPT = "./alert.sh" class ShopAdapter(QtCore.QObject): @@ -76,7 +83,14 @@ class ShopAdapter(QtCore.QObject): @QtCore.Slot('QVariant', 'QVariant', result='QVariant') def canSellItem(self, itemid, userid): user = brmbar.Account.load(db, id = userid) - return -user.balance() > LIMIT_BALANCE + if -user.balance() > ALERT_BALANCE: + return True + elif -user.balance() > LIMIT_BALANCE: + subprocess.call(["sh", ALERT_SCRIPT, "alert"]) + return True + else: + subprocess.call(["sh", ALERT_SCRIPT, "limit"]) + return False @QtCore.Slot('QVariant', 'QVariant', result='QVariant') def sellItem(self, itemid, userid): From 934873c2fc73fc9cff1bfd5201f0e69f8ec7ebb9 Mon Sep 17 00:00:00 2001 From: Mrkva Date: Wed, 1 Jul 2015 01:06:59 +0200 Subject: [PATCH 049/112] Old uncommited changes, restock from cli, "cleanup bounty" feature --- brmbar3/alert.sh | 6 ++++++ brmbar3/brmbar-cli.py | 11 ++++++++++- brmbar3/brmbar-gui-qt4/MainPage.qml | 10 ++++++++++ brmbar3/brmbar-web.py | 2 +- brmbar3/uklid-refill.sh | 6 ++++++ brmbar3/uklid-watchdog.sh | 18 ++++++++++++++++++ 6 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 brmbar3/alert.sh create mode 100644 brmbar3/uklid-refill.sh create mode 100644 brmbar3/uklid-watchdog.sh diff --git a/brmbar3/alert.sh b/brmbar3/alert.sh new file mode 100644 index 0000000..af20fe3 --- /dev/null +++ b/brmbar3/alert.sh @@ -0,0 +1,6 @@ +#!/bin/sh +if [ "$1" = "alert" ]; then + mplayer ~/trombone.wav & +else + mplayer ~/much.wav & +fi diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index d91a24b..cb4cac9 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -17,6 +17,7 @@ Usage: brmbar-cli.py COMMAND ARGS... changecredit USER +-AMT sellitem USER ITEM +-AMT You can use negative AMT to undo a sale. + restock ITEM AMT userinfo USER iteminfo ITEM @@ -199,7 +200,15 @@ elif sys.argv[1] == "consolidate": print ("Invalid number of parameters, check your parameters.") else: shop.consolidate() - +elif sys.argv[1] == "restock": + if (len(sys.argv) != 4): + print ("Invalid number of parameters, check your parameters.") + else: + iacct = load_item(sys.argv[2]) + amt = int(sys.argv[3]) + cash = shop.buy_for_cash(iacct, amt); + print("Old amount {}, increased by {}, take {} from cashbox".format(iacct.balance(), amt, cash)) + else: help() diff --git a/brmbar3/brmbar-gui-qt4/MainPage.qml b/brmbar3/brmbar-gui-qt4/MainPage.qml index 8155943..effc9ad 100644 --- a/brmbar3/brmbar-gui-qt4/MainPage.qml +++ b/brmbar3/brmbar-gui-qt4/MainPage.qml @@ -43,4 +43,14 @@ Item { loadPage("Management") } } + + BarButton { + x: 65 + y: 438 + width: 1150 + text: "* Mroze a Termixy najdes v lednici *" + } + + + } diff --git a/brmbar3/brmbar-web.py b/brmbar3/brmbar-web.py index 0ec70dc..5d84378 100755 --- a/brmbar3/brmbar-web.py +++ b/brmbar3/brmbar-web.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/python import sys diff --git a/brmbar3/uklid-refill.sh b/brmbar3/uklid-refill.sh new file mode 100644 index 0000000..898a081 --- /dev/null +++ b/brmbar3/uklid-refill.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +if [ `./brmbar-cli.py iteminfo uklid|grep -o '[0-9]*.[0-9]* pcs'|cut -d '.' -f 1` -eq 0 ]; then + BOUNTY=`./brmbar-cli.py restock uklid 1 | grep -o 'take -[0-9]*'|grep -o '[0-9]*'` + echo "Brmlab cleanup bounty for ${BOUNTY}CZK!!!"|ssh jenda@fry.hrach.eu +fi diff --git a/brmbar3/uklid-watchdog.sh b/brmbar3/uklid-watchdog.sh new file mode 100644 index 0000000..c0fd55b --- /dev/null +++ b/brmbar3/uklid-watchdog.sh @@ -0,0 +1,18 @@ +#!/bin/bash +LASTIDF=/home/brmlab/uklid.last + +LASTID=`cat $LASTIDF 2>/dev/null || echo 0` + + +RES=`psql brmbar -Atq -c "select id,description from transactions where id>$LASTID and description like 'BrmBar sale of 1x uklid%' LIMIT 1;"` +if [ ! -z "$RES" ]; then + LASTID=`echo "$RES"|cut -d '|' -f 1` + echo $LASTID > $LASTIDF + + WINNER=`echo "$RES"|grep -o 'to [^ ]*'|cut -d ' ' -f 2` + if [ -z "$WINNER" ]; then + WINNER="anonymous hunter" + fi + echo "Brmlab cleanup bounty was claimed by $WINNER! Thanks!"|ssh jenda@fry.hrach.eu +fi + From fe80200e49a4af871709393057d388366fe422f1 Mon Sep 17 00:00:00 2001 From: TrimenZ Date: Wed, 14 Oct 2015 22:39:36 +0200 Subject: [PATCH 050/112] alert.sh Add charge sound. --- brmbar3/alert.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/brmbar3/alert.sh b/brmbar3/alert.sh index af20fe3..0b643bc 100644 --- a/brmbar3/alert.sh +++ b/brmbar3/alert.sh @@ -1,6 +1,7 @@ #!/bin/sh -if [ "$1" = "alert" ]; then - mplayer ~/trombone.wav & -else - mplayer ~/much.wav & -fi + +case $1 in +alert) mplayer ~/trombone.wav & ;; +limit) mplayer ~/much.wav & ;; +charge) mplayer ~/charge.wav & ;; +esac From 906a3b62ed46e8ca6821ac15eb7dd460c3e28dae Mon Sep 17 00:00:00 2001 From: TrimenZ Date: Wed, 14 Oct 2015 22:44:12 +0200 Subject: [PATCH 051/112] brmbar-qt4: Add charge sound .. --- brmbar3/brmbar-gui-qt4.py | 1 + 1 file changed, 1 insertion(+) diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index a351bee..5b63552 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -107,6 +107,7 @@ class ShopAdapter(QtCore.QObject): @QtCore.Slot('QVariant', 'QVariant', result='QVariant') def chargeCredit(self, credit, userid): + subprocess.call(["sh", ALERT_SCRIPT, "charge"]) user = brmbar.Account.load(db, id = userid) shop.add_credit(credit = credit, user = user) balance = user.negbalance_str() From a162e544c1a74e1d8963d7c144b37874699c355e Mon Sep 17 00:00:00 2001 From: brmbar Date: Sat, 31 Oct 2015 21:16:01 +0100 Subject: [PATCH 052/112] alert.sh: +x --- brmbar3/alert.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 brmbar3/alert.sh diff --git a/brmbar3/alert.sh b/brmbar3/alert.sh old mode 100644 new mode 100755 From 5c84bd6a8fcd8c38478f2aa08f0cf97feb53b373 Mon Sep 17 00:00:00 2001 From: brmbar Date: Sat, 31 Oct 2015 22:02:12 +0100 Subject: [PATCH 053/112] GUI: Support for money transfer (by sachy + pasky) --- brmbar3/brmbar-gui-qt4/Transfer.qml | 174 ++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 brmbar3/brmbar-gui-qt4/Transfer.qml diff --git a/brmbar3/brmbar-gui-qt4/Transfer.qml b/brmbar3/brmbar-gui-qt4/Transfer.qml new file mode 100644 index 0000000..d6a3a4e --- /dev/null +++ b/brmbar3/brmbar-gui-qt4/Transfer.qml @@ -0,0 +1,174 @@ +import QtQuick 1.1 + +Item { + id: page + anchors.fill: parent + + property variant userfrom: "" + property variant uidfrom: "" + property variant userto: "" + property variant uidto: "" + property string amount: amount_pad.enteredText + + BarcodeInput { + color: "#00ff00" /* just for debugging */ + focus: !(parent.userfrom != "" && parent.userto != "") + onAccepted: { + var acct = shop.barcodeInput(text) + text = "" + if (typeof(acct) == "undefined") { + status_text.setStatus("Unknown barcode", "#ff4444") + return + } + if (acct.acctype == "debt") { + if (userfrom == "") { + userfrom = acct.name + uidfrom = acct.id + } else { + userto = acct.name + uidto = acct.id + } + } else if (acct.acctype == "recharge") { + amount = acct.amount + } else { + status_text.setStatus("Unknown barcode", "#ff4444") + } + } + } + + Item { + id: amount_row + visible: parent.userfrom != "" && parent.userto != "" + x: 65; + y: 166; + width: 890 + height: 60 + + Text { + id: item_sellprice_label + x: 0 + y: 0 + height: 60 + width: 200 + color: "#ffffff" + text: "Money Amount:" + verticalAlignment: Text.AlignVCenter + font.pixelSize: 0.768 * 46 + } + + Text { + id: amount_input + x: 320 + y: 0 + height: 60 + width: 269 + color: "#ffff7c" + text: amount + horizontalAlignment: Text.AlignRight + verticalAlignment: Text.AlignVCenter + font.pixelSize: 0.768 * 122 + } + } + + BarNumPad { + id: amount_pad + x: 65 + y: 239 + visible: parent.userfrom != "" && parent.userto != "" + focus: parent.userfrom != "" && parent.userto != "" + Keys.onReturnPressed: { transfer.buttonClick() } + Keys.onEscapePressed: { cancel.buttonClick() } + } + + BarTextHint { + id: barcode_row + x: 65 + y: parent.userfrom == "" ? 314 : 414 + hint_goal: (parent.userfrom == "" ? "Take money from:" : parent.userto == "" ? "Give money to:" : parent.amount == "" ? "Specify amount" : "") + hint_action: (parent.userfrom == "" || parent.userto == "" ? "Scan barcode now" : (parent.amount ? "" : "(or scan barcode now)")) + } + + Text { + id: legend + visible: !(parent.userfrom != "" && parent.userto != "") + x: 65 + y: 611 + height: 154 + width: 894 + color: "#71cccc" + text: "This is for transfering credit between two brmbar users.\n May be used instead of *check next club-mate to me*." + wrapMode: Text.WordWrap + horizontalAlignment: Text.AlignHCenter + font.pixelSize: 0.768 * 27 + } + + Text { + id: item_name + x: 422 + y: 156 + width: 537 + height: 80 + color: "#ffffff" + text: parent.userfrom ? parent.userfrom + " →" : "Money Transfer" + wrapMode: Text.WordWrap + horizontalAlignment: Text.AlignRight + verticalAlignment: Text.AlignVCenter + font.pixelSize: 0.768 * 60 + } + + Text { + id: item_name2 + x: 422 + y: 256 + width: 537 + height: 80 + color: "#ffffff" + text: parent.userto ? "→ " + parent.userto : "" + wrapMode: Text.WordWrap + horizontalAlignment: Text.AlignRight + verticalAlignment: Text.AlignVCenter + font.pixelSize: 0.768 * 60 + } + + BarButton { + id: transfer + x: 65 + y: 838 + width: 360 + text: "Transfer" + onButtonClick: { + if (userfrom == "") { + status_text.setStatus("Select FROM account.", "#ff4444") + return + } + if (userto == "") { + status_text.setStatus("Select TO account.", "#ff4444") + return + } + if (amount == "") { + status_text.setStatus("Enter amount.", "#ff4444") + return + } + var amount_str = shop.newTransfer(uidfrom, uidto, amount) + if (typeof(amount_str) == "undefined") { + status_text.setStatus("Transfer error.", "#ff4444") + return + } + + status_text.setStatus("Transferred " + amount_str + " from " + userfrom + " to " + userto, "#ffff7c") + loadPage("MainPage") + } + } + + BarButton { + id: cancel + x: 855 + y: 838 + width: 360 + text: "Cancel" + onButtonClick: { + status_text.setStatus("Transfer cancelled", "#ff4444") + loadPage("MainPage") + } + } +} From 099d775102bff5a46ccfbdf0333f7d84d7b99447 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 13 Dec 2015 16:10:03 +0100 Subject: [PATCH 054/112] Add missing collateral changes for Transfer --- brmbar3/brmbar-gui-qt4.py | 8 ++++++++ brmbar3/brmbar-gui-qt4/MainPage.qml | 13 ++++++++++--- brmbar3/brmbar/Shop.py | 4 ++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index 5b63552..a41fd2c 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -122,6 +122,14 @@ class ShopAdapter(QtCore.QObject): db.commit() return balance + @QtCore.Slot('QVariant', 'QVariant', 'QVariant', result='QVariant') + def newTransfer(self, uidfrom, uidto, amount): + ufrom = brmbar.Account.load(db, id=uidfrom) + uto = brmbar.Account.load(db, id=uidto) + shop.transfer_credit(ufrom, uto, amount = amount) + db.commit() + return currency.str(float(amount)) + @QtCore.Slot('QVariant', result='QVariant') def balance_user(self, userid): user = brmbar.Account.load(db, id=userid) diff --git a/brmbar3/brmbar-gui-qt4/MainPage.qml b/brmbar3/brmbar-gui-qt4/MainPage.qml index effc9ad..6e555eb 100644 --- a/brmbar3/brmbar-gui-qt4/MainPage.qml +++ b/brmbar3/brmbar-gui-qt4/MainPage.qml @@ -33,6 +33,16 @@ Item { } } + BarButton { + x: 450 + y: 838 + width: 360 + text: "Transfer" + onButtonClick: { + loadPage("Transfer") + } + } + BarButton { id: management x: 855 @@ -50,7 +60,4 @@ Item { width: 1150 text: "* Mroze a Termixy najdes v lednici *" } - - - } diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 1c4ffb0..55c26ba 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -78,6 +78,10 @@ class Shop: user.debit(transaction, credit, "Credit withdrawal") self.db.commit() + def transfer_credit(self, userfrom, userto, amount): + self.add_credit(amount, userto) + self.withdraw_credit(amount, userfrom) + def buy_for_cash(self, item, amount = 1): # Buy: Currency conversion from item currency to shop currency (buy, sell) = item.currency.rates(self.currency) From 76bb2cac98a3ff33307f97e169d85d24b01420d4 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sun, 13 Dec 2015 16:10:34 +0100 Subject: [PATCH 055/112] brmbar-cli help: Nicer inventorization split --- brmbar3/brmbar-cli.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index cb4cac9..a2d6f6a 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -31,15 +31,18 @@ Usage: brmbar-cli.py COMMAND ARGS... screen of the GUI. adduser USER Add user (debt) account with given username. - inventory ITEM1 NEW_AMOUNT1 ITEM2 NEW_AMOUNT2 - Inventory recounting (fixing the number of items) - inventory-interactive - Launches interactive mode for performing inventory with barcode reader - changecash AMT - Fixes the cash and puts money difference into excess or deficit account - consolidate - Wraps up inventory + cash recounting, transferring the excess and - deficit accounts balance to the profits account and resetting them + +3. Inventorization + + inventory ITEM1 NEW_AMOUNT1 ITEM2 NEW_AMOUNT2 + Inventory recounting (fixing the number of items) + inventory-interactive + Launches interactive mode for performing inventory with barcode reader + changecash AMT + Fixes the cash and puts money difference into excess or deficit account + consolidate + Wraps up inventory + cash recounting, transferring the excess and + deficit accounts balance to the profits account and resetting them USER and ITEM may be barcodes or account ids. AMT may be both positive and negative amount (big difference to other From feec2c9ac42de3d202ec64802f4df264456d27cc Mon Sep 17 00:00:00 2001 From: brmbar Date: Sun, 13 Dec 2015 16:36:21 +0100 Subject: [PATCH 056/112] brmbar-cli restock: Print correct old balance --- brmbar3/brmbar-cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index a2d6f6a..ca99a73 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -208,9 +208,10 @@ elif sys.argv[1] == "restock": print ("Invalid number of parameters, check your parameters.") else: iacct = load_item(sys.argv[2]) + oldbal = iacct.balance() amt = int(sys.argv[3]) cash = shop.buy_for_cash(iacct, amt); - print("Old amount {}, increased by {}, take {} from cashbox".format(iacct.balance(), amt, cash)) + print("Old amount {}, increased by {}, take {} from cashbox".format(oldbal, amt, cash)) else: From 3ea61f01d7b18de9c0b385f4458ad51c502e6b9f Mon Sep 17 00:00:00 2001 From: brmbar Date: Sun, 13 Dec 2015 16:36:50 +0100 Subject: [PATCH 057/112] brmbar-cli undo: New feature --- brmbar3/brmbar-cli.py | 7 +++++++ brmbar3/brmbar/Shop.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index ca99a73..28fbe87 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -31,6 +31,9 @@ Usage: brmbar-cli.py COMMAND ARGS... screen of the GUI. adduser USER Add user (debt) account with given username. + undo TRANSID + Commit a transaction that reverses all splits of a transaction with + a given id (to find out that id: select * from transaction_cashsums;) 3. Inventorization @@ -152,6 +155,10 @@ elif sys.argv[1] == "adduser": acct.add_barcode(sys.argv[2]) # will commit print("{}: id {}".format(acct.name, acct.id)); +elif sys.argv[1] == "undo": + newtid = shop.undo(int(sys.argv[2])) + print("Transaction %d undone by reverse transaction %d" % (int(sys.argv[2]), newtid)) + elif sys.argv[1] == "inventory": if (len(sys.argv) % 2 != 0 or len(sys.argv) < 4): print ("Invalid number of parameters, count your parameters.") diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 55c26ba..40b7872 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -177,6 +177,7 @@ class Shop: item.credit(transaction, 0, "Inventory fix - amount was correct") self.db.commit() return False + def fix_cash(self, amount): amount_in_reality = amount amount_in_system = self.cash.balance() @@ -196,6 +197,7 @@ class Shop: return True else: return False + def consolidate(self): transaction = self._transaction(description = "BrmBar inventory consolidation") @@ -210,3 +212,16 @@ class Shop: self.deficit.credit(transaction, deficit_balance, "Deficit balance removed from profit.") self.profits.credit(transaction, deficit_balance, "Deficit balance removed from profit.") self.db.commit() + + def undo(self, oldtid): + description = self.db.execute_and_fetch("SELECT description FROM transactions WHERE id = %s", [oldtid])[0] + description = 'undo %d (%s)' % (oldtid, description) + + transaction = self._transaction(description=description) + for split in self.db.execute_and_fetchall("SELECT id, side, account, amount, memo FROM transaction_splits WHERE transaction = %s", [oldtid]): + splitid, side, account, amount, memo = split + memo = 'undo %d (%s)' % (splitid, memo) + amount = -amount + self.db.execute("INSERT INTO transaction_splits (transaction, side, account, amount, memo) VALUES (%s, %s, %s, %s, %s)", [transaction, side, account, amount, memo]) + self.db.commit() + return transaction From 7a8e4ef794c2d65bee139d54fb1f7cffbf226155 Mon Sep 17 00:00:00 2001 From: Pavel Ruzicka Date: Tue, 15 Dec 2015 02:54:46 +0100 Subject: [PATCH 058/112] mplayer -really-quiet --- brmbar3/alert.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/brmbar3/alert.sh b/brmbar3/alert.sh index 0b643bc..05496e6 100755 --- a/brmbar3/alert.sh +++ b/brmbar3/alert.sh @@ -1,7 +1,7 @@ #!/bin/sh case $1 in -alert) mplayer ~/trombone.wav & ;; -limit) mplayer ~/much.wav & ;; -charge) mplayer ~/charge.wav & ;; +alert) mplayer -really-quiet ~/trombone.wav & ;; +limit) mplayer -really-quiet ~/much.wav & ;; +charge) mplayer -really-quiet ~/charge.wav & ;; esac From edc99f1ff9ed2f9c95b35b9080c544c5ac8193de Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 4 Jan 2016 20:35:43 +0100 Subject: [PATCH 059/112] brmbar-cli.py docs: All commands are implemented --- brmbar3/brmbar-cli.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 28fbe87..427c4dd 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -53,9 +53,7 @@ user interfaces; you can e.g. undo a sale!). For users, you can use their name as USER as their username is also the barcode. For items, use listitems command first -to find out the item id. - -Commands prefixed with ! are not implemented yet.""") +to find out the item id.""") sys.exit(1) From 6b67dd372e92d82176178dd6a49103bdadbf1b87 Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 4 Jan 2016 20:39:51 +0100 Subject: [PATCH 060/112] brmbar-cli.py inventory-interactive: Try to detect mistaken barcode scan --- brmbar3/brmbar-cli.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 427c4dd..1e63adf 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -182,16 +182,18 @@ elif sys.argv[1] == "inventory-interactive": break else: iacct = brmbar.Account.load_by_barcode(db, barcode) - amount = str(input("What is the amount of {} in reality current is {}:".format(iacct.name, iacct.balance()))) + amount = str(input("What is the amount of {} in reality (expected: {} pcs):".format(iacct.name, iacct.balance()))) if amount == "": break + elif int(amount) > 10000: + print("Ignoring too high amount {}, assuming barcode was mistakenly scanned instead".format(amount)) else: iamt = int(amount) print("Current state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) if shop.fix_inventory(item = iacct, amount = iamt): print("New state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) else: - print ("No action needed amount is correct.") + print("No action needed, amount is correct.") print("End of processing. Bye") elif sys.argv[1] == "changecash": if (len(sys.argv) != 3): From 2bbb46d4eab6f74188d1860e8a4334dc46fc5d0f Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 4 Jan 2016 20:41:19 +0100 Subject: [PATCH 061/112] brmbar-cli.py inventory-interactive: Tidy up code --- brmbar3/brmbar-cli.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 1e63adf..0b4fc42 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -174,27 +174,26 @@ elif sys.argv[1] == "inventory": elif sys.argv[1] == "inventory-interactive": print("Inventory interactive mode. To exit interactive mode just enter empty barcode") - keep_entering = True - while keep_entering: + while True: barcode = str(input("Enter barcode:")) fuckyou = input("fuckyou") if barcode == "": break + iacct = brmbar.Account.load_by_barcode(db, barcode) + amount = str(input("What is the amount of {} in reality (expected: {} pcs):".format(iacct.name, iacct.balance()))) + if amount == "": + break + elif int(amount) > 10000: + print("Ignoring too high amount {}, assuming barcode was mistakenly scanned instead".format(amount)) else: - iacct = brmbar.Account.load_by_barcode(db, barcode) - amount = str(input("What is the amount of {} in reality (expected: {} pcs):".format(iacct.name, iacct.balance()))) - if amount == "": - break - elif int(amount) > 10000: - print("Ignoring too high amount {}, assuming barcode was mistakenly scanned instead".format(amount)) + iamt = int(amount) + print("Current state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) + if shop.fix_inventory(item = iacct, amount = iamt): + print("New state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) else: - iamt = int(amount) - print("Current state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) - if shop.fix_inventory(item = iacct, amount = iamt): - print("New state {} (id {}): {} pcs".format(iacct.name, iacct.id, iacct.balance())) - else: - print("No action needed, amount is correct.") + print("No action needed, amount is correct.") print("End of processing. Bye") + elif sys.argv[1] == "changecash": if (len(sys.argv) != 3): print ("Invalid number of parameters, check your parameters.") @@ -205,11 +204,13 @@ elif sys.argv[1] == "changecash": print("New Cash is : {}".format(shop.cash.balance_str())) else: print ("No action needed amount is the same.") + elif sys.argv[1] == "consolidate": if (len(sys.argv) != 2): print ("Invalid number of parameters, check your parameters.") else: shop.consolidate() + elif sys.argv[1] == "restock": if (len(sys.argv) != 4): print ("Invalid number of parameters, check your parameters.") From 9e95724556a511f30f12fa01bc4ce8de99f7cab4 Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 4 Jan 2016 20:49:08 +0100 Subject: [PATCH 062/112] brmbar-cli.py sellitem: Allow selling for cash --- brmbar3/brmbar-cli.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 0b4fc42..86332e2 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -15,7 +15,7 @@ Usage: brmbar-cli.py COMMAND ARGS... 1. Commands pertaining the standard operation showcredit USER changecredit USER +-AMT - sellitem USER ITEM +-AMT + sellitem {USER|"cash"} ITEM +-AMT You can use negative AMT to undo a sale. restock ITEM AMT userinfo USER @@ -105,14 +105,20 @@ elif sys.argv[1] == "changecredit": print("{}: {}".format(acct.name, acct.negbalance_str())) elif sys.argv[1] == "sellitem": - uacct = load_user(sys.argv[2]) + if sys.argv[2] == "cash": + uacct = shop.cash + else: + uacct = load_user(sys.argv[2]) iacct = load_item(sys.argv[3]) amt = int(sys.argv[4]) if amt > 0: - shop.sell(item = iacct, user = uacct, amount = amt) + if uacct == shop.cash: + shop.sell_for_cash(item = iacct, amount = amt) + else: + shop.sell(item = iacct, user = uacct, amount = amt) elif amt < 0: shop.undo_sale(item = iacct, user = uacct, amount = -amt) - print("{}: {}".format(uacct.name, uacct.negbalance_str())) + print("{}: {}".format(uacct.name, uacct.balance_str() if uacct == shop.cash else uacct.negbalance_str())) print("{}: {}".format(iacct.name, iacct.balance_str())) elif sys.argv[1] == "userinfo": From 4494dafbb889c62987b434be8abefa06b7a32b5b Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 4 Jan 2016 21:04:35 +0100 Subject: [PATCH 063/112] Shop.inventory_balance(): Tidy up, commented out code for easy per-item balance dumps --- brmbar3/brmbar/Shop.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 40b7872..4822bdd 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -135,13 +135,16 @@ class Shop: # might have been bought for a different price! Therefore, # we need to replace the command below with a complex SQL # statement that will... ugh, accounting is hard! - balance += inv.balance() * inv.currency.rates(self.currency)[0] + b = inv.balance() * inv.currency.rates(self.currency)[0] + # if b != 0: + # print(str(b) + ',' + inv.name) + balance += b return balance # XXX bypass hack def inventory_balance_str(self): - return "XXX" -# return self.currency.str(self.inventory_balance()) + # return self.currency.str(self.inventory_balance()) + return "XXX" def account_list(self, acctype, like_str="%%"): """list all accounts (people or items, as per acctype)""" From e3a4fe880d3221d863685bdcb2ef3ded75949364 Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 4 Jan 2016 21:04:49 +0100 Subject: [PATCH 064/112] brmbar-cli.py docs: Two examples --- brmbar3/brmbar-cli.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 86332e2..be38c5f 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -53,7 +53,21 @@ user interfaces; you can e.g. undo a sale!). For users, you can use their name as USER as their username is also the barcode. For items, use listitems command first -to find out the item id.""") +to find out the item id. + +EXAMPLES: + +Transfer 35Kc from pasky to sachy: + + $ ./brmbar-cli.py changecredit pasky -35 + $ ./brmbar-cli.py changecredit sachy +35 + +Buy one RaspberryPi for cash from commandline: + + $ ./brmbar-cli.py listitems | grep -i raspberry + Raspberry Pi 2 1277 1.00 pcs + $ ./brmbar-cli.py sellitem cash 1277 1 +""") sys.exit(1) From 0c349f6c6abb8b047134d93d95bd018ccd423ad4 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Tue, 5 Jan 2016 22:00:03 +0100 Subject: [PATCH 065/112] USAGE: A few new scenarios for Administrative Usage --- brmbar3/USAGE.md | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/brmbar3/USAGE.md b/brmbar3/USAGE.md index 5821232..5f31c5e 100644 --- a/brmbar3/USAGE.md +++ b/brmbar3/USAGE.md @@ -68,12 +68,26 @@ Administrative Usage account (and so will typing "joehacker" on a physical keyboard). * If your inventory stock count or cash box amount does not match -the in-system data, you will need to make a corrective transaction. -In the future, brmbar-cli.py will support this, but there is no -implementation yet; it's not entirely clear yet what is the proper -way to do this from the accounting standpoint. In the meantime, you -can use SQL INSERTs to manually create a transaction with appropriate -transaction splits (see doc/architecture for details on splits). + the in-system data, you will need to make a corrective transaction. + To fix cash amount to reality in which you counted 1234Kč, use + + ./brmbar-cli.py fixcash 1234 + + whereas to fix amount of a particular stock, use + + ./brmbar-cli.py inventory-interactive + + then scan the item barcode and then enter the right amount. + +* If you want to view recent transactions, run + + psql brmbar + select * from transaction_cashsums; + +* If you want to undo a transaction, get its id (using the select above) + and run + + ./brmbar-cli.py undo ID Useful SQL queries From 11eaecfccf23b3d3c6a9fc362d383ede0aaaea91 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 6 Jan 2016 02:00:56 +0100 Subject: [PATCH 066/112] brmbar-cli changecash -> fixcash (changecash remains an alias) --- brmbar3/brmbar-cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index be38c5f..d12081c 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -41,7 +41,7 @@ Usage: brmbar-cli.py COMMAND ARGS... Inventory recounting (fixing the number of items) inventory-interactive Launches interactive mode for performing inventory with barcode reader - changecash AMT + fixcash AMT Fixes the cash and puts money difference into excess or deficit account consolidate Wraps up inventory + cash recounting, transferring the excess and @@ -214,7 +214,7 @@ elif sys.argv[1] == "inventory-interactive": print("No action needed, amount is correct.") print("End of processing. Bye") -elif sys.argv[1] == "changecash": +elif sys.argv[1] == "fixcash" or sys.argv[1] == "changecash": if (len(sys.argv) != 3): print ("Invalid number of parameters, check your parameters.") else: From 9a700885912f163714ddd2809d0533d815b8f2c5 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Wed, 6 Jan 2016 05:02:24 +0100 Subject: [PATCH 067/112] USAGE: Fix pre within lists --- brmbar3/USAGE.md | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/brmbar3/USAGE.md b/brmbar3/USAGE.md index 5f31c5e..fd02e0e 100644 --- a/brmbar3/USAGE.md +++ b/brmbar3/USAGE.md @@ -95,32 +95,32 @@ Useful SQL queries * Compute sum of sold stock: - select sum(amount) from transactions - left join transaction_splits on transaction_splits.transaction = transactions.id - where description like '% sale %' and side = 'debit'; + select sum(amount) from transactions + left join transaction_splits on transaction_splits.transaction = transactions.id + where description like '% sale %' and side = 'debit'; * List of items not covered by inventory check: - select * from account_balances - where id not in (select account from transactions - left join transaction_splits on transaction_splits.transaction = transactions.id - where description like '% inventory %') - and acctype = 'inventory'; + select * from account_balances + where id not in (select account from transactions + left join transaction_splits on transaction_splits.transaction = transactions.id + where description like '% inventory %') + and acctype = 'inventory'; * List all cash transactions: - select time, transactions.id, description, responsible, amount from transactions - left join transaction_splits on transaction_splits.transaction = transactions.id - where transaction_splits.account = 1; + select time, transactions.id, description, responsible, amount from transactions + left join transaction_splits on transaction_splits.transaction = transactions.id + where transaction_splits.account = 1; * List all inventory items ordered by their cummulative worth: - select foo.*, foo.rate * -foo.crbalance as worth from - (select account_balances.*, - (select exchange_rates.rate from exchange_rates, accounts - where exchange_rates.target = accounts.currency - and accounts.id = account_balances.id - order by exchange_rates.valid_since limit 1) as rate - from account_balances where account_balances.acctype = 'inventory') - as foo order by worth; + select foo.*, foo.rate * -foo.crbalance as worth from + (select account_balances.*, + (select exchange_rates.rate from exchange_rates, accounts + where exchange_rates.target = accounts.currency + and accounts.id = account_balances.id + order by exchange_rates.valid_since limit 1) as rate + from account_balances where account_balances.acctype = 'inventory') + as foo order by worth; From 2bfd1796d01cd008dc386b20ef45dc58ca1e3756 Mon Sep 17 00:00:00 2001 From: brmbar Date: Thu, 7 Jan 2016 05:06:37 +0100 Subject: [PATCH 068/112] brmbar-cli stats: Separate credit and overflow --- brmbar3/brmbar-cli.py | 5 +++-- brmbar3/brmbar/Shop.py | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index d12081c..9ad8e7c 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -162,9 +162,10 @@ elif sys.argv[1] == "listitems": elif sys.argv[1] == "stats": print("Cash: {}".format(shop.cash.balance_str())) - print("Profit: {}".format(shop.profits.balance_str())) - print("Credit: {}".format(shop.credit_negbalance_str())) + print("Overflow: {}".format(shop.currency.str(shop.credit_balance(overflow='only')))) print("Inventory: {}".format(shop.inventory_balance_str())) + print("Credit: {}".format(shop.credit_negbalance_str(overflow='exclude'))) + print("Profit: {}".format(shop.profits.balance_str())) print("Excess: {}".format(shop.excess.negbalance_str())) print("Deficit: {}".format(shop.deficit.balance_str())) diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 4822bdd..6027084 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -106,7 +106,7 @@ class Shop: transaction = transaction[0] return transaction - def credit_balance(self): + def credit_balance(self, overflow=None): # We assume all debt accounts share a currency sumselect = """ SELECT SUM(ts.amount) @@ -114,13 +114,15 @@ class Shop: LEFT JOIN transaction_splits AS ts ON a.id = ts.account WHERE a.acctype = %s AND ts.side = %s """ + if overflow is not None: + sumselect += ' AND a.name ' + ('NOT ' if overflow == 'exclude' else '') + ' LIKE \'%%-overflow\'' 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 - def credit_negbalance_str(self): - return self.currency.str(-self.credit_balance()) + def credit_negbalance_str(self, overflow=None): + return self.currency.str(-self.credit_balance(overflow=overflow)) # XXX causing extra heavy delay ( thousands of extra SQL queries ), disabled def inventory_balance(self): From c5becfcabee3575c0a7df469d7a854689de18311 Mon Sep 17 00:00:00 2001 From: brmbar Date: Thu, 7 Jan 2016 05:20:50 +0100 Subject: [PATCH 069/112] brmbar-cli stats: sum excess and deficit to Fixups --- brmbar3/brmbar-cli.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 9ad8e7c..bb49daa 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -166,8 +166,10 @@ elif sys.argv[1] == "stats": print("Inventory: {}".format(shop.inventory_balance_str())) print("Credit: {}".format(shop.credit_negbalance_str(overflow='exclude'))) print("Profit: {}".format(shop.profits.balance_str())) - print("Excess: {}".format(shop.excess.negbalance_str())) - print("Deficit: {}".format(shop.deficit.balance_str())) + print("Fixups: {} (excess {}, deficit {})".format( + -shop.excess.balance() - shop.deficit.balance(), + shop.excess.negbalance_str(), + shop.deficit.balance_str())) elif sys.argv[1] == "adduser": acct = brmbar.Account.create(db, sys.argv[2], brmbar.Currency.load(db, id = 1), 'debt') From c419c91a40e21777ddcd8dcf395fa8471a8c2518 Mon Sep 17 00:00:00 2001 From: brmbar Date: Thu, 7 Jan 2016 05:26:13 +0100 Subject: [PATCH 070/112] brmbar-cli stats: separate material, logical accounts --- brmbar3/brmbar-cli.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index bb49daa..e967f0a 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -161,9 +161,11 @@ elif sys.argv[1] == "listitems": print("{}\t{}\t{} pcs".format(acct.name, acct.id, acct.balance())) elif sys.argv[1] == "stats": + print("--- Material Assets ---") print("Cash: {}".format(shop.cash.balance_str())) print("Overflow: {}".format(shop.currency.str(shop.credit_balance(overflow='only')))) print("Inventory: {}".format(shop.inventory_balance_str())) + print("--- Logical Accounts ---") print("Credit: {}".format(shop.credit_negbalance_str(overflow='exclude'))) print("Profit: {}".format(shop.profits.balance_str())) print("Fixups: {} (excess {}, deficit {})".format( From 0e306912d7c0a83eab63c8622846e13130f2e339 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Thu, 7 Jan 2016 05:26:40 +0100 Subject: [PATCH 071/112] USAGE: Document brmbar-cli stats --- brmbar3/USAGE.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/brmbar3/USAGE.md b/brmbar3/USAGE.md index fd02e0e..d749a3a 100644 --- a/brmbar3/USAGE.md +++ b/brmbar3/USAGE.md @@ -89,6 +89,33 @@ Administrative Usage ./brmbar-cli.py undo ID +* If you want to get overview of the financial situation, run + + ./brmbar-cli.py stats + + The following items represent "material", "tangible" assets: + + * Cash - how much should be in the money box + * Overflow - how much cash is stored in overflow credit accounts (pockets of admins) + * Inventory - how much worth (buy price) is the current inventory stock + + I.e., cash plus overflow plus inventory is how much brmbar is worth + and cash plus overflow is how much brmbar can spend right now. + + The following items represent "virtual" accounts which determine + the logical composition of the assets: + + * Credit - sum of all credit accounts, i.e. money stored in brmbar by its users; + i.e. how much of the assets is users' money + * Profit - accumulated profit made by brmbar on buy/sell margins (but receipts + and inventory deficits are subtracted); i.e. how much of the assets is brmbar's + own money + * Fixups - sum of gains and losses accrued by inventory fixups, i.e. stemming + from differences between accounting and reality - positive is good, negative + is bad; this amount is added to profit on consolidation + + The total worth of the material and virtual accounts should be equal. + Useful SQL queries ------------------ From a9e72d736cef4770bd12a924bca5aa9d254074fd Mon Sep 17 00:00:00 2001 From: brmbar Date: Sun, 10 Jan 2016 21:03:59 +0100 Subject: [PATCH 072/112] brmbar-cli userlog --- brmbar3/SQL | 5 ++--- brmbar3/brmbar-cli.py | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/brmbar3/SQL b/brmbar3/SQL index 7e640a6..b25908f 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -107,8 +107,7 @@ CREATE VIEW transaction_nicesplits AS FROM transaction_splits AS ts LEFT JOIN accounts AS a ON a.id = ts.account ORDER BY ts.id; --- List transactions with summary information regarding their cash element --- (except in case of transfers between cash and debt accounts, which will cancel out). +-- List transactions with summary information regarding their cash element. CREATE VIEW transaction_cashsums AS SELECT t.id AS id, t.time AS time, SUM(credit_cash) AS cash_credit, SUM(debit_cash) AS cash_debit, t.description AS description FROM transactions AS t @@ -124,4 +123,4 @@ CREATE VIEW transaction_cashsums AS WHERE a.currency = (SELECT currency FROM accounts WHERE name = 'BrmBar Cash') AND a.acctype IN ('cash', 'debt') AND dts.amount > 0) debit ON dts_t = t.id - GROUP BY t.id ORDER BY t.id; + GROUP BY t.id ORDER BY t.id DESC; diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index e967f0a..4ad26ab 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -19,6 +19,7 @@ Usage: brmbar-cli.py COMMAND ARGS... You can use negative AMT to undo a sale. restock ITEM AMT userinfo USER + userlog USER TIMESTAMP iteminfo ITEM 2. Management commands @@ -142,6 +143,14 @@ elif sys.argv[1] == "userinfo": res = db.execute_and_fetchall("SELECT barcode FROM barcodes WHERE account = %s", [acct.id]) print("Barcodes: " + ", ".join(map((lambda r: r[0]), res))) +elif sys.argv[1] == "userlog": + acct = load_user(sys.argv[2]) + timestamp = sys.argv[3] + + res = db.execute_and_fetchall("SELECT transactions.time,transactions.description FROM transactions INNER JOIN accounts ON accounts.id=transactions.responsible WHERE accounts.name=%s and time > TIMESTAMP %s ORDER BY time", [acct.name,timestamp]) + for transaction in res: + print("{}\t{}\t".format(transaction[0],transaction[1])) + elif sys.argv[1] == "iteminfo": acct = load_item(sys.argv[2]) print("{} (id {}): {} pcs".format(acct.name, acct.id, acct.balance())) From 8f6776f3d0fda4ab2e36b84f038c109006b7378d Mon Sep 17 00:00:00 2001 From: Ruzicka Pavel Date: Sun, 10 Jan 2016 21:07:47 +0100 Subject: [PATCH 073/112] brmbar-cli userlog --- brmbar3/brmbar-cli.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index e967f0a..4ad26ab 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -19,6 +19,7 @@ Usage: brmbar-cli.py COMMAND ARGS... You can use negative AMT to undo a sale. restock ITEM AMT userinfo USER + userlog USER TIMESTAMP iteminfo ITEM 2. Management commands @@ -142,6 +143,14 @@ elif sys.argv[1] == "userinfo": res = db.execute_and_fetchall("SELECT barcode FROM barcodes WHERE account = %s", [acct.id]) print("Barcodes: " + ", ".join(map((lambda r: r[0]), res))) +elif sys.argv[1] == "userlog": + acct = load_user(sys.argv[2]) + timestamp = sys.argv[3] + + res = db.execute_and_fetchall("SELECT transactions.time,transactions.description FROM transactions INNER JOIN accounts ON accounts.id=transactions.responsible WHERE accounts.name=%s and time > TIMESTAMP %s ORDER BY time", [acct.name,timestamp]) + for transaction in res: + print("{}\t{}\t".format(transaction[0],transaction[1])) + elif sys.argv[1] == "iteminfo": acct = load_item(sys.argv[2]) print("{} (id {}): {} pcs".format(acct.name, acct.id, acct.balance())) From 466b92e7c20ed13f72d0d5bf1390c60dc32ac586 Mon Sep 17 00:00:00 2001 From: brmbar Date: Fri, 19 Aug 2016 04:26:40 +0200 Subject: [PATCH 074/112] uncommitted changes from other people... --- brmbar3/SQL | 5 +++-- brmbar3/brmbar-cli.py | 4 ++-- brmbar3/brmbar-gui-qt4/MainPage.qml | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/brmbar3/SQL b/brmbar3/SQL index b25908f..a61dba8 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -109,7 +109,7 @@ CREATE VIEW transaction_nicesplits AS -- List transactions with summary information regarding their cash element. CREATE VIEW transaction_cashsums AS - SELECT t.id AS id, t.time AS time, SUM(credit_cash) AS cash_credit, SUM(debit_cash) AS cash_debit, t.description AS description + SELECT t.id AS id, t.time AS time, SUM(credit_cash) AS cash_credit, SUM(debit_cash) AS cash_debit, a.name AS responsible, t.description AS description FROM transactions AS t LEFT JOIN (SELECT cts.amount AS credit_cash, cts.transaction AS cts_t FROM transaction_nicesplits AS cts @@ -123,4 +123,5 @@ CREATE VIEW transaction_cashsums AS WHERE a.currency = (SELECT currency FROM accounts WHERE name = 'BrmBar Cash') AND a.acctype IN ('cash', 'debt') AND dts.amount > 0) debit ON dts_t = t.id - GROUP BY t.id ORDER BY t.id DESC; + LEFT JOIN accounts AS a ON a.id = t.responsible + GROUP BY t.id, a.name ORDER BY t.id DESC; diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index 4ad26ab..b641e07 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -147,9 +147,9 @@ elif sys.argv[1] == "userlog": acct = load_user(sys.argv[2]) timestamp = sys.argv[3] - res = db.execute_and_fetchall("SELECT transactions.time,transactions.description FROM transactions INNER JOIN accounts ON accounts.id=transactions.responsible WHERE accounts.name=%s and time > TIMESTAMP %s ORDER BY time", [acct.name,timestamp]) + res = db.execute_and_fetchall("SELECT * FROM transaction_cashsums WHERE responsible=%s and time > TIMESTAMP %s ORDER BY time", [acct.name,timestamp]) for transaction in res: - print("{}\t{}\t".format(transaction[0],transaction[1])) + print('\t'.join([str(f) for f in transaction])) elif sys.argv[1] == "iteminfo": acct = load_item(sys.argv[2]) diff --git a/brmbar3/brmbar-gui-qt4/MainPage.qml b/brmbar3/brmbar-gui-qt4/MainPage.qml index 6e555eb..d11fd0f 100644 --- a/brmbar3/brmbar-gui-qt4/MainPage.qml +++ b/brmbar3/brmbar-gui-qt4/MainPage.qml @@ -58,6 +58,6 @@ Item { x: 65 y: 438 width: 1150 - text: "* Mroze a Termixy najdes v lednici *" + text: "* Za uklid brmlabu vam nabijeme kredit. *" } } From 1f3f1cdd8f9343dab6105514994c6185a2e81bc0 Mon Sep 17 00:00:00 2001 From: brmbar Date: Fri, 19 Aug 2016 04:37:04 +0200 Subject: [PATCH 075/112] disable exchange rate and balance in StockMgmt because of unbearable slowdown --- brmbar3/brmbar-gui-qt4.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/brmbar3/brmbar-gui-qt4.py b/brmbar3/brmbar-gui-qt4.py index a41fd2c..9ca11d0 100755 --- a/brmbar3/brmbar-gui-qt4.py +++ b/brmbar3/brmbar-gui-qt4.py @@ -39,6 +39,14 @@ class ShopAdapter(QtCore.QObject): map["price"] = str(sell) return map + def acct_inventory_map2(self, acct): + buy, sell = 666, 666 + map = acct.__dict__.copy() + map["balance"] = "{:.0f}".format(666) + map["buy_price"] = str(buy) + map["price"] = str(sell) + return map + def acct_cash_map(self, acct): map = acct.__dict__.copy() return map @@ -164,7 +172,7 @@ class ShopAdapter(QtCore.QObject): @QtCore.Slot('QVariant', result='QVariant') def itemList(self, query): - alist = [ self.acct_inventory_map(a) for a in shop.account_list("inventory", like_str="%%"+query+"%%") ] + alist = [ self.acct_inventory_map2(a) for a in shop.account_list("inventory", like_str="%%"+query+"%%") ] db.commit() return alist From e2e6632df085123e61e84d619ef94bc382923d02 Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 2 Apr 2018 01:40:37 +0200 Subject: [PATCH 076/112] sample crontab file --- brmbar3/crontab | 15 +++++++++++++++ brmbar3/dluhy.sh | 3 +++ brmbar3/log.sh | 6 ++++++ 3 files changed, 24 insertions(+) create mode 100644 brmbar3/crontab create mode 100755 brmbar3/dluhy.sh create mode 100755 brmbar3/log.sh diff --git a/brmbar3/crontab b/brmbar3/crontab new file mode 100644 index 0000000..2e66748 --- /dev/null +++ b/brmbar3/crontab @@ -0,0 +1,15 @@ +# cleanup bounty +*/5 * * * * ~/brmbar/brmbar3/uklid-watchdog.sh +0 0 * * 1 ~/brmbar/brmbar3/uklid-refill.sh +# overall summary +5 4 * * * ~/brmbar/brmbar3/daily-summary.sh | mail -s "daily brmbar summary" yyy@yyy +# debt track +5 0 * * * ~/brmbar/brmbar3/dluhy.sh 2>/dev/null + +# per-user summary +1 0 * * * /home/brmlab/brmbar/brmbar3/log.sh yyy yyy@yyy + +# backup +6 * * * * echo "SELECT * FROM account_balances;" | psql brmbar | gzip -9 | ssh -Tp 110 -i /home/brmlab/.ssh/id_ecdsa jenda@coralmyn.hrach.eu +16 1 * * * pg_dump brmbar | gzip -9 | ssh -Tp 110 -i /home/brmlab/.ssh/id_ecdsa jenda@coralmyn.hrach.eu + diff --git a/brmbar3/dluhy.sh b/brmbar3/dluhy.sh new file mode 100755 index 0000000..5ec9adc --- /dev/null +++ b/brmbar3/dluhy.sh @@ -0,0 +1,3 @@ +p1=`echo -n "brmbar - dluhy: "; echo "SELECT name, crbalance FROM account_balances WHERE acctype = 'debt' AND crbalance < -100 AND name NOT LIKE '%overflow%' AND name NOT LIKE 'sachyo' ORDER BY crbalance ASC" | psql brmbar | tail -n +3 | grep '|' | tr -s " " | sed -e "s/ |/:/g" -e "s/$/;/" | tr -d "\n"` +p2=`echo "SELECT sum(crbalance) FROM account_balances WHERE acctype = 'debt' AND crbalance < 0 AND name NOT LIKE '%overflow%' AND name NOT LIKE 'sachyo'" | psql brmbar | tail -n +3 | head -n 1 | tr -s " "` +echo "$p1 total$p2 Kc" | ssh -p 110 -i /home/brmlab/.ssh/id_rsa jenda@coralmyn.hrach.eu diff --git a/brmbar3/log.sh b/brmbar3/log.sh new file mode 100755 index 0000000..b1afbdb --- /dev/null +++ b/brmbar3/log.sh @@ -0,0 +1,6 @@ +#!/bin/bash +p=`/home/brmlab/brmbar/brmbar3/brmbar-cli.py userlog "$1" yesterday` + +if [ -n "$p" ]; then + echo "$p" | mail -s "brmbar report" "$2" +fi From 6f3fc6767acd2fe9f776918f7989d3ed399af58a Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 2 Apr 2018 01:40:46 +0200 Subject: [PATCH 077/112] db purge howto --- brmbar3/PURGE.txt | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 brmbar3/PURGE.txt diff --git a/brmbar3/PURGE.txt b/brmbar3/PURGE.txt new file mode 100644 index 0000000..c5e5b30 --- /dev/null +++ b/brmbar3/PURGE.txt @@ -0,0 +1,64 @@ +How to "reset" the database - drop all history and keep only accounts with non-zero balance. + +Legend: +> - SQL commands +$ - shell commands + +Run the (full) inventory. + +Get number of the first inventory TX. + +> select id from account_balances where id in (select id from accounts where currency not in (select distinct currency from + transaction_nicesplits where transaction >= NUMBER_HERE and currency != 1 and memo like '%Inventory fix%') and acctype = 'inventory') and crbalance != 0 \g 'vynulovat' +$ ./brmbar-cli.py inventory `cat vynulovat | while read x; do echo $x 0; done` + +Backup the database +$ pg_dump brmbar > backup.sql + +Dump "> SELECT * FROM account_balances;" to file N. + +Dump inventory to file nastavit FIXME. + +Drop all transactions: +> delete from transaction_splits; +> delete from transactions; + +Restore inventory: +$ cat nastavit | while read acc p amt; do ./brmbar-cli.py inventory $acc `echo $amt | grep -oE "^[0-9-]+"`; done + +Restore cash balance: +$ cat N | grep debt | tr -s " " |cut -d \| -f 2,4 | while read acc p amt; do ./brmbar-cli.py changecredit $acc `echo $amt | grep -oE "^[0-9-]+"`; done + +Delete zero-balance accounts: +> delete from accounts where accounts.id not in (select id from account_balances); + +Delete orphaned barcodes: +> delete from barcodes where barcodes.account not in (select id from account_balances); + +Delete orphaned currencies and exchange rates: +> CREATE OR REPLACE VIEW "a_tmp" AS +SELECT ts.account AS id, accounts.name, accounts.acctype, accounts.currency AS fff, (- sum(CASE WHEN (ts.side = 'credit'::transaction_split_side) THEN (- ts.amount) ELSE ts.amount END)) AS crbalance FROM (transaction_splits ts LEFT JOIN accounts ON ((accounts.id = ts.account))) GROUP BY ts.account, accounts.name, accounts.id, accounts.acctype ORDER BY (- sum(CASE WHEN (ts.side = 'credit'::transaction_split_side) THEN (- ts.amount) ELSE ts.amount END)); + +> delete from exchange_rates where source not in (select fff from a_tmp); +> delete from currencies where id not in (select fff from a_tmp); + +> DROP VIEW "a_tmp"; + +Drop obsolete exchange rates: + +> delete from exchange_rates where + valid_since <> (SELECT max(valid_since) + FROM exchange_rates e + WHERE e.target = exchange_rates.target and e.source = exchange_rates.source) + +Restore system accounts: +> INSERT INTO "accounts" ("name", "currency", "acctype", "active") + VALUES ('BrmBar Profits', '1', 'income', '1'); +> INSERT INTO "accounts" ("name", "currency", "acctype", "active") + VALUES ('BrmBar Excess', '1', 'income', '1'); +> INSERT INTO "accounts" ("name", "currency", "acctype", "active") + VALUES ('BrmBar Deficit', '1', 'expense', '1'); +> INSERT INTO "accounts" ("name", "currency", "acctype", "active") + VALUES ('BrmBar Cash', '1', 'cash', '1'); + +Restart brmbar. From f061cc7f7b2a6413b154486fdb40f726beea2c23 Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 2 Apr 2018 01:41:02 +0200 Subject: [PATCH 078/112] gitignore --- brmbar3/.gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/brmbar3/.gitignore b/brmbar3/.gitignore index bee8a64..4a5b2ab 100644 --- a/brmbar3/.gitignore +++ b/brmbar3/.gitignore @@ -1 +1,3 @@ __pycache__ +*.log +brmbar/*.pyc From a1c37cb69523b89664aec03ba19cc01857caf6c5 Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 2 Apr 2018 01:41:24 +0200 Subject: [PATCH 079/112] brmbar-cli: restock by EAN (for automated restocking) --- brmbar3/brmbar-cli.py | 12 +++++++++--- brmbar3/uklid-watchdog.sh | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/brmbar3/brmbar-cli.py b/brmbar3/brmbar-cli.py index b641e07..cc5c6e3 100755 --- a/brmbar3/brmbar-cli.py +++ b/brmbar3/brmbar-cli.py @@ -97,6 +97,12 @@ def load_item(inp): exit(1) return acct +def load_item_by_barcode(inp): + acct = brmbar.Account.load_by_barcode(db, inp) + if acct.acctype != "inventory": + print("Bad EAN " + inp + " type " + acct.acctype, file=sys.stderr) + exit(1) + return acct db = Database.Database("dbname=brmbar") shop = brmbar.Shop.new_with_defaults(db) @@ -245,16 +251,16 @@ elif sys.argv[1] == "consolidate": else: shop.consolidate() -elif sys.argv[1] == "restock": +elif sys.argv[1] in {"restock", "restock_ean"}: if (len(sys.argv) != 4): print ("Invalid number of parameters, check your parameters.") else: - iacct = load_item(sys.argv[2]) + iacct = (load_item if sys.argv[1] == "restock" else load_item_by_barcode)(sys.argv[2]) oldbal = iacct.balance() amt = int(sys.argv[3]) cash = shop.buy_for_cash(iacct, amt); print("Old amount {}, increased by {}, take {} from cashbox".format(oldbal, amt, cash)) - + else: help() diff --git a/brmbar3/uklid-watchdog.sh b/brmbar3/uklid-watchdog.sh index c0fd55b..7f86870 100644 --- a/brmbar3/uklid-watchdog.sh +++ b/brmbar3/uklid-watchdog.sh @@ -13,6 +13,6 @@ if [ ! -z "$RES" ]; then if [ -z "$WINNER" ]; then WINNER="anonymous hunter" fi - echo "Brmlab cleanup bounty was claimed by $WINNER! Thanks!"|ssh jenda@fry.hrach.eu + echo "Brmlab cleanup bounty was claimed by $WINNER! Thanks!"|ssh -p 110 jenda@coralmyn.hrach.eu fi From 6749b2c97ae8cd7e351196a16ba6e22793b9dc3c Mon Sep 17 00:00:00 2001 From: brmbar Date: Mon, 2 Apr 2018 01:46:01 +0200 Subject: [PATCH 080/112] autostock.py --- brmbar3/autostock.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 brmbar3/autostock.py diff --git a/brmbar3/autostock.py b/brmbar3/autostock.py new file mode 100755 index 0000000..0e8afac --- /dev/null +++ b/brmbar3/autostock.py @@ -0,0 +1,42 @@ +#! /usr/bin/env python3 + +import argparse +import brmbar +import math +from brmbar import Database + +def main(): + parser = argparse.ArgumentParser(usage = "File format: EAN amount total_price name, e.g. 4001242002377 6 167.40 Chio Tortillas") + parser.add_argument("filename") + args = parser.parse_args() + + db = Database.Database("dbname=brmbar") + shop = brmbar.Shop.new_with_defaults(db) + currency = shop.currency + + # ... + total = 0 + with open(args.filename) as fin: + for line in fin: + split = line.split(" ") + ean, amount, price_total, name = split[0], int(split[1]), float(split[2]), " ".join(split[3:]) + name = name.strip() + + price_buy = price_total / amount + acct = brmbar.Account.load_by_barcode(db, ean) + if not acct: + print("Creating account for EAN {} '{}'".format(ean, name)) + invcurr = brmbar.Currency.create(db, name) + acct = brmbar.Account.create(db, name, invcurr, "inventory") + acct.add_barcode(ean) + price_sell = max(math.ceil(price_buy * 1.15), price_buy) + acct.currency.update_sell_rate(currency, price_sell) + acct.currency.update_buy_rate(currency, price_buy) + cash = shop.buy_for_cash(acct, amount) + total += cash + print("Increased by {}, take {} from cashbox".format(amount, cash)) + print("Total is {}".format(total)) + +if __name__ == "__main__": + main() + From dbd3835dfb33e017b842552d9d81799dd5b645ce Mon Sep 17 00:00:00 2001 From: niekt0 Date: Tue, 30 Jan 2024 20:18:07 +0100 Subject: [PATCH 081/112] fixed NaN issue in withdraw/charge --- brmbar3/brmbar-gui-qt4/ChargeCredit.qml | 13 +++++++++++-- brmbar3/brmbar-gui-qt4/Withdraw.qml | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/brmbar3/brmbar-gui-qt4/ChargeCredit.qml b/brmbar3/brmbar-gui-qt4/ChargeCredit.qml index 8eda557..0ff8dc8 100644 --- a/brmbar3/brmbar-gui-qt4/ChargeCredit.qml +++ b/brmbar3/brmbar-gui-qt4/ChargeCredit.qml @@ -100,8 +100,17 @@ Item { } function chargeCredit() { - var balance = shop.chargeCredit(amount, userdbid) - status_text.setStatus("Charged! "+username+"'s credit is "+balance+".", "#ffff7c") + var balance=0 + if (!isNaN(amount)) { + if(amount>=0) { + balance = shop.chargeCredit(amount, userdbid) + status_text.setStatus("Charged "+amount+"! "+username+"'s credit is "+balance+".", "#ffff7c") + } else { + balance = shop.withdrawCredit((amount*(-1)), userdbid) + status_text.setStatus("Withdrawn "+amount+"! "+username+"'s credit is "+balance+".", "#ffff7c") + } + } loadPage("MainPage") + } } diff --git a/brmbar3/brmbar-gui-qt4/Withdraw.qml b/brmbar3/brmbar-gui-qt4/Withdraw.qml index a9e7700..b37b670 100644 --- a/brmbar3/brmbar-gui-qt4/Withdraw.qml +++ b/brmbar3/brmbar-gui-qt4/Withdraw.qml @@ -100,8 +100,17 @@ Item { } function withdrawCredit() { - var balance = shop.withdrawCredit(amount, userdbid) - status_text.setStatus("Withdrawn! "+username+"'s credit is "+balance+".", "#ffff7c") + var balance=0 + if (!isNaN(amount)) { + amount=(amount*1) + if(amount>=0) { + balance = shop.withdrawCredit(amount, userdbid) + status_text.setStatus("Withdrawn "+amount+"! "+username+"'s credit is "+balance+".", "#ffff7c") + } else { + balance = shop.chargeCredit((amount*(-1)),userdbid) + status_text.setStatus("Charged "+amount+"! "+username+"'s credit is "+balance+".", "#ffff7c") + } + } loadPage("MainPage") } } From e04d614e159dacbe443e22813893bcd5ee10b479 Mon Sep 17 00:00:00 2001 From: TMA Date: Sun, 30 Mar 2025 20:55:00 +0200 Subject: [PATCH 082/112] uncommitted changes from other people... again --- .gitignore | 1 + brmbar3/USEFUL.txt | 8 ++++++++ brmbar3/brmbar-gui-qt4/ChargeCredit.qml | 13 +++++++++++-- brmbar3/brmbar-gui-qt4/Withdraw.qml | 13 +++++++++++-- brmbar3/dluhy.sh | 2 +- 5 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 brmbar3/USEFUL.txt diff --git a/.gitignore b/.gitignore index 3268211..9eccd17 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .*.sw? +*~ diff --git a/brmbar3/USEFUL.txt b/brmbar3/USEFUL.txt new file mode 100644 index 0000000..11c0071 --- /dev/null +++ b/brmbar3/USEFUL.txt @@ -0,0 +1,8 @@ +Accounts with multiple barcodes: + +SELECT accounts.name,barcodes.account,barcodes.barcode +FROM "barcodes" +join accounts on accounts.id = barcodes.account +where barcodes.account in (select a from (select count(*) as c, account as a from barcodes group by account) as dt where c > 1) +ORDER BY "account" DESC + diff --git a/brmbar3/brmbar-gui-qt4/ChargeCredit.qml b/brmbar3/brmbar-gui-qt4/ChargeCredit.qml index 8eda557..cdcc006 100644 --- a/brmbar3/brmbar-gui-qt4/ChargeCredit.qml +++ b/brmbar3/brmbar-gui-qt4/ChargeCredit.qml @@ -100,8 +100,17 @@ Item { } function chargeCredit() { - var balance = shop.chargeCredit(amount, userdbid) - status_text.setStatus("Charged! "+username+"'s credit is "+balance+".", "#ffff7c") + var balance=0 + if (!isNaN(amount)) { + if(amount>=0) { + balance = shop.chargeCredit(amount, userdbid) + status_text.setStatus("Charged "+amount+"! "+username+"'s credit is "+balance+".", "#ffff7c") + } else { + balance = shop.withdrawCredit((amount*(-1)), userdbid) + status_text.setStatus("Withdrawn "+amount+"! "+username+"'s credit is "+balance+".", "#ffff7c") + } + } loadPage("MainPage") + } } diff --git a/brmbar3/brmbar-gui-qt4/Withdraw.qml b/brmbar3/brmbar-gui-qt4/Withdraw.qml index a9e7700..2d8c49b 100644 --- a/brmbar3/brmbar-gui-qt4/Withdraw.qml +++ b/brmbar3/brmbar-gui-qt4/Withdraw.qml @@ -100,8 +100,17 @@ Item { } function withdrawCredit() { - var balance = shop.withdrawCredit(amount, userdbid) - status_text.setStatus("Withdrawn! "+username+"'s credit is "+balance+".", "#ffff7c") + var balance=0 + if (!isNaN(amount)) { + amount=(amount*1) + if(amount>=0) { + balance = shop.withdrawCredit(amount, userdbid) + status_text.setStatus("Withdrawn "+amount+"! "+username+"'s credit is "+balance+".", "#ffff7c") + } else { + balance = shop.chargeCredit((amount*(-1)),userdbid) + status_text.setStatus("Charged "+amount+"! "+username+"'s credit is "+balance+".", "#ffff7c") + } + } loadPage("MainPage") } } diff --git a/brmbar3/dluhy.sh b/brmbar3/dluhy.sh index 5ec9adc..61c2a46 100755 --- a/brmbar3/dluhy.sh +++ b/brmbar3/dluhy.sh @@ -1,3 +1,3 @@ p1=`echo -n "brmbar - dluhy: "; echo "SELECT name, crbalance FROM account_balances WHERE acctype = 'debt' AND crbalance < -100 AND name NOT LIKE '%overflow%' AND name NOT LIKE 'sachyo' ORDER BY crbalance ASC" | psql brmbar | tail -n +3 | grep '|' | tr -s " " | sed -e "s/ |/:/g" -e "s/$/;/" | tr -d "\n"` p2=`echo "SELECT sum(crbalance) FROM account_balances WHERE acctype = 'debt' AND crbalance < 0 AND name NOT LIKE '%overflow%' AND name NOT LIKE 'sachyo'" | psql brmbar | tail -n +3 | head -n 1 | tr -s " "` -echo "$p1 total$p2 Kc" | ssh -p 110 -i /home/brmlab/.ssh/id_rsa jenda@coralmyn.hrach.eu +echo "$p1 total$p2 Kc. https://www.elektro-obojky.cz/" | ssh -p 110 -i /home/brmlab/.ssh/id_rsa jenda@coralmyn.hrach.eu From 2f601a0b1ac9750e19373f267d36129870273b07 Mon Sep 17 00:00:00 2001 From: TMA Date: Fri, 11 Apr 2025 13:39:07 +0200 Subject: [PATCH 083/112] functions for querying sequence values for read only access --- brmbar3/SQL-for-RO-access.sql | 57 +++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 brmbar3/SQL-for-RO-access.sql diff --git a/brmbar3/SQL-for-RO-access.sql b/brmbar3/SQL-for-RO-access.sql new file mode 100644 index 0000000..b8c7291 --- /dev/null +++ b/brmbar3/SQL-for-RO-access.sql @@ -0,0 +1,57 @@ +CREATE OR REPLACE FUNCTION accounts_id_seq_value() +RETURNS bigint +LANGUAGE plpgsql +SECURITY DEFINER +AS $$ +DECLARE + result bigint; +BEGIN + SELECT last_value FROM accounts_id_seq + INTO result; + RETURN result; +END; +$$; + +CREATE OR REPLACE FUNCTION transactions_id_seq_value() +RETURNS bigint +LANGUAGE plpgsql +SECURITY DEFINER +AS $$ +DECLARE + result bigint; +BEGIN + SELECT last_value FROM transactions_id_seq + INTO result; + RETURN result; +END; +$$; + +CREATE OR REPLACE FUNCTION transaction_splits_id_seq_value() +RETURNS bigint +LANGUAGE plpgsql +SECURITY DEFINER +AS $$ +DECLARE + result bigint; +BEGIN + SELECT last_value FROM transaction_splits_id_seq + INTO result; + RETURN result; +END; +$$; + +CREATE OR REPLACE FUNCTION currencies_id_seq_value() +RETURNS bigint +LANGUAGE plpgsql +SECURITY DEFINER +AS $$ +DECLARE + result bigint; +BEGIN + SELECT last_value FROM currencies_id_seq + INTO result; + RETURN result; +END; +$$; + + From 3000731ac7b4bbcba045276a0df93acbb02b9af8 Mon Sep 17 00:00:00 2001 From: TMA Date: Fri, 11 Apr 2025 20:56:17 +0200 Subject: [PATCH 084/112] SQL schema v002 script part 1 --- brmbar3/SQL | 4 +- brmbar3/SQL-schema-v002.sql | 80 +++++++++++++++++++++++++++++++++++++ brmbar3/brmbar/Shop.py | 17 ++++---- 3 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 brmbar3/SQL-schema-v002.sql diff --git a/brmbar3/SQL b/brmbar3/SQL index a61dba8..04667f4 100644 --- a/brmbar3/SQL +++ b/brmbar3/SQL @@ -1,11 +1,11 @@ -CREATE SEQUENCE currencies_id_seq START WITH 1 INCREMENT BY 1; +CREATE SEQUENCE currencies_id_seq START WITH 2 INCREMENT BY 1; CREATE TABLE currencies ( id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('currencies_id_seq'::regclass), name VARCHAR(128) NOT NULL, UNIQUE(name) ); -- Some code depends on the primary physical currency to have id 1. -INSERT INTO currencies (name) VALUES ('Kč'); +INSERT INTO currencies (id, name) VALUES (1, 'Kč'); CREATE TYPE exchange_rate_direction AS ENUM ('source_to_target', 'target_to_source'); CREATE TABLE exchange_rates ( diff --git a/brmbar3/SQL-schema-v002.sql b/brmbar3/SQL-schema-v002.sql new file mode 100644 index 0000000..afe6c80 --- /dev/null +++ b/brmbar3/SQL-schema-v002.sql @@ -0,0 +1,80 @@ +-- intoduce implementation schema +CREATE SCHEMA IF NOT EXISTS brmbar_implementation; +-- version table (with initialization) +CREATE TABLE IF NOT EXISTS brmbar_implementation.brmbar_schema ( + ver INTEGER NOT NULL +); +DO $$ +DECLARE v INTEGER; +BEGIN + SELECT ver FROM brmbar_implementation.brmbar_schema INTO v; + IF v IS NULL THEN + INSERT INTO brmbar_implementation.brmbar_schema (ver) VALUES (1); + END IF; +END; +$$; + + +--- upgrade schema +DO $$ +DECLARE +current_ver INTEGER; +BEGIN + +SELECT ver FROM brmbar_implementation.brmbar_schema INTO current_ver; +IF current_ver <> 1 THEN + RAISE EXCEPTION 'BrmBar schema version % cannot be upgraded to version 2.', current_ver; +END IF; + +ALTER TYPE public.account_type ADD VALUE 'trading'; + +--drop function if exists public.create_currency; +CREATE OR REPLACE PROCEDURE public.create_currency( + IN i_name public.currencies.name%TYPE, + OUT o_id public.currencies.id%TYPE) +LANGUAGE plpgsql SECURITY DEFINER AS $fn$ +BEGIN + INSERT INTO currencies ("name") VALUES (i_name) RETURNING o_id; +END +$fn$; + +CREATE OR REPLACE PROCEDURE public.undo_transaction( + IN i_id public.transactions.id%TYPE, + OUT o_id public.transactions.id%TYPE) +LANGUAGE plpgsql SECURITY DEFINER AS $fn$ +DECLARE + v_ntrn_id public.transactions.id%TYPE; + v_old_trn public.transactions%ROWTYPE; + v_old_split public.transaction_splits%ROWTYPE; +BEGIN + SELECT * INTO v_old_trn FROM public.transactions WHERE id = i_id; + INSERT INTO transactions ("description") VALUES ('undo '||o_id||' ('||v_old_trn.description||')') RETURNING id into v_ntrn_id; + FOR v_old_split IN + SELECT * FROM transaction_splits WHERE "transaction" = i_id + LOOP + INSERT INTO transaction_splits ("transaction", "side", "account", "amount", "memo") + VALUES (v_ntrn_id, v_old_split.side, v_old_split.account, -v_old_split.amount, + 'undo ' || v_old_split.id || ' (' || v_old_split.memo || ')' ); + END LOOP; + o_id := v_ntrn_id; +END +$fn$; + +-- this is for the case that psycopg2 cannot do output arguments +CREATE OR REPLACE FUNCTION public.undo_transaction_fn( + IN i_id public.transactions.id%TYPE) +RETURNS public.transactions.id%TYPE +VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $fn$ +DECLARE + v_ntrn_id public.transactions.id%TYPE; +BEGIN + CALL public.undo_transaction(i_id, v_ntrn_id); + RETURN v_ntrn_id; +END +$fn$; + + +-- end of upgrade do block +end +$$; + diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 6027084..5049fcc 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -219,14 +219,15 @@ class Shop: self.db.commit() def undo(self, oldtid): - description = self.db.execute_and_fetch("SELECT description FROM transactions WHERE id = %s", [oldtid])[0] - description = 'undo %d (%s)' % (oldtid, description) + #description = self.db.execute_and_fetch("SELECT description FROM transactions WHERE id = %s", [oldtid])[0] + #description = 'undo %d (%s)' % (oldtid, description) - transaction = self._transaction(description=description) - for split in self.db.execute_and_fetchall("SELECT id, side, account, amount, memo FROM transaction_splits WHERE transaction = %s", [oldtid]): - splitid, side, account, amount, memo = split - memo = 'undo %d (%s)' % (splitid, memo) - amount = -amount - self.db.execute("INSERT INTO transaction_splits (transaction, side, account, amount, memo) VALUES (%s, %s, %s, %s, %s)", [transaction, side, account, amount, memo]) + #transaction = self._transaction(description=description) + #for split in self.db.execute_and_fetchall("SELECT id, side, account, amount, memo FROM transaction_splits WHERE transaction = %s", [oldtid]): + # splitid, side, account, amount, memo = split + # memo = 'undo %d (%s)' % (splitid, memo) + # amount = -amount + # self.db.execute("INSERT INTO transaction_splits (transaction, side, account, amount, memo) VALUES (%s, %s, %s, %s, %s)", [transaction, side, account, amount, memo]) + transaction = self.db.execute_and_fetch("CALL undo_transaction(%s)",[oldtid])[0] self.db.commit() return transaction From f6ec2be215bbfbdf99156507b5e53fc0a185c5ac Mon Sep 17 00:00:00 2001 From: TMA Date: Sat, 19 Apr 2025 22:44:01 +0200 Subject: [PATCH 085/112] schema v1 setup script --- brmbar3/SQL-schema-v001.sql | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 brmbar3/SQL-schema-v001.sql diff --git a/brmbar3/SQL-schema-v001.sql b/brmbar3/SQL-schema-v001.sql new file mode 100644 index 0000000..44b9693 --- /dev/null +++ b/brmbar3/SQL-schema-v001.sql @@ -0,0 +1,59 @@ +--RESET search_path; +SELECT pg_catalog.set_config('search_path', '', false); +-- intoduce implementation schema +CREATE SCHEMA IF NOT EXISTS brmbar_implementation; +-- version table (with initialization) +CREATE TABLE IF NOT EXISTS brmbar_implementation.brmbar_schema ( + ver INTEGER NOT NULL +); +DO $$ +DECLARE v INTEGER; +BEGIN + SELECT ver FROM brmbar_implementation.brmbar_schema INTO v; + IF v IS NULL THEN + INSERT INTO brmbar_implementation.brmbar_schema (ver) VALUES (1); + END IF; +END; +$$; + +CREATE OR REPLACE FUNCTION brmbar_implementation.has_exact_schema_version( + IN i_ver INTEGER NOT NULL +) RETURNS INTEGER +VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $$ +DECLARE + v_ver INTEGER; +BEGIN + SELECT ver INTO STRICT v_ver FROM brmbar_implementation.brmbar_schema; + IF v_ver IS NULL or v_ver <> i_ver THEN + RAISE EXCEPTION 'Invalid brmbar schema version'; + END IF; + RETURN v_ver; +/* +EXCEPTION + WHEN NO_DATA_FOUND THEN + RAISE EXCEPTION 'PID % not found'; + WHEN TOO_MANY_ROWS THEN + RAISE EXCEPTION 'PID % not unique'; +*/ +END; +$$; + +CREATE OR REPLACE FUNCTION brmbar_implementation.upgrade_schema_version_to( + IN i_ver INTEGER NOT NULL +) RETURNS INTEGER +VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $$ +DECLARE + v_ver INTEGER; +BEGIN + SELECT brmbar_implementation.has_exact_schema_version(i_ver) INTO v_ver; + IF v_ver + 1 = i_ver THEN + UPDATE brmbar_implementation.brmbar_schema SET ver = i_ver; + ELSE + RAISE EXCEPTION 'Invalid brmbar schema version'; + END IF; + RETURN i_ver; +END; +$$; + +-- vim: set ft=plsql : + From 900264469a9f925749246477b7803b052b91ae07 Mon Sep 17 00:00:00 2001 From: TMA Date: Sun, 20 Apr 2025 10:13:31 +0200 Subject: [PATCH 086/112] schema v2 setup script --- brmbar3/SQL-schema-v002.sql | 105 +++++++++++++++--------------------- 1 file changed, 43 insertions(+), 62 deletions(-) diff --git a/brmbar3/SQL-schema-v002.sql b/brmbar3/SQL-schema-v002.sql index afe6c80..bfb63fa 100644 --- a/brmbar3/SQL-schema-v002.sql +++ b/brmbar3/SQL-schema-v002.sql @@ -1,80 +1,61 @@ --- intoduce implementation schema -CREATE SCHEMA IF NOT EXISTS brmbar_implementation; --- version table (with initialization) -CREATE TABLE IF NOT EXISTS brmbar_implementation.brmbar_schema ( - ver INTEGER NOT NULL -); -DO $$ -DECLARE v INTEGER; -BEGIN - SELECT ver FROM brmbar_implementation.brmbar_schema INTO v; - IF v IS NULL THEN - INSERT INTO brmbar_implementation.brmbar_schema (ver) VALUES (1); - END IF; -END; -$$; - +--RESET search_path +SELECT pg_catalog.set_config('search_path', '', false); --- upgrade schema -DO $$ +DO $upgrade_block$ DECLARE current_ver INTEGER; BEGIN -SELECT ver FROM brmbar_implementation.brmbar_schema INTO current_ver; +-- confirm that we are upgrading from version 1 +SELECT brmbar_implementation.has_exact_schema_version(1) INTO current_ver; IF current_ver <> 1 THEN RAISE EXCEPTION 'BrmBar schema version % cannot be upgraded to version 2.', current_ver; END IF; -ALTER TYPE public.account_type ADD VALUE 'trading'; +-- structural changes ---drop function if exists public.create_currency; -CREATE OR REPLACE PROCEDURE public.create_currency( - IN i_name public.currencies.name%TYPE, - OUT o_id public.currencies.id%TYPE) -LANGUAGE plpgsql SECURITY DEFINER AS $fn$ -BEGIN - INSERT INTO currencies ("name") VALUES (i_name) RETURNING o_id; -END -$fn$; +-- TRADING ACCOUNTS +--START TRANSACTION ISOLATION LEVEL SERIALIZABLE; -CREATE OR REPLACE PROCEDURE public.undo_transaction( - IN i_id public.transactions.id%TYPE, - OUT o_id public.transactions.id%TYPE) -LANGUAGE plpgsql SECURITY DEFINER AS $fn$ -DECLARE - v_ntrn_id public.transactions.id%TYPE; - v_old_trn public.transactions%ROWTYPE; - v_old_split public.transaction_splits%ROWTYPE; -BEGIN - SELECT * INTO v_old_trn FROM public.transactions WHERE id = i_id; - INSERT INTO transactions ("description") VALUES ('undo '||o_id||' ('||v_old_trn.description||')') RETURNING id into v_ntrn_id; - FOR v_old_split IN - SELECT * FROM transaction_splits WHERE "transaction" = i_id - LOOP - INSERT INTO transaction_splits ("transaction", "side", "account", "amount", "memo") - VALUES (v_ntrn_id, v_old_split.side, v_old_split.account, -v_old_split.amount, - 'undo ' || v_old_split.id || ' (' || v_old_split.memo || ')' ); - END LOOP; - o_id := v_ntrn_id; -END -$fn$; +-- currency trading accounts - account type +ALTER TYPE public.account_type ADD VALUE IF NOT EXISTS 'trading'; --- this is for the case that psycopg2 cannot do output arguments -CREATE OR REPLACE FUNCTION public.undo_transaction_fn( - IN i_id public.transactions.id%TYPE) -RETURNS public.transactions.id%TYPE -VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $fn$ -DECLARE - v_ntrn_id public.transactions.id%TYPE; -BEGIN - CALL public.undo_transaction(i_id, v_ntrn_id); - RETURN v_ntrn_id; -END -$fn$; +-- constraint needed for foreign key in currencies table +ALTER TABLE public.accounts ADD CONSTRAINT accounts_id_acctype_key UNIQUE(id, acctype); + +-- add columns to currencies to record the trading account associated with the currency +ALTER TABLE public.currencies + ADD COLUMN IF NOT EXISTS trading_account integer, + ADD COLUMN IF NOT EXISTS trading_account_type account_type GENERATED ALWAYS AS ('trading'::public.account_type) STORED; + +-- make trading accounts (without making duplicates) +INSERT INTO public.accounts ("name", "currency", acctype) +SELECT + 'Currency Trading Account: ' || c."name", + c.id, + 'trading'::public.account_type +FROM public.currencies AS c +WHERE NOT EXISTS ( + SELECT 1 + FROM public.accounts a + WHERE a.currency = c.id AND a.acctype = 'trading'::public.account_type +); +-- record the trading account IDs in currencies table +UPDATE public.currencies AS c SET (trading_account) = (SELECT a.id FROM public.accounts AS a WHERE a.currency = c.id AND c.acctype = 'trading'::public.account_type); + +-- foreign key to check the validity of currency trading account reference +ALTER TABLE public.currencies + ADD CONSTRAINT currencies_trading_fkey FOREIGN KEY (trading_account, trading_account_type) + REFERENCES xaccounts(id,acctype) DEFERRABLE INITIALLY DEFERRED; + +--COMMIT AND CHAIN; + +SELECT brmbar_implementation.upgrade_schema_version_to(2) INTO current_ver; -- end of upgrade do block end -$$; +$upgrade_block$; +-- vim: set ft=plsql : From c04f934340929607a88251c66522e66f2ae94a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Sun, 20 Apr 2025 17:01:05 +0200 Subject: [PATCH 087/112] #1: add initial SQL schema as the database currently in use --- brmbar3/schema/0001-init.sql | 313 +++++++++++++++++++++++++++++++++++ 1 file changed, 313 insertions(+) create mode 100644 brmbar3/schema/0001-init.sql diff --git a/brmbar3/schema/0001-init.sql b/brmbar3/schema/0001-init.sql new file mode 100644 index 0000000..52afd7e --- /dev/null +++ b/brmbar3/schema/0001-init.sql @@ -0,0 +1,313 @@ +-- +-- 0000-init.sql +-- +-- Initial SQL schema construction as of 2025-04-20 (or so) +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- Dominik Pantůček +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +-- Privileged schema with protected data +CREATE SCHEMA IF NOT EXISTS brmbar_privileged; + +-- Initial versioning +CREATE TABLE IF NOT EXISTS brmbar_privileged.brmbar_schema( + ver INTEGER NOT NULL +); + +-- ---------------------------------------------------------------- +-- Legacy Schema Initialization +-- ---------------------------------------------------------------- + +DO $$ +DECLARE v INTEGER; +BEGIN + SELECT ver FROM brmbar_privileged.brmbar_schema INTO v; + IF v IS NULL THEN + -- -------------------------------- + -- Legacy Types + + SELECT COUNT(*) INTO v + FROM pg_catalog.pg_type typ + INNER JOIN pg_catalog.pg_namespace nsp + ON nsp.oid = typ.typnamespace + WHERE nsp.nspname = 'public' + AND typ.typname='exchange_rate_direction'; + IF v=0 THEN + RAISE NOTICE 'Creating type exchange_rate_direction'; + CREATE TYPE public.exchange_rate_direction + AS ENUM ('source_to_target', 'target_to_source'); + ELSE + RAISE NOTICE 'Type exchange_rate_direction already exists'; + END IF; + + SELECT COUNT(*) INTO v + FROM pg_catalog.pg_type typ + INNER JOIN pg_catalog.pg_namespace nsp + ON nsp.oid = typ.typnamespace + WHERE nsp.nspname = 'public' + AND typ.typname='account_type'; + IF v=0 THEN + RAISE NOTICE 'Creating type account_type'; + CREATE TYPE public.account_type + AS ENUM ('cash', 'debt', 'inventory', 'income', 'expense', + 'starting_balance', 'ending_balance'); + ELSE + RAISE NOTICE 'Type account_type already exists'; + END IF; + + SELECT COUNT(*) INTO v + FROM pg_catalog.pg_type typ + INNER JOIN pg_catalog.pg_namespace nsp + ON nsp.oid = typ.typnamespace + WHERE nsp.nspname = 'public' + AND typ.typname='transaction_split_side'; + IF v=0 THEN + RAISE NOTICE 'Creating type transaction_split_side'; + CREATE TYPE public.transaction_split_side + AS ENUM ('credit', 'debit'); + ELSE + RAISE NOTICE 'Type transaction_split_side already exists'; + END IF; + + -- -------------------------------- + -- Currencies sequence, table and potential initial data + + CREATE SEQUENCE IF NOT EXISTS public.currencies_id_seq + START WITH 2 INCREMENT BY 1; + CREATE TABLE IF NOT EXISTS public.currencies ( + id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('public.currencies_id_seq'::regclass), + name VARCHAR(128) NOT NULL, + UNIQUE(name) + ); + INSERT INTO public.currencies (id, name) VALUES (1, 'Kč') + ON CONFLICT DO NOTHING; + + -- -------------------------------- + -- Exchange rates table - no initial data required + + CREATE TABLE IF NOT EXISTS public.exchange_rates ( + valid_since TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() NOT NULL, + + target INTEGER NOT NULL, + FOREIGN KEY (target) REFERENCES public.currencies (id), + + source INTEGER NOT NULL, + FOREIGN KEY (source) REFERENCES public.currencies (id), + + rate DECIMAL(12,2) NOT NULL, + rate_dir public.exchange_rate_direction NOT NULL + ); + + -- -------------------------------- + -- Accounts sequence and table and 4 initial accounts + + CREATE SEQUENCE IF NOT EXISTS public.accounts_id_seq + START WITH 2 INCREMENT BY 1; + CREATE TABLE IF NOT EXISTS public.accounts ( + id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('public.accounts_id_seq'::regclass), + + name VARCHAR(128) NOT NULL, + UNIQUE (name), + + currency INTEGER NOT NULL, + FOREIGN KEY (currency) REFERENCES public.currencies (id), + + acctype public.account_type NOT NULL, + + active BOOLEAN NOT NULL DEFAULT TRUE + ); + INSERT INTO public.accounts (id, name, currency, acctype) + VALUES (1, 'BrmBar Cash', (SELECT id FROM public.currencies WHERE name='Kč'), 'cash') + ON CONFLICT DO NOTHING; + INSERT INTO public.accounts (name, currency, acctype) + VALUES ('BrmBar Profits', (SELECT id FROM public.currencies WHERE name='Kč'), 'income') + ON CONFLICT DO NOTHING; + INSERT INTO public.accounts (name, currency, acctype) + VALUES ('BrmBar Excess', (SELECT id FROM public.currencies WHERE name='Kč'), 'income') + ON CONFLICT DO NOTHING; + INSERT INTO public.accounts (name, currency, acctype) + VALUES ('BrmBar Deficit', (SELECT id FROM public.currencies WHERE name='Kč'), 'expense') + ON CONFLICT DO NOTHING; + + -- -------------------------------- + -- Barcodes + + CREATE TABLE IF NOT EXISTS public.barcodes ( + barcode VARCHAR(128) PRIMARY KEY NOT NULL, + + account INTEGER NOT NULL, + FOREIGN KEY (account) REFERENCES public.accounts (id) + ); + INSERT INTO public.barcodes (barcode, account) + VALUES ('_cash_', (SELECT id FROM public.accounts WHERE acctype = 'cash')) + ON CONFLICT DO NOTHING; + + -- -------------------------------- + -- Transactions + + CREATE SEQUENCE IF NOT EXISTS public.transactions_id_seq + START WITH 1 INCREMENT BY 1; + CREATE TABLE IF NOT EXISTS public.transactions ( + id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('public.transactions_id_seq'::regclass), + time TIMESTAMP DEFAULT NOW() NOT NULL, + + responsible INTEGER, + FOREIGN KEY (responsible) REFERENCES public.accounts (id), + + description TEXT + ); + + -- -------------------------------- + -- Transaction splits + + CREATE SEQUENCE IF NOT EXISTS public.transaction_splits_id_seq + START WITH 1 INCREMENT BY 1; + CREATE TABLE IF NOT EXISTS public.transaction_splits ( + id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('public.transaction_splits_id_seq'::regclass), + + transaction INTEGER NOT NULL, + FOREIGN KEY (transaction) REFERENCES public.transactions (id), + + side public.transaction_split_side NOT NULL, + + account INTEGER NOT NULL, + FOREIGN KEY (account) REFERENCES public.accounts (id), + amount DECIMAL(12,2) NOT NULL, + + memo TEXT + ); + + -- -------------------------------- + -- Account balances view + + CREATE OR REPLACE VIEW public.account_balances AS + SELECT ts.account AS id, + accounts.name, + accounts.acctype, + - sum( + CASE + WHEN ts.side = 'credit'::public.transaction_split_side THEN - ts.amount + ELSE ts.amount + END) AS crbalance + FROM public.transaction_splits ts + LEFT JOIN public.accounts ON accounts.id = ts.account + GROUP BY ts.account, accounts.name, accounts.acctype + ORDER BY (- sum( + CASE + WHEN ts.side = 'credit'::public.transaction_split_side THEN - ts.amount + ELSE ts.amount + END)); + + -- -------------------------------- + -- Transaction nice splits view + + CREATE OR REPLACE VIEW public.transaction_nicesplits AS + SELECT ts.id, + ts.transaction, + ts.account, + CASE + WHEN ts.side = 'credit'::public.transaction_split_side THEN - ts.amount + ELSE ts.amount + END AS amount, + a.currency, + ts.memo + FROM public.transaction_splits ts + LEFT JOIN public.accounts a ON a.id = ts.account + ORDER BY ts.id; + + -- -------------------------------- + -- Transaction cash sums view + + CREATE OR REPLACE VIEW public.transaction_cashsums AS + SELECT t.id, + t."time", + sum(credit.credit_cash) AS cash_credit, + sum(debit.debit_cash) AS cash_debit, + a.name AS responsible, + t.description + FROM public.transactions t + LEFT JOIN ( SELECT cts.amount AS credit_cash, + cts.transaction AS cts_t + FROM public.transaction_nicesplits cts + LEFT JOIN public.accounts a_1 ON a_1.id = cts.account OR a_1.id = cts.account + WHERE a_1.currency = (( SELECT accounts.currency + FROM public.accounts + WHERE accounts.name::text = 'BrmBar Cash'::text)) + AND (a_1.acctype = ANY (ARRAY['cash'::public.account_type, 'debt'::public.account_type])) + AND cts.amount < 0::numeric) credit ON credit.cts_t = t.id + LEFT JOIN ( SELECT dts.amount AS debit_cash, + dts.transaction AS dts_t + FROM public.transaction_nicesplits dts + LEFT JOIN public.accounts a_1 ON a_1.id = dts.account OR a_1.id = dts.account + WHERE a_1.currency = (( SELECT accounts.currency + FROM public.accounts + WHERE accounts.name::text = 'BrmBar Cash'::text)) + AND (a_1.acctype = ANY (ARRAY['cash'::public.account_type, 'debt'::public.account_type])) + AND dts.amount > 0::numeric) debit ON debit.dts_t = t.id + LEFT JOIN public.accounts a ON a.id = t.responsible + GROUP BY t.id, a.name + ORDER BY t.id DESC; + + -- -------------------------------- + -- Function to check schema version (used in migrations) + + CREATE OR REPLACE FUNCTION brmbar_privileged.has_exact_schema_version( + IN i_ver INTEGER + ) RETURNS INTEGER + VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $x$ + DECLARE + v_ver INTEGER; + BEGIN + SELECT ver INTO v_ver FROM brmbar_privileged.brmbar_schema; + IF v_ver is NULL THEN + RETURN false; + ELSE + RETURN v_ver = i_ver; + END IF; + END; + $x$; + + -- -------------------------------- + -- + + CREATE OR REPLACE FUNCTION brmbar_privileged.upgrade_schema_version_to( + IN i_ver INTEGER + ) RETURNS INTEGER + VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $x$ + DECLARE + v_ver INTEGER; + BEGIN + SELECT ver FROM brmbar_privileged.brmbar_schema INTO v_ver; + IF v_ver=(i_ver-1) THEN + UPDATE brmbar_privileged.brmbar_schema SET ver = i_ver; + ELSE + RAISE EXCEPTION 'Invalid brmbar schema version transition (% -> %)', v_ver, i_ver; + END IF; + END; + $x$; + + -- Initialize version 1 + INSERT INTO brmbar_privileged.brmbar_schema(ver) VALUES(1); + END IF; +END; +$$; From 111a8c9b63138e405f4073fd38787f9e7267d03c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Sun, 20 Apr 2025 17:11:49 +0200 Subject: [PATCH 088/112] #2: add trading account type type and fix schema migrations machinery --- brmbar3/schema/0001-init.sql | 6 ++-- brmbar3/schema/0002-trading-accounts.sql | 37 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 brmbar3/schema/0002-trading-accounts.sql diff --git a/brmbar3/schema/0001-init.sql b/brmbar3/schema/0001-init.sql index 52afd7e..8e7f8db 100644 --- a/brmbar3/schema/0001-init.sql +++ b/brmbar3/schema/0001-init.sql @@ -1,5 +1,5 @@ -- --- 0000-init.sql +-- 0001-init.sql -- -- Initial SQL schema construction as of 2025-04-20 (or so) -- @@ -273,7 +273,7 @@ BEGIN CREATE OR REPLACE FUNCTION brmbar_privileged.has_exact_schema_version( IN i_ver INTEGER - ) RETURNS INTEGER + ) RETURNS BOOLEAN VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $x$ DECLARE v_ver INTEGER; @@ -292,7 +292,7 @@ BEGIN CREATE OR REPLACE FUNCTION brmbar_privileged.upgrade_schema_version_to( IN i_ver INTEGER - ) RETURNS INTEGER + ) RETURNS VOID VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $x$ DECLARE v_ver INTEGER; diff --git a/brmbar3/schema/0002-trading-accounts.sql b/brmbar3/schema/0002-trading-accounts.sql new file mode 100644 index 0000000..08397cd --- /dev/null +++ b/brmbar3/schema/0002-trading-accounts.sql @@ -0,0 +1,37 @@ +-- +-- 0002-trading-accounts.sql +-- +-- #2 - add trading accounts to account type type +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- Dominik Pantůček +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(1) THEN + + ALTER TYPE public.account_type ADD VALUE 'trading'; + + PERFORM brmbar_privileged.upgrade_schema_version_to(2); +END IF; + +END; +$upgrade_block$; From c5d1fc3402e9aafbf6de1fd1ae0a7c9d905b4cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Sun, 20 Apr 2025 17:42:12 +0200 Subject: [PATCH 089/112] #3: create account in SQL not in Python. --- brmbar3/brmbar/Account.py | 3 +- brmbar3/schema/0002-trading-accounts.sql | 3 ++ brmbar3/schema/0003-new-account.sql | 52 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 brmbar3/schema/0003-new-account.sql diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index 29dd79e..fca6d82 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -40,7 +40,8 @@ class Account: @classmethod def create(cls, db, name, currency, acctype): """ Constructor for new account """ - id = db.execute_and_fetch("INSERT INTO accounts (name, currency, acctype) VALUES (%s, %s, %s) RETURNING id", [name, currency.id, acctype]) + # id = db.execute_and_fetch("INSERT INTO accounts (name, currency, acctype) VALUES (%s, %s, %s) RETURNING id", [name, currency.id, acctype]) + id = db.execute_and_fetch("SELECT public.create_account(%s, %s, %s)", [name, currency.id, acctype]) id = id[0] return cls(db, name = name, id = id, currency = currency, acctype = acctype) diff --git a/brmbar3/schema/0002-trading-accounts.sql b/brmbar3/schema/0002-trading-accounts.sql index 08397cd..021df3d 100644 --- a/brmbar3/schema/0002-trading-accounts.sql +++ b/brmbar3/schema/0002-trading-accounts.sql @@ -23,6 +23,9 @@ -- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- +-- Require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + DO $upgrade_block$ BEGIN diff --git a/brmbar3/schema/0003-new-account.sql b/brmbar3/schema/0003-new-account.sql new file mode 100644 index 0000000..9ac02b5 --- /dev/null +++ b/brmbar3/schema/0003-new-account.sql @@ -0,0 +1,52 @@ +-- +-- 0003-new-account.sql +-- +-- #3 - stored procedure for creating new account +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- Dominik Pantůček +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- Require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(2) THEN + + CREATE OR REPLACE FUNCTION public.create_account( + IN i_name public.accounts.name%TYPE, + IN i_currency public.accounts.currency%TYPE, + IN i_acctype public.accounts.acctype%TYPE + ) RETURNS INTEGER LANGUAGE plpgsql AS $$ + DECLARE + r_id INTEGER; + BEGIN + INSERT INTO public.accounts (name, currency, acctype) + VALUES (i_name, i_currency, i_acctype) RETURNING id INTO r_id; + RETURN r_id; + END + $$; + + PERFORM brmbar_privileged.upgrade_schema_version_to(3); +END IF; + +END; +$upgrade_block$; From 58ab1d00be2914c9c215c67d81ba02ad86ee78f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Sun, 20 Apr 2025 17:55:18 +0200 Subject: [PATCH 090/112] #4: add account to barcode stored procedure. --- brmbar3/brmbar/Account.py | 3 +- brmbar3/schema/0004-add-account-barcode.sql | 50 +++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 brmbar3/schema/0004-add-account-barcode.sql diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index fca6d82..8615dfd 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -69,7 +69,8 @@ class Account: 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): - 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.execute("SELECT public.add_barcode_to_account(%s, %s)", [self.id, barcode]) self.db.commit() def rename(self, name): diff --git a/brmbar3/schema/0004-add-account-barcode.sql b/brmbar3/schema/0004-add-account-barcode.sql new file mode 100644 index 0000000..dbdba9a --- /dev/null +++ b/brmbar3/schema/0004-add-account-barcode.sql @@ -0,0 +1,50 @@ +-- +-- 0004-add-account-barcode.sql +-- +-- #4 - stored procedure for adding barcode to account +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- Dominik Pantůček +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- Require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(3) THEN + + CREATE OR REPLACE FUNCTION public.add_barcode_to_account( + IN i_account public.barcodes.account%TYPE, + IN i_barcode public.barcodes.barcode%TYPE + ) RETURNS VOID LANGUAGE plpgsql AS $$ + DECLARE + r_id INTEGER; + BEGIN + INSERT INTO public.barcodes (account, barcode) + VALUES (i_account, i_barcode); + END + $$; + + PERFORM brmbar_privileged.upgrade_schema_version_to(4); +END IF; + +END; +$upgrade_block$; From 8f42145bee0bb6e265d7a1f7e63816c6666d5999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Sun, 20 Apr 2025 18:00:40 +0200 Subject: [PATCH 091/112] #5: rename account stored function --- brmbar3/brmbar/Account.py | 3 +- brmbar3/schema/0005-rename-account.sql | 51 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 brmbar3/schema/0005-rename-account.sql diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index 8615dfd..3e3a24d 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -74,5 +74,6 @@ class Account: self.db.commit() def rename(self, name): - self.db.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.db.execute("SELECT public.rename_account(%s, %s)", [self.id, name]) self.name = name diff --git a/brmbar3/schema/0005-rename-account.sql b/brmbar3/schema/0005-rename-account.sql new file mode 100644 index 0000000..a957029 --- /dev/null +++ b/brmbar3/schema/0005-rename-account.sql @@ -0,0 +1,51 @@ +-- +-- 0005-rename-account.sql +-- +-- #5 - stored procedure for renaming account +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- Dominik Pantůček +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- Require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(4) THEN + + CREATE OR REPLACE FUNCTION public.rename_account( + IN i_account public.accounts.id%TYPE, + IN i_name public.accounts.name%TYPE + ) RETURNS VOID LANGUAGE plpgsql AS $$ + DECLARE + r_id INTEGER; + BEGIN + UPDATE public.accounts + SET name = i_name + WHERE id = i_account; + END + $$; + + PERFORM brmbar_privileged.upgrade_schema_version_to(5); +END IF; + +END; +$upgrade_block$; From 9235607d4cae9f9a262a1aa85b02d4b6c8ca3311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Sun, 20 Apr 2025 19:15:23 +0200 Subject: [PATCH 092/112] #6: new currency stored function --- brmbar3/brmbar/Account.py | 2 +- brmbar3/brmbar/Currency.py | 5 +-- brmbar3/schema/0006-new-currency.sql | 50 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 brmbar3/schema/0006-new-currency.sql diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index 3e3a24d..40e86be 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -42,7 +42,7 @@ class Account: """ Constructor for new account """ # id = db.execute_and_fetch("INSERT INTO accounts (name, currency, acctype) VALUES (%s, %s, %s) RETURNING id", [name, currency.id, acctype]) id = db.execute_and_fetch("SELECT public.create_account(%s, %s, %s)", [name, currency.id, acctype]) - id = id[0] + # id = id[0] return cls(db, name = name, id = id, currency = currency, acctype = acctype) def balance(self): diff --git a/brmbar3/brmbar/Currency.py b/brmbar3/brmbar/Currency.py index d2216ca..a03a893 100644 --- a/brmbar3/brmbar/Currency.py +++ b/brmbar3/brmbar/Currency.py @@ -31,8 +31,9 @@ class Currency: @classmethod def create(cls, db, name): """ Constructor for new currency """ - id = db.execute_and_fetch("INSERT INTO currencies (name) VALUES (%s) RETURNING id", [name]) - id = id[0] + # id = db.execute_and_fetch("INSERT INTO currencies (name) VALUES (%s) RETURNING id", [name]) + id = db.execute_and_fetch("SELECT public.create_currency(%s)", [name]) + # id = id[0] return cls(db, name = name, id = id) def rates(self, other): diff --git a/brmbar3/schema/0006-new-currency.sql b/brmbar3/schema/0006-new-currency.sql new file mode 100644 index 0000000..0ed9a94 --- /dev/null +++ b/brmbar3/schema/0006-new-currency.sql @@ -0,0 +1,50 @@ +-- +-- 0006-new-currency.sql +-- +-- #6 - stored procedure for creating new currency +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- Dominik Pantůček +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- Require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(5) THEN + + CREATE OR REPLACE FUNCTION public.create_currency( + IN i_name public.currencies.name%TYPE + ) RETURNS INTEGER LANGUAGE plpgsql AS $$ + DECLARE + r_id INTEGER; + BEGIN + INSERT INTO public.currencies (name) + VALUES (i_name) RETURNING id INTO r_id; + RETURN r_id; + END + $$; + + PERFORM brmbar_privileged.upgrade_schema_version_to(6); +END IF; + +END; +$upgrade_block$; From 66870bbc8c943b16919a5659cb229d24c502d2f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Sun, 20 Apr 2025 19:22:19 +0200 Subject: [PATCH 093/112] #7: migrate to stored function for updating currency sell rate. --- brmbar3/brmbar/Currency.py | 4 +- .../schema/0007-update-currency-sell-rate.sql | 49 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 brmbar3/schema/0007-update-currency-sell-rate.sql diff --git a/brmbar3/brmbar/Currency.py b/brmbar3/brmbar/Currency.py index a03a893..e204ac4 100644 --- a/brmbar3/brmbar/Currency.py +++ b/brmbar3/brmbar/Currency.py @@ -70,6 +70,8 @@ class Currency: return "{:.2f} {}".format(amount, self.name) def update_sell_rate(self, target, rate): - self.db.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [self.id, target.id, rate, "source_to_target"]) + # self.db.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [self.id, target.id, rate, "source_to_target"]) + self.db.execute("SELECT update_currency_sell_rate(%s, %s, %s)", + [self.id, target.id, rate]) def update_buy_rate(self, source, rate): self.db.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [source.id, self.id, rate, "target_to_source"]) diff --git a/brmbar3/schema/0007-update-currency-sell-rate.sql b/brmbar3/schema/0007-update-currency-sell-rate.sql new file mode 100644 index 0000000..f627897 --- /dev/null +++ b/brmbar3/schema/0007-update-currency-sell-rate.sql @@ -0,0 +1,49 @@ +-- +-- 0007-update-currency-sell-rate.sql +-- +-- #7 - stored procedure for updating sell rate +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- Dominik Pantůček +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- Require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(6) THEN + + CREATE OR REPLACE FUNCTION public.update_currency_sell_rate( + IN i_currency public.exchange_rates.source%TYPE, + IN i_target public.exchange_rates.target%TYPE, + IN i_rate public.exchange_rates.rate%TYPE + ) RETURNS VOID LANGUAGE plpgsql AS $$ + BEGIN + INSERT INTO public.exchange_rates(source, target, rate, rate_dir) + VALUES (i_currency, i_target, i_rate, 'source_to_target'); + END + $$; + + PERFORM brmbar_privileged.upgrade_schema_version_to(7); +END IF; + +END; +$upgrade_block$; From aded7a5769570f63c4e62e88e762c65ada4d77ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Sun, 20 Apr 2025 19:27:02 +0200 Subject: [PATCH 094/112] #8: stored function to update currency buy rate --- brmbar3/brmbar/Currency.py | 6 ++- .../schema/0008-update-currency-buy-rate.sql | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 brmbar3/schema/0008-update-currency-buy-rate.sql diff --git a/brmbar3/brmbar/Currency.py b/brmbar3/brmbar/Currency.py index e204ac4..294d9f4 100644 --- a/brmbar3/brmbar/Currency.py +++ b/brmbar3/brmbar/Currency.py @@ -71,7 +71,9 @@ class Currency: def update_sell_rate(self, target, rate): # self.db.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [self.id, target.id, rate, "source_to_target"]) - self.db.execute("SELECT update_currency_sell_rate(%s, %s, %s)", + self.db.execute("SELECT public.update_currency_sell_rate(%s, %s, %s)", [self.id, target.id, rate]) def update_buy_rate(self, source, rate): - self.db.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [source.id, self.id, rate, "target_to_source"]) + # self.db.execute("INSERT INTO exchange_rates (source, target, rate, rate_dir) VALUES (%s, %s, %s, %s)", [source.id, self.id, rate, "target_to_source"]) + self.db.execute("SELECT public.update_currency_buy_rate(%s, %s, %s)", + [source.id, self.id, rate]) diff --git a/brmbar3/schema/0008-update-currency-buy-rate.sql b/brmbar3/schema/0008-update-currency-buy-rate.sql new file mode 100644 index 0000000..cbab11b --- /dev/null +++ b/brmbar3/schema/0008-update-currency-buy-rate.sql @@ -0,0 +1,49 @@ +-- +-- 0008-update-currency-buy-rate.sql +-- +-- #8 - stored procedure for updating buy rate +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- Dominik Pantůček +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- Require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(7) THEN + + CREATE OR REPLACE FUNCTION public.update_currency_buy_rate( + IN i_currency public.exchange_rates.target%TYPE, + IN i_source public.exchange_rates.source%TYPE, + IN i_rate public.exchange_rates.rate%TYPE + ) RETURNS VOID LANGUAGE plpgsql AS $$ + BEGIN + INSERT INTO public.exchange_rates(source, target, rate, rate_dir) + VALUES (i_source, i_currency, i_rate, 'target_to_source'); + END + $$; + + PERFORM brmbar_privileged.upgrade_schema_version_to(8); +END IF; + +END; +$upgrade_block$; From cef95c43136a642f660bd1f3f59176d6fe5fd705 Mon Sep 17 00:00:00 2001 From: TMA Date: Sun, 20 Apr 2025 23:43:52 +0200 Subject: [PATCH 095/112] #9: stored function for sell transaction --- brmbar3/brmbar/Currency.py | 18 ++-- brmbar3/brmbar/Shop.py | 25 +++-- brmbar3/schema/0009-shop-sell.sql | 149 ++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 17 deletions(-) create mode 100644 brmbar3/schema/0009-shop-sell.sql diff --git a/brmbar3/brmbar/Currency.py b/brmbar3/brmbar/Currency.py index 294d9f4..7c56984 100644 --- a/brmbar3/brmbar/Currency.py +++ b/brmbar3/brmbar/Currency.py @@ -40,18 +40,20 @@ class Currency: """ Return tuple ($buy, $sell) of rates of $self in relation to $other (brmbar.Currency): $buy is the price of $self in means of $other when buying it (into brmbar) $sell is the price of $self in means of $other when selling it (from brmbar) """ - - res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [self.id, other.id]) + # buy rate + res = self.db.execute_and_fetch("SELECT public.find_buy_rate(%s, %s)",[self.id, other.id]) if res is None: + raise NameError("Something fishy in find_buy_rate."); + buy = res[0] + if buy < 0: raise NameError("Currency.rate(): Unknown conversion " + other.name() + " to " + self.name()) - buy_rate, buy_rate_dir = res - buy = buy_rate if buy_rate_dir == "target_to_source" else 1/buy_rate - - res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [other.id, self.id]) + # sell rate + res = self.db.execute_and_fetch("SELECT public.find_sell_rate(%s, %s)",[self.id, other.id]) if res is None: + raise NameError("Something fishy in find_sell_rate."); + sell = res[0] + if sell < 0: raise NameError("Currency.rate(): Unknown conversion " + self.name() + " to " + other.name()) - sell_rate, sell_rate_dir = res - sell = sell_rate if sell_rate_dir == "source_to_target" else 1/sell_rate return (buy, sell) diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 5049fcc..f005d7b 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -25,18 +25,25 @@ class Shop: deficit = Account.load(db, name = "BrmBar Deficit")) def sell(self, item, user, amount = 1): - # Sale: Currency conversion from item currency to shop currency - (buy, sell) = item.currency.rates(self.currency) - cost = amount * sell - profit = amount * (sell - buy) + # Call the stored procedure for the sale + cost = self.db.execute_and_fetch( + "SELECT public.sell_item(%s, %s, %s, %s, %s)", + [item.id, amount, user.id, self.currency.id, f"BrmBar sale of {amount}x {item.name} to {user.name}"] + )[0]#[0] - transaction = self._transaction(responsible = user, description = "BrmBar sale of {}x {} to {}".format(amount, item.name, user.name)) - item.credit(transaction, amount, user.name) - user.debit(transaction, cost, item.name) # debit (increase) on a _debt_ account - self.profits.debit(transaction, profit, "Margin on " + item.name) self.db.commit() - return cost + # Sale: Currency conversion from item currency to shop currency + #(buy, sell) = item.currency.rates(self.currency) + #cost = amount * sell + #profit = amount * (sell - buy) + + #transaction = self._transaction(responsible = user, description = "BrmBar sale of {}x {} to {}".format(amount, item.name, user.name)) + #item.credit(transaction, amount, user.name) + #user.debit(transaction, cost, item.name) # debit (increase) on a _debt_ account + #self.profits.debit(transaction, profit, "Margin on " + item.name) + #self.db.commit() + #return cost def sell_for_cash(self, item, amount = 1): # Sale: Currency conversion from item currency to shop currency diff --git a/brmbar3/schema/0009-shop-sell.sql b/brmbar3/schema/0009-shop-sell.sql new file mode 100644 index 0000000..64d4353 --- /dev/null +++ b/brmbar3/schema/0009-shop-sell.sql @@ -0,0 +1,149 @@ +-- +-- 0009-shop-sell.sql +-- +-- #9 - stored function for sell transaction +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(8) THEN + +-- return negative number on rate not found +CREATE OR REPLACE FUNCTION public.find_buy_rate( + IN i_item_id public.accounts.id%TYPE; + IN i_other_id public.accounts.id%TYPE; +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +DECLARE + v_rate public.exchange_rates.rate%TYPE; + v_rate_dir public.exchange_rates.rate_dir%TYPE; +BEGIN + SELECT rate INTO STRICT v_rate, rate_dir INTO STRICT v_rate_dir FROM public.exchange_rates WHERE target = i_item_id AND source = i_other_id AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1; + IF v_rate_dir = 'target_to_source'::public.exchange_rate_direction THEN + RETURN v_rate; + ELSE + RETURN 1/v_rate; + END IF; +EXCEPTION + WHEN NO_DATA_FOUND THEN + RETURN -1; +END; +$$ + + +-- return negative number on rate not found +CREATE OR REPLACE FUNCTION public.find_sell_rate( + IN i_item_id public.accounts.id%TYPE; + IN i_other_id public.accounts.id%TYPE; +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +DECLARE + v_rate public.exchange_rates.rate%TYPE; + v_rate_dir public.exchange_rates.rate_dir%TYPE; +BEGIN + SELECT rate INTO STRICT v_rate, rate_dir INTO STRICT v_rate_dir FROM public.exchange_rates WHERE target = i_other_id AND source = i_item_id AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1; + IF v_rate_dir = 'source_to_target'::public.exchange_rate_direction THEN + RETURN v_rate; + ELSE + RETURN 1/v_rate; + END IF; +EXCEPTION + WHEN NO_DATA_FOUND THEN + RETURN -1; +END; +$$ + +CREATE OR REPLACE FUNCTION public.create_transaction( + i_responsible_id public.accounts.id%TYPE, + i_description public.transactions.description%TYPE +) RETURNS public.transactions.id%TYPE AS $$ +DECLARE + new_transaction_id public.transactions%TYPE; +BEGIN + -- Create a new transaction + INSERT INTO transactions (responsible, description) + VALUES (i_responsible_id, i_description) + RETURNING id INTO new_transaction_id; + -- Return the new transaction ID + RETURN new_transaction_id; +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION public.sell_item( + i_item_id public.accounts.id%TYPE, + i_amount INTEGER, + i_user_id public.accounts.id%TYPE, + i_target_currency_id public.currencies.id%TYPE, + i_description TEXT +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +DECLARE + v_buy_rate NUMERIC; + v_sell_rate NUMERIC; + v_cost NUMERIC; + v_profit NUMERIC; + v_transaction_id public.transactions.id%TYPE; +BEGIN + -- Get the buy and sell rates from the stored functions + v_buy_rate := find_buy_rate(i_item_id, i_target_currency_id); + v_sell_rate := find_sell_rate(i_item_id, i_target_currency_id); + + -- Calculate cost and profit + v_cost := i_amount * v_sell_rate; + v_profit := i_amount * (v_sell_rate - v_buy_rate); + + -- Create a new transaction + v_transaction_id := create_transaction(i_user_id, i_description); + + -- the item (decrease stock) + INSERT INTO transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', i_item_id, i_amount, + (SELECT "name" FROM public.accounts WHERE id = i_user_id)); + + -- the user + INSERT INTO transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'debit', i_user_id, v_cost, + (SELECT "name" FROM public.accounts WHERE id = i_item_id)); + + -- the profit + INSERT INTO transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'debit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin on ' || "name" FROM public.accounts WHERE id = i_item_id)); + + -- Return the cost + RETURN v_cost; +END; +$$; + +PERFORM brmbar_privileged.upgrade_schema_version_to(9); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From 4c7012c517b3933468a844c78ded98b0c83e9f5f Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 12:22:22 +0200 Subject: [PATCH 096/112] #10: stored function for cash sell transaction --- brmbar3/brmbar/Shop.py | 25 ++-- brmbar3/schema/0010-shop-sell-for-cash.sql | 141 +++++++++++++++++++++ 2 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 brmbar3/schema/0010-shop-sell-for-cash.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index f005d7b..0e9b3b2 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -46,18 +46,25 @@ class Shop: #return cost def sell_for_cash(self, item, amount = 1): - # Sale: Currency conversion from item currency to shop currency - (buy, sell) = item.currency.rates(self.currency) - cost = amount * sell - profit = amount * (sell - buy) + cost = self.db.execute_and_fetch( + "SELECT public.sell_item_for_cash(%s, %s, %s, %s, %s)", + [item.id, amount, user.id, self.currency.id, f"BrmBar sale of {amount}x {item.name} for cash"] + )[0]#[0] - transaction = self._transaction(description = "BrmBar sale of {}x {} for cash".format(amount, item.name)) - item.credit(transaction, amount, "Cash") - self.cash.debit(transaction, cost, item.name) - self.profits.debit(transaction, profit, "Margin on " + item.name) self.db.commit() - return cost + ## Sale: Currency conversion from item currency to shop currency + #(buy, sell) = item.currency.rates(self.currency) + #cost = amount * sell + #profit = amount * (sell - buy) + + #transaction = self._transaction(description = "BrmBar sale of {}x {} for cash".format(amount, item.name)) + #item.credit(transaction, amount, "Cash") + #self.cash.debit(transaction, cost, item.name) + #self.profits.debit(transaction, profit, "Margin on " + item.name) + #self.db.commit() + + #return cost def undo_sale(self, item, user, amount = 1): # Undo sale; rarely needed diff --git a/brmbar3/schema/0010-shop-sell-for-cash.sql b/brmbar3/schema/0010-shop-sell-for-cash.sql new file mode 100644 index 0000000..03dabda --- /dev/null +++ b/brmbar3/schema/0010-shop-sell-for-cash.sql @@ -0,0 +1,141 @@ +-- +-- 0009-shop-sell.sql +-- +-- #10 - stored function for cash sell transaction +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(9) THEN + +CREATE OR REPLACE FUNCTION brmbar_privileged.create_transaction( + i_responsible_id public.accounts.id%TYPE, + i_description public.transactions.description%TYPE +) RETURNS public.transactions.id%TYPE AS $$ +DECLARE + new_transaction_id public.transactions%TYPE; +BEGIN + -- Create a new transaction + INSERT INTO public.transactions (responsible, description) + VALUES (i_responsible_id, i_description) + RETURNING id INTO new_transaction_id; + -- Return the new transaction ID + RETURN new_transaction_id; +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION brmbar_privileged.sell_item_internal( + i_item_id public.accounts.id%TYPE, + i_amount INTEGER, + i_user_id public.accounts.id%TYPE, + i_target_currency_id public.currencies.id%TYPE, + i_other_memo TEXT, + i_description TEXT +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +DECLARE + v_buy_rate NUMERIC; + v_sell_rate NUMERIC; + v_cost NUMERIC; + v_profit NUMERIC; + v_transaction_id public.transactions.id%TYPE; +BEGIN + -- Get the buy and sell rates from the stored functions + v_buy_rate := public.find_buy_rate(i_item_id, i_target_currency_id); + v_sell_rate := public.find_sell_rate(i_item_id, i_target_currency_id); + + -- Calculate cost and profit + v_cost := i_amount * v_sell_rate; + v_profit := i_amount * (v_sell_rate - v_buy_rate); + + -- Create a new transaction + v_transaction_id := brmbar_privileged.create_transaction(i_user_id, i_description); + + -- the item (decrease stock) + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', i_item_id, i_amount, + i_other_memo); + + -- the user + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'debit', i_user_id, v_cost, + (SELECT "name" FROM public.accounts WHERE id = i_item_id)); + + -- the profit + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'debit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin on ' || "name" FROM public.accounts WHERE id = i_item_id)); + + -- Return the cost + RETURN v_cost; +END; +$$; + +CREATE OR REPLACE FUNCTION public.sell_item( + i_item_id public.accounts.id%TYPE, + i_amount INTEGER, + i_user_id public.accounts.id%TYPE, + i_target_currency_id public.currencies.id%TYPE, + i_description TEXT +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +BEGIN + RETURN brmbar_privileged.sell_item_internal(i_item_id, + i_amount, + i_other_id, + i_target_currency_id, + (SELECT "name" FROM public.accounts WHERE id = i_user_id), + i_description); +END; +$$; + +CREATE OR REPLACE FUNCTION public.sell_item_for_cash( + i_item_id public.accounts.id%TYPE, + i_amount INTEGER, + i_user_id public.accounts.id%TYPE, + i_target_currency_id public.currencies.id%TYPE, + i_description TEXT +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +BEGIN + RETURN brmbar_privileged.sell_item_internal(i_item_id, + i_amount, + i_other_id, + i_target_currency_id, + 'Cash', + i_description); +END; +$$; + +PERFORM brmbar_privileged.upgrade_schema_version_to(10); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From deb3faa71707811dafa84303cca39f7bda482fba Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 12:27:29 +0200 Subject: [PATCH 097/112] minor fixes in schemata 0009 and 0010 --- brmbar3/schema/0009-shop-sell.sql | 14 +++++++------- brmbar3/schema/0010-shop-sell-for-cash.sql | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/brmbar3/schema/0009-shop-sell.sql b/brmbar3/schema/0009-shop-sell.sql index 64d4353..811a5f5 100644 --- a/brmbar3/schema/0009-shop-sell.sql +++ b/brmbar3/schema/0009-shop-sell.sql @@ -86,7 +86,7 @@ DECLARE new_transaction_id public.transactions%TYPE; BEGIN -- Create a new transaction - INSERT INTO transactions (responsible, description) + INSERT INTO public.transactions (responsible, description) VALUES (i_responsible_id, i_description) RETURNING id INTO new_transaction_id; -- Return the new transaction ID @@ -111,28 +111,28 @@ DECLARE v_transaction_id public.transactions.id%TYPE; BEGIN -- Get the buy and sell rates from the stored functions - v_buy_rate := find_buy_rate(i_item_id, i_target_currency_id); - v_sell_rate := find_sell_rate(i_item_id, i_target_currency_id); + v_buy_rate := public.find_buy_rate(i_item_id, i_target_currency_id); + v_sell_rate := public.find_sell_rate(i_item_id, i_target_currency_id); -- Calculate cost and profit v_cost := i_amount * v_sell_rate; v_profit := i_amount * (v_sell_rate - v_buy_rate); -- Create a new transaction - v_transaction_id := create_transaction(i_user_id, i_description); + v_transaction_id := public.create_transaction(i_user_id, i_description); -- the item (decrease stock) - INSERT INTO transaction_splits (transaction, side, account, amount, memo) + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) VALUES (i_transaction_id, 'credit', i_item_id, i_amount, (SELECT "name" FROM public.accounts WHERE id = i_user_id)); -- the user - INSERT INTO transaction_splits (transaction, side, account, amount, memo) + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) VALUES (i_transaction_id, 'debit', i_user_id, v_cost, (SELECT "name" FROM public.accounts WHERE id = i_item_id)); -- the profit - INSERT INTO transaction_splits (transaction, side, account, amount, memo) + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) VALUES (i_transaction_id, 'debit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin on ' || "name" FROM public.accounts WHERE id = i_item_id)); -- Return the cost diff --git a/brmbar3/schema/0010-shop-sell-for-cash.sql b/brmbar3/schema/0010-shop-sell-for-cash.sql index 03dabda..ea30445 100644 --- a/brmbar3/schema/0010-shop-sell-for-cash.sql +++ b/brmbar3/schema/0010-shop-sell-for-cash.sql @@ -132,6 +132,8 @@ BEGIN END; $$; +DROP FUNCTION public.create_transaction; + PERFORM brmbar_privileged.upgrade_schema_version_to(10); END IF; From 028d4d98dbe7dfcdd5352d9cc885be9038a73828 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 17:06:21 +0200 Subject: [PATCH 098/112] #11: stored function for sale undo transaction --- brmbar3/brmbar/Shop.py | 20 +++-- brmbar3/schema/0011-shop-undo-sale.sql | 102 +++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 brmbar3/schema/0011-shop-undo-sale.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 0e9b3b2..eb57d1a 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -68,14 +68,20 @@ class Shop: def undo_sale(self, item, user, amount = 1): # Undo sale; rarely needed - (buy, sell) = item.currency.rates(self.currency) - cost = amount * sell - profit = amount * (sell - buy) + #(buy, sell) = item.currency.rates(self.currency) + #cost = amount * sell + #profit = amount * (sell - buy) + + #transaction = self._transaction(responsible = user, description = "BrmBar sale UNDO of {}x {} to {}".format(amount, item.name, user.name)) + #item.debit(transaction, amount, user.name + " (sale undo)") + #user.credit(transaction, cost, item.name + " (sale undo)") + #self.profits.credit(transaction, profit, "Margin repaid on " + item.name) + # Call the stored procedure for undoing a sale + cost = self.db.execute_and_fetch( + "SELECT public.undo_sale_of_item(%s, %s, %s, %s)", + [item.id, amount, user.id, user.currency.id, f"BrmBar sale UNDO of {amount}x {item.name} to {user.name}"] + )[0]#[0] - transaction = self._transaction(responsible = user, description = "BrmBar sale UNDO of {}x {} to {}".format(amount, item.name, user.name)) - item.debit(transaction, amount, user.name + " (sale undo)") - user.credit(transaction, cost, item.name + " (sale undo)") - self.profits.credit(transaction, profit, "Margin repaid on " + item.name) self.db.commit() return cost diff --git a/brmbar3/schema/0011-shop-undo-sale.sql b/brmbar3/schema/0011-shop-undo-sale.sql new file mode 100644 index 0000000..67d47cc --- /dev/null +++ b/brmbar3/schema/0011-shop-undo-sale.sql @@ -0,0 +1,102 @@ +-- +-- 0011-shop-undo-sale.sql +-- +-- #11 - stored function for sale undo transaction +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(10) THEN + +CREATE OR REPLACE FUNCTION brmbar_privileged.create_transaction( + i_responsible_id public.accounts.id%TYPE, + i_description public.transactions.description%TYPE +) RETURNS public.transactions.id%TYPE AS $$ +DECLARE + new_transaction_id public.transactions%TYPE; +BEGIN + -- Create a new transaction + INSERT INTO public.transactions (responsible, description) + VALUES (i_responsible_id, i_description) + RETURNING id INTO new_transaction_id; + -- Return the new transaction ID + RETURN new_transaction_id; +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION public.undo_sale_of_item( + i_item_id public.accounts.id%TYPE, + i_amount INTEGER, + i_user_id public.accounts.id%TYPE, + i_target_currency_id public.currencies.id%TYPE, + i_description TEXT +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +DECLARE + v_buy_rate NUMERIC; + v_sell_rate NUMERIC; + v_cost NUMERIC; + v_profit NUMERIC; + v_transaction_id public.transactions.id%TYPE; +BEGIN + -- Get the buy and sell rates from the stored functions + v_buy_rate := public.find_buy_rate(i_item_id, i_target_currency_id); + v_sell_rate := public.find_sell_rate(i_item_id, i_target_currency_id); + + -- Calculate cost and profit + v_cost := i_amount * v_sell_rate; + v_profit := i_amount * (v_sell_rate - v_buy_rate); + + -- Create a new transaction + v_transaction_id := brmbar_privileged.create_transaction(i_user_id, i_description); + + -- the item (decrease stock) + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'debit', i_item_id, i_amount, + (SELECT "name" || ' (sale undo)' FROM public.accounts WHERE id = i_user_id)); + + -- the user + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', i_user_id, v_cost, + (SELECT "name" || ' (sale undo)' FROM public.accounts WHERE id = i_item_id)); + + -- the profit + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin repaid on ' || "name" FROM public.accounts WHERE id = i_item_id)); + + -- Return the cost + RETURN v_cost; +END; +$$; + +PERFORM brmbar_privileged.upgrade_schema_version_to(11); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From 7ed7417492464d1e7a2e7168b4a42a84812a8ca5 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 17:06:42 +0200 Subject: [PATCH 099/112] schema 0010: fix header id --- brmbar3/schema/0010-shop-sell-for-cash.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brmbar3/schema/0010-shop-sell-for-cash.sql b/brmbar3/schema/0010-shop-sell-for-cash.sql index ea30445..23ad131 100644 --- a/brmbar3/schema/0010-shop-sell-for-cash.sql +++ b/brmbar3/schema/0010-shop-sell-for-cash.sql @@ -1,5 +1,5 @@ -- --- 0009-shop-sell.sql +-- 0010-shop-sell-for-cash.sql -- -- #10 - stored function for cash sell transaction -- From f0cd8361d1fd1f5831e70e42d47db9c53d87c098 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 17:23:24 +0200 Subject: [PATCH 100/112] #12 - stored function for cash deposit transactions --- brmbar3/brmbar/Shop.py | 13 ++- brmbar3/schema/0012-shop-add-credit.sql | 110 ++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 brmbar3/schema/0012-shop-add-credit.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index eb57d1a..0f672c2 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -87,10 +87,17 @@ class Shop: return cost def add_credit(self, credit, user): - transaction = self._transaction(responsible = user, description = "BrmBar credit replenishment for " + user.name) - self.cash.debit(transaction, credit, user.name) - user.credit(transaction, credit, "Credit replenishment") + trn = self.db.execute_and_fetch( + "SELECT public.add_credit(%s, %s, %s, %s)", + [self.cash.id, credit, user.id, user.name] + )[0] self.db.commit() + return trn # unused + + #transaction = self._transaction(responsible = user, description = "BrmBar credit replenishment for " + user.name) + #self.cash.debit(transaction, credit, user.name) + #user.credit(transaction, credit, "Credit replenishment") + #self.db.commit() def withdraw_credit(self, credit, user): transaction = self._transaction(responsible = user, description = "BrmBar credit withdrawal for " + user.name) diff --git a/brmbar3/schema/0012-shop-add-credit.sql b/brmbar3/schema/0012-shop-add-credit.sql new file mode 100644 index 0000000..48f2da6 --- /dev/null +++ b/brmbar3/schema/0012-shop-add-credit.sql @@ -0,0 +1,110 @@ +-- +-- 0012-shop-add-credit.sql +-- +-- #12 - stored function for cash deposit transactions +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(11) THEN + +CREATE OR REPLACE FUNCTION public.add_credit( + i_cash_account_id public.accounts.id%TYPE, + i_credit NUMERIC, + i_user_id public.accounts.id%TYPE, + i_user_name TEXT +) RETURNS VOID +LANGUAGE plpgsql +AS $$ +DECLARE + v_transaction_id public.transactions.id%TYPE; +BEGIN + -- Create a new transaction + v_transaction_id := brmbar_privileged.create_transaction(i_user_id, 'BrmBar credit replenishment for ' || i_user_name); + -- Debit cash (credit replenishment) + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (v_transaction_id, 'debit', i_cash_account_id, i_credit, i_user_name); + -- Credit the user + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (v_transaction_id, 'credit', i_user_id, i_credit, 'Credit replenishment'); +END; +$$; + + + +CREATE OR REPLACE FUNCTION public.undo_sale_of_item( + i_item_id public.accounts.id%TYPE, + i_amount INTEGER, + i_user_id public.accounts.id%TYPE, + i_target_currency_id public.currencies.id%TYPE, + i_description TEXT +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +DECLARE + v_buy_rate NUMERIC; + v_sell_rate NUMERIC; + v_cost NUMERIC; + v_profit NUMERIC; + v_transaction_id public.transactions.id%TYPE; +BEGIN + -- Get the buy and sell rates from the stored functions + v_buy_rate := public.find_buy_rate(i_item_id, i_target_currency_id); + v_sell_rate := public.find_sell_rate(i_item_id, i_target_currency_id); + + -- Calculate cost and profit + v_cost := i_amount * v_sell_rate; + v_profit := i_amount * (v_sell_rate - v_buy_rate); + + -- Create a new transaction + v_transaction_id := brmbar_privileged.create_transaction(i_user_id, i_description); + + -- the item (decrease stock) + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'debit', i_item_id, i_amount, + (SELECT "name" || ' (sale undo)' FROM public.accounts WHERE id = i_user_id)); + + -- the user + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', i_user_id, v_cost, + (SELECT "name" || ' (sale undo)' FROM public.accounts WHERE id = i_item_id)); + + -- the profit + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin repaid on ' || "name" FROM public.accounts WHERE id = i_item_id)); + + -- Return the cost + RETURN v_cost; +END; +$$; + +PERFORM brmbar_privileged.upgrade_schema_version_to(12); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From 7b11a8c954c71f52d64f0b0c180ffbe7f1372b19 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 17:30:57 +0200 Subject: [PATCH 101/112] #12 - stored function for cash deposit transactions - remove duplicate fn --- brmbar3/schema/0012-shop-add-credit.sql | 46 ------------------------- 1 file changed, 46 deletions(-) diff --git a/brmbar3/schema/0012-shop-add-credit.sql b/brmbar3/schema/0012-shop-add-credit.sql index 48f2da6..00318ae 100644 --- a/brmbar3/schema/0012-shop-add-credit.sql +++ b/brmbar3/schema/0012-shop-add-credit.sql @@ -55,52 +55,6 @@ $$; -CREATE OR REPLACE FUNCTION public.undo_sale_of_item( - i_item_id public.accounts.id%TYPE, - i_amount INTEGER, - i_user_id public.accounts.id%TYPE, - i_target_currency_id public.currencies.id%TYPE, - i_description TEXT -) RETURNS NUMERIC -LANGUAGE plpgsql -AS $$ -DECLARE - v_buy_rate NUMERIC; - v_sell_rate NUMERIC; - v_cost NUMERIC; - v_profit NUMERIC; - v_transaction_id public.transactions.id%TYPE; -BEGIN - -- Get the buy and sell rates from the stored functions - v_buy_rate := public.find_buy_rate(i_item_id, i_target_currency_id); - v_sell_rate := public.find_sell_rate(i_item_id, i_target_currency_id); - - -- Calculate cost and profit - v_cost := i_amount * v_sell_rate; - v_profit := i_amount * (v_sell_rate - v_buy_rate); - - -- Create a new transaction - v_transaction_id := brmbar_privileged.create_transaction(i_user_id, i_description); - - -- the item (decrease stock) - INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) - VALUES (i_transaction_id, 'debit', i_item_id, i_amount, - (SELECT "name" || ' (sale undo)' FROM public.accounts WHERE id = i_user_id)); - - -- the user - INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) - VALUES (i_transaction_id, 'credit', i_user_id, v_cost, - (SELECT "name" || ' (sale undo)' FROM public.accounts WHERE id = i_item_id)); - - -- the profit - INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) - VALUES (i_transaction_id, 'credit', (SELECT account_id FROM accounts WHERE name = 'BrmBar Profits'), v_profit, (SELECT 'Margin repaid on ' || "name" FROM public.accounts WHERE id = i_item_id)); - - -- Return the cost - RETURN v_cost; -END; -$$; - PERFORM brmbar_privileged.upgrade_schema_version_to(12); END IF; From e15f1646ce460c288a17067905bd95889ad32e8a Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 17:35:03 +0200 Subject: [PATCH 102/112] #13: stored function for cash withdrawal transactions --- brmbar3/brmbar/Shop.py | 12 +++- brmbar3/schema/0013-shop-withdraw-credit.sql | 64 ++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 brmbar3/schema/0013-shop-withdraw-credit.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 0f672c2..85251de 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -100,10 +100,16 @@ class Shop: #self.db.commit() def withdraw_credit(self, credit, user): - transaction = self._transaction(responsible = user, description = "BrmBar credit withdrawal for " + user.name) - self.cash.credit(transaction, credit, user.name) - user.debit(transaction, credit, "Credit withdrawal") + trn = self.db.execute_and_fetch( + "SELECT public.withdraw_credit(%s, %s, %s, %s)", + [self.cash.id, credit, user.id, user.name] + )[0] self.db.commit() + return trn # unused + #transaction = self._transaction(responsible = user, description = "BrmBar credit withdrawal for " + user.name) + #self.cash.credit(transaction, credit, user.name) + #user.debit(transaction, credit, "Credit withdrawal") + #self.db.commit() def transfer_credit(self, userfrom, userto, amount): self.add_credit(amount, userto) diff --git a/brmbar3/schema/0013-shop-withdraw-credit.sql b/brmbar3/schema/0013-shop-withdraw-credit.sql new file mode 100644 index 0000000..d379186 --- /dev/null +++ b/brmbar3/schema/0013-shop-withdraw-credit.sql @@ -0,0 +1,64 @@ +-- +-- 0013-shop-withdraw-credit.sql +-- +-- #13 - stored function for cash withdrawal transactions +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(12) THEN + +CREATE OR REPLACE FUNCTION public.withdraw_credit( + i_cash_account_id public.accounts.id%TYPE, + i_credit NUMERIC, + i_user_id public.accounts.id%TYPE, + i_user_name TEXT +) RETURNS VOID +LANGUAGE plpgsql +AS $$ +DECLARE + v_transaction_id public.transactions.id%TYPE; +BEGIN + -- Create a new transaction + v_transaction_id := brmbar_privileged.create_transaction(i_user_id, 'BrmBar credit withdrawal for ' || i_user_name); + -- Debit cash (credit replenishment) + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (v_transaction_id, 'credit', i_cash_account_id, i_credit, i_user_name); + -- Credit the user + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (v_transaction_id, 'debit', i_user_id, i_credit, 'Credit withdrawal'); +END; +$$; + + + +PERFORM brmbar_privileged.upgrade_schema_version_to(13); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From ad832fc71ba7a0dfcc45ee6f2d6f851c8c453481 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 17:45:40 +0200 Subject: [PATCH 103/112] #14: stored function for "credit" transfer transactions --- brmbar3/brmbar/Shop.py | 19 ++++--- brmbar3/schema/0014-shop-transfer-credit.sql | 58 ++++++++++++++++++++ 2 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 brmbar3/schema/0014-shop-transfer-credit.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 85251de..df29c18 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -87,12 +87,11 @@ class Shop: return cost def add_credit(self, credit, user): - trn = self.db.execute_and_fetch( + self.db.execute_and_fetch( "SELECT public.add_credit(%s, %s, %s, %s)", [self.cash.id, credit, user.id, user.name] - )[0] + ) self.db.commit() - return trn # unused #transaction = self._transaction(responsible = user, description = "BrmBar credit replenishment for " + user.name) #self.cash.debit(transaction, credit, user.name) @@ -100,20 +99,24 @@ class Shop: #self.db.commit() def withdraw_credit(self, credit, user): - trn = self.db.execute_and_fetch( + self.db.execute_and_fetch( "SELECT public.withdraw_credit(%s, %s, %s, %s)", [self.cash.id, credit, user.id, user.name] - )[0] + ) self.db.commit() - return trn # unused #transaction = self._transaction(responsible = user, description = "BrmBar credit withdrawal for " + user.name) #self.cash.credit(transaction, credit, user.name) #user.debit(transaction, credit, "Credit withdrawal") #self.db.commit() def transfer_credit(self, userfrom, userto, amount): - self.add_credit(amount, userto) - self.withdraw_credit(amount, userfrom) + self.db.execute_and_fetch( + "SELECT public.transfer_credit(%s, %s, %s, %s)", + [self.cash.id, credit, user.id, user.name] + ) + self.db.commit() + #self.add_credit(amount, userto) + #self.withdraw_credit(amount, userfrom) def buy_for_cash(self, item, amount = 1): # Buy: Currency conversion from item currency to shop currency diff --git a/brmbar3/schema/0014-shop-transfer-credit.sql b/brmbar3/schema/0014-shop-transfer-credit.sql new file mode 100644 index 0000000..48dc644 --- /dev/null +++ b/brmbar3/schema/0014-shop-transfer-credit.sql @@ -0,0 +1,58 @@ +-- +-- 0014-shop-transfer-credit.sql +-- +-- #14 - stored function for "credit" transfer transactions +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(12) THEN + +CREATE OR REPLACE FUNCTION public.transfer_credit( + i_cash_account_id public.accounts.id%TYPE, + i_credit NUMERIC, + i_userfrom_id public.accounts.id%TYPE, + i_userfrom_name TEXT, + i_userto_id public.accounts.id%TYPE, + i_userto_name TEXT +) RETURNS VOID +LANGUAGE plpgsql +AS $$ +BEGIN + PERFORM public.add_credit(i_cash_account_id, i_credit, i_userto_id, i_userto_name); + PERFORM public.withdraw_credit(i_cash_account_id, i_credit, i_userfrom_id, i_userfrom_name); +END; +$$; + + + +PERFORM brmbar_privileged.upgrade_schema_version_to(13); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From 629b35655dd6fd97f506fc5598ab34a4d399589d Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 18:23:11 +0200 Subject: [PATCH 104/112] #15: stored function for cash-based stock replenishment transaction --- brmbar3/brmbar/Shop.py | 14 ++-- brmbar3/schema/0015-shop-buy-for-cash.sql | 82 +++++++++++++++++++++++ 2 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 brmbar3/schema/0015-shop-buy-for-cash.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index df29c18..f669c75 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -119,13 +119,17 @@ class Shop: #self.withdraw_credit(amount, userfrom) def buy_for_cash(self, item, amount = 1): + cost = self.db.execute_and_fetch( + "SELECT public.buy_for_cash(%s, %s, %s, %s, %s)", + [self.cash.id, item.id, amount, self.currency.id, item.name] + )[0] # Buy: Currency conversion from item currency to shop currency - (buy, sell) = item.currency.rates(self.currency) - cost = amount * buy + #(buy, sell) = item.currency.rates(self.currency) + #cost = amount * buy - transaction = self._transaction(description = "BrmBar stock replenishment of {}x {} for cash".format(amount, item.name)) - item.debit(transaction, amount, "Cash") - self.cash.credit(transaction, cost, item.name) + #transaction = self._transaction(description = "BrmBar stock replenishment of {}x {} for cash".format(amount, item.name)) + #item.debit(transaction, amount, "Cash") + #self.cash.credit(transaction, cost, item.name) self.db.commit() return cost diff --git a/brmbar3/schema/0015-shop-buy-for-cash.sql b/brmbar3/schema/0015-shop-buy-for-cash.sql new file mode 100644 index 0000000..9736d2e --- /dev/null +++ b/brmbar3/schema/0015-shop-buy-for-cash.sql @@ -0,0 +1,82 @@ +-- +-- 0015-shop-buy-for-cash.sql +-- +-- #15 - stored function for cash-based stock replenishment transaction +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(9) THEN + +CREATE OR REPLACE FUNCTION public.buy_for_cash( + i_cash_account_id public.accounts.id%TYPE, + i_item_id public.accounts.id%TYPE, + i_amount INTEGER, + i_target_currency_id public.currencies.id%TYPE, + i_item_name TEXT +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +DECLARE + v_buy_rate NUMERIC; + v_cost NUMERIC; + v_transaction_id public.transactions.id%TYPE; +BEGIN + -- this could fail and it would generate exception in python + -- FIXME: convert v_buy_rate < 0 into python exception + v_buy_rate := public.find_buy_rate(i_item_id, i_target_currency_id); + -- this could fail and it would generate exception in python, even though it is not used + --v_sell_rate := public.find_sell_rate(i_item_id, i_target_currency_id); + + -- Calculate cost and profit + v_cost := i_amount * v_buy_rate; + + -- Create a new transaction + v_transaction_id := brmbar_privileged.create_transaction(NULL, + 'BrmBar stock replenishment of ' || i_amount || 'x ' || i_item_name || ' for cash'); + + -- the item + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'debit', i_item_id, i_amount, + 'Cash'); + + -- the cash + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', i_cash_account_id, v_cost, + i_item_name); + + -- Return the cost + RETURN v_cost; +END; +$$; + +PERFORM brmbar_privileged.upgrade_schema_version_to(10); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From 1df7db93e71dd00db7e556f724879e3c1cc4528b Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 18:36:50 +0200 Subject: [PATCH 105/112] #16: stored function for receipt reimbursement transaction --- brmbar3/brmbar/Shop.py | 10 ++- .../schema/0016-shop-receipt-to-credit.sql | 64 +++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 brmbar3/schema/0016-shop-receipt-to-credit.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index f669c75..1bdd4c2 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -135,9 +135,13 @@ class Shop: return cost def receipt_to_credit(self, user, credit, description): - transaction = self._transaction(responsible = user, description = "Receipt: " + description) - self.profits.credit(transaction, credit, user.name) - user.credit(transaction, credit, "Credit from receipt: " + description) + #transaction = self._transaction(responsible = user, description = "Receipt: " + description) + #self.profits.credit(transaction, credit, user.name) + #user.credit(transaction, credit, "Credit from receipt: " + description) + self.db.execute_and_fetch( + "SELECT public.buy_for_cash(%s, %s, %s, %s, %s)", + [self.profits.id, user.id, user.name, credit, description] + )[0] self.db.commit() def _transaction(self, responsible = None, description = None): diff --git a/brmbar3/schema/0016-shop-receipt-to-credit.sql b/brmbar3/schema/0016-shop-receipt-to-credit.sql new file mode 100644 index 0000000..1b91e20 --- /dev/null +++ b/brmbar3/schema/0016-shop-receipt-to-credit.sql @@ -0,0 +1,64 @@ +-- +-- 0016-shop-buy-for-cash.sql +-- +-- #16 - stored function for receipt reimbursement transaction +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(9) THEN + +CREATE OR REPLACE FUNCTION public.receipt_reimbursement( + i_profits_id public.accounts.id%TYPE, + i_user_id public.accounts.id%TYPE, + i_user_name public.accounts.name%TYPE, + i_amount NUMERIC, + i_description TEXT +) RETURNS VOID +LANGUAGE plpgsql +AS $$ +DECLARE + v_transaction_id public.transactions.id%TYPE; +BEGIN + -- Create a new transaction + v_transaction_id := brmbar_privileged.create_transaction(i_user_id, + 'Receipt: ' || i_description); + -- the "profit" + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', i_profits_id, i_amount, i_user_name); + -- the user + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', i_user_id, i_amount, 'Credit from receipt: ' || i_description); +END; +$$; + +PERFORM brmbar_privileged.upgrade_schema_version_to(10); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From aad79dafa6a75ec60e1d6b129f0099489d22c796 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 18:45:00 +0200 Subject: [PATCH 106/112] fix bugs in schema version checking --- brmbar3/schema/0014-shop-transfer-credit.sql | 4 ++-- brmbar3/schema/0015-shop-buy-for-cash.sql | 4 ++-- brmbar3/schema/0016-shop-receipt-to-credit.sql | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/brmbar3/schema/0014-shop-transfer-credit.sql b/brmbar3/schema/0014-shop-transfer-credit.sql index 48dc644..7e13c03 100644 --- a/brmbar3/schema/0014-shop-transfer-credit.sql +++ b/brmbar3/schema/0014-shop-transfer-credit.sql @@ -29,7 +29,7 @@ SELECT pg_catalog.set_config('search_path', '', false); DO $upgrade_block$ BEGIN -IF brmbar_privileged.has_exact_schema_version(12) THEN +IF brmbar_privileged.has_exact_schema_version(13) THEN CREATE OR REPLACE FUNCTION public.transfer_credit( i_cash_account_id public.accounts.id%TYPE, @@ -49,7 +49,7 @@ $$; -PERFORM brmbar_privileged.upgrade_schema_version_to(13); +PERFORM brmbar_privileged.upgrade_schema_version_to(14); END IF; END; diff --git a/brmbar3/schema/0015-shop-buy-for-cash.sql b/brmbar3/schema/0015-shop-buy-for-cash.sql index 9736d2e..5cf12bb 100644 --- a/brmbar3/schema/0015-shop-buy-for-cash.sql +++ b/brmbar3/schema/0015-shop-buy-for-cash.sql @@ -29,7 +29,7 @@ SELECT pg_catalog.set_config('search_path', '', false); DO $upgrade_block$ BEGIN -IF brmbar_privileged.has_exact_schema_version(9) THEN +IF brmbar_privileged.has_exact_schema_version(14) THEN CREATE OR REPLACE FUNCTION public.buy_for_cash( i_cash_account_id public.accounts.id%TYPE, @@ -73,7 +73,7 @@ BEGIN END; $$; -PERFORM brmbar_privileged.upgrade_schema_version_to(10); +PERFORM brmbar_privileged.upgrade_schema_version_to(15); END IF; END; diff --git a/brmbar3/schema/0016-shop-receipt-to-credit.sql b/brmbar3/schema/0016-shop-receipt-to-credit.sql index 1b91e20..21af8b1 100644 --- a/brmbar3/schema/0016-shop-receipt-to-credit.sql +++ b/brmbar3/schema/0016-shop-receipt-to-credit.sql @@ -29,7 +29,7 @@ SELECT pg_catalog.set_config('search_path', '', false); DO $upgrade_block$ BEGIN -IF brmbar_privileged.has_exact_schema_version(9) THEN +IF brmbar_privileged.has_exact_schema_version(15) THEN CREATE OR REPLACE FUNCTION public.receipt_reimbursement( i_profits_id public.accounts.id%TYPE, @@ -55,7 +55,7 @@ BEGIN END; $$; -PERFORM brmbar_privileged.upgrade_schema_version_to(10); +PERFORM brmbar_privileged.upgrade_schema_version_to(16); END IF; END; From 96027ca66ae65c14287b6ead3ac2a3ef49eb9791 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 19:49:07 +0200 Subject: [PATCH 107/112] #19: stored function for "consolidation" transaction --- brmbar3/brmbar/Account.py | 15 ++-- brmbar3/brmbar/Shop.py | 29 ++++--- brmbar3/schema/0019-shop-consolidate.sql | 97 ++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 brmbar3/schema/0019-shop-consolidate.sql diff --git a/brmbar3/brmbar/Account.py b/brmbar3/brmbar/Account.py index 40e86be..215df11 100644 --- a/brmbar3/brmbar/Account.py +++ b/brmbar3/brmbar/Account.py @@ -46,11 +46,16 @@ 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 + bal = self.db.execute_and_fetch( + "SELECT public.compute_account_balance(%s)", + [self.id] + )[0] + return bal + #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): return self.currency.str(self.balance()) diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 1bdd4c2..e0084f5 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -248,18 +248,23 @@ class Shop: return False def consolidate(self): - transaction = self._transaction(description = "BrmBar inventory consolidation") - - excess_balance = self.excess.balance() - if excess_balance != 0: - print("Excess balance {} debited to profit".format(-excess_balance)) - self.excess.debit(transaction, -excess_balance, "Excess balance added to profit.") - self.profits.debit(transaction, -excess_balance, "Excess balance added to profit.") - deficit_balance = self.deficit.balance() - if deficit_balance != 0: - print("Deficit balance {} credited to profit".format(deficit_balance)) - self.deficit.credit(transaction, deficit_balance, "Deficit balance removed from profit.") - self.profits.credit(transaction, deficit_balance, "Deficit balance removed from profit.") + msg = self.db.execute_and_fetch( + "SELECT public.make_consolidate_transaction(%s, %s, %s)", + [self.excess.id, self.deficit.id, self.profits.id] + )[0] + #transaction = self._transaction(description = "BrmBar inventory consolidation") + #excess_balance = self.excess.balance() + #if excess_balance != 0: + # print("Excess balance {} debited to profit".format(-excess_balance)) + # self.excess.debit(transaction, -excess_balance, "Excess balance added to profit.") + # self.profits.debit(transaction, -excess_balance, "Excess balance added to profit.") + #deficit_balance = self.deficit.balance() + #if deficit_balance != 0: + # print("Deficit balance {} credited to profit".format(deficit_balance)) + # self.deficit.credit(transaction, deficit_balance, "Deficit balance removed from profit.") + # self.profits.credit(transaction, deficit_balance, "Deficit balance removed from profit.") + if msg != None: + print(msg) self.db.commit() def undo(self, oldtid): diff --git a/brmbar3/schema/0019-shop-consolidate.sql b/brmbar3/schema/0019-shop-consolidate.sql new file mode 100644 index 0000000..29e767b --- /dev/null +++ b/brmbar3/schema/0019-shop-consolidate.sql @@ -0,0 +1,97 @@ +-- +-- 0019-shop-buy-for-cash.sql +-- +-- #19 - stored function for "consolidation" transaction +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(18) THEN + +CREATE OR REPLACE FUNCTION public.compute_account_balance( + i_account_id public.accounts.id%TYPE +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +DECLARE + v_crsum NUMERIC; + v_dbsum NUMERIC; +BEGIN + SELECT COALESCE(SUM(CASE WHEN side='credit' THEN amount ELSE 0 END),0) crsum INTO v_crsum, + COALESCE(SUM(CASE WHEN side='debit' THEN amount ELSE 0 END),0) dbsum into v_dbsum + FROM public.transaction_splits ts WHERE ts.account=4 + RETURN v_dbsum - v_crsum; +END; $$; + +CREATE OR REPLACE FUNCTION public.make_consolidate_transaction( + i_excess_id public.accounts.id%TYPE, + i_deficit_id public.accounts.id%TYPE, + i_profits_id public.accounts.id%TYPE +) RETURNS TEXT +LANGUAGE plpgsql +AS $$ +DECLARE + v_transaction_id public.transactions.id%TYPE; + v_excess_balance NUMERIC; + v_deficit_balance NUMERIC; + v_ret TEXT; +BEGIN + v_ret := NULL; + -- Create a new transaction + v_transaction_id := brmbar_privileged.create_transaction(NULL, + 'BrmBar inventory consolidation'); + v_excess_balance := public.compute_account_balance(i_excess_id); + v_deficit_balance := public.compute_account_balance(i_deficit_id); + IF v_excess_balance <> 0 THEN + v_ret := 'Excess balance ' || -v_excess_balance || ' debited to profit'; + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'debit', i_excess_id, -v_excess_balance, + 'Excess balance added to profit.'); + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'debit', i_profits_id, -v_excess_balance, + 'Excess balance added to profit.'); + END IF; + IF v_deficit_balance <> 0 THEN + v_ret := COALESCE(v_ret, ''); + v_ret := v_ret || 'Deficit balance ' || v_deficit_balance || ' credited to profit'; + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', i_deficit_id, v_deficit_balance, + 'Deficit balance removed from profit.'); + INSERT INTO public.transaction_splits (transaction, side, account, amount, memo) + VALUES (i_transaction_id, 'credit', i_profits_id, v_deficit_balance, + 'Deficit balance removed from profit.'); + END IF; + RETURN v_ret; +END; +$$; + +PERFORM brmbar_privileged.upgrade_schema_version_to(19); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From c8892f825cf34956c2b7593790136fce45d7078b Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 19:56:15 +0200 Subject: [PATCH 108/112] cosmetic --- brmbar3/schema/0019-shop-consolidate.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brmbar3/schema/0019-shop-consolidate.sql b/brmbar3/schema/0019-shop-consolidate.sql index 29e767b..1290cf4 100644 --- a/brmbar3/schema/0019-shop-consolidate.sql +++ b/brmbar3/schema/0019-shop-consolidate.sql @@ -1,5 +1,5 @@ -- --- 0019-shop-buy-for-cash.sql +-- 0019-shop-consolidate.sql -- -- #19 - stored function for "consolidation" transaction -- From 69c405f7151a5cc048261745af316546e42367e5 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 19:56:39 +0200 Subject: [PATCH 109/112] #20: stored function for undo transaction --- brmbar3/brmbar/Shop.py | 2 +- brmbar3/schema/0020-shop-undo.sql | 64 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 brmbar3/schema/0020-shop-undo.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index e0084f5..c8f4a36 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -277,6 +277,6 @@ class Shop: # memo = 'undo %d (%s)' % (splitid, memo) # amount = -amount # self.db.execute("INSERT INTO transaction_splits (transaction, side, account, amount, memo) VALUES (%s, %s, %s, %s, %s)", [transaction, side, account, amount, memo]) - transaction = self.db.execute_and_fetch("CALL undo_transaction(%s)",[oldtid])[0] + transaction = self.db.execute_and_fetch("SELECT public.undo_transaction(%s)",[oldtid])[0] self.db.commit() return transaction diff --git a/brmbar3/schema/0020-shop-undo.sql b/brmbar3/schema/0020-shop-undo.sql new file mode 100644 index 0000000..a279580 --- /dev/null +++ b/brmbar3/schema/0020-shop-undo.sql @@ -0,0 +1,64 @@ +-- +-- 0020-shop-undo.sql +-- +-- #20 - stored function for undo transaction +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(19) THEN + +CREATE OR REPLACE FUNCTION public.undo_transaction( + IN i_id public.transactions.id%TYPE) +RETURNS public.transactions.id%TYPE +VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $fn$ +DECLARE + v_ntrn_id public.transactions.id%TYPE; + v_old_trn public.transactions%ROWTYPE; + v_old_split public.transaction_splits%ROWTYPE; +BEGIN + SELECT * INTO v_old_trn FROM public.transactions WHERE id = i_id; + INSERT INTO transactions ("description") VALUES ('undo '||o_id||' ('||v_old_trn.description||')') RETURNING id into v_ntrn_id; + FOR v_old_split IN + SELECT * FROM transaction_splits WHERE "transaction" = i_id + LOOP + INSERT INTO transaction_splits ("transaction", "side", "account", "amount", "memo") + VALUES (v_ntrn_id, v_old_split.side, v_old_split.account, -v_old_split.amount, + 'undo ' || v_old_split.id || ' (' || v_old_split.memo || ')' ); + END LOOP; + RETURN v_ntrn_id; +END; +$fn$; + + + +PERFORM brmbar_privileged.upgrade_schema_version_to(20); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From c21b394b429c449f32995228623ead792b4e1953 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 21:14:18 +0200 Subject: [PATCH 110/112] #17: stored function for "fixing" inventory transaction --- brmbar3/brmbar/Shop.py | 53 ++++--- brmbar3/schema/0017-shop-fix-inventory.sql | 157 +++++++++++++++++++++ brmbar3/schema/0019-shop-consolidate.sql | 15 -- 3 files changed, 187 insertions(+), 38 deletions(-) create mode 100644 brmbar3/schema/0017-shop-fix-inventory.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index c8f4a36..2d96fce 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -202,30 +202,37 @@ class Shop: return accts def fix_inventory(self, item, amount): - amount_in_reality = amount - amount_in_system = item.balance() - (buy, sell) = item.currency.rates(self.currency) + rv = self.db.execute_and_fetch( + "SELECT public.fix_inventory(%s, %s, %s, %s, %s, %s)", + [item.id, item.currency.id, self.excess.id, self.deficit.id, self.currency.id, amount] + )[0] - diff = abs(amount_in_reality - amount_in_system) - buy_total = buy * diff - if amount_in_reality > amount_in_system: - transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) - item.debit(transaction, diff, "Inventory fix excess") - self.excess.credit(transaction, buy_total, "Inventory fix excess " + item.name) - self.db.commit() - return True - elif amount_in_reality < amount_in_system: - transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) - item.credit(transaction, diff, "Inventory fix deficit") - self.deficit.debit(transaction, buy_total, "Inventory fix deficit " + item.name) - self.db.commit() - return True - else: - transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) - item.debit(transaction, 0, "Inventory fix - amount was correct") - item.credit(transaction, 0, "Inventory fix - amount was correct") - self.db.commit() - return False + self.db.commit() + return rv + #amount_in_reality = amount + #amount_in_system = item.balance() + #(buy, sell) = item.currency.rates(self.currency) + + #diff = abs(amount_in_reality - amount_in_system) + #buy_total = buy * diff + #if amount_in_reality > amount_in_system: + # transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) + # item.debit(transaction, diff, "Inventory fix excess") + # self.excess.credit(transaction, buy_total, "Inventory fix excess " + item.name) + # self.db.commit() + # return True + #elif amount_in_reality < amount_in_system: + # transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) + # item.credit(transaction, diff, "Inventory fix deficit") + # self.deficit.debit(transaction, buy_total, "Inventory fix deficit " + item.name) + # self.db.commit() + # return True + #else: + # transaction = self._transaction(description = "BrmBar inventory fix of {}pcs {} in system to {}pcs in reality".format(amount_in_system, item.name,amount_in_reality)) + # item.debit(transaction, 0, "Inventory fix - amount was correct") + # item.credit(transaction, 0, "Inventory fix - amount was correct") + # self.db.commit() + # return False def fix_cash(self, amount): amount_in_reality = amount diff --git a/brmbar3/schema/0017-shop-fix-inventory.sql b/brmbar3/schema/0017-shop-fix-inventory.sql new file mode 100644 index 0000000..3bacd8d --- /dev/null +++ b/brmbar3/schema/0017-shop-fix-inventory.sql @@ -0,0 +1,157 @@ +-- +-- 0017-shop-fix-inventory.sql +-- +-- #17 - stored function for "fixing" inventory transaction +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(16) THEN + +CREATE OR REPLACE FUNCTION public.compute_account_balance( + i_account_id public.accounts.id%TYPE +) RETURNS NUMERIC +LANGUAGE plpgsql +AS $$ +DECLARE + v_crsum NUMERIC; + v_dbsum NUMERIC; +BEGIN + SELECT COALESCE(SUM(CASE WHEN side='credit' THEN amount ELSE 0 END),0) crsum INTO v_crsum, + COALESCE(SUM(CASE WHEN side='debit' THEN amount ELSE 0 END),0) dbsum into v_dbsum + FROM public.transaction_splits ts WHERE ts.account=4 + RETURN v_dbsum - v_crsum; +END; $$; + +CREATE OR REPLACE FUNCTION brmbar_privileged.fix_account_balance( + IN i_account_id public.acounts.id%TYPE, + IN i_account_currency_id public.currencies.id%TYPE, + IN i_excess_id public.acounts.id%TYPE, + IN i_deficit_id public.acounts.id%TYPE, + IN i_shop_currency_id public.currencies.id%TYPE, + IN i_amount_in_reality NUMERIC +) RETURNS BOOLEAN +VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $fn$ +DECLARE + v_amount_in_system NUMERIC; + v_buy_rate NUMERIC; + v_currency_id public.currencies.id%TYPE; + v_diff NUMERIC; + v_buy_total NUMERIC; + v_ntrn_id public.transactions.id%TYPE; + v_transaction_memo TEXT; + v_item_name TEXT; + v_excess_memo TEXT; + v_deficit_memo TEXT; + + v_old_trn public.transactions%ROWTYPE; + v_old_split public.transaction_splits%ROWTYPE; +BEGIN + v_amount_in_system := public.compute_account_balance(i_account_id); + IF i_account_currency_id <> i_shop_currency_id THEN + v_buy_rate := public.find_buy_rate(i_item_id, i_shop_currency_id); + ELSE + v_buy_rate := 1; + END IF; + + v_diff := ABS(i_amount_in_reality - v_amount_in_system); + v_buy_total := v_buy_rate * v_diff; + -- compute memo strings + IF i_item_id = 1 THEN -- cash account recognized by magic id + -- fixing cash + v_transaction_memo := + 'BrmBar cash inventory fix of ' || v_amount_in_system + || ' in system to ' || i_amount_in_reality || ' in reality'; + v_excess_memo := 'Inventory cash fix excess.'; + v_deficit_memo := 'Inventory fix deficit.'; + ELSE + -- fixing other account + SELECT "name" INTO v_item_name FROM public.accounts WHERE id = i_account_id; + v_transaction_memo := + 'BrmBar inventory fix of ' || v_amount_in_system || 'pcs ' + || v_item_name + || ' in system to ' || i_amount_in_reality || 'pcs in reality'; + v_excess_memo := 'Inventory fix excess ' || v_item_name; + v_deficit_memo := 'Inventory fix deficit ' || v_item_name; + END IF; + -- create transaction based on the relation between counting and accounting + IF i_amount_in_reality > v_amount_in_system THEN + v_ntrn_id := brmbar_privileged.create_transaction(NULL, v_transaction_memo); + INSERT INTO transaction_splits ("transaction", "side", "account", "amount", "memo") + VALUES (v_ntrn_id, 'debit', i_item_id, v_diff, 'Inventory fix excess'); + INSERT INTO transaction_splits ("transaction", "side", "account", "amount", "memo") + VALUES (v_ntrn_id, 'credit', i_excess_id, v_buy_total, v_excess_memo); + RETURN TRUE; + ELSIF i_amount_in_reality < v_amount_in_system THEN + v_ntrn_id := brmbar_privileged.create_transaction(NULL, v_transaction_memo); + INSERT INTO transaction_splits ("transaction", "side", "account", "amount", "memo") + VALUES (v_ntrn_id, 'credit', i_item_id, v_diff, 'Inventory fix deficit'); + INSERT INTO transaction_splits ("transaction", "side", "account", "amount", "memo") + VALUES (v_ntrn_id, 'debit', i_deficit_id, v_buy_total, v_deficit_memo); + RETURN TRUE; + ELSIF i_account_id <> 1 THEN -- cash account recognized by magic id + -- record that everything is going on swimmingly only for noncash accounts (WTF) + v_ntrn_id := brmbar_privileged.create_transaction(NULL, v_transaction_memo); + INSERT INTO transaction_splits ("transaction", "side", "account", "amount", "memo") + VALUES (v_ntrn_id, 'debit', i_item_id, 0, 'Inventory fix - amount was correct'); + INSERT INTO transaction_splits ("transaction", "side", "account", "amount", "memo") + VALUES (v_ntrn_id, 'credit', i_item_id, 0, 'Inventory fix - amount was correct'); + RETURN FALSE; + END IF; + RETURN FALSE; +END; +$fn$; + + +CREATE OR REPLACE FUNCTION public.fix_inventory( + IN i_account_id public.acounts.id%TYPE, + IN i_account_currency_id public.currencies.id%TYPE, + IN i_excess_id public.acounts.id%TYPE, + IN i_deficit_id public.acounts.id%TYPE, + IN i_shop_currency_id public.currencies.id%TYPE, + IN i_amount_in_reality NUMERIC +) RETURNS BOOLEAN +VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $fn$ +BEGIN + RETURN brmbar_privileged.fix_account_balance( + i_account_id, + i_account_currency_id, + i_excess_id, + i_deficit_id, + i_shop_currency_id, + i_amount_in_reality + ); +END; +$fn$; + + +PERFORM brmbar_privileged.upgrade_schema_version_to(17); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : diff --git a/brmbar3/schema/0019-shop-consolidate.sql b/brmbar3/schema/0019-shop-consolidate.sql index 1290cf4..db06685 100644 --- a/brmbar3/schema/0019-shop-consolidate.sql +++ b/brmbar3/schema/0019-shop-consolidate.sql @@ -31,21 +31,6 @@ BEGIN IF brmbar_privileged.has_exact_schema_version(18) THEN -CREATE OR REPLACE FUNCTION public.compute_account_balance( - i_account_id public.accounts.id%TYPE -) RETURNS NUMERIC -LANGUAGE plpgsql -AS $$ -DECLARE - v_crsum NUMERIC; - v_dbsum NUMERIC; -BEGIN - SELECT COALESCE(SUM(CASE WHEN side='credit' THEN amount ELSE 0 END),0) crsum INTO v_crsum, - COALESCE(SUM(CASE WHEN side='debit' THEN amount ELSE 0 END),0) dbsum into v_dbsum - FROM public.transaction_splits ts WHERE ts.account=4 - RETURN v_dbsum - v_crsum; -END; $$; - CREATE OR REPLACE FUNCTION public.make_consolidate_transaction( i_excess_id public.accounts.id%TYPE, i_deficit_id public.accounts.id%TYPE, From f0058aad682da46f051486f0eeb4618f9e9b7b00 Mon Sep 17 00:00:00 2001 From: TMA Date: Mon, 21 Apr 2025 21:14:34 +0200 Subject: [PATCH 111/112] #18: stored function for "fixing cash" transaction --- brmbar3/brmbar/Shop.py | 41 ++++++++++-------- brmbar3/schema/0018-shop-fix-cash.sql | 60 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 brmbar3/schema/0018-shop-fix-cash.sql diff --git a/brmbar3/brmbar/Shop.py b/brmbar3/brmbar/Shop.py index 2d96fce..e7dbc6c 100644 --- a/brmbar3/brmbar/Shop.py +++ b/brmbar3/brmbar/Shop.py @@ -235,24 +235,31 @@ class Shop: # return False def fix_cash(self, amount): - amount_in_reality = amount - amount_in_system = self.cash.balance() + rv = self.db.execute_and_fetch( + "SELECT public.fix_cash(%s, %s, %s, %s)", + [self.excess.id, self.deficit.id, self.currency.id, amount] + )[0] - diff = abs(amount_in_reality - amount_in_system) - if amount_in_reality > amount_in_system: - transaction = self._transaction(description = "BrmBar cash inventory fix of {} in system to {} in reality".format(amount_in_system, amount_in_reality)) - self.cash.debit(transaction, diff, "Inventory fix excess") - self.excess.credit(transaction, diff, "Inventory cash fix excess.") - self.db.commit() - return True - elif amount_in_reality < amount_in_system: - transaction = self._transaction(description = "BrmBar cash inventory fix of {} in system to {} in reality".format(amount_in_system, amount_in_reality)) - self.cash.credit(transaction, diff, "Inventory fix deficit") - self.deficit.debit(transaction, diff, "Inventory fix deficit.") - self.db.commit() - return True - else: - return False + self.db.commit() + return rv + #amount_in_reality = amount + #amount_in_system = self.cash.balance() + + #diff = abs(amount_in_reality - amount_in_system) + #if amount_in_reality > amount_in_system: + # transaction = self._transaction(description = "BrmBar cash inventory fix of {} in system to {} in reality".format(amount_in_system, amount_in_reality)) + # self.cash.debit(transaction, diff, "Inventory fix excess") + # self.excess.credit(transaction, diff, "Inventory cash fix excess.") + # self.db.commit() + # return True + #elif amount_in_reality < amount_in_system: + # transaction = self._transaction(description = "BrmBar cash inventory fix of {} in system to {} in reality".format(amount_in_system, amount_in_reality)) + # self.cash.credit(transaction, diff, "Inventory fix deficit") + # self.deficit.debit(transaction, diff, "Inventory fix deficit.") + # self.db.commit() + # return True + #else: + # return False def consolidate(self): msg = self.db.execute_and_fetch( diff --git a/brmbar3/schema/0018-shop-fix-cash.sql b/brmbar3/schema/0018-shop-fix-cash.sql new file mode 100644 index 0000000..2af5d72 --- /dev/null +++ b/brmbar3/schema/0018-shop-fix-cash.sql @@ -0,0 +1,60 @@ +-- +-- 0018-shop-fix-cash.sql +-- +-- #18 - stored function for "fixing cash" transaction +-- +-- ISC License +-- +-- Copyright 2023-2025 Brmlab, z.s. +-- TMA +-- +-- Permission to use, copy, modify, and/or distribute this software +-- for any purpose with or without fee is hereby granted, provided +-- that the above copyright notice and this permission notice appear +-- in all copies. +-- +-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE +-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR +-- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS +-- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +-- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +-- CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +-- + +-- To require fully-qualified names +SELECT pg_catalog.set_config('search_path', '', false); + +DO $upgrade_block$ +BEGIN + +IF brmbar_privileged.has_exact_schema_version(17) THEN + +CREATE OR REPLACE FUNCTION public.fix_cash( + IN i_excess_id public.acounts.id%TYPE, + IN i_deficit_id public.acounts.id%TYPE, + IN i_shop_currency_id public.currencies.id%TYPE, + IN i_amount_in_reality NUMERIC +) RETURNS BOOLEAN +VOLATILE NOT LEAKPROOF LANGUAGE plpgsql AS $fn$ +BEGIN + RETURN brmbar_privileged.fix_account_balance( + 1, + 1, + i_excess_id, + i_deficit_id, + i_shop_currency_id, + i_amount_in_reality + ); +END; +$fn$; + + +PERFORM brmbar_privileged.upgrade_schema_version_to(18); +END IF; + +END; +$upgrade_block$; + +-- vim: set ft=plsql : From 0bff27da29bd51e613b65b8b0a7c6992a1917105 Mon Sep 17 00:00:00 2001 From: TMA Date: Tue, 22 Apr 2025 00:00:38 +0200 Subject: [PATCH 112/112] test reimplementation of brmbar.Currency.rates method --- brmbar3/brmbar/Currency.py | 17 ++++++++ brmbar3/test--currency-rates.py | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 brmbar3/test--currency-rates.py diff --git a/brmbar3/brmbar/Currency.py b/brmbar3/brmbar/Currency.py index 7c56984..29da4a3 100644 --- a/brmbar3/brmbar/Currency.py +++ b/brmbar3/brmbar/Currency.py @@ -57,6 +57,23 @@ class Currency: return (buy, sell) + def rates2(self, other): + # the original code for compare testing + res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [self.id, other.id]) + if res is None: + raise NameError("Currency.rate(): Unknown conversion " + other.name() + " to " + self.name()) + buy_rate, buy_rate_dir = res + buy = buy_rate if buy_rate_dir == "target_to_source" else 1/buy_rate + + res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [other.id, self.id]) + if res is None: + raise NameError("Currency.rate(): Unknown conversion " + self.name() + " to " + other.name()) + sell_rate, sell_rate_dir = res + sell = sell_rate if sell_rate_dir == "source_to_target" else 1/sell_rate + + return (buy, sell) + + def convert(self, amount, target): res = self.db.execute_and_fetch("SELECT rate, rate_dir FROM exchange_rates WHERE target = %s AND source = %s AND valid_since <= NOW() ORDER BY valid_since DESC LIMIT 1", [target.id, self.id]) if res is None: diff --git a/brmbar3/test--currency-rates.py b/brmbar3/test--currency-rates.py new file mode 100644 index 0000000..9ef93bb --- /dev/null +++ b/brmbar3/test--currency-rates.py @@ -0,0 +1,73 @@ +#!/usr/bin/python3 + +import sys +import subprocess + +#from brmbar import Database +#from brmbar import Currency + +from contextlib import closing +import psycopg2 +from brmbar.Database import Database +from brmbar.Currency import Currency +import math + +#import brmbar + + + +def approx_equal(a, b, tol=1e-6): + """Check if two (buy, sell) rate tuples are approximately equal.""" + return ( + isinstance(a, tuple) and isinstance(b, tuple) and + math.isclose(a[0], b[0], abs_tol=tol) and + math.isclose(a[1], b[1], abs_tol=tol) + ) + +def compare_exceptions(e1, e2): + """Compare exception types and messages.""" + return type(e1) == type(e2) and str(e1) == str(e2) + +def main(): + db = Database("dbname=brmbar") + + # Get all currencies + with closing(db.db_conn.cursor()) as cur: + cur.execute("SELECT id, name FROM currencies") + currencies = cur.fetchall() + + # Build Currency objects + currency_objs = [Currency(db, id, name) for id, name in currencies] + + # Test all currency pairs + for c1 in currency_objs: + for c2 in currency_objs: + #if c1.id == c2.id: + # continue + + try: + rates1 = c1.rates(c2) + exc1 = None + except (RuntimeError, NameError) as e1: + rates1 = None + exc1 = e1 + + try: + rates2 = c1.rates2(c2) + exc2 = None + except (RuntimeError, NameError) as e2: + rates2 = None + exc2 = e2 + + if exc1 or exc2: + if not compare_exceptions(exc1, exc2): + print(f"[EXCEPTION DIFFERENCE] {c1.name} -> {c2.name}") + print(f" rates() exception: {type(exc1).__name__}: {exc1}") + print(f" rates2() exception: {type(exc2).__name__}: {exc2}") + elif not approx_equal(rates1, rates2): + print(f"[VALUE DIFFERENCE] {c1.name} -> {c2.name}") + print(f" rates(): {rates1}") + print(f" rates2(): {rates2}") + +if __name__ == "__main__": + main()