Add a Form1040.filingStatus accessor.
[ustaxlib.git] / src / fed2019 / Form1040.ts
1 import { Form, TaxReturn } from '../core';
2 import { Line, AccumulatorLine, ComputedLine, ReferenceLine, sumLineOfForms } from '../core/Line';
3 import { UnsupportedFeatureError } from '../core/Errors';
4 import { clampToZero, reduceBySum } from '../core/Math';
5
6 import Form8606 from './Form8606';
7 import Form8959 from './Form8959';
8 import Form1099INT from './Form1099INT';
9 import Form1099DIV from './Form1099DIV';
10 import Form1099R, { Box7Code } from './Form1099R';
11 import W2 from './W2';
12 import Schedule1 from './Schedule1';
13 import Schedule2 from './Schedule2';
14 import Schedule3 from './Schedule3';
15 import ScheduleD, { ScheduleDTaxWorksheet } from './ScheduleD';
16
17 export enum FilingStatus {
18 Single = 'S',
19 MarriedFilingSeparate = 'MFS',
20 MarriedFilingJoint = 'MFJ',
21 };
22
23 export interface Form1040Input {
24 filingStatus: FilingStatus;
25 };
26
27 export default class Form1040 extends Form<Form1040['_lines'], Form1040Input> {
28 readonly name = '1040';
29
30 protected readonly _lines = {
31 '1': new AccumulatorLine(W2, '1', 'Wages, salaries, tips, etc.'),
32 '2a': new AccumulatorLine(Form1099INT, '8', 'Tax-exempt interest'),
33 '2b': new AccumulatorLine(Form1099INT, '1', 'Taxable interest'),
34 '3a': new AccumulatorLine(Form1099DIV, '1b', 'Qualified dividends'),
35 '3b': new AccumulatorLine(Form1099DIV, '1a', 'Ordinary dividends'),
36 '4a': new ComputedLine((tr): number => {
37 const f1099Rs = tr.findForms(Form1099R).filter(f => !f.getValue(tr, '7').includes(Box7Code.G));
38 return sumLineOfForms(tr, f1099Rs, '1');
39 }),
40 '4b': new ComputedLine((tr): number => {
41 const f8606s = tr.findForms(Form8606);
42 return sumLineOfForms(tr, f8606s, '15c') + sumLineOfForms(tr, f8606s, '18');
43 }, 'IRA distributions, taxadble amount'),
44 '4d': new ComputedLine(() => 0),
45 // 4c and 4d are not supported
46 // 5a and 5b are not supported
47 '6': new ComputedLine((tr): number => {
48 const schedD = tr.findForm(ScheduleD);
49 if (!schedD)
50 return 0;
51
52 const l6 = schedD.getValue(tr, '16');
53 if (l6 > 0)
54 return l6;
55 return schedD.getValue(tr, '21');
56 }, 'Capital gain/loss'),
57 '7a': new ReferenceLine(Schedule1, '9', 'Other income from Schedule 1', 0),
58
59 '7b': new ComputedLine((tr): number => {
60 let income = 0;
61 income += this.getValue(tr, '1');
62 income += this.getValue(tr, '2b');
63 income += this.getValue(tr, '3b');
64 income += this.getValue(tr, '4b');
65 income += this.getValue(tr, '4d');
66 //income += this.getValue(tr, '5b');
67 income += this.getValue(tr, '6');
68 income += this.getValue(tr, '7a');
69 return income;
70 }, 'Total income'),
71
72 '8a': new ReferenceLine(Schedule1, '22', 'Adjustments to income', 0),
73
74 '8b': new ComputedLine((tr): number => {
75 return this.getValue(tr, '7b') - this.getValue(tr, '8a');
76 }, 'Adjusted gross income'),
77
78 '9': new ComputedLine((): number => {
79 // TODO - Itemized deductions.
80 switch (this.filingStatus) {
81 case FilingStatus.Single:
82 case FilingStatus.MarriedFilingSeparate:
83 return 12200;
84 case FilingStatus.MarriedFilingJoint:
85 return 24400;
86 }
87 }, 'Deduction'),
88
89 '10': new ComputedLine((tr): number => {
90 const taxableIncome = this.getValue(tr, '8b');
91 let use8995a = false;
92 switch (this.filingStatus) {
93 case FilingStatus.Single: use8995a = taxableIncome <= 160700; break;
94 case FilingStatus.MarriedFilingSeparate: use8995a = taxableIncome <= 160725; break;
95 case FilingStatus.MarriedFilingJoint: use8995a = taxableIncome <= 321400; break;
96 };
97 return 0;
98 }, 'Qualified business income deduction'),
99
100 '11a': new ComputedLine((tr): number => {
101 return this.getValue(tr, '9') + this.getValue(tr, '10');
102 }),
103 '11b': new ComputedLine((tr): number => {
104 return clampToZero(this.getValue(tr, '8b') - this.getValue(tr, '11a'));
105 }, 'Taxable income'),
106
107 '12a': new ComputedLine((tr): number => {
108 // Not supported:
109 // Form 8814 (election to report child's interest or dividends)
110 // Form 4972 (relating to lump-sum distributions)
111 const taxableIncome = this.getValue(tr, '11b');
112
113 if (this.getValue(tr, '3a') > 0 && !tr.findForm(ScheduleD))
114 throw new UnsupportedFeatureError('Qualified Dividends and Captial Gains Tax Worksheet not supported, Schedule D requried');
115
116 const schedD = tr.findForm(ScheduleDTaxWorksheet);
117 if (schedD)
118 return schedD.getValue(tr, '47');
119
120 return computeTax(taxableIncome, this.filingStatus);
121 }, 'Tax'),
122
123 '12b': new ComputedLine((tr): number => {
124 return this.getValue(tr, '12a') + tr.getForm(Schedule2).getValue(tr, '3');
125 }, 'Additional tax'),
126
127 // Not supported: 13a - child tax credit
128
129 '13b': new ReferenceLine(Schedule3, '7', 'Additional credits', 0),
130
131 '14': new ComputedLine((tr): number => {
132 return clampToZero(this.getValue(tr, '12b') - this.getValue(tr, '13b'));
133 }),
134
135 '15': new ReferenceLine(Schedule2, '10', undefined, 0),
136
137 '16': new ComputedLine((tr): number => {
138 return this.getValue(tr, '14') + this.getValue(tr, '15');
139 }, 'Total tax'),
140
141 '17': new ComputedLine((tr): number => {
142 const fedTaxWithheldBoxes = [
143 new AccumulatorLine(W2, '2'),
144 new AccumulatorLine(Form1099R, '4'),
145 new AccumulatorLine(Form1099DIV, '4'),
146 new AccumulatorLine(Form1099INT, '4'),
147 ];
148 const withholding: number[] = fedTaxWithheldBoxes.map(b => b.value(tr));
149
150 let additionalMedicare = 0;
151 const f8959 = tr.findForm(Form8959)
152 if (f8959) {
153 additionalMedicare = f8959.getValue(tr, '24');
154 }
155
156 return reduceBySum(withholding) + additionalMedicare;
157 }, 'Federal income tax withheld'),
158
159 // 18a not supported - Earned income credit (EIC)
160 // 18b not supported - Additional child tax credit. Attach Schedule 8812
161 // 18c not supported - American opportunity credit from Form 8863, line 8
162 '18d': new ReferenceLine(Schedule3, '14', undefined, 0),
163 '18e': new ComputedLine((tr): number => {
164 // Should include 18a-18c.
165 return this.getValue(tr, '18d');
166 }),
167
168 '19': new ComputedLine((tr): number => {
169 return this.getValue(tr, '17') + this.getValue(tr, '18e');
170 }, 'Total payments'),
171
172 '20': new ComputedLine((tr): number => {
173 return clampToZero(this.getValue(tr, '19') - this.getValue(tr, '16'));
174 }, 'Amount overpaid'),
175
176 '23': new ComputedLine((tr): number => {
177 return clampToZero(this.getValue(tr, '16') - this.getValue(tr, '19'));
178 }, 'Amount you owe'),
179 }
180
181 get filingStatus(): FilingStatus {
182 return this.getInput('filingStatus');
183 }
184 };
185
186 export function computeTax(income: number, filingStatus: FilingStatus): number {
187 // From https://www.irs.gov/pub/irs-drop/rp-18-57.pdf, Section 3.01 and
188 // https://www.irs.gov/pub/irs-pdf/p17.pdf, 2019 Tax Rate Schedules (p254).
189 const taxBrackets = {
190 // Format is:
191 // [ limit-of-taxable-income, marginal-rate, base-tax ]
192 // If Income is over Row[0], pay Row[2] + (Row[1] * (Income - PreviousRow[0]))
193 [FilingStatus.MarriedFilingJoint]: [
194 [ 19400, 0.10, 0 ],
195 [ 78950, 0.12, 1940 ],
196 [ 168400, 0.22, 9086 ],
197 [ 321450, 0.24, 28765 ],
198 [ 408200, 0.32, 65497 ],
199 [ 612350, 0.35, 93257 ],
200 [ Infinity, 0.37, 164709.50 ]
201 ],
202 [FilingStatus.Single]: [
203 [ 9700, 0.10, 0 ],
204 [ 39475, 0.12, 970 ],
205 [ 84200, 0.22, 4543 ],
206 [ 160725, 0.24, 14382.50 ],
207 [ 204100, 0.32, 32748.50 ],
208 [ 510300, 0.35, 46628.50 ],
209 [ Infinity, 0.37, 153798.50 ]
210 ],
211 [FilingStatus.MarriedFilingSeparate]: [
212 [ 9700, 0.10, 0 ],
213 [ 39475, 0.12, 970 ],
214 [ 84200, 0.22, 4543 ],
215 [ 160725, 0.24, 14382.50 ],
216 [ 204100, 0.32, 32748.50 ],
217 [ 306175, 0.35, 46628.50 ],
218 [ Infinity, 0.37, 82354.75 ]
219 ]
220 }[filingStatus];
221
222 let i = 0;
223 while (taxBrackets[i][0] < income)
224 ++i;
225
226 const bracket = taxBrackets[i];
227 const bracketStart = i == 0 ? 0 : taxBrackets[i - 1][0];
228
229 return ((income - bracketStart) * bracket[1]) + bracket[2];
230 };