restock form

This commit is contained in:
metamuffin 2024-11-03 21:21:35 +01:00
parent 7b07957b6e
commit f7af00c0d2
No known key found for this signature in database
GPG key ID: 718F9749DCDBD654
2 changed files with 39 additions and 21 deletions

View file

@ -172,30 +172,27 @@ end
local function r_user_post(username) local function r_user_post(username)
local data = form_data() local data = form_data()
local amount = nil local amount = tonumber(data.amount)
local comment = "" local comment = data.comment
local pcode = nil local pcode = nil
local pcount = nil local pcount = nil
if data.product then if data.pcode then
for p_barcode, p_amount, p_name in read_products() do for p_barcode, p_amount, p_name in read_products() do
if p_barcode == data.product then if p_barcode == data.pcode then
pcount = tonumber(data.count) or -1 pcount = (tonumber(data.pcount) or 1) * (data.negate_pcount ~= nil and -1 or 1)
pcode = p_barcode pcode = p_barcode
amount = pcount * p_amount if amount == nil then amount = pcount * p_amount end
comment = p_name if comment == nil then comment = string.format("%s %d %s", pcount < 0 and "Buy" or "Restock", math.abs(pcount), p_name) end
end end
end end
if amount == nil then if amount == nil then
return error_box("unknown product") return error_box("unknown product")
end end
else
amount = tonumber(data.amount)
comment = data.comment or ""
end end
if amount == nil then if amount == nil then
return error_box("amount invalid") return error_box("amount invalid")
end end
if comment:match("^[%w_ -]*$") == nil then if comment == nil or comment:match("^[%w_ -]*$") == nil then
return error_box("comment invalid") return error_box("comment invalid")
end end
local log = io.open("log", "a+") local log = io.open("log", "a+")
@ -241,7 +238,7 @@ local function r_user(username)
]], format_duration(os.time() - last_txn), username)) ]], format_duration(os.time() - last_txn), username))
end end
print([[<div class="transactions container firstchildlarge">]]) print([[<div class="transactions container firstchildlarge">]])
print([[<div class="amount-presets backgroundbox">]]) print([[<div class="amount-presets backgroundbox">]])
for _, type in ipairs({ 1, -1 }) do for _, type in ipairs({ 1, -1 }) do
for _, amount in ipairs({ 50, 100, 150, 200, 500, 1000 }) do for _, amount in ipairs({ 50, 100, 150, 200, 500, 1000 }) do
print(string.format([[ print(string.format([[
@ -266,10 +263,23 @@ local function r_user(username)
</form> </form>
<form class="transaction box backgroundbox" action="" method="POST" id="buy_product"> <form class="transaction box backgroundbox" action="" method="POST" id="buy_product">
<h3>Buy Product</h3> <h3>Buy Product</h3>
<label for="product">Product: </label> <input type="text" name="negate_pcount" value="1" hidden />
<input type="text" name="product" id="product" /> <label for="pcount">Count: </label>
<input type="number" name="pcount" id="pcount" value="1" />
<label for="pcode">Product Barcode: </label>
<input type="text" name="pcode" id="pcode" />
<input class="amount-neg button" type="submit" value="Buy" /> <input class="amount-neg button" type="submit" value="Buy" />
</form> </form>
<form class="transaction box backgroundbox" action="" method="POST" id="buy_product">
<h3>Restock Product</h3>
<label for="pcount">Count: </label>
<input type="number" name="pcount" id="pcount" value="1" />
<label for="amount">Upstream price: </label>
<input type="number" name="amount" id="amount" />
<label for="pcode">Product Barcode: </label>
<input type="text" name="pcode" id="pcode" />
<input type="submit" value="Restock" />
</form>
]]) ]])
print("</div>") print("</div>")
end) end)
@ -278,7 +288,15 @@ end
local function r_log(filter) local function r_log(filter)
return respond(200, "Abrechnungen", function() return respond(200, "Abrechnungen", function()
print([[<table class="log">]]) print([[<table class="log">]])
print("<tr><th>Time</th><th>Username</th><th>Amount</th><th>P.-Barcode</th><th>P.-Count</th><th>Comment</th></tr>") print([[<tr>
<th>Time</th>
<th>Username</th>
<th>Amount</th>
<th>P.-Barcode</th>
<th>P.-Count</th>
<th>Comment</th>
<th>Actions</th>
</tr>]])
for time, username, amount, pcode, pcount, comment in read_log() do for time, username, amount, pcode, pcount, comment in read_log() do
if filter == nil or filter == username then if filter == nil or filter == username then
print(string.format([[ print(string.format([[
@ -287,7 +305,7 @@ local function r_log(filter)
<td>%s</td> <td>%s</td>
<td class="amount-%s">%.02f</td> <td class="amount-%s">%.02f</td>
<td>%s</td> <td>%s</td>
<td>%s</td> <td>%s %s</td>
<td>%s</td> <td>%s</td>
<td> <td>
<form action="/%s" method="POST"> <form action="/%s" method="POST">
@ -302,7 +320,7 @@ local function r_log(filter)
escape(username), escape(username),
amount >= 0 and "pos" or "neg", amount / 100, amount >= 0 and "pos" or "neg", amount / 100,
escape(pcode) or "", escape(pcode) or "",
pcount and tostring(pcount) or "", pcount and (pcount < 0 and "buy" or "stock") or "", pcount and tostring(math.abs(pcount)) or "",
escape(comment), escape(comment),
escape(username), escape(username),
-amount, -amount,
@ -399,12 +417,12 @@ local function r_products()
<div class="container"> <div class="container">
<form action="/?products" method="POST" class="box backgroundbox"> <form action="/?products" method="POST" class="box backgroundbox">
<h3>Add Product</h3> <h3>Add Product</h3>
<label for="barcode">Barcode: </label>
<input type="text" name="barcode" id="barcode" />
<label for="name">Name: </label> <label for="name">Name: </label>
<input type="text" name="name" id="name" /> <input type="text" name="name" id="name" />
<label for="price">Price (ct): </label> <label for="price">Price (ct): </label>
<input type="number" name="price" id="price" /> <input type="number" name="price" id="price" />
<label for="barcode">Barcode: </label>
<input type="text" name="barcode" id="barcode" />
<input type="submit" value="Add" class="amount-ntr button" /> <input type="submit" value="Add" class="amount-ntr button" />
</form> </form>
<form action="/?products" method="POST" class="box backgroundbox"> <form action="/?products" method="POST" class="box backgroundbox">
@ -423,7 +441,7 @@ local function r_products()
<tr><td>%s</td><td class="amount-%s">%.02f</td><td>%s</td><td>%s</td></tr> <tr><td>%s</td><td class="amount-%s">%.02f</td><td>%s</td><td>%s</td></tr>
]], ]],
name, name,
price >= 0 and "pos" or "neg", price / 100, -price >= 0 and "pos" or "neg", -price / 100,
barcode, barcode,
pbals[barcode] or "0" pbals[barcode] or "0"
)) ))

View file

@ -3,7 +3,7 @@
document.addEventListener("keypress", ev => { document.addEventListener("keypress", ev => {
if (!(document.activeElement instanceof HTMLInputElement)) { if (!(document.activeElement instanceof HTMLInputElement)) {
if (ev.code.startsWith("Digit")) if (ev.code.startsWith("Digit"))
document.forms.buy_product.product.value += ev.code.substring(5) document.forms.buy_product.pcode.value += ev.code.substring(5)
if (ev.code == "Enter") if (ev.code == "Enter")
document.forms.buy_product.submit() document.forms.buy_product.submit()
} }