Skip to content

Instantly share code, notes, and snippets.

@AmrAbdulrahman
Created January 19, 2024 15:04
Show Gist options
  • Save AmrAbdulrahman/0949efb94332b2ff983d9e194a47c0eb to your computer and use it in GitHub Desktop.
Save AmrAbdulrahman/0949efb94332b2ff983d9e194a47c0eb to your computer and use it in GitHub Desktop.
UK tax calculator
/**
* Calculates the employee national insurance contribution based on annual net salary.
* @param {number} annual net salary.
* @return The employee national insurance contribution based on annual net salary
* @customfunction
*/
function EMPLOYEE_NATIONAL_INSURANCE(income) {
/**
* If you're an employee you start paying National Insurance when you earn more than £242 a week (2023/24). The National Insurance rate you pay depends on how much you earn, and is made up of: 12% of your weekly earnings between £242 and £967 (2023/24) 2% of your weekly earnings above £967.
*/
let weeklyIncome = income / 52;
let remaining = weeklyIncome;
let bands = [{
amount: 242,
percentage: 0,
}, {
amount: 725,
percentage: 0.12,
}, {
amount: -1,
percentage: 0.02,
}];
let ni = 0;
for (let i = 0; i < bands.length; i++) {
let { amount, percentage } = bands[i];
if (amount === -1) {
ni += remaining * percentage;
break;
}
let actual = Math.min(remaining, amount);
ni += actual * percentage;
remaining -= actual;
}
return ni * 52;
}
/**
* Calculates the self employed national insurance contribution based on annual net salary. https://www.gov.uk/self-employed-national-insurance-rates
* @param {number} annual profits
* @return The self employeed national insurance contribution
* @customfunction
*/
function SELF_EMPLOYEED_NATIONAL_INSURANCE(annualProfits) {
/**
How much you pay
Class Rate for tax year 2023 to 2024
Class 2 | £3.45 a week
Class 4 | 9% on profits between £12,570 and £50,270. 2% on profits over £50,270
*/
let class2_weekly_NI = 3.45;
let bands = [{
amount: 12570,
percentage: 0,
}, {
amount: (50270 - 12570),
percentage: 0.09,
}, {
amount: -1,
percentage: 0.02,
}];
let remaining = annualProfits;
let class4_NI = 0;
for (let i = 0; i < bands.length; i++) {
let { amount, percentage } = bands[i];
if (amount === -1) {
class4_NI += remaining * percentage;
break;
}
let actual = Math.min(remaining, amount);
class4_NI += actual * percentage;
remaining -= actual;
}
return (class2_weekly_NI * 52) + class4_NI;
}
/**
* Calculates the employee taxes.
* @param {number} annual gross salary.
* @return The employee taxes
* @customfunction
*/
function EMPLOYEE_INCOME_TAX(annualGrossSalary) {
/**
Band Taxable income Tax rate
Personal Allowance Up to £12,570 0% (amount = 12570)
Basic rate £12,571 to £50,270 20%. (amount = 37699)
Higher rate £50,271 to £125,140 40%. (amount = 74869)
Additional rate over £125,140 45%
*/
let remaining = annualGrossSalary;
let bands = [{
// Your adjusted net income is over the personal allowance threshold of £100,000. Your allowance has been reduced to £7,570 (from the default £12,570).
amount: annualGrossSalary >= 100000 ? 7570 : 12570,
percentage: 0,
}, {
amount: 50270 - 12570,
percentage: 0.2,
}, {
amount: 125140 - 50270,
percentage: 0.4,
}, {
amount: -1,
percentage: 0.45,
}];
let taxes = 0;
for (let i = 0; i < bands.length; i++) {
let { amount, percentage } = bands[i];
if (amount === -1) {
taxes += remaining * percentage;
break;
}
let actual = Math.min(remaining, amount);
taxes += actual * percentage;
remaining -= actual;
}
return taxes;
}
/**
* Calculates the employee annual take home.
* @param {number} annual gross salary.
* @return The employee annual take home
* @customfunction
*/
function EMPLOYEE_TAKE_HOME(annualGrossSalary) {
return annualGrossSalary - EMPLOYEE_NATIONAL_INSURANCE(annualGrossSalary) - EMPLOYEE_INCOME_TAX(annualGrossSalary);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment