-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcustom.js
200 lines (174 loc) · 6.6 KB
/
custom.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
$(document).ready(function() {
var NapsaRate = 0.05;
var NapsaCeiling = 894.61;
var ZraPensionRate = 0.15;
var ZraPensionMax = 255;
//Income Bands
var incomeBand0 = 0;
var incomeBand1 = 3300;
var incomeBand2 = 4100;
var incomeBand3 = 6200;
//Tax Bands
var taxBand0 = 0.000;
var taxBand1 = 0.250;
var taxBand2 = 0.300;
var taxBand3 = 0.375;
var resultsPaye;
var resultsNapsa;
var resultsNet;
// Storing of variables to use later on
var $resultsName = $('#results-name');
var $resultsUserIncome = $('#results-user-income');
var $resultsPaye = $('#results-paye');
var $resultsNapsa = $('#results-napsa');
var $resultsNet = $('#results-net');
// Initialisation of the modal
$('#results-modal').modal();
// $('#results-modal').modal('open');
// Button Must Trigger Modal PopUp
var $button = $('#modal-trigger');
// Trigger Modal on click
$button.on('click', function(){
if (verifyIncome()) {
Materialize.toast("<span><i class='material-icons' style='transform: translateY(5px)'>warning</i> Your income must be atleast K10</span>", 2750);
} else if (checkValidity() && !verifyIncome()) {
calculate();
$('#results-modal').modal('open');
} else {
Materialize.toast("<span><i class='material-icons' style='transform: translateY(5px)'>error</i> Please enter a valid amount</span>", 2750);
}
});
var $userIncome = $('#userIncome');
// add event Listener on Income to trigger validity of entry and underline on input
$userIncome.on('input blur keyup', function() {
if(checkValidity()){
// Remove Invalid class if it exists
if($userIncome.hasClass('invalid')) {
$userIncome.removeClass('invalid');
}
// Add Valid Class if it doesn't exist
if(!$userIncome.hasClass('valid')) {
$userIncome.addClass('valid');
}
} else {
// Remove Valid Class if it exists
if($userIncome.hasClass('valid')){
$userIncome.removeClass('valid');
}
// Add Invalid Class if it doesn't exist
if(!$userIncome.hasClass('invalid')) {
$userIncome.addClass('invalid');
}
}
});
////////////////////////////////////////
//Tax Calculation
function calculate() {
//Username and Income Values
var tempUsername = $('#userName').val();
var userName = ( tempUsername.trim().length === 0 ) ? 'Anonymous' : tempUsername;
var userIncome = $('#userIncome').val();
var NapsaCont = Math.min(NapsaCeiling, userIncome * NapsaRate);
var NonTaxPension = Math.min((ZraPensionRate * userIncome), ZraPensionMax);
var TaxableIncome = userIncome - NonTaxPension;
//Pay As You Earn Tax (PAYE) Deductible
var noTax = (TaxableIncome - incomeBand0) * taxBand0;
var lowTax = (TaxableIncome - incomeBand1) * taxBand1 + (incomeBand1 - incomeBand0) * taxBand0;
var midTax = (TaxableIncome - incomeBand2) * taxBand2 + (incomeBand2 - incomeBand1) * taxBand1 + (incomeBand1 - incomeBand0) * taxBand0;
var highTax = (TaxableIncome - incomeBand3) * taxBand3 + (incomeBand3 - incomeBand2) * taxBand2 + (incomeBand2 - incomeBand1) * taxBand1 + (incomeBand1 - incomeBand0) * taxBand0;
// Statements determining the values
if (TaxableIncome > incomeBand0 && TaxableIncome <= incomeBand1) {
var tempUserIncome = (+userIncome).toFixed(2);
var tempTax = noTax.toFixed(2);
var tempNapsa = NapsaCont.toFixed(2);
var tempNet = (+tempUserIncome - +tempNapsa - +tempTax).toFixed(2);
resultsPaye = tempTax;
resultsNapsa = tempNapsa;
resultsNet = tempNet;
} else if (TaxableIncome > incomeBand1 && TaxableIncome <= incomeBand2){
var tempUserIncome = (+userIncome).toFixed(2);
var tempNapsa = NapsaCont.toFixed(2);
var tempTax = lowTax.toFixed(2);
var tempNet = (+tempUserIncome - +tempNapsa - +tempTax).toFixed(2);
resultsPaye = tempTax;
resultsNapsa = tempNapsa;
resultsNet = tempNet;
} else if (TaxableIncome > incomeBand2 && TaxableIncome <= incomeBand3){
var tempUserIncome = (+userIncome).toFixed(2);
var tempNapsa = NapsaCont.toFixed(2);
var tempTax = midTax.toFixed(2);
var tempNet = (+tempUserIncome - +tempNapsa - +tempTax).toFixed(2);
resultsPaye = tempTax;
resultsNapsa = tempNapsa;
resultsNet = tempNet;
} else if (TaxableIncome > incomeBand3) {
var tempUserIncome = (+userIncome).toFixed(2);
var tempNapsa = NapsaCont.toFixed(2);
var tempTax = highTax.toFixed(2);
var tempNet = (+tempUserIncome - +tempNapsa - +tempTax).toFixed(2);
resultsPaye = tempTax;
resultsNapsa = tempNapsa;
resultsNet = tempNet;
} else {
userIncome = '0.00';
resultsPaye = '0.00';
resultsNapsa = '0.00';
resultsNet = '0.00';
}
// Set the content on the modal
$resultsName.text(userName);
$resultsUserIncome.text(checkDelimit((+userIncome).toFixed(2)));
$resultsPaye.text(checkDelimit(resultsPaye));
$resultsNapsa.text(checkDelimit(resultsNapsa));
$resultsNet.text(checkDelimit(resultsNet));
}
// ======================================================================//
// Functions to help with the presentation and other calculations
// ======================================================================//
// Function to check if value is less than 10
function verifyIncome(){
return $('#userIncome').val() < 10;
}
// Function to check validity of Money Input to match RegEx on Input
function checkValidity() {
var userIncome = $('#userIncome').val();
return /^[1-9]\d+\.?\d?\d?$/.test(userIncome.toString());
}
// Function to check if a value should be delemited
function checkDelimit (string) {
// final string to return
var finalString;
var index = string.indexOf('.');
// Split string to before and after decimal point
var afterPoint = string.slice(index);
var beforePoint = string.slice(0, index);
// If the length of the string before the decimal point is greater than a thousand (4), delemit.
if(beforePoint.length >= 4) {
var delemitedBeforePoint = delimit(beforePoint);
finalString = delemitedBeforePoint + afterPoint;
} else {
// if before point is less than 1000. Final String = original string;
finalString = string;
}
return finalString;
}
// Function to delemit a string
function delimit (string) {
var splitString = string.split('');
var finalString = []
// initialise the count outside loop. Variable to count elements pushed
var count = 0;
// loop through the string backwards and add a comma after every 3 digits
for ( var i = splitString.length - 1; i > -1; i-- ) {
// Add elements to the final String
finalString.push(splitString[i]);
// increase the count
count++;
// if the count is perfectly divisible by 3 add a comma;
if(count % 3 === 0 && i !== 0){
finalString.push(',');
}
}
return finalString.reverse().join('');
}
});