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';
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';
15 export enum FilingStatus {
17 MarriedFilingSeparate = 'MFS',
18 MarriedFilingJoint = 'MFJ',
21 export interface Form1040Input {
22 filingStatus: FilingStatus;
25 export default class Form1040 extends Form<Form1040['_lines'], Form1040Input> {
26 readonly name = '1040';
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');
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);
50 const l6 = schedD.getValue(tr, '16');
53 return schedD.getValue(tr, '21');
54 }, 'Capital gain/loss'),
55 '7a': new ReferenceLine(/*'Schedule 1'*/ undefined, '9', 'Other income from Schedule 1', 0),
57 '7b': new ComputedLine((tr): number => {
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');
70 '8a': new ReferenceLine(undefined /*'Schedule 1'*/, '22', 'Adjustments to income', 0),
72 '8b': new ComputedLine((tr): number => {
73 return this.getValue(tr, '7b') - this.getValue(tr, '8a');
74 }, 'Adjusted gross income'),
77 '9': new ComputedLine(() => 0, 'Deduction'),
79 '10': new ComputedLine((tr): number => {
80 const taxableIncome = this.getValue(tr, '8b');
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;
88 }, 'Qualified business income deduction'),
90 '11a': new ComputedLine((tr): number => {
91 return this.getValue(tr, '9') + this.getValue(tr, '10');
93 '11b': new ComputedLine((tr): number => {
94 const value = this.getValue(tr, '8b') - this.getValue(tr, '11a');
95 return value < 0 ? 0 : value;
98 '12a': new ComputedLine((tr): number => {
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');
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');
107 const schedD = tr.findForm(ScheduleDTaxWorksheet);
109 return schedD.getValue(tr, '47');
111 return computeTax(taxableIncome, this.getInput('filingStatus'));
114 '12b': new ComputedLine((tr): number => {
115 return this.getValue(tr, '12a') + tr.getForm(Schedule2).getValue(tr, '3');
116 }, 'Additional tax'),
118 // Not supported: 13a - child tax credit
120 '13b': new ComputedLine((tr): number => {
121 // TODO: add Sched 3.L7
123 }, 'Additional credits'),
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;
132 '15': new ReferenceLine(Schedule2, '10', undefined, 0),
134 '16': new ComputedLine((tr): number => {
135 return this.getValue(tr, '14') + this.getValue(tr, '15');
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'),
145 const withholding: number[] = fedTaxWithheldBoxes.map(b => b.value(tr));
147 let additionalMedicare = 0;
148 const f8959 = tr.findForm(Form8959)
150 additionalMedicare = f8959.getValue(tr, '24');
153 return reduceBySum(withholding) + additionalMedicare;
154 }, 'Federal income tax withheld'),
158 '19': new ReferenceLine(Form1040 as any, '17', 'Total payments'),
160 '20': new ComputedLine((tr): number => {
161 const l16: number = this.getValue(tr, '16');
162 const l19: number = this.getValue(tr, '19');
166 }, 'Amount overpaid'),
168 '23': new ComputedLine((tr): number => {
169 const l16 = this.getValue(tr, '16');
170 const l19 = this.getValue(tr, '19');
174 }, 'Amount you owe'),
178 export class Schedule2 extends Form<Schedule2['_lines']> {
179 readonly name = 'Schedule 2';
181 protected readonly _lines = {
182 '1': new ComputedLine((tr): number => {
183 // TODO - this is just using Taxable Income, rather than AMT-limited
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)
191 case FilingStatus.MarriedFilingJoint:
192 if (taxableIncome < 1020600)
194 case FilingStatus.MarriedFilingSeparate:
195 if (taxableIncome < 510300)
198 throw new UnsupportedFeatureError('The AMT is not supported');
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');
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);
215 const filingStatus = f1040.getInput('filingStatus');
217 const additionalMedicare = wages > Form8959.filingStatusLimit(filingStatus);
219 switch (f1040.getInput('filingStatus')) {
220 case FilingStatus.Single:
221 if (wages > 200000) {
225 case FilingStatus.MarriedFilingJoint:
226 if (wages > 250000) {
230 case FilingStatus.MarriedFilingSeparate:
231 if (wages > 125000) {
239 if (additionalMedicare) {
240 const f8959 = tr.getForm(Form8959);
241 value += f8959.getValue(tr, '18');
245 //const f8960 = tr.getForm('8960');
250 // 9 is not supported (Section 965 net tax liability installment from Form 965-A)
252 '10': new ComputedLine((tr): number => {
253 // Should be lines 4 - 8.
254 return this.getValue(tr, '8');
259 export function computeTax(income: number, filingStatus: FilingStatus): number {
261 throw new UnsupportedFeatureError('Tax-table tax liability not supported');
263 switch (filingStatus) {
264 case FilingStatus.Single:
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;
272 return (income * 0.38) - 35012.50;
273 case FilingStatus.MarriedFilingJoint:
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;
283 return (income * 0.37) - 61860.00;
284 case FilingStatus.MarriedFilingSeparate:
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;
292 return (income * 0.37) - 30930.00;
294 throw new UnsupportedFeatureError('Unexpected return type');