Initial work on Schedule D, Form 8949, and 1099-B.
[ustaxlib.git] / src / fed2019 / Form1040.ts
1 import Form, { FormClass } from '../Form';
2 import TaxReturn from '../TaxReturn';
3 import { Line, AccumulatorLine, ComputedLine, ReferenceLine } from '../Line';
4 import { UnsupportedFeatureError } from '../Errors';
5
6 import Form8959 from './Form8959';
7 import Form1099INT from './Form1099INT';
8 import Form1099DIV from './Form1099DIV';
9 import FormW2 from './FormW2';
10 import ScheduleD from './ScheduleD';
11
12 export enum FilingStatus {
13 Single,
14 MarriedFilingSeparate,
15 MarriedFilingJoint,
16 };
17
18 export interface Form1040Input {
19 filingStatus: FilingStatus;
20 };
21
22 const reduceBySum = (list: number[]) => list.reduce((acc, curr) => acc + curr, 0);
23
24 export default class Form1040 extends Form<Form1040['_lines'], Form1040Input> {
25 readonly name = '1040';
26
27 protected readonly _lines = {
28 '1': new AccumulatorLine(FormW2, '1', 'Wages, salaries, tips, etc.'),
29 '2a': new AccumulatorLine(Form1099INT, '8', 'Tax-exempt interest'),
30 '2b': new AccumulatorLine(Form1099INT, '1', 'Taxable interest'),
31 '3a': new AccumulatorLine(Form1099DIV, '1b', 'Qualified dividends'),
32 '3b': new AccumulatorLine(Form1099DIV, '1a', 'Ordinary dividends'),
33 // 4a and 4b are complex
34 '4b': new ComputedLine(() => 0),
35 '4d': new ComputedLine(() => 0),
36 // 4c and 4d are not supported
37 // 5a and 5b are not supported
38 '6': new ComputedLine((tr: TaxReturn): number => {
39 const schedD = tr.findForm(ScheduleD);
40 if (!schedD)
41 return 0;
42
43 const l6 = schedD.getValue(tr, '16');
44 if (l6 > 0)
45 return l6;
46 return schedD.getValue(tr, '21');
47 }, 'Capital gain/loss'),
48 '7a': new ReferenceLine(/*'Schedule 1'*/ undefined, '9', 'Other income from Schedule 1', 0),
49
50 '7b': new ComputedLine((tr: TaxReturn): number => {
51 let income = 0;
52 income += this.getValue(tr, '1');
53 income += this.getValue(tr, '2b');
54 income += this.getValue(tr, '3b');
55 income += this.getValue(tr, '4b');
56 income += this.getValue(tr, '4d');
57 //income += this.getValue(tr, '5b');
58 income += this.getValue(tr, '6');
59 income += this.getValue(tr, '7a');
60 return income;
61 }, 'Total income'),
62
63 '8a': new ReferenceLine(undefined /*'Schedule 1'*/, '22', 'Adjustments to income', 0),
64
65 '8b': new ComputedLine((tr: TaxReturn): number => {
66 return this.getValue(tr, '7b') - this.getValue(tr, '8a');
67 }, 'Adjusted gross income'),
68
69 // TODO - Deduction
70 '9': new ComputedLine(() => 0, 'Deduction'),
71
72 '10': new ComputedLine((tr: TaxReturn): number => {
73 const taxableIncome = this.getValue(tr, '8b');
74 let use8995a = false;
75 switch (this.getInput('filingStatus')) {
76 case FilingStatus.Single: use8995a = taxableIncome <= 160700; break;
77 case FilingStatus.MarriedFilingSeparate: use8995a = taxableIncome <= 160725; break;
78 case FilingStatus.MarriedFilingJoint: use8995a = taxableIncome <= 321400; break;
79 };
80 return 0;
81 }, 'Qualified business income deduction'),
82
83 '11a': new ComputedLine((tr: TaxReturn): number => {
84 return this.getValue(tr, '9') + this.getValue(tr, '10');
85 }),
86 '11b': new ComputedLine((tr: TaxReturn): number => {
87 const value = this.getValue(tr, '8b') - this.getValue(tr, '11a');
88 return value < 0 ? 0 : value;
89 }, 'Taxable income'),
90
91 '12a': new ComputedLine((tr: TaxReturn): number => {
92 // Not supported:
93 // Form 8814 (election to report child's interest or dividends)
94 // Form 4972 (relating to lump-sum distributions)
95 const taxableIncome = this.getValue(tr, '11b');
96 if (taxableIncome < 100000)
97 throw new UnsupportedFeatureError('Tax-table tax liability not supported');
98
99 const l11b = this.getValue(tr, '11b');
100
101 switch (this.getInput('filingStatus')) {
102 case FilingStatus.Single:
103 if (taxableIncome < 160725)
104 return (l11b * 0.24) - 5825.50;
105 else if (taxableIncome < 204100)
106 return (l11b * 0.32) - 18683.50;
107 else if (taxableIncome < 510300)
108 return (l11b * 0.35) - 24806.50;
109 else
110 return (l11b * 0.38) - 35012.50;
111 case FilingStatus.MarriedFilingJoint:
112 if (taxableIncome < 168400)
113 return (l11b * 0.22) - 8283.00;
114 else if (taxableIncome < 321450)
115 return (l11b * 0.24) - 11651.00;
116 else if (taxableIncome < 408200)
117 return (l11b * 0.32) - 37367.00;
118 else if (taxableIncome < 612350)
119 return (l11b * 0.35) - 49613.00;
120 else
121 return (l11b * 0.37) - 61860.00;
122 case FilingStatus.MarriedFilingSeparate:
123 if (taxableIncome < 160725)
124 return (l11b * 0.24) - 5825.50;
125 else if (taxableIncome < 204100)
126 return (l11b * 0.32) - 18683.50;
127 else if (taxableIncome < 306175)
128 return (l11b * 0.35) - 24806.50;
129 else
130 return (l11b * 0.37) - 30930.00;
131 }
132 throw new UnsupportedFeatureError('Unexpected return type');
133 }, 'Tax'),
134
135 '12b': new ComputedLine((tr: TaxReturn): number => {
136 return this.getValue(tr, '12a') + tr.getForm(Schedule2).getValue(tr, '3');
137 }, 'Additional tax'),
138
139 // Not supported: 13a - child tax credit
140
141 '13b': new ComputedLine((tr: TaxReturn): number => {
142 // TODO: add Sched 3.L7
143 return 0;
144 }, 'Additional credits'),
145
146 '14': new ComputedLine((tr: TaxReturn): number => {
147 const l12b = this.getValue(tr, '12b');
148 const l13b = this.getValue(tr, '13b');
149 const value = l12b - l13b;
150 return value < 0 ? 0 : value;
151 }),
152
153 '15': new ReferenceLine(undefined /*'Schedule 2'*/, '10', undefined, 0),
154
155 '16': new ComputedLine((tr: TaxReturn): number => {
156 return this.getValue(tr, '14') + this.getValue(tr, '15');
157 }, 'Total tax'),
158
159 '17': new ComputedLine((tr: TaxReturn): number => {
160 const fedTaxWithheldBoxes = [
161 new AccumulatorLine(FormW2, '2'),
162 //new AccumulatorLine(Form1099R, '4'),
163 new AccumulatorLine(Form1099DIV, '4'),
164 new AccumulatorLine(Form1099INT, '4'),
165 ];
166 const withholding: number[] = fedTaxWithheldBoxes.map(b => b.value(tr));
167
168 let additionalMedicare = 0;
169 const f8959 = tr.findForm(Form8959)
170 if (f8959) {
171 additionalMedicare = f8959.getValue(tr, '24');
172 }
173
174 return reduceBySum(withholding) + additionalMedicare;
175 }, 'Federal income tax withheld'),
176
177 // 18 not supported
178
179 '19': new ReferenceLine(Form1040 as any, '17', 'Total payments'),
180
181 '20': new ComputedLine((tr: TaxReturn): number => {
182 const l16: number = this.getValue(tr, '16');
183 const l19: number = this.getValue(tr, '19');
184 if (l19 > l16)
185 return l19 - l16;
186 return 0;
187 }, 'Amount overpaid'),
188
189 '23': new ComputedLine((tr: TaxReturn): number => {
190 const l16 = this.getValue(tr, '16');
191 const l19 = this.getValue(tr, '19');
192 if (l19 < l16)
193 return l16 - l19;
194 return 0;
195 }, 'Amount you owe'),
196 };
197 };
198
199 export class Schedule2 extends Form<Schedule2['_lines']> {
200 readonly name = 'Schedule 2';
201
202 protected readonly _lines = {
203 '1': new ComputedLine((tr: TaxReturn): number => {
204 // TODO - this is just using Taxable Income, rather than AMT-limited
205 // income
206 const f1040 = tr.getForm(Form1040);
207 const taxableIncome = f1040.getValue(tr, '11b');
208 switch (f1040.getInput('filingStatus')) {
209 case FilingStatus.Single:
210 if (taxableIncome < 510300)
211 return 0;
212 case FilingStatus.MarriedFilingJoint:
213 if (taxableIncome < 1020600)
214 return 0;
215 case FilingStatus.MarriedFilingSeparate:
216 if (taxableIncome < 510300)
217 return 0;
218 }
219 throw new UnsupportedFeatureError('The AMT is not supported');
220 }, 'AMT'),
221 // 2 is not supported (Excess advance premium tax credit repayment)
222 '3': new ComputedLine((tr: TaxReturn): number => {
223 // Should include line 2.
224 return this.getValue(tr, '1');
225 }),
226
227 // 4 is not supported (Self-employment tax.)
228 // 5 is not supported (Unreported social security and Medicare tax from)
229 // 6 is not supported (Additional tax on IRAs, other qualified retirement plans, and other tax-favored accounts)
230 // 7 is not supported (Household employment taxes.)
231 '8': new ComputedLine((tr: TaxReturn): number => {
232 const f1040 = tr.getForm(Form1040);
233 const wages = f1040.getLine('1').value(tr);
234 const agi = f1040.getLine('8b').value(tr);
235
236 let niit: boolean;
237 const filingStatus = f1040.getInput('filingStatus');
238
239 const additionalMedicare = wages > Form8959.filingStatusLimit(filingStatus);
240
241 switch (f1040.getInput('filingStatus')) {
242 case FilingStatus.Single:
243 if (wages > 200000) {
244 niit = true;
245 }
246 break;
247 case FilingStatus.MarriedFilingJoint:
248 if (wages > 250000) {
249 niit = true;
250 }
251 break;
252 case FilingStatus.MarriedFilingSeparate:
253 if (wages > 125000) {
254 niit = true;
255 }
256 break;
257 }
258
259 let value = 0;
260
261 if (additionalMedicare) {
262 const f8959 = tr.getForm(Form8959);
263 value += f8959.getValue(tr, '18');
264 }
265
266 if (niit) {
267 //const f8960 = tr.getForm('8960');
268 }
269
270 return value;
271 }),
272 // 9 is not supported (Section 965 net tax liability installment from Form 965-A)
273
274 '10': new ComputedLine((tr: TaxReturn): number => {
275 // Should be lines 4 - 8.
276 return this.getValue(tr, '8');
277 })
278 };
279 };