fiscal-es
v1.0.0
Published
Spanish personal income tax (IRPF) on the savings base: exact integer-cent engine with executable YAML test cases
Maintainers
Readme
fiscal-es
Spanish personal income tax (IRPF) on the savings base (Spain, common territory): integrates capital gains, capital losses and investment income for one tax year and applies the state and regional scales separately.
TypeScript, ESM, no runtime dependencies, pure functions. Amounts in integer cents (Cents) and rates in integer
basis points (Bp); never floating point.
Installation
npm install fiscal-esESM only (import); there is no CommonJS build.
Usage
Computing the savings tax
import { computeSavingsTax, formatEuros, parseEuros } from "fiscal-es";
const r = computeSavingsTax({
taxYear: 2026,
capitalGains: parseEuros("10000.00"),
capitalLosses: parseEuros("3000.00"),
investmentIncome: parseEuros("0.00"),
});
// r = { savingsTaxableBase: 700000, stateTax: 67500, regionalTax: 67500, totalTax: 135000 }
formatEuros(r.totalTax); // "1350.00"parseEuros only accepts the format "-1234.56" (decimal point, two decimals); anything else throws
SyntaxError. Input that makes no sense (non-integer amounts, negative gains or losses) throws InvalidInput;
a tax year without a table throws UnknownTaxYear.
Available tax years
import { availableTaxYears, getTaxTable } from "fiscal-es";
for (const taxYear of availableTaxYears()) {
console.log(taxYear, getTaxTable(taxYear).source.verifiedOn); // 2026 "2026-10-25"
}Range and errors
The computation is exact for amounts up to ≈ 9 000 million euros per operation: with rates up to 10 000 bp, the
intermediate product cents × basis points fits in Number.MAX_SAFE_INTEGER. Above that, any operation whose result
or intermediate product leaves the safe integer range throws RangeError.
SyntaxError:parseEurosreceives a format other than"-1234.56"(see Usage).InvalidInput:computeSavingsTaxreceives an amount that is not an integer number of cents (NaN, a fraction of a cent), negative gains or losses, or a non-integer tax year.UnknownTaxYear:computeSavingsTaxreceives a tax year without a table.RangeError:parseEuros,add,subtract,timesBporroundCentsreceive or produce a value outside the safe integer range.
Supported tax years
| Tax year | Scale verified on | | -------- | ----------------- | | 2026 | 2026-10-25 |
cases/: the executable spec
Each YAML file in cases/ is a case with its input, the expected output to the cent, the legal reference and the
date on which a person verified it. The package's test suite runs all of them against the engine; a change to the
engine that alters the output of a verified case is a major version change. The format is described in
cases/SCHEMA.md. The cases are written in Spanish, with Spanish keys (ganancias_patrimoniales, cuota_ahorro…),
because they are checked against the Spanish tax return manual; Terminology maps them to the API.
Terminology
| Identifier | Spanish term | LIRPF |
| ---------- | ------------ | ----- |
| computeSavingsTax | cuota del ahorro | arts. 49, 50.2, 66.1 y 76 |
| SavingsInput / SavingsResult | entrada / resultado del ahorro | |
| taxYear | ejercicio | |
| capitalGains (ganancias_patrimoniales) | ganancias patrimoniales | art. 33 |
| capitalLosses (perdidas_patrimoniales) | pérdidas patrimoniales | art. 33 |
| investmentIncome (rendimientos_capital_mobiliario) | rendimientos del capital mobiliario | art. 25 |
| savingsTaxableBase (base_liquidable_ahorro) | base liquidable del ahorro | arts. 49 y 50.2 |
| stateTax (cuota_estatal) | cuota estatal | art. 66.1 |
| regionalTax (cuota_autonomica) | cuota autonómica | art. 76 |
| totalTax (cuota_ahorro) | cuota del ahorro (estatal + autonómica) | |
| TaxTable / getTaxTable / availableTaxYears | tabla normativa del ejercicio / ejercicios disponibles | |
| TaxBracket (upTo, stateBp, regionalBp) | tramo de la escala del ahorro (hasta, tipo estatal, tipo autonómico) | arts. 66.1 y 76 |
| UnknownTaxYear | ejercicio sin tabla | |
| InvalidInput | entrada inválida | |
| Cents / Bp | céntimos / puntos básicos | |
| add / subtract / clampZero / ZERO | sumar / restar / acotar a cero / cero | |
| timesBp / roundCents | céntimos × puntos básicos / redondeo al céntimo | |
Known limitations
- No cross-offsetting: a negative balance of gains and losses does not offset positive investment income, nor the other way round (art. 49.1 LIRPF). The negative balance simply is not taxed.
- No carry-forward of negative balances to the following four tax years (art. 49.2 LIRPF).
- No foral regimes (Navarra, País Vasco).
- Savings base only: does not compute the general base, personal allowances or deductions.
Development in the monorepo
exports has a custom condition, fiscal-es-src, pointing to src/ so the monorepo app can use the engine without
building it; external consumers do not enable it and get dist/.
Disclaimer
A calculation tool. Not tax or financial advice. Check your situation against current legislation or with a professional.
License
MIT
