add optional owners to products to realise different cash pools

This commit is contained in:
Riley L. 2024-11-04 12:24:17 +01:00
parent 292a8f0de7
commit 3922bb59f5

View file

@ -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()
<input type="text" name="name" id="name" />
<label for="price">Price (ct): </label>
<input type="number" name="price" id="price" />
<label for="owner">Owner: </label>
<input type="text" name="owner" id="owner" />
<label for="barcode">Barcode: </label>
<input type="text" name="barcode" id="barcode" />
<input type="submit" value="Add" class="amount-ntr button" />
@ -485,16 +506,17 @@ local function r_products()
</form>
</div>
]])
print([[<table class="productlist"><tr><th>Name</th><th>Price</th><th>Barcode</th><th>Count</th></tr>]])
print([[<table class="productlist"><tr><th>Name</th><th>Price</th><th>Barcode</th><th>Count</th><th>Owner</th></tr>]])
local pbals = product_balances()
for barcode, price, name in read_products() do
for barcode, price, name, owner in read_products() do
print(string.format([[
<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><td>%s</td></tr>
]],
name,
-price >= 0 and "pos" or "neg", -price / 100,
barcode,
pbals[barcode] or "0"
pbals[barcode] or "0",
owner
))
end
print("</table>")