# Row type scripts

A row type script is a formula for one row type. Each time Salaxy recalculates a calculation, it runs the formula for each row of that type and puts the result in the row. This is the most common way to add a customer's own pay rules. For the big picture, see [Custom payroll logic](https://code.salaxy.com/integration/payroll-logic.md).

**Beta:**
In production, scripts run only for customers that Salaxy has turned the feature on for. See [Custom payroll logic](https://code.salaxy.com/integration/payroll-logic.md).

## How Salaxy finds the formula

1. Salaxy goes through the rows of the calculation.
2. For each row, it takes the row type name:
   - a **built-in** row type is the `rowType` of the row, for example `hourlySalary` (the list is on [Wage types](https://code.salaxy.com/reference/wage-types.md));
   - a **custom** row type is the last part of the row's `type`: for `/custom/eveningBonus` it is `eveningBonus`.
3. It looks for a class with the same name in the scripts, ignoring case: `HourlySalary` for `hourlySalary`. The class must be listed in the metadata with the interface `rowTypeScripts`.
4. It calls each method that the metadata lists, and writes the result to the row property of the same name. For example, `price()` sets the row's `price`.

A method that returns `undefined` keeps the value that the row already has.

## The methods

All methods are optional. Each gets `(row, ctx, sxy)`: the row, the [context](https://code.salaxy.com/integration/payroll-logic.md#what-a-script-gets) and the helpers.

| Method | Sets |
|---|---|
| `price` | The unit price. |
| `count` | The count, for example hours. |
| `message` | The row text on the salary slip. |
| `unit` | The unit, for example hours or days. |
| `data` | The row type's own data object. |
| `kind` | `data.kind`, the sub-type of the row type. |
| `period`, `periodStart`, `periodEnd`, `periodDaysCount`, `periodDays` | The row's own period, when it differs from the calculation's period. |
| `accounting`, `dimensions` | Accounting data and cost accounting dimensions, such as the cost centre. |
| `vatPercent`, `vatEntries` | VAT for expense rows. |
| `sourceId` | Your own identifier for the row; Salaxy passes it on. |
| `isDeleted` | `true` removes the row from the result. |

## Examples

A one-line formula: the price of an overtime row is half the hourly salary of the employment.

```js
class Overtime {
  price(row, ctx, sxy) {
    return sxy.prices.employment("hourlySalary") * 0.5;
  }
}
```

A longer rule: the overtime price uses the average hourly pay of the last quarter, or the hourly salary if it is higher, and writes it into the row's data so that the user interface can show it.

```js
class Overtime {
  data(row, ctx, sxy) {
    const price = sxy.prices.averageHourlyPay(0.5, "lastQuartal");
    if (row.data?.compensationRows?.length) {
      row.data.compensationRows[0].price = price;
    }
    return row.data;
  }
}
```

The metadata for both classes is `{ "interface": "rowTypeScripts", "class": "Overtime", "methods": ["price"] }` (or `["data"]`). See [Where the scripts are stored](https://code.salaxy.com/integration/payroll-logic.md#where-the-scripts-are-stored).

## Validation

A script can add errors or warnings to `ctx.validation`. Use this when the business rule finds something wrong, for example a missing price in the employment. The user sees the message with the calculation.
