From 868fa10610a4341e7c0d634310049930a33fa9e0 Mon Sep 17 00:00:00 2001 From: "Riley L." Date: Mon, 12 May 2025 12:34:19 +0200 Subject: add shortcuts to user list to purchase items without scanning slight mirgation, i.e. sorting products by user is necessary --- abrechenbarkeit.lua | 113 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 87 insertions(+), 26 deletions(-) (limited to 'abrechenbarkeit.lua') diff --git a/abrechenbarkeit.lua b/abrechenbarkeit.lua index 0210865..4820094 100755 --- a/abrechenbarkeit.lua +++ b/abrechenbarkeit.lua @@ -373,25 +373,27 @@ local function r_transaction_post() local pcode = data.pcode local pcount = tonumber(data.pcount) local comment = data.comment - local barcode_name = nil + local pname = data.pname - if pcode ~= nil and pcode ~= "" then + if pname ~= nil or (pcode ~= nil and pcode ~= "") then -- check if barcode exists - local exists = false - for p_name, p_barcode in read_barcodes() do - if p_barcode == pcode then - exists = true - barcode_name = p_name + if pname == nil then + local exists = false + for p_name, p_barcode in read_barcodes() do + if p_barcode == pcode then + exists = true + pname = p_name + end end - end - if not exists then - return error_box("{+error.unknown_barcode}") + if not exists then + return error_box("{+error.unknown_barcode}") + end end -- check if product exists local exists = false for p_amount, p_user, p_name in read_products() do - if barcode_name == p_name then + if pname == p_name then pcount = (tonumber(data.pcount) or 1) * (data.negate_pcount ~= nil and -1 or 1) amount = amount or pcount * p_amount user_src = user_src or p_user @@ -401,7 +403,7 @@ local function r_transaction_post() end end if not exists then - return error_box("{+error.unknown_product}"..barcode_name) + return error_box("{+error.unknown_product}: "..pname) end end @@ -467,8 +469,8 @@ local function r_user(username) { time = format_duration(os.time() - last_txn), username = urlencode(username) })) print([[]]) end - print([[ - ") end) end @@ -743,7 +785,8 @@ local function r_products_post() if name == nil or name:match("^" .. matchers.name .. "$") == nil then return error_box("{+error.invalid_name}") end - + + -- delete product if data.delete then local new_products = io.open("products.new", "w+") if new_products == nil then @@ -798,13 +841,31 @@ local function r_products_post() if user == nil or user:match(matchers_global.user) == nil then return error_box("{+error.invalid_user}") end - local products = io.open("products", "a+") - if products == nil then - return error_box("{+error.open_products}") + -- add product + local new_products = io.open("products.new", "w+") + if new_products == nil then + return error_box("{+error.open_new_products}") + end + local wrote = nil + -- prepend to any block in the file containing products of the same user + -- the shortcuts feature expects this! + for a_price, a_user, a_name in read_products() do + if user == a_user and not wrote then + wrote = true + products:write(string.format("%d,%s,%s\n", price, user, name)) + end + + new_products:write(string.format("%d,%s,%s\n", a_price, a_user, a_name)) + end + + -- append if not wrote already + if not wrote then + products:write(string.format("%d,%s,%s\n", price, user, name)) end - products:write(string.format("%d,%s,%s\n", price, user, name)) - products:flush() - products:close() + + new_products:flush() + new_products:close() + os.rename("products.new", "products") end end -- cgit v1.2.3-70-g09d2