diff --git a/abrechenbarkeit.lua b/abrechenbarkeit.lua index 6a1543e..5db7337 100755 --- a/abrechenbarkeit.lua +++ b/abrechenbarkeit.lua @@ -163,8 +163,8 @@ local function read_products() if l == "" or l == nil then return nil end - local barcode, price, name = string.match(l, "([%w_-]+),(-?%d+),([%w_ -]*)") - return barcode, tonumber(price), name + local barcode, price, name, owner = string.match(l, "([%w_-]+),(-?%d+),([%w_ -]*),([%w_ -]*)") + return barcode, tonumber(price), name, owner end end @@ -223,14 +223,24 @@ local function r_user_post(username) local comment = data.comment local pcode = nil local pcount = nil + local powner = nil if data.pcode then - for p_barcode, p_amount, p_name in read_products() do + for p_barcode, p_amount, p_name, p_owner in read_products() do if p_barcode == data.pcode then + powner = p_owner pcount = (tonumber(data.pcount) or 1) * (data.negate_pcount ~= nil and -1 or 1) pcode = p_barcode if amount == nil then amount = pcount * p_amount end - if comment == nil then comment = string.format("%s %d %s", pcount < 0 and "Buy" or "Restock", - math.abs(pcount), p_name) end + if comment == nil then + comment = string.format("%s %d %s", pcount < 0 and "Buy" or "Restock", + math.abs(pcount), p_name) + + powner_comment = string.format("%s %d %s %s %s", + pcount < 0 and "Sell" or "Restock", -- TOOD: look for name + math.abs(pcount), p_name, + pcount < 0 and "to" or "by", + username) + end end end if amount == nil then @@ -248,7 +258,12 @@ local function r_user_post(username) return error_box("failed to open log") end local time = os.time() + -- subtract from buyer log:write(string.format("%d,%s,%d,%s,%s,%s\n", time, username, amount, pcode or "", pcount or "", comment)) + -- add to owner + if powner then + log:write(string.format("%d,%s,%d,%s,%s,%s\n", time, powner, -amount, pcode or "", pcount or "", powner_comment)) + end log:flush() log:close() return string.format([[ @@ -429,9 +444,9 @@ local function r_products_post() if new_products == nil then return error_box("failed to open new products") end - for a_barcode, price, name in read_products() do + for a_barcode, price, name, owner in read_products() do if barcode ~= a_barcode then - new_products:write(string.format("%s,%d,%s\n", a_barcode, price, name)) + new_products:write(string.format("%s,%d,%s,%s\n", a_barcode, price, name, owner)) end end new_products:flush() @@ -450,7 +465,11 @@ local function r_products_post() if products == nil then return error_box("failed to open products") end - products:write(string.format("%s,%d,%s\n", barcode, price, name)) + local owner = data.owner or "" + if name:match("^[%w_ -]*$") == nil then + return error_box("owner invalid") + end + products:write(string.format("%s,%d,%s,%s\n", barcode, price, name, owner)) products:flush() products:close() end @@ -472,6 +491,8 @@ local function r_products() + + @@ -485,16 +506,17 @@ local function r_products() ]]) - print([[]]) + print([[
NamePriceBarcodeCount
]]) local pbals = product_balances() - for barcode, price, name in read_products() do + for barcode, price, name, owner in read_products() do print(string.format([[ - + ]], name, -price >= 0 and "pos" or "neg", -price / 100, barcode, - pbals[barcode] or "0" + pbals[barcode] or "0", + owner )) end print("
NamePriceBarcodeCountOwner
%s%.02f€%s%s
%s%.02f€%s%s%s
")