Jump to content

Module:Sandbox/Greenbreen/MinimumWage

From Wikipedia, the free encyclopedia
local PPP = require("Module:Sandbox/Greenbreen/PPP")

local p = {}

local function trim(s)
    return s and s:match("^%s*(.-)%s*$")
end

local function annualize(value, frequency, workweek)
    frequency = trim(frequency)
    frequency = (frequency or "annually"):lower()
    workweek = tonumber(workweek) or 40

    if frequency == "hourly" then
        return value * workweek * 52
    elseif frequency == "daily" then
        return value * 5 * 52
    elseif frequency == "weekly" then
        return value * 52
    elseif frequency == "monthly" then
        return value * 12
    elseif frequency == "13 times annually" then
        return value * 13
    elseif frequency == "annually" then
        return value
    else
        return nil
    end
end

function p.render(frame)
    local a = frame.args
    if not a[1] then
        a = frame:getParent().args
    end

    local value = tonumber(a[1])
    local country = trim(a[2])
    local frequency = a[3]

    if not value then
        return "Error: missing value"
    end

	if not country then
		return "Error: missing country"
	end

    local annual = annualize(value, frequency, a.workweek)
    if not annual then
        return "Error: unknown frequency"
    end

    local result = PPP.convert(annual, country, a.year)
    if not result then
        return "Error: PPP conversion failed"
    end

    if a.round then
        result = math.floor(result + 0.5)
    end

    if a.prefix == "no" then
        return result
    else
        return mw.getCurrentFrame():expandTemplate{
            title = "nts",
            args = { result }
        }
    end
end

return p