Skip to content

Instantly share code, notes, and snippets.

@jmnsf
Last active April 1, 2016 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmnsf/465caec52b79aa142f2888e000f3f118 to your computer and use it in GitHub Desktop.
Save jmnsf/465caec52b79aa142f2888e000f3f118 to your computer and use it in GitHub Desktop.
Calculate Portuguese TAX in Google Sheets
Echelon Tax Overtax
€7,000.00 0.145 0
€20,000.00 0.285 0.01
€40,000.00 0.37 0.0175
€80,000.00 0.45 0.03
€99,999,999,999.00 0.48 0.035
/*
* @param double gross_yearly Gross yearly income.
* @param double net_yearly Net yearly income.
* @param matrix overtax_echelons Table with overtax echelons. Use table in
* PT_IRS but add overtax column.
*
* @return The IRS overtax to the given income.
*/
function PT_IRS_OVERTAX(gross_yearly, net_yearly, overtax_echelons) {
if (!(overtax_echelons && overtax_echelons.length && overtax_echelons.length > 0)) {
return 0;
}
for (var i = 0; i < overtax_echelons[i].length && overtax_echelons[i][0] && gross_yearly > overtax_echelons[i][0]; i++);
var overtax = (net_yearly - 530) * overtax_echelons[i][2];
return overtax;
}
/**
* @param double gross Gross yearly income.
* @param matrix tax_echelons The tax echelons to use for tax calculation.
* Expects 3 columns:
* 1. Max echelon amount (eg: €20000 for second ech);
* 2. Tax for echelon (eg: 0.285);
* Full table for 2016:
* €7,000.00 0.145
* €20,000.00 0.285
* €40,000.00 0.37
* €80,000.00 0.45
* €99,999,999,999.00 0.48
* @return IRS tax for given year and tax echelons.
*/
function PT_IRS(gross_yearly, tax_echelons) {
if (!(tax_echelons && tax_echelons.length && tax_echelons.length > 0)) {
return 0;
}
var accounted = 0;
var tax_total = 0;
var tax, affected, i = 0;
while (accounted < gross_yearly) {
if (!(tax_echelons[i] && tax_echelons[i][0] && tax_echelons[i][1])) {
break;
}
affected = Math.min(gross_yearly, tax_echelons[i][0]) - accounted;
tax = affected * tax_echelons[i][1];
accounted += affected;
tax_total += tax;
i += 1;
}
return tax_total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment