From 20c3a25e43eca445b34329547a619a874ea7fbd4 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 3 Nov 2024 19:38:24 +0100 Subject: [PATCH] add columns for product txns --- abrechenbarkeit.lua | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/abrechenbarkeit.lua b/abrechenbarkeit.lua index 0e246af..17e08f4 100755 --- a/abrechenbarkeit.lua +++ b/abrechenbarkeit.lua @@ -118,8 +118,9 @@ local function read_log() if l == "" or l == nil then return nil end - local time, username, amount, comment = string.match(l, "(%d+),([%w_ -]+),(-?%d+),([%w_ -]*)") - return tonumber(time), username, tonumber(amount), comment + local time, username, amount, pcode, pcount, comment = string.match(l, + "(%d+),([%w_ -]+),(-?%d+),([%w_-]*),(-?%d*),([%w_ -]*)") + return tonumber(time), username, tonumber(amount), pcode, tonumber(pcount), comment end end @@ -141,15 +142,25 @@ end local function balances() local users = {} - for _, username, amount, _ in read_log() do + for _, username, amount, _, _, _ in read_log() do users[username] = (users[username] or 0) + amount end return users end +local function product_balances() + local products = {} + for _, _, _, pcode, pcount, _ in read_log() do + if pcode ~= nil and pcount ~= nil then + products[pcode] = (products[pcode] or 0) + pcount + end + end + return products +end + local function last_txns() local users = {} - for time, username, _, _ in read_log() do + for time, username, _, _, _, _ in read_log() do users[username] = time end return users @@ -163,10 +174,14 @@ local function r_user_post(username) local data = form_data() local amount = nil local comment = "" + local pcode = nil + local pcount = nil if data.product then for p_barcode, p_amount, p_name in read_products() do if p_barcode == data.product then - amount = p_amount + pcount = tonumber(data.count) or -1 + pcode = p_barcode + amount = pcount * p_amount comment = p_name end end @@ -188,7 +203,7 @@ local function r_user_post(username) return error_box("failed to open log") end local time = os.time() - log:write(string.format("%d,%s,%d,%s\n", time, username, amount, comment)) + log:write(string.format("%d,%s,%d,%s,%s,%s\n", time, username, amount, pcode or "", pcount or "", comment)) log:flush() log:close() return string.format([[ @@ -263,8 +278,8 @@ end local function r_log(filter) return respond(200, "Abrechnungen", function() print("") - print("") - for time, username, amount, comment in read_log() do + print("") + for time, username, amount, pcode, pcount, comment in read_log() do if filter == nil or filter == username then print(string.format([[ @@ -272,6 +287,8 @@ local function r_log(filter) + +
TimeUsernameAmountComment
TimeUsernameAmountP.-BarcodeP.-CountComment
%s %.02f€ %s%s%s
@@ -284,6 +301,8 @@ local function r_log(filter) time, format_duration(os.time() - time), escape(username), amount >= 0 and "pos" or "neg", amount / 100, + escape(pcode) or "", + pcount and tostring(pcount) or "", escape(comment), escape(username), -amount, @@ -397,14 +416,16 @@ local function r_products()
]]) - print("") + print("
NamePriceBarcode
") + local pbals = product_balances() for barcode, price, name in read_products() do print(string.format([[ - + ]], name, price >= 0 and "pos" or "neg", price / 100, - barcode + barcode, + pbals[barcode] or "0" )) end print("
NamePriceBarcodeCount
%s%.02f€%s
%s%.02f€%s%s
")