forked from Code4SA/taxclock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincome-calculator.js
176 lines (142 loc) · 5.49 KB
/
income-calculator.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
var IncomeCalculator = function() {
var self = this;
this.VAT = 0.14;
function TaxBand(marginalRate, baseAmount, threshold, limit) {
this.marginalRate = marginalRate;
this.baseAmount = baseAmount;
this.threshold = threshold;
this.limit = (arguments.length > 3) ? this.limit = limit : this.limit = Number.POSITIVE_INFINITY;
}
// tax bands -- with thanks to http://www.oldmutual.co.za/markets/south-african-budget/income-tax-calculator
this.TAX_TABLE = [
new TaxBand(0.18, 0, 0, 188000),
new TaxBand(0.26, 33840, 188001, 293600),
new TaxBand(0.31, 61296, 293601, 406400),
new TaxBand(0.36, 96264, 406401, 550100),
new TaxBand(0.39, 147996, 550101, 701300),
new TaxBand(0.41, 206964, 701301)
];
this.PRIMARY_REBATE = 13500;
// Budget revenue streams from individuals (billions)
this.PERSONAL_INCOME_TAX_REVENUE = 350;
this.VAT_REVENUE = 260.6;
// Budget expenditure by category, in millions
// see https://docs.google.com/spreadsheets/d/18pS6-GXmV2AE6TqKtYYzL6Ag-ZuwiE4jb53U9heWF1M/edit#gid=0
// Categorised expenditure (should, but doesn't have to, total to CONSOLIDATED_EXPENDITURE)
this.EXPENDITURE = {
'Basic education': 228803,
'Higher education & training': 68715,
'Health': 168393,
'Social grants': 167479,
'Employment & labour affairs': 73127,
'Trade & industry': 31844,
'Economic infrastructure': 87105,
'Defence & state security': 52344,
'Law courts & prisons': 41667,
'Police services': 87508,
'Local government and housing': 182631,
'Agriculture, rural development & land reform': 26417,
'Science & Technology and environment': 19886,
'General public services': 73652,
'National debt': 147720,
'Unallocated reserves': 6000,
};
// override ordering
this.ORDERING = {
'Working for yourself': 9999,
'National debt': -1,
};
// Total budget expenditure
this.CONSOLIDATED_EXPENDITURE = _.reduce(_.values(this.EXPENDITURE), function(t, n) { return t + n; }, 0);
// fraction of budget line items that are funded through
// personal tax and VAT
this.TAXPAYER_RATIO = (this.PERSONAL_INCOME_TAX_REVENUE + this.VAT_REVENUE) / this.CONSOLIDATED_EXPENDITURE;
// start of day as a moment.js object. The date is irrelevant.
this.START_OF_DAY = moment().hour(9).minute(0).second(0);
this.WORKDAY_HOURS = 8;
this.WORKDAY_MINS = this.WORKDAY_HOURS * 60;
this.END_OF_DAY = this.START_OF_DAY.clone().add(this.WORKDAY_MINS, 'minutes');
this.calculateIncomeBreakdown = function(income) {
var info = {};
info.income = income;
// income tax
info.incomeTax = self.incomeTax(info);
// after tax income
info.netIncome = income - info.incomeTax;
// VAT calculated on net income
info.vatTax = self.vatTax(info);
// total personal tax
info.personalTax = info.incomeTax + info.vatTax;
// income after tax and VAT
info.disposableIncome = income - info.personalTax;
// fraction of day spent working for yourself
info.personal_fraction = info.disposableIncome / info.income;
// times spent working for yourself
info.personal_minutes = info.personal_fraction * self.WORKDAY_MINS;
// times spent working for the man
info.taxman_minutes = self.WORKDAY_MINS - info.personal_minutes;
// fraction of day spent working for the man
info.taxman_fraction = 1 - info.personal_fraction;
info.breakdown = this.doBreakdown(info);
// time spent working for myself
info.breakdown.push(this.workingForSelf(info));
// sort
info.breakdown = _.sortBy(info.breakdown, function(b) {
return self.ORDERING[b.name] || -b.fraction;
});
// add times of day
this.addTimesOfDay(info.breakdown);
return info;
};
this.incomeTax = function(info) {
var gross_income_tax = 0;
var band = _.find(this.TAX_TABLE, function(b) {
return (info.income >= b.threshold) && (info.income <= b.limit);
});
if (band) {
gross_income_tax = band.baseAmount + (band.marginalRate * (info.income - band.threshold));
gross_income_tax = gross_income_tax - this.PRIMARY_REBATE;
}
if (gross_income_tax < 0) gross_income_tax = 0;
return gross_income_tax;
};
this.vatTax = function(info) {
return info.netIncome * this.VAT / (1 + this.VAT);
};
this.workingForSelf = function(info) {
return {
name: 'Working for yourself',
amount: info.income,
taxpayer_amount: info.disposableIncome,
fraction: info.personal_fraction,
minutes: info.personal_minutes,
};
};
this.doBreakdown = function(info) {
return _.map(this.EXPENDITURE, function(amount, category) {
// scale amount to that contributed by personal taxpayers
var taxpayer_amount = self.TAXPAYER_RATIO * amount;
var fraction = amount / self.CONSOLIDATED_EXPENDITURE * info.taxman_fraction;
return {
name: category,
// absolute amount from budget
amount: amount,
// amount contributed by the taxpayer
taxpayer_amount: taxpayer_amount,
// fraction of time spent on this amount
fraction: fraction,
// minutes per day spent on this amount
minutes: self.WORKDAY_MINS * fraction,
};
});
};
this.addTimesOfDay = function(cats) {
var time = this.START_OF_DAY;
_.each(cats, function(cat) {
// time of day when you FINISH working for this category
time = time.clone().add(cat.minutes, 'm');
cat.finish_time = time.clone();
cat.finish_time_s = time.format('h:mm a');
});
};
};