Skip to content

Instantly share code, notes, and snippets.

@sergioccrr
Created October 12, 2023 11:36
Show Gist options
  • Save sergioccrr/dd73e1544d7571b332ef26a1e789fa0c to your computer and use it in GitHub Desktop.
Save sergioccrr/dd73e1544d7571b332ef26a1e789fa0c to your computer and use it in GitHub Desktop.
Apps Script para calcular el salario neto en Google Sheets (España)
// Based on: https://gist.github.com/straach/30789d4c30fb5174ec52ea7521cc4e4a
// Note: taxpayers without children
// Usage example: =net_salary(18000)
function calculate_tax_by_slice(total, fromCurrency, toCurrency, percentage) {
if (total < fromCurrency) {
return 0;
}
if (total > toCurrency) {
let toSubtract = Math.abs(toCurrency - total);
total = total - toSubtract;
}
let taxableAmount = total - fromCurrency;
let percentageFactor = percentage / 100;
let taxAmount = taxableAmount * percentageFactor;
return taxAmount;
}
function calculate_income_tax(value) {
const taxSlices = [
{ start: 0, end: 12450, rate: 19 },
{ start: 12450, end: 20200, rate: 24 },
{ start: 20200, end: 35200, rate: 30 },
{ start: 35200, end: 60000, rate: 37 },
{ start: 60000, end: 300000, rate: 45 },
{ start: 300000, end: 100000000, rate: 47 },
];
let result = 0;
for (const slice of taxSlices) {
result += calculate_tax_by_slice(value, slice.start, slice.end, slice.rate);
}
return result;
}
function net_salary(annualIncome) {
// https://sede.agenciatributaria.gob.es/Sede/ciudadanos-familias-personas-discapacidad/minimo-personal-familiar/minimo-contribuyente.html
const MINIMO_DEL_CONTRIBUYENTE = 5550;
let socialSecurity = (6.35 * annualIncome) / 100;
let relevantIncome = annualIncome - socialSecurity - MINIMO_DEL_CONTRIBUYENTE;
let irpf = calculate_income_tax(relevantIncome);
return annualIncome - socialSecurity - irpf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment