# Before-calculation scripts

A before-calculation script runs first in each recalculation, before the [row type scripts](https://code.salaxy.com/integration/row-type-scripts.md) and before Salaxy's own calculation. It gets the whole list of rows and returns a new list. 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).

Use it when you must change the list itself:

- **Add a default row** when it is missing, for example a fixed monthly allowance.
- **Remove rows** that a rule does not allow.
- **Split or combine rows.**

To set a value of each row of one type, use a [row type script](https://code.salaxy.com/integration/row-type-scripts.md) instead. It is simpler.

## The method

Salaxy supports one method today: `getRows(current, ctx, sxy)`.

- **`current`** is the list of rows of the calculation.
- **`ctx`** is the [context](https://code.salaxy.com/integration/payroll-logic.md#what-a-script-gets): the calculation, the employment and the company settings.
- **Return** the new list of rows, or `undefined` to keep the rows as they are.

At this step, Salaxy has not yet copied the employment data into the calculation, so the employment snapshot in `ctx.calculation` can be old. Read the employment from `ctx.employment` instead.

## Example

Add a monthly salary row with the employment's monthly salary if the calculation has none.

```js
class MyRules {
  getRows(current, ctx, sxy) {
    if (current.some((r) => r.rowType === "monthlySalary")) {
      return undefined;
    }
    const price = sxy.prices.employment("monthlySalary");
    return [...current, { rowType: "monthlySalary", count: 1, price }];
  }
}
```

The metadata for this class is `{ "interface": "beforeCalcScripts", "class": "MyRules", "methods": ["getRows"] }`. See [Where the scripts are stored](https://code.salaxy.com/integration/payroll-logic.md#where-the-scripts-are-stored).

A row that this script adds also gets its row type script, because the row type scripts run after it.

## What a script cannot change

- **Employer and worker data.** Salaxy writes these snapshots again before it calculates.
- **The result.** Salaxy calculates the totals, the worker and employer sides and the Incomes Register rows; a script cannot write them. A script can add messages to `ctx.validation`.
