Simplify core imports.
[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 FormW2 from './FormW2';
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(FormW2, '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(FormW2, '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 if (income < 100000)
196 throw new UnsupportedFeatureError('Tax-table tax liability not supported');
197
198 switch (filingStatus) {
199 case FilingStatus.Single:
200 if (income < 160725)
201 return (income * 0.24) - 5825.50;
202 else if (income < 204100)
203 return (income * 0.32) - 18683.50;
204 else if (income < 510300)
205 return (income * 0.35) - 24806.50;
206 else
207 return (income * 0.38) - 35012.50;
208 case FilingStatus.MarriedFilingJoint:
209 if (income < 168400)
210 return (income * 0.22) - 8283.00;
211 else if (income < 321450)
212 return (income * 0.24) - 11651.00;
213 else if (income < 408200)
214 return (income * 0.32) - 37367.00;
215 else if (income < 612350)
216 return (income * 0.35) - 49613.00;
217 else
218 return (income * 0.37) - 61860.00;
219 case FilingStatus.MarriedFilingSeparate:
220 if (income < 160725)
221 return (income * 0.24) - 5825.50;
222 else if (income < 204100)
223 return (income * 0.32) - 18683.50;
224 else if (income < 306175)
225 return (income * 0.35) - 24806.50;
226 else
227 return (income * 0.37) - 30930.00;
228 }
229 throw new UnsupportedFeatureError('Unexpected return type');
230 };