add columns for product txns

This commit is contained in:
metamuffin 2024-11-03 19:38:24 +01:00
parent 79657b9e94
commit 20c3a25e43
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654

View file

@ -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("<table>")
print("<tr><th>Time</th><th>Username</th><th>Amount</th><th>Comment</th></tr>")
for time, username, amount, comment in read_log() do
print("<tr><th>Time</th><th>Username</th><th>Amount</th><th>P.-Barcode</th><th>P.-Count</th><th>Comment</th></tr>")
for time, username, amount, pcode, pcount, comment in read_log() do
if filter == nil or filter == username then
print(string.format([[
<tr>
@ -272,6 +287,8 @@ local function r_log(filter)
<td>%s</td>
<td class="amount-%s">%.02f</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<form action="/%s" method="POST">
<input type="number" name="amount" id="amount" value="%d" hidden />
@ -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()
</form>
</div>
]])
print("<table><tr><th>Name</th><th>Price</th><th>Barcode</th></tr>")
print("<table><tr><th>Name</th><th>Price</th><th>Barcode</th><th>Count</th></tr>")
local pbals = product_balances()
for barcode, price, name in read_products() do
print(string.format([[
<tr><td>%s</td><td class="amount-%s">%.02f</td><td>%s</td></tr>
<tr><td>%s</td><td class="amount-%s">%.02f</td><td>%s</td><td>%s</td></tr>
]],
name,
price >= 0 and "pos" or "neg", price / 100,
barcode
barcode,
pbals[barcode] or "0"
))
end
print("</table>")