From 83275a5131fff2530476a3ee76d96e6ddbf71141 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 5 Nov 2024 01:13:19 +0100 Subject: [PATCH] Make new scheme work basically --- abrechenbarkeit.lua | 88 ++++++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 36 deletions(-) diff --git a/abrechenbarkeit.lua b/abrechenbarkeit.lua index 7de97ac..2cf0ba3 100755 --- a/abrechenbarkeit.lua +++ b/abrechenbarkeit.lua @@ -17,14 +17,18 @@ along with this program. If not, see . ]] -- --- replace german chars with a better %w that allows unicode +-- TODO: allow unicode +-- TODO: somehow remove _opt variants local matchers = { time = "(%d+)", - user = "([%w_@ -öäüÖÄÜßẞ]+)", + user = "([%w_@ -]+)", amount = "(-?%d+)", - comment = "([%w_ -öäüÖÄÜßẞ]*)", - barcode = "([%w_-]*)", - name = "([%w_ -öäüÖÄÜßẞ]*)", + amount_opt = "(-?%d*)", + comment = "([%w_ -]+)", + comment_opt = "([%w_ -]*)", + barcode = "([%w_-]+)", + barcode_opt = "([%w_-]*)", + name = "([%w_ -]+)", } local matchers_global = (function() local s = {} @@ -235,7 +239,7 @@ local function read_log() return nil end local time, user_src, user_dst, amount, pcode, pcount, comment = string.match(l, - format("{time},{user},{user},{amount},{barcode},{amount},{comment}", matchers)) + format("^{time},{user},{user},{amount},{barcode_opt},{amount_opt},{comment_opt}$", matchers)) return tonumber(time), user_src, user_dst, tonumber(amount), pcode, tonumber(pcount), comment end end @@ -251,7 +255,7 @@ local function read_products() if l == "" or l == nil then return nil end - local barcode, price, user, name = string.match(l, "{barcode},{amount},{user},{name}") + local barcode, price, user, name = string.match(l, format("^{barcode},{amount},{user}?,{name}$", matchers)) return barcode, tonumber(price), user, name end end @@ -289,12 +293,12 @@ local function get_active_users() for time, user_src, user_dst, amount, _, _, _ in read_log() do user_balances[user_src] = { time = time, - user_src = user_src, + name = user_src, balance = (user_balances[user_src] or { balance = 0 }).balance - amount } user_balances[user_dst] = { time = time, - user_dst = user_dst, + name = user_dst, balance = (user_balances[user_dst] or { balance = 0 }).balance + amount } end @@ -308,15 +312,15 @@ local function get_active_users() return users end -local function r_user_post(username) +local function r_transaction_post() local data = form_data() - local user_src = data.user_src or username + local user_src = data.user_src local user_dst = data.user_dst local amount = tonumber(data.amount) local pcode = data.pcode local pcount = tonumber(data.pcount) local comment = data.comment - if pcode then + if pcode ~= nil and pcode ~= "" then local exists = false for p_barcode, p_amount, p_user, p_name in read_products() do if p_barcode == pcode then @@ -332,11 +336,11 @@ local function r_user_post(username) return error_box("unknown product") end end - user_dst = user_dst or "@global" + user_src = user_src or "@global" if amount == nil then return error_box("amount invalid") end - if comment == nil or comment:match(matchers_global.comment) == nil then + if comment == nil or comment:match(matchers_global.comment_opt) == nil then return error_box("comment invalid") end if user_src == nil or user_src:match(matchers_global.user) == nil then @@ -368,7 +372,7 @@ end local function r_user(username) local notif = nil if method == "POST" then - notif = r_user_post(username) + notif = r_transaction_post() end return respond(200, string.format("Abrechenbarheit: %s", username), function() print(format("

{username}

", { username = username })) @@ -393,10 +397,12 @@ local function r_user(username) for _, amount in ipairs({ 50, 100, 150, 200, 500, 1000 }) do local a = amount * type print(format([[
- - + + +
]], { + username = username, a_raw = a, amount = format_amount(a), sign = a < 0 and "neg" or "pos" @@ -407,6 +413,7 @@ local function r_user(username) print(format([[

{+user.form.transaction}

+ @@ -415,6 +422,7 @@ local function r_user(username)

{+user.form.buy}

+ @@ -424,6 +432,7 @@ local function r_user(username)

{+user.form.restock}

+ @@ -432,13 +441,18 @@ local function r_user(username)
- ]])) + ]], { username = username })) print("") end) end local function r_log(filter) + local notif = nil + if method == "POST" then + notif = r_transaction_post() + end return respond(200, "Abrechnungen", function() + if notif then print(notif) end print([[ @@ -455,17 +469,17 @@ local function r_log(filter) print(format([[ - + {amount} ]]) local pbals = product_balances() for barcode, price, user, name in read_products() do - print(string.format([[ + print(format([[ {price} @@ -618,7 +634,7 @@ local function r_products() ]], { name = name, - price = format_amount(-price), + price = format_amount(-price, "td"), barcode = barcode, count = pbals[barcode] or "0", user = user,
{+field.time}
{time} ({time_delta}){user_src} → {user_dst{user_src} → {user_dst}{pcode} {pcount} {comment} -
- - + + + - + @@ -483,7 +497,7 @@ local function r_log(filter) (pcount and tostring(math.abs(pcount)) or ""), comment = escape(comment), revert_amount = -amount, - revert_pcount = -pcount, + revert_pcount = -(pcount or 0), })) end end @@ -505,16 +519,18 @@ local function r_index() print([[
]]) -- for printing print([[
    ]]) for _, user in ipairs(get_active_users()) do - print(format([[
  • - - {!username} - {balance} - -
  • ]], { - username_url = urlencode(user.username), - username = user.username, - balance = format_amount(user.balance, "span") - })) + if user.name.sub(1, 1) ~= "@" then + print(format([[
  • + + {!username} + {balance} + +
  • ]], { + username_url = urlencode(user.name), + username = user.name, + balance = format_amount(user.balance, "span") + })) + end end print("
") end) @@ -610,7 +626,7 @@ local function r_products()
{!name}{!barcode}{!user}