Module:PriceCalculator: Difference between revisions

No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- Internal function to calculate price
function p._calculate(price, priceType)
function p._calculate(price, priceType)
-- Ensure price is a string, then strip commas
-- Sanitize input: convert to string, remove commas
price = tostring(price):gsub(",", "")
price = tostring(price):gsub(",", "")
price = tonumber(price)
price = tonumber(price)


-- Error handling
if not price or price < 0 then
if not price or price < 0 then
return "Invalid price: " .. tostring(price)
return "Invalid price: " .. tostring(price)
end
end


-- Lookup conversion factors
local percent = {
local percent = {
sell = 0.6675,
sell = 0.6675,
Line 18: Line 21:
local factor = percent[priceType]
local factor = percent[priceType]
if not factor then
if not factor then
return "Invalid type"
return "Invalid type: " .. tostring(priceType)
end
end


-- Calculate and return rounded result
return math.floor(price * factor + 0.5)
return math.floor(price * factor + 0.5)
end
end


-- Entry point for #invoke
function p.calculate(frame)
function p.calculate(frame)
local args = frame:getParent().args
local args = frame.args -- Use direct args from #invoke
local price = args.price or args[1]
local price = args.price or args[1]
local priceType = args.type or args[2]
local priceType = args.type or args[2]