-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
177 lines (148 loc) · 4.9 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
class AffordabilityWindow {
constructor() {
this.personalAllowance = 12570;
this.highestBasicRate = 50270;
this.basicRate = this.highestBasicRate - this.personalAllowance;
this.higherBracket = 150000;
}
calculateTax(income, taxableIncome) {
let tax = 0;
if (taxableIncome <= 0) {
return 0;
} else if (income <= this.highestBasicRate) {
tax = taxableIncome * 0.2;
} else if (income <= this.higherBracket) {
let remainder = taxableIncome - this.basicRate;
tax = this.basicRate * 0.2 + remainder * 0.4;
} else {
let higherRemainder = taxableIncome - this.higherBracket;
let twentyPercent = taxableIncome - this.basicRate;
let fortyPercent = twentyPercent - higherRemainder;
tax = this.basicRate * 0.2 + fortyPercent * 0.4 + higherRemainder * 0.45;
}
return tax;
}
calculateNationalInsurance(income, taxableIncome) {
let nationalInsurance = 0;
let firstNIDeduction;
let secondNIDeduction;
if (income >= this.personalAllowance && income <= this.highestBasicRate) {
nationalInsurance = (taxableIncome / 12) * 0.12 * 12;
} else if (income > this.highestBasicRate) {
let salaryAfterMidBracket = income - this.highestBasicRate;
firstNIDeduction =
((this.highestBasicRate - this.personalAllowance) / 12) * 0.12 * 12;
secondNIDeduction = (salaryAfterMidBracket / 12) * 0.02 * 12;
nationalInsurance = firstNIDeduction + secondNIDeduction;
}
return nationalInsurance;
}
monthlySalaryAfterTaxandNI(annualWage, overStatePensionAge) {
const income = annualWage;
const taxableIncome = income - this.personalAllowance;
const tax = this.calculateTax(income, taxableIncome);
let nationalInsurance = 0;
if (!overStatePensionAge) {
nationalInsurance = this.calculateNationalInsurance(
income,
taxableIncome
);
}
const totalDeductions = tax + nationalInsurance;
const netIncome = income - totalDeductions;
return netIncome / 12;
}
}
const affordabilityWindow = new AffordabilityWindow();
// applicant 1
const annual_salary_0 = document.getElementById("annual_salary_0");
const monthly_salary_after_tax_0 = document.getElementById(
"monthly_salary_after_tax_0"
);
const deposit_value_0 = document.getElementById("deposit_value_0");
const mortgage_percentage = document.getElementById("mortgage_percentage");
const repayment_term = document.getElementById("repayment_term");
// applicant 2
const annual_salary_1 = document.getElementById("annual_salary_1");
const monthly_salary_after_tax_1 = document.getElementById(
"monthly_salary_after_tax_1"
);
const deposit_value_1 = document.getElementById("deposit_value_1");
const annual_salaries = [annual_salary_0, annual_salary_1];
const other_elements = [
monthly_salary_after_tax_0,
monthly_salary_after_tax_1,
deposit_value_0,
deposit_value_1,
mortgage_percentage,
repayment_term,
];
annual_salaries.forEach((as, i) => {
as.addEventListener("keyup", (e) => {
let monthlySalary = affordabilityWindow.monthlySalaryAfterTaxandNI(
e.target.value,
false
);
e.target.parentElement.nextElementSibling.nextElementSibling.firstElementChild.value =
monthlySalary.toFixed();
setChromeLocalStorage(as.name, e.target.value, true);
setChromeLocalStorage(
"monthly_salary_after_tax_" + i,
monthlySalary.toFixed(),
true
);
});
});
other_elements.forEach((element, i) => {
element.addEventListener("keyup", (e) => {
setChromeLocalStorage(element.name, e.target.value, true);
});
if (element === mortgage_percentage) {
element.addEventListener("input", (e) => {
e.target.value > 15 ? (e.target.value = 15) : e.target.value;
});
}
if (element === repayment_term) {
element.addEventListener("input", (e) => {
e.target.value > 35 ? (e.target.value = 35) : e.target.value;
});
}
});
function setChromeLocalStorage(key, input, directInputValue = false) {
if (directInputValue) {
chrome.storage.local.set({ [key]: input }).then(() => {
console.log(key + " is set to " + input);
});
} else {
chrome.storage.local.set({ [key]: input.value }).then(() => {
console.log(key + " is set to " + input.value);
});
}
}
// setting to chrome storage
const keys = [
annual_salary_0,
annual_salary_1,
monthly_salary_after_tax_0,
monthly_salary_after_tax_1,
deposit_value_0,
deposit_value_1,
mortgage_percentage,
repayment_term,
];
getKeys(keys);
async function getKeys(keys) {
for (const key of keys) {
const result = await new Promise((resolve) => {
chrome.storage.local.get(key.name, (items) => {
resolve(items);
});
});
result[key.name] !== undefined ? (key.value = result[key.name]) : "";
}
}
const updateBtn = document.getElementById("update");
updateBtn.addEventListener("click", () => {
window.location.reload();
chrome.tabs.reload();
});