Rewrite Form1040.computeTax based off of Revenue Procedure 2018-57.
[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 { 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.getInput('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.getInput('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 const value = this.getValue(tr, '8b') - this.getValue(tr, '11a');
105 return value < 0 ? 0 : value;
106 }, 'Taxable income'),
107
108 '12a': new ComputedLine((tr): number => {
109 // Not supported:
110 // Form 8814 (election to report child's interest or dividends)
111 // Form 4972 (relating to lump-sum distributions)
112 const taxableIncome = this.getValue(tr, '11b');
113
114 if (this.getValue(tr, '3a') > 0 && !tr.findForm(ScheduleD))
115 throw new UnsupportedFeatureError('Qualified Dividends and Captial Gains Tax Worksheet not supported, Schedule D requried');
116
117 const schedD = tr.findForm(ScheduleDTaxWorksheet);
118 if (schedD)
119 return schedD.getValue(tr, '47');
120
121 return computeTax(taxableIncome, this.getInput('filingStatus'));
122 }, 'Tax'),
123
124 '12b': new ComputedLine((tr): number => {
125 return this.getValue(tr, '12a') + tr.getForm(Schedule2).getValue(tr, '3');
126 }, 'Additional tax'),
127
128 // Not supported: 13a - child tax credit
129
130 '13b': new ReferenceLine(Schedule3, '7', 'Additional credits', 0),
131
132 '14': new ComputedLine((tr): number => {
133 const l12b = this.getValue(tr, '12b');
134 const l13b = this.getValue(tr, '13b');
135 const value = l12b - l13b;
136 return value < 0 ? 0 : value;
137 }),
138
139 '15': new ReferenceLine(Schedule2, '10', undefined, 0),
140
141 '16': new ComputedLine((tr): number => {
142 return this.getValue(tr, '14') + this.getValue(tr, '15');
143 }, 'Total tax'),
144
145 '17': new ComputedLine((tr): number => {
146 const fedTaxWithheldBoxes = [
147 new AccumulatorLine(W2, '2'),
148 new AccumulatorLine(Form1099R, '4'),
149 new AccumulatorLine(Form1099DIV, '4'),
150 new AccumulatorLine(Form1099INT, '4'),
151 ];
152 const withholding: number[] = fedTaxWithheldBoxes.map(b => b.value(tr));
153
154 let additionalMedicare = 0;
155 const f8959 = tr.findForm(Form8959)
156 if (f8959) {
157 additionalMedicare = f8959.getValue(tr, '24');
158 }
159
160 return reduceBySum(withholding) + additionalMedicare;
161 }, 'Federal income tax withheld'),
162
163 // 18a not supported - Earned income credit (EIC)
164 // 18b not supported - Additional child tax credit. Attach Schedule 8812
165 // 18c not supported - American opportunity credit from Form 8863, line 8
166 '18d': new ReferenceLine(Schedule3, '14', undefined, 0),
167 '18e': new ComputedLine((tr): number => {
168 // Should include 18a-18c.
169 return this.getValue(tr, '18d');
170 }),
171
172 '19': new ComputedLine((tr): number => {
173 return this.getValue(tr, '17') + this.getValue(tr, '18e');
174 }, 'Total payments'),
175
176 '20': new ComputedLine((tr): number => {
177 const l16: number = this.getValue(tr, '16');
178 const l19: number = this.getValue(tr, '19');
179 if (l19 > l16)
180 return l19 - l16;
181 return 0;
182 }, 'Amount overpaid'),
183
184 '23': new ComputedLine((tr): number => {
185 const l16 = this.getValue(tr, '16');
186 const l19 = this.getValue(tr, '19');
187 if (l19 < l16)
188 return l16 - l19;
189 return 0;
190 }, 'Amount you owe'),
191 };
192 };
193
194 export function computeTax(income: number, filingStatus: FilingStatus): number {
195 // From https://www.irs.gov/pub/irs-drop/rp-18-57.pdf, Section 3.01 and
196 // https://www.irs.gov/pub/irs-pdf/p17.pdf, 2019 Tax Rate Schedules (p254).
197 const taxBrackets = {
198 // Format is:
199 // [ limit-of-taxable-income, marginal-rate, base-tax ]
200 // If Income is over Row[0], pay Row[2] + (Row[1] * (Income - PreviousRow[0]))
201 [FilingStatus.MarriedFilingJoint]: [
202 [ 19400, 0.10, 0 ],
203 [ 78950, 0.12, 1940 ],
204 [ 168400, 0.22, 9086 ],
205 [ 321450, 0.24, 28765 ],
206 [ 408200, 0.32, 65497 ],
207 [ 612350, 0.35, 93257 ],
208 [ Infinity, 0.37, 164709.50 ]
209 ],
210 [FilingStatus.Single]: [
211 [ 9700, 0.10, 0 ],
212 [ 39475, 0.12, 970 ],
213 [ 84200, 0.22, 4543 ],
214 [ 160725, 0.24, 14382.50 ],
215 [ 204100, 0.32, 32748.50 ],
216 [ 510300, 0.35, 46628.50 ],
217 [ Infinity, 0.37, 153798.50 ]
218 ],
219 [FilingStatus.MarriedFilingSeparate]: [
220 [ 9700, 0.10, 0 ],
221 [ 39475, 0.12, 970 ],
222 [ 84200, 0.22, 4543 ],
223 [ 160725, 0.24, 14382.50 ],
224 [ 204100, 0.32, 32748.50 ],
225 [ 306175, 0.35, 46628.50 ],
226 [ Infinity, 0.37, 82354.75 ]
227 ]
228 }[filingStatus];
229
230 let i = 0;
231 while (taxBrackets[i][0] < income)
232 ++i;
233
234 const bracket = taxBrackets[i];
235 const bracketStart = i == 0 ? 0 : taxBrackets[i - 1][0];
236
237 return ((income - bracketStart) * bracket[1]) + bracket[2];
238 };