Extract constant values from fed2019 Forms as named definitions.
[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 ScheduleA from './ScheduleA';
22 import ScheduleD, { ScheduleDTaxWorksheet } from './ScheduleD';
23
24 export enum FilingStatus {
25 Single = 'S',
26 MarriedFilingSeparate = 'MFS',
27 MarriedFilingJoint = 'MFJ',
28 };
29
30 export interface Form1040Input {
31 filingStatus: FilingStatus;
32 };
33
34 export default class Form1040 extends Form<Form1040Input> {
35 readonly name = '1040';
36
37 readonly lines = {
38 '1': new AccumulatorLine(W2, '1', 'Wages, salaries, tips, etc.'),
39 '2a': new ComputedLine((tr): number => {
40 const value = (new AccumulatorLine(Form1099INT, '8')).value(tr) +
41 (new AccumulatorLine(Form1099DIV, '11')).value(tr);
42 return value;
43 }, 'Tax-exempt interest'),
44 '2b': new ComputedLine((tr): number => {
45 const value = (new AccumulatorLine(Form1099INT, '1')).value(tr) +
46 (new AccumulatorLine(Form1099INT, '3')).value(tr);
47 return value;
48 }, 'Taxable interest'),
49 '3a': new AccumulatorLine(Form1099DIV, '1b', 'Qualified dividends'),
50 '3b': new AccumulatorLine(Form1099DIV, '1a', 'Ordinary dividends'),
51 '4a': new ComputedLine((tr): number => {
52 const f1099Rs = tr.findForms(Form1099R).filter(f => !f.getValue(tr, '7').includes(Box7Code.G));
53 return sumLineOfForms(tr, f1099Rs, '1');
54 }),
55 '4b': new ComputedLine((tr): number => {
56 const f8606s = tr.findForms(Form8606);
57 return sumLineOfForms(tr, f8606s, '15c') + sumLineOfForms(tr, f8606s, '18');
58 }, 'IRA distributions, taxable amount'),
59 '4c': new UnsupportedLine('Pensions and annuities'),
60 '4d': new UnsupportedLine('Pensions and annuities, taxable amount'),
61 '5a': new UnsupportedLine('Social security benefits'),
62 '5b': new UnsupportedLine('Social security benefits, taxable amount'),
63 '6': new ComputedLine((tr): number => {
64 const schedD = tr.findForm(ScheduleD);
65 if (!schedD)
66 return 0;
67
68 const l6 = schedD.getValue(tr, '16');
69 if (l6 >= 0)
70 return l6;
71 return schedD.getValue(tr, '21');
72 }, 'Capital gain/loss'),
73 '7a': new ReferenceLine(Schedule1, '9', 'Other income from Schedule 1', 0),
74
75 '7b': new ComputedLine((tr): number => {
76 return sumFormLines(tr, this, ['1', '2b', '3b', '4b', '4d', '5b', '6', '7a']);
77 }, 'Total income'),
78
79 '8a': new ReferenceLine(Schedule1, '22', 'Adjustments to income', 0),
80
81 '8b': new ComputedLine((tr): number => {
82 return this.getValue(tr, '7b') - this.getValue(tr, '8a');
83 }, 'Adjusted gross income'),
84
85 '9': new ComputedLine((tr): number => {
86 let deduction = 0;
87 const schedA = tr.findForm(ScheduleA);
88 if (schedA) {
89 deduction = schedA.getValue(tr, '17');
90 if (schedA.getValue(tr, '18')) {
91 return deduction;
92 }
93 }
94
95 return Math.max(deduction, tr.constants.standardDeduction[this.filingStatus]);
96 }, 'Deduction'),
97
98 '10': new ComputedLine((tr): number => {
99 const f8995 = tr.findForm(Form8995REIT);
100 if (f8995)
101 return f8995.getValue(tr, '39');
102 return 0;
103 }, 'Qualified business income deduction'),
104
105 '11a': new ComputedLine((tr): number => {
106 return this.getValue(tr, '9') + this.getValue(tr, '10');
107 }),
108 '11b': new ComputedLine((tr): number => {
109 return clampToZero(this.getValue(tr, '8b') - this.getValue(tr, '11a'));
110 }, 'Taxable income'),
111
112 '12a': new ComputedLine((tr): number => {
113 // Not supported:
114 // Form 8814 (election to report child's interest or dividends)
115 // Form 4972 (relating to lump-sum distributions)
116
117 const schedD = tr.findForm(ScheduleD);
118 // Line 18 and 19 are not undefined or 0.
119 if (schedD && !schedD.getValue(tr, '20')) {
120 // Use ScheD tax worksheet;
121 const schedDtw = tr.findForm(ScheduleDTaxWorksheet);
122 if (schedDtw)
123 return schedDtw.getValue(tr, '47');
124 }
125
126 // If there are qualified dividends, use the QDCGTW.
127 if (this.getValue(tr, '3a') > 0) {
128 const qdcgtw = tr.getForm(QDCGTaxWorksheet);
129 return qdcgtw.getValue(tr, '27');
130 }
131
132 // Otherwise, compute just on taxable income.
133 return computeTax(this.getValue(tr, '11b'), tr);
134 }, 'Tax'),
135
136 '12b': new ComputedLine((tr): number => {
137 return this.getValue(tr, '12a') + tr.getForm(Schedule2).getValue(tr, '3');
138 }, 'Additional tax'),
139
140 '13a': new UnsupportedLine('Child tax credit'),
141
142 '13b': new ComputedLine((tr): number => {
143 let value: number = this.getValue(tr, '13a');
144 const sched3 = tr.findForm(Schedule3);
145 if (sched3)
146 value += undefinedToZero(sched3.getValue(tr, '7'));
147 return value;
148 }, 'Additional credits'),
149
150 '14': new ComputedLine((tr): number => {
151 return clampToZero(this.getValue(tr, '12b') - this.getValue(tr, '13b'));
152 }),
153
154 '15': new ReferenceLine(Schedule2, '10', undefined, 0),
155
156 '16': new ComputedLine((tr): number => {
157 return this.getValue(tr, '14') + this.getValue(tr, '15');
158 }, 'Total tax'),
159
160 '17': new ComputedLine((tr): number => {
161 const fedTaxWithheldBoxes = [
162 new AccumulatorLine(W2, '2'),
163 new AccumulatorLine(Form1099R, '4'),
164 new AccumulatorLine(Form1099DIV, '4'),
165 new AccumulatorLine(Form1099INT, '4'),
166 ];
167 const withholding: number[] = fedTaxWithheldBoxes.map(b => b.value(tr));
168
169 let additionalMedicare = 0;
170 const f8959 = tr.findForm(Form8959)
171 if (f8959) {
172 additionalMedicare = f8959.getValue(tr, '24');
173 }
174
175 return reduceBySum(withholding) + additionalMedicare;
176 }, 'Federal income tax withheld'),
177
178 '18a': new UnsupportedLine('Earned income credit (EIC)'),
179 '18b': new UnsupportedLine('Additional child tax credit. Attach Schedule 8812'),
180 '18c': new UnsupportedLine('American opportunity credit from Form 8863, line 8'),
181 '18d': new ReferenceLine(Schedule3, '14', undefined, 0),
182 '18e': new ComputedLine((tr): number => {
183 return sumFormLines(tr, this, ['18a', '18b', '18c', '18d']);
184 }),
185
186 '19': new ComputedLine((tr): number => {
187 return this.getValue(tr, '17') + this.getValue(tr, '18e');
188 }, 'Total payments'),
189
190 '20': new ComputedLine((tr): number => {
191 return clampToZero(this.getValue(tr, '19') - this.getValue(tr, '16'));
192 }, 'Amount overpaid'),
193
194 '23': new ComputedLine((tr): number => {
195 return clampToZero(this.getValue(tr, '16') - this.getValue(tr, '19'));
196 }, 'Amount you owe'),
197 }
198
199 get filingStatus(): FilingStatus {
200 return this.getInput('filingStatus');
201 }
202 };
203
204 export function computeTax(income: number, tr: TaxReturn): number {
205 const f1040 = tr.getForm(Form1040);
206 const taxBrackets = tr.constants.taxBrackets[f1040.filingStatus];
207
208 let i = 0;
209 while (taxBrackets[i][0] < income)
210 ++i;
211
212 const bracket = taxBrackets[i];
213 const bracketStart = i == 0 ? 0 : taxBrackets[i - 1][0];
214
215 return ((income - bracketStart) * bracket[1]) + bracket[2];
216 };
217
218 export class QDCGTaxWorksheet extends Form {
219 readonly name = 'QDCG Tax Worksheet';
220
221 readonly lines = {
222 '1': new ReferenceLine(Form1040, '11b', 'Taxable income'),
223 '2': new ReferenceLine(Form1040, '3a', 'Qualified dividends'),
224 '3': new ComputedLine((tr): number => {
225 const schedD = tr.findForm(ScheduleD);
226 if (schedD)
227 return clampToZero(Math.min(schedD.getValue(tr, '15'), schedD.getValue(tr, '16')));
228 return tr.getForm(Form1040).getValue(tr, '6');
229 }),
230 '4': new ComputedLine((tr): number => this.getValue(tr, '2') + this.getValue(tr, '3')),
231 '5': new UnsupportedLine('Form 4952@4g (Investment interest expense deduction)'),
232 '6': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '4') - this.getValue(tr, '5'))),
233 '7': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '1') - this.getValue(tr, '6'))),
234 '8': new ComputedLine((tr): number => {
235 const fs = tr.getForm(Form1040).filingStatus;
236 return tr.constants.capitalGains.rate0MaxIncome[fs];
237 }),
238 '9': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '8'))),
239 '10': new ComputedLine((tr): number => Math.min(this.getValue(tr, '7'), this.getValue(tr, '9'))),
240 '11': new ComputedLine((tr): number => {
241 return this.getValue(tr, '9') - this.getValue(tr, '10');
242 }, 'Amount taxed at 0%'),
243 '12': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '6'))),
244 '13': new ReferenceLine(QDCGTaxWorksheet as any, '11'),
245 '14': new ComputedLine((tr): number => this.getValue(tr, '12') - this.getValue(tr, '13')),
246 '15': new ComputedLine((tr): number => {
247 const fs = tr.getForm(Form1040).filingStatus;
248 return tr.constants.capitalGains.rate15MaxIncome[fs];
249 }),
250 '16': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '15'))),
251 '17': new ComputedLine((tr): number => this.getValue(tr, '7') + this.getValue(tr, '11')),
252 '18': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '16') - this.getValue(tr, '17'))),
253 '19': new ComputedLine((tr): number => Math.min(this.getValue(tr, '14'), this.getValue(tr, '18'))),
254 '20': new ComputedLine((tr): number => {
255 return this.getValue(tr, '19') * 0.15;
256 }, 'Amount taxed at 15%'),
257 '21': new ComputedLine((tr): number => this.getValue(tr, '11') + this.getValue(tr, '19')),
258 '22': new ComputedLine((tr): number => this.getValue(tr, '12') - this.getValue(tr, '21')),
259 '23': new ComputedLine((tr): number => {
260 return this.getValue(tr, '22') * 0.20;
261 }, 'Amount taxed at 20%'),
262 '24': new ComputedLine((tr): number => {
263 return computeTax(this.getValue(tr, '7'), tr);
264 }, 'Tax on line 7'),
265 '25': new ComputedLine((tr): number => {
266 return this.getValue(tr, '20') +
267 this.getValue(tr, '23') +
268 this.getValue(tr, '24');
269 }),
270 '26': new ComputedLine((tr): number => {
271 return computeTax(this.getValue(tr, '1'), tr);
272 }, 'Tax on line 1'),
273 '27': new ComputedLine((tr): number => {
274 return Math.min(this.getValue(tr, '25'), this.getValue(tr, '26'));
275 }, 'Tax on all taxable income')
276 };
277 };