Skip to content

Payroll exports with earning codes

The rule

Payroll does not know the policy’s categories: it pays earning codes (REG, OT, HOL). An export maps each category to its code and writes the hours the way the payroll system imports them. A category without a code, or a person without a payroll id, must stop the export: hours sent under a guessed code or to a guessed person are paid wrongly.

Who uses it

Every organization that tracks time in one system and pays in another, which is most of them. Summary CSV files suit modern payroll services; fixed-width files are what many older payroll and banking systems read.

How the recipe does it

An export names an exporter and its parameters when a file is asked for. The engine gives an exporter one person’s ledger of the period at a time, with hours by category for the period and for each day, the person’s employment attributes, and the period’s first and last day. Hours are text in the tenant’s decimals, carried forward line by line, never rounded again in the file.

  • The summary CSV is the standard exporter, standard/csv, with codes (each category’s earning code) and by = "period": one line per person per category for the whole period, under its code.

    uses = "standard/csv"
    params = { id_attribute = "payroll_id", codes = { work = "REG", overtime = "OT", holiday = "HOL" }, by = "period" }
  • The fixed-width file is the package’s own exporter, payroll_export/fixed_width, with the same id_attribute and codes: one record per person, day and category, each field at its column (the payroll id in 10 characters, the code in 4, the date as YYYYMMDD, the hours in 7 digits with two implied decimals), then a trailer record with how many people the file holds.

Both read the payroll id from the employment attribute id_attribute and stop, with an error naming what is missing, when a person has none or a category has no code. Both send paid time only: a category the setup says is not paid (a meal deducted, say) has no record. A category’s code is the one in codes, else the one the setup gives it.

The package’s one policy layer is the policy whose hours are exported: past 8 hours a day is overtime (standard/threshold).

The Lua the recipe keeps is fixed_width: the one payroll system’s own file layout is the business decision there, and no standard exporter writes it.

Parameters

ParameterMeaning
id_attributeThe employment attribute holding the payroll id.
codesThe earning code of each category: { work = "REG", overtime = "OT" }. A category not named here is sent under the code the setup gives it.
include_unpaid(standard/csv) true: write the categories that are not paid too.
by(standard/csv) period: one line per category for the period, not per day.

What the scenarios show

  • one-line-per-earning-code: standard/csv with codes, for a tenant whose layer pays holidays and weekly overtime in place of the daily: a week with a holiday and overtime, three lines under REG, OT and HOL.
  • a-fixed-width-file: two days with daily overtime as fixed-width records (15 minutes is 0000025), and the trailer.
  • a-category-without-a-code-stops-the-export: overtime with no code stops the export, saying so.
  • the-standard-csv-export: standard/csv without codes or by: a line per person, workday and category.

Scenarios check an exporter with [expect.export]: the exporter, its parameters, a day of the period and the whole file expected.

This is a common way companies hand hours to payroll; check the rules where you operate and your payroll provider’s file specification.

Get the recipe

The recipe’s package and its scenarios are in the example packages repository: attendance/payroll_export. Test it, or your own copy of it, with hourctl attend test, and publish it with hourctl attend pkg publish.

The policy package

The package’s layers, with the standard steps each one runs and their settings:

payroll_export/package.toml
name = "payroll_export"
version = "1.0.0"
description = "A pay period's hours written for a payroll system, categories mapped to its earning codes, as a summary CSV or a fixed-width file."
# The policy whose hours are exported: past 8 hours a day is overtime.
# The exports themselves are named when a file is asked for: standard/csv
# with codes, or this package's fixed_width.
[[layer]]
id = "daily"
[[layer.step]]
id = "daily"
uses = "standard/threshold"
params = { over = "8h", to = "overtime", window = "day" }

Steps of its own, for what no standard step does (write a policy step):

payroll_export/main.lua
-- A payroll system's own file layout: a pay period's hours as fixed-width
-- records, with the policy's categories mapped to the payroll's earning
-- codes. An exporter is given one person's ledger at a time (`person`),
-- and writes text; `footer` comes after everyone.
-- An export names the exporter, `payroll_export/fixed_width`, and its
-- parameters: { id_attribute = "payroll_id", codes = { work = "REG", overtime = "OT" } }.
local M = {}
-- The person's id in the payroll system: the employment attribute
-- `id_attribute` names. A person without one stops the export: an id is
-- never made up.
local function payroll_id(x, p)
local id = x.attributes[p.id_attribute]
if id == nil then
error("person " .. x.person .. " has no " .. p.id_attribute)
end
return tostring(id)
end
-- The earning code of a category: the one `codes` names, else the one the
-- setup gives the category. A category with neither stops the export
-- rather than sending hours under a guessed code.
local function code_of(category, x, p)
local code = p.codes[category]
if code == nil and x.category_use[category] ~= nil then
code = x.category_use[category].code
end
if code == nil then
error("category " .. category .. " has no earning code")
end
return code
end
-- Whether a category's time is paid: unpaid time (a meal deducted) is not
-- sent to payroll.
local function paid(category, x)
local used = x.category_use[category]
return used == nil or used.paid
end
-- The category names of a table, sorted, so the file is the same each time.
local function sorted_keys(t)
local keys = {}
for k in pairs(t) do
table.insert(keys, k)
end
table.sort(keys)
return keys
end
-- Left-justified in `width` characters, or right-justified with `fill`
-- ("0" for numbers). Text too long for its field stops the export.
local function field(text, width, fill)
text = tostring(text)
if #text > width then
error("'" .. text .. "' does not fit in " .. width .. " characters")
end
if fill == nil then
return text .. string.rep(" ", width - #text)
end
return string.rep(fill, width - #text) .. text
end
-- Fixed width, for older payroll systems: one record per person, day and
-- category, each field at its column, hours with implied decimals
-- ("8.25" is 0000825), and a trailer record with how many people.
-- columns 1-10 employee, 11-14 code, 15-22 date YYYYMMDD, 23-29 hours
M.fixed_width = {
stage = "export",
content_type = "text/plain",
extension = "txt",
person = function(x, p)
local id = payroll_id(x, p)
local out = {}
for _, d in ipairs(x.days) do
local date = string.gsub(d.date, "-", "")
for _, category in ipairs(sorted_keys(d.categories)) do
if paid(category, x) then
local hours = string.gsub(d.categories[category], "%.", "")
table.insert(out, field(id, 10) .. field(code_of(category, x, p), 4) .. date .. field(hours, 7, "0") .. "\n")
end
end
end
return table.concat(out)
end,
footer = function(summary, p)
return "TRAILER" .. field(summary.people, 6, "0") .. "\n"
end,
}
return M

Scenarios

Each scenario gives a person’s punches and setup and the timecard they must produce, worked out with this package adopted (the format).

Overtime has no earning code in the parameters: the export stops, naming the category, rather than send hours under a guessed code
scenarios/a-category-without-a-code-stops-the-export.toml
description = "Overtime has no earning code in the parameters: the export stops, naming the category, rather than send hours under a guessed code"
punch = [
{ at = "2026-10-05 08:00", kind = "in" }, { at = "2026-10-05 18:00", kind = "out" },
]
[policy]
zone = "America/Chicago"
[[employment]]
effective = "2024-01-08"
kind = "hired"
attributes = { payroll_id = "E1042" }
[expect]
error = "category overtime has no code"
[expect.export]
uses = "standard/csv"
params = { id_attribute = "payroll_id", codes = { work = "REG" }, by = "period" }
day = "2026-10-05"
file = ""
The same kind of week as a fixed-width file: a record per day and category, each field at its column, hours with two implied decimals, and a trailer counting the people
scenarios/a-fixed-width-file.toml
description = "The same kind of week as a fixed-width file: a record per day and category, each field at its column, hours with two implied decimals, and a trailer counting the people"
punch = [
{ at = "2026-10-05 08:00", kind = "in" }, { at = "2026-10-05 16:15", kind = "out" },
{ at = "2026-10-06 08:00", kind = "in" }, { at = "2026-10-06 18:00", kind = "out" },
]
[policy]
zone = "America/Chicago"
[[employment]]
effective = "2024-01-08"
kind = "hired"
attributes = { payroll_id = "E1042" }
[expect.export]
uses = "payroll_export/fixed_width"
params = { id_attribute = "payroll_id", codes = { work = "REG", overtime = "OT" } }
day = "2026-10-05"
file = """
E1042 OT 202610050000025
E1042 REG 202610050000800
E1042 OT 202610060000200
E1042 REG 202610060000800
TRAILER000001
"""
A week of 42.5 hours and a holiday: one CSV line per category for the period, under the payroll's earning codes, for the payroll id held in the person's employment
scenarios/one-line-per-earning-code.toml
description = "A week of 42.5 hours and a holiday: one CSV line per category for the period, under the payroll's earning codes, for the payroll id held in the person's employment"
punch = [
{ at = "2026-11-23 08:00", kind = "in" }, { at = "2026-11-23 18:00", kind = "out" },
{ at = "2026-11-24 08:00", kind = "in" }, { at = "2026-11-24 18:00", kind = "out" },
{ at = "2026-11-25 08:00", kind = "in" }, { at = "2026-11-25 18:00", kind = "out" },
{ at = "2026-11-27 08:00", kind = "in" }, { at = "2026-11-27 20:30", kind = "out" },
]
[policy]
zone = "America/Chicago"
# This tenant pays holidays and weekly overtime in place of the daily.
[[layer]]
id = "holiday-and-weekly"
order = 10
[[layer.step]]
id = "daily"
change = "disable"
[[layer.step]]
id = "holiday"
uses = "standard/holiday_pay"
params = { time = "8h", category = "holiday" }
[[layer.step]]
id = "weekly"
uses = "standard/threshold"
params = { over = "40h", to = "overtime", window = "week" }
[[category]]
id = "holiday"
worked = false
[[holiday]]
id = "thanks"
name = "Thanksgiving"
month = 11
weekday = "thursday"
n = 4
[[employment]]
effective = "2024-01-08"
kind = "hired"
attributes = { payroll_id = "E1042" }
[expect.export]
uses = "standard/csv"
params = { id_attribute = "payroll_id", codes = { work = "REG", overtime = "OT", holiday = "HOL" }, by = "period" }
day = "2026-11-23"
file = """
person,first,last,workday,category,hours
E1042,2026-11-23,2026-11-29,2026-11-29,HOL,8.00
E1042,2026-11-23,2026-11-29,2026-11-29,OT,2.50
E1042,2026-11-23,2026-11-29,2026-11-29,REG,40.00
"""
Without an exporter of its own, a policy can use the standard one: a CSV line per person, workday and category, the person named by the payroll id their employment holds
scenarios/the-standard-csv-export.toml
description = "Without an exporter of its own, a policy can use the standard one: a CSV line per person, workday and category, the person named by the payroll id their employment holds"
punch = [
{ at = "2026-10-05 08:00", kind = "in" }, { at = "2026-10-05 18:00", kind = "out" },
{ at = "2026-10-06 08:00", kind = "in" }, { at = "2026-10-06 16:30", kind = "out" },
]
[policy]
zone = "America/Chicago"
[[employment]]
effective = "2024-01-08"
kind = "hired"
attributes = { payroll_id = "E1042" }
[expect.export]
uses = "standard/csv"
params = { id_attribute = "payroll_id" }
day = "2026-10-05"
file = """
person,first,last,workday,category,hours
E1042,2026-10-05,2026-10-11,2026-10-05,overtime,2.00
E1042,2026-10-05,2026-10-11,2026-10-05,work,8.00
E1042,2026-10-05,2026-10-11,2026-10-06,overtime,0.50
E1042,2026-10-05,2026-10-11,2026-10-06,work,8.00
"""
A day with a lunch punched out and in, a category that is not paid: the fixed-width file has no record for the meal, and the earning code of work, which the codes param does not name, is the one the setup gives it
scenarios/unpaid-time-is-not-sent-and-a-setup-code-stands-in.toml
description = "A day with a lunch punched out and in, a category that is not paid: the fixed-width file has no record for the meal, and the earning code of work, which the codes param does not name, is the one the setup gives it"
punch = [
{ at = "2026-10-05 08:00", kind = "in" }, { at = "2026-10-05 12:00", kind = "lunch_start" },
{ at = "2026-10-05 12:30", kind = "lunch_end" }, { at = "2026-10-05 16:30", kind = "out" },
]
[policy]
zone = "America/Chicago"
categories = { lunch = "meal" }
[[category]]
id = "work"
code = "REG"
[[category]]
id = "meal"
worked = false
paid = false
[[employment]]
effective = "2024-01-08"
kind = "hired"
attributes = { payroll_id = "E1042" }
[expect.export]
uses = "payroll_export/fixed_width"
params = { id_attribute = "payroll_id", codes = { overtime = "OT" } }
day = "2026-10-05"
file = """
E1042 REG 202610050000800
TRAILER000001
"""