Skip to content

Instantly share code, notes, and snippets.

@mintsoft
Created August 4, 2023 13:02
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 mintsoft/4330dce3117e8cc3724919c29797cffd to your computer and use it in GitHub Desktop.
Save mintsoft/4330dce3117e8cc3724919c29797cffd to your computer and use it in GitHub Desktop.
Adds functions to calculate income tax and national insurance on raw income
/** @CustomFunction */
function incomeTax(pay: number): number {
var additionalTax = 0;
var higherTax = 0;
var basicTax = 0;
var personalAllowanceToRemove = Math.floor((pay - 100000) / 2);
var defaultPersonalAllowance = 12570;
var personalAllowance = defaultPersonalAllowance;
if (personalAllowanceToRemove > 0) {
personalAllowance -= personalAllowanceToRemove
personalAllowance = Math.max(personalAllowance, 0)
}
if (pay < personalAllowance) {
return 0;
}
var taxableAmount = pay - personalAllowance;
var additionalAmountThreshold = (125140)
var additionalAmount = taxableAmount - additionalAmountThreshold;
if (taxableAmount > additionalAmountThreshold) {
additionalTax = 0.45 * additionalAmount;
taxableAmount = additionalAmountThreshold;
}
var higherAmountThreshold = (50270 - defaultPersonalAllowance)
var higherAmount = taxableAmount - higherAmountThreshold;
if (taxableAmount > higherAmountThreshold) {
higherTax = 0.4 * higherAmount;
taxableAmount = higherAmountThreshold
}
var basicAmount = taxableAmount;
if (basicAmount > 0) {
basicTax = 0.2 * basicAmount;
}
console.log("persallowance:" + personalAllowance + "additional: " + additionalTax + " higher:" + higherTax + " basic: " + basicTax)
var totalTax = basicTax + higherTax + additionalTax
return totalTax
}
/** @CustomFunction */
function nationalInsuranceClass1(pay: number): number {
var twelvePercentThreshold = 1048 * 12;
var twoPercentThreshold = 4189 * 12;
var nic = 0;
var applicableAmount = pay;
if (applicableAmount > twoPercentThreshold) {
nic += (applicableAmount - twoPercentThreshold) * 0.02
applicableAmount = twoPercentThreshold;
}
if (applicableAmount > twelvePercentThreshold) {
nic += (applicableAmount - twelvePercentThreshold) * 0.12;
}
return nic;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment