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