Add UnsupportedLine, to formalize a comment convention that exists in fed2019.
[ustaxlib.git] / src / fed2019 / Form1040.ts
1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
5
6 import { Form, TaxReturn } from '../core';
7 import { Line, AccumulatorLine, ComputedLine, ReferenceLine, UnsupportedLine, sumFormLines, sumLineOfForms } from '../core/Line';
8 import { UnsupportedFeatureError } from '../core/Errors';
9 import { clampToZero, reduceBySum, undefinedToZero } from '../core/Math';
10
11 import Form8606 from './Form8606';
12 import Form8959 from './Form8959';
13 import Form1099INT from './Form1099INT';
14 import Form1099DIV from './Form1099DIV';
15 import Form1099R, { Box7Code } from './Form1099R';
16 import Form8995REIT from './Form8995';
17 import W2 from './W2';
18 import Schedule1 from './Schedule1';
19 import Schedule2 from './Schedule2';
20 import Schedule3 from './Schedule3';
21 import ScheduleD, { ScheduleDTaxWorksheet } from './ScheduleD';
22
23 export enum FilingStatus {
24 Single = 'S',
25 MarriedFilingSeparate = 'MFS',
26 MarriedFilingJoint = 'MFJ',
27 };
28
29 export interface Form1040Input {
30 filingStatus: FilingStatus;
31 };
32
33 export default class Form1040 extends Form<Form1040['lines'], Form1040Input> {
34 readonly name = '1040';
35
36 readonly lines = {
37 '1': new AccumulatorLine(W2, '1', 'Wages, salaries, tips, etc.'),
38 '2a': new ComputedLine((tr): number => {
39 const value = (new AccumulatorLine(Form1099INT, '8')).value(tr) +
40 (new AccumulatorLine(Form1099DIV, '11')).value(tr);
41 return value;
42 }, 'Tax-exempt interest'),
43 '2b': new ComputedLine((tr): number => {
44 const value = (new AccumulatorLine(Form1099INT, '1')).value(tr) +
45 (new AccumulatorLine(Form1099INT, '3')).value(tr);
46 return value;
47 }, 'Taxable interest'),
48 '3a': new AccumulatorLine(Form1099DIV, '1b', 'Qualified dividends'),
49 '3b': new AccumulatorLine(Form1099DIV, '1a', 'Ordinary dividends'),
50 '4a': new ComputedLine((tr): number => {
51 const f1099Rs = tr.findForms(Form1099R).filter(f => !f.getValue(tr, '7').includes(Box7Code.G));
52 return sumLineOfForms(tr, f1099Rs, '1');
53 }),
54 '4b': new ComputedLine((tr): number => {
55 const f8606s = tr.findForms(Form8606);
56 return sumLineOfForms(tr, f8606s, '15c') + sumLineOfForms(tr, f8606s, '18');
57 }, 'IRA distributions, taxable amount'),
58 '4c': new UnsupportedLine('Pensions and annuities'),
59 '4d': new UnsupportedLine('Pensions and annuities, taxable amount'),
60 '5a': new UnsupportedLine('Social security benefits'),
61 '5b': new UnsupportedLine('Social security benefits, taxable amount'),
62 '6': new ComputedLine((tr): number => {
63 const schedD = tr.findForm(ScheduleD);
64 if (!schedD)
65 return 0;
66
67 const l6 = schedD.getValue(tr, '16');
68 if (l6 >= 0)
69 return l6;
70 return schedD.getValue(tr, '21');
71 }, 'Capital gain/loss'),
72 '7a': new ReferenceLine(Schedule1, '9', 'Other income from Schedule 1', 0),
73
74 '7b': new ComputedLine((tr): number => {
75 return sumFormLines(tr, this, ['1', '2b', '3b', '4b', '4d', '5b', '6', '7a']);
76 }, 'Total income'),
77
78 '8a': new ReferenceLine(Schedule1, '22', 'Adjustments to income', 0),
79
80 '8b': new ComputedLine((tr): number => {
81 return this.getValue(tr, '7b') - this.getValue(tr, '8a');
82 }, 'Adjusted gross income'),
83
84 '9': new ComputedLine((): number => {
85 // TODO - Itemized deductions.
86 switch (this.filingStatus) {
87 case FilingStatus.Single:
88 case FilingStatus.MarriedFilingSeparate:
89 return 12200;
90 case FilingStatus.MarriedFilingJoint:
91 return 24400;
92 }
93 }, 'Deduction'),
94
95 '10': new ComputedLine((tr): number => {
96 const f8995 = tr.findForm(Form8995REIT);
97 if (f8995)
98 return f8995.getValue(tr, '39');
99 return 0;
100 }, 'Qualified business income deduction'),
101
102 '11a': new ComputedLine((tr): number => {
103 return this.getValue(tr, '9') + this.getValue(tr, '10');
104 }),
105 '11b': new ComputedLine((tr): number => {
106 return clampToZero(this.getValue(tr, '8b') - this.getValue(tr, '11a'));
107 }, 'Taxable income'),
108
109 '12a': new ComputedLine((tr): number => {
110 // Not supported:
111 // Form 8814 (election to report child's interest or dividends)
112 // Form 4972 (relating to lump-sum distributions)
113
114 const schedD = tr.findForm(ScheduleD);
115 // Line 18 and 19 are not undefined or 0.
116 if (schedD && !schedD.getValue(tr, '20')) {
117 // Use ScheD tax worksheet;
118 const schedDtw = tr.findForm(ScheduleDTaxWorksheet);
119 if (schedDtw)
120 return schedDtw.getValue(tr, '47');
121 }
122
123 // If there are qualified dividends, use the QDCGTW.
124 if (this.getValue(tr, '3a') > 0) {
125 const qdcgtw = tr.getForm(QDCGTaxWorksheet);
126 return qdcgtw.getValue(tr, '27');
127 }
128
129 // Otherwise, compute just on taxable income.
130 return computeTax(this.getValue(tr, '11b'), this.filingStatus);
131 }, 'Tax'),
132
133 '12b': new ComputedLine((tr): number => {
134 return this.getValue(tr, '12a') + tr.getForm(Schedule2).getValue(tr, '3');
135 }, 'Additional tax'),
136
137 '13a': new UnsupportedLine('Child tax credit'),
138
139 '13b': new ComputedLine((tr): number => {
140 let value = this.getValue(tr, '13a');
141 const sched3 = tr.findForm(Schedule3);
142 if (sched3)
143 value += undefinedToZero(sched3.getValue(tr, '7'));
144 return value;
145 }, 'Additional credits'),
146
147 '14': new ComputedLine((tr): number => {
148 return clampToZero(this.getValue(tr, '12b') - this.getValue(tr, '13b'));
149 }),
150
151 '15': new ReferenceLine(Schedule2, '10', undefined, 0),
152
153 '16': new ComputedLine((tr): number => {
154 return this.getValue(tr, '14') + this.getValue(tr, '15');
155 }, 'Total tax'),
156
157 '17': new ComputedLine((tr): number => {
158 const fedTaxWithheldBoxes = [
159 new AccumulatorLine(W2, '2'),
160 new AccumulatorLine(Form1099R, '4'),
161 new AccumulatorLine(Form1099DIV, '4'),
162 new AccumulatorLine(Form1099INT, '4'),
163 ];
164 const withholding: number[] = fedTaxWithheldBoxes.map(b => b.value(tr));
165
166 let additionalMedicare = 0;
167 const f8959 = tr.findForm(Form8959)
168 if (f8959) {
169 additionalMedicare = f8959.getValue(tr, '24');
170 }
171
172 return reduceBySum(withholding) + additionalMedicare;
173 }, 'Federal income tax withheld'),
174
175 '18a': new UnsupportedLine('Earned income credit (EIC)'),
176 '18b': new UnsupportedLine('Additional child tax credit. Attach Schedule 8812'),
177 '18c': new UnsupportedLine('American opportunity credit from Form 8863, line 8'),
178 '18d': new ReferenceLine(Schedule3, '14', undefined, 0),
179 '18e': new ComputedLine((tr): number => {
180 return sumFormLines(tr, this, ['18a', '18b', '18c', '18d']);
181 }),
182
183 '19': new ComputedLine((tr): number => {
184 return this.getValue(tr, '17') + this.getValue(tr, '18e');
185 }, 'Total payments'),
186
187 '20': new ComputedLine((tr): number => {
188 return clampToZero(this.getValue(tr, '19') - this.getValue(tr, '16'));
189 }, 'Amount overpaid'),
190
191 '23': new ComputedLine((tr): number => {
192 return clampToZero(this.getValue(tr, '16') - this.getValue(tr, '19'));
193 }, 'Amount you owe'),
194 }
195
196 get filingStatus(): FilingStatus {
197 return this.getInput('filingStatus');
198 }
199 };
200
201 export function computeTax(income: number, filingStatus: FilingStatus): number {
202 // From https://www.irs.gov/pub/irs-drop/rp-18-57.pdf, Section 3.01 and
203 // https://www.irs.gov/pub/irs-pdf/p17.pdf, 2019 Tax Rate Schedules (p254).
204 const taxBrackets = {
205 // Format is:
206 // [ limit-of-taxable-income, marginal-rate, base-tax ]
207 // If Income is over Row[0], pay Row[2] + (Row[1] * (Income - PreviousRow[0]))
208 [FilingStatus.MarriedFilingJoint]: [
209 [ 19400, 0.10, 0 ],
210 [ 78950, 0.12, 1940 ],
211 [ 168400, 0.22, 9086 ],
212 [ 321450, 0.24, 28765 ],
213 [ 408200, 0.32, 65497 ],
214 [ 612350, 0.35, 93257 ],
215 [ Infinity, 0.37, 164709.50 ]
216 ],
217 [FilingStatus.Single]: [
218 [ 9700, 0.10, 0 ],
219 [ 39475, 0.12, 970 ],
220 [ 84200, 0.22, 4543 ],
221 [ 160725, 0.24, 14382.50 ],
222 [ 204100, 0.32, 32748.50 ],
223 [ 510300, 0.35, 46628.50 ],
224 [ Infinity, 0.37, 153798.50 ]
225 ],
226 [FilingStatus.MarriedFilingSeparate]: [
227 [ 9700, 0.10, 0 ],
228 [ 39475, 0.12, 970 ],
229 [ 84200, 0.22, 4543 ],
230 [ 160725, 0.24, 14382.50 ],
231 [ 204100, 0.32, 32748.50 ],
232 [ 306175, 0.35, 46628.50 ],
233 [ Infinity, 0.37, 82354.75 ]
234 ]
235 }[filingStatus];
236
237 let i = 0;
238 while (taxBrackets[i][0] < income)
239 ++i;
240
241 const bracket = taxBrackets[i];
242 const bracketStart = i == 0 ? 0 : taxBrackets[i - 1][0];
243
244 return ((income - bracketStart) * bracket[1]) + bracket[2];
245 };
246
247 export class QDCGTaxWorksheet extends Form<QDCGTaxWorksheet['lines']> {
248 readonly name = 'QDCG Tax Worksheet';
249
250 readonly lines = {
251 '1': new ReferenceLine(Form1040, '11b', 'Taxable income'),
252 '2': new ReferenceLine(Form1040, '3a', 'Qualified dividends'),
253 '3': new ComputedLine((tr): number => {
254 const schedD = tr.findForm(ScheduleD);
255 if (schedD)
256 return clampToZero(Math.min(schedD.getValue(tr, '15'), schedD.getValue(tr, '16')));
257 return tr.getForm(Form1040).getValue(tr, '6');
258 }),
259 '4': new ComputedLine((tr): number => this.getValue(tr, '2') + this.getValue(tr, '3')),
260 '5': new UnsupportedLine('Form 4952@4g (Investment interest expense deduction)'),
261 '6': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '4') - this.getValue(tr, '5'))),
262 '7': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '1') - this.getValue(tr, '6'))),
263 '8': new ComputedLine((tr): number => {
264 switch (tr.getForm(Form1040).filingStatus) {
265 case FilingStatus.Single:
266 case FilingStatus.MarriedFilingSeparate:
267 return 39375;
268 case FilingStatus.MarriedFilingJoint:
269 return 78750;
270 };
271 }),
272 '9': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '8'))),
273 '10': new ComputedLine((tr): number => Math.min(this.getValue(tr, '7'), this.getValue(tr, '9'))),
274 '11': new ComputedLine((tr): number => {
275 return this.getValue(tr, '9') - this.getValue(tr, '10');
276 }, 'Amount taxed at 0%'),
277 '12': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '6'))),
278 '13': new ReferenceLine(QDCGTaxWorksheet as any, '11'),
279 '14': new ComputedLine((tr): number => this.getValue(tr, '12') - this.getValue(tr, '13')),
280 '15': new ComputedLine((tr): number => {
281 switch (tr.getForm(Form1040).filingStatus) {
282 case FilingStatus.Single: return 434550;
283 case FilingStatus.MarriedFilingSeparate: return 244425;
284 case FilingStatus.MarriedFilingJoint: return 488850;
285 };
286 }),
287 '16': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '15'))),
288 '17': new ComputedLine((tr): number => this.getValue(tr, '7') + this.getValue(tr, '11')),
289 '18': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '16') - this.getValue(tr, '17'))),
290 '19': new ComputedLine((tr): number => Math.min(this.getValue(tr, '14'), this.getValue(tr, '18'))),
291 '20': new ComputedLine((tr): number => {
292 return this.getValue(tr, '19') * 0.15;
293 }, 'Amount taxed at 15%'),
294 '21': new ComputedLine((tr): number => this.getValue(tr, '11') + this.getValue(tr, '19')),
295 '22': new ComputedLine((tr): number => this.getValue(tr, '12') - this.getValue(tr, '21')),
296 '23': new ComputedLine((tr): number => {
297 return this.getValue(tr, '22') * 0.20;
298 }, 'Amount taxed at 20%'),
299 '24': new ComputedLine((tr): number => {
300 return computeTax(this.getValue(tr, '7'), tr.getForm(Form1040).filingStatus);
301 }, 'Tax on line 7'),
302 '25': new ComputedLine((tr): number => {
303 return this.getValue(tr, '20') +
304 this.getValue(tr, '23') +
305 this.getValue(tr, '24');
306 }),
307 '26': new ComputedLine((tr): number => {
308 return computeTax(this.getValue(tr, '1'), tr.getForm(Form1040).filingStatus);
309 }, 'Tax on line 1'),
310 '27': new ComputedLine((tr): number => {
311 return Math.min(this.getValue(tr, '25'), this.getValue(tr, '26'));
312 }, 'Tax on all taxable income')
313 };
314 };