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'),
76 '9': new ComputedLine((): number => {
77 // TODO - Itemized deductions.
78 switch (this.getInput('filingStatus')) {
79 case FilingStatus.Single:
80 case FilingStatus.MarriedFilingSeparate:
82 case FilingStatus.MarriedFilingJoint:
87 '10': new ComputedLine((tr): number => {
88 const taxableIncome = this.getValue(tr, '8b');
90 switch (this.getInput('filingStatus')) {
91 case FilingStatus.Single: use8995a = taxableIncome <= 160700; break;
92 case FilingStatus.MarriedFilingSeparate: use8995a = taxableIncome <= 160725; break;
93 case FilingStatus.MarriedFilingJoint: use8995a = taxableIncome <= 321400; break;
96 }, 'Qualified business income deduction'),
98 '11a': new ComputedLine((tr): number => {
99 return this.getValue(tr, '9') + this.getValue(tr, '10');
101 '11b': new ComputedLine((tr): number => {
102 const value = this.getValue(tr, '8b') - this.getValue(tr, '11a');
103 return value < 0 ? 0 : value;
104 }, 'Taxable income'),
106 '12a': new ComputedLine((tr): number => {
108 // Form 8814 (election to report child's interest or dividends)
109 // Form 4972 (relating to lump-sum distributions)
110 const taxableIncome = this.getValue(tr, '11b');
112 if (this.getValue(tr, '3a') > 0 && !tr.findForm(ScheduleD))
113 throw new UnsupportedFeatureError('Qualified Dividends and Captial Gains Tax Worksheet not supported, Schedule D requried');
115 const schedD = tr.findForm(ScheduleDTaxWorksheet);
117 return schedD.getValue(tr, '47');
119 return computeTax(taxableIncome, this.getInput('filingStatus'));
122 '12b': new ComputedLine((tr): number => {
123 return this.getValue(tr, '12a') + tr.getForm(Schedule2).getValue(tr, '3');
124 }, 'Additional tax'),
126 // Not supported: 13a - child tax credit
128 '13b': new ComputedLine((tr): number => {
129 // TODO: add Sched 3.L7
131 }, 'Additional credits'),
133 '14': new ComputedLine((tr): number => {
134 const l12b = this.getValue(tr, '12b');
135 const l13b = this.getValue(tr, '13b');
136 const value = l12b - l13b;
137 return value < 0 ? 0 : value;
140 '15': new ReferenceLine(Schedule2, '10', undefined, 0),
142 '16': new ComputedLine((tr): number => {
143 return this.getValue(tr, '14') + this.getValue(tr, '15');
146 '17': new ComputedLine((tr): number => {
147 const fedTaxWithheldBoxes = [
148 new AccumulatorLine(FormW2, '2'),
149 new AccumulatorLine(Form1099R, '4'),
150 new AccumulatorLine(Form1099DIV, '4'),
151 new AccumulatorLine(Form1099INT, '4'),
153 const withholding: number[] = fedTaxWithheldBoxes.map(b => b.value(tr));
155 let additionalMedicare = 0;
156 const f8959 = tr.findForm(Form8959)
158 additionalMedicare = f8959.getValue(tr, '24');
161 return reduceBySum(withholding) + additionalMedicare;
162 }, 'Federal income tax withheld'),
166 '19': new ReferenceLine(Form1040 as any, '17', 'Total payments'),
168 '20': new ComputedLine((tr): number => {
169 const l16: number = this.getValue(tr, '16');
170 const l19: number = this.getValue(tr, '19');
174 }, 'Amount overpaid'),
176 '23': new ComputedLine((tr): number => {
177 const l16 = this.getValue(tr, '16');
178 const l19 = this.getValue(tr, '19');
182 }, 'Amount you owe'),
186 export class Schedule2 extends Form<Schedule2['_lines']> {
187 readonly name = 'Schedule 2';
189 protected readonly _lines = {
190 '1': new ComputedLine((tr): number => {
191 // TODO - this is just using Taxable Income, rather than AMT-limited
193 const f1040 = tr.getForm(Form1040);
194 const taxableIncome = f1040.getValue(tr, '11b');
195 switch (f1040.getInput('filingStatus')) {
196 case FilingStatus.Single:
197 if (taxableIncome < 510300)
199 case FilingStatus.MarriedFilingJoint:
200 if (taxableIncome < 1020600)
202 case FilingStatus.MarriedFilingSeparate:
203 if (taxableIncome < 510300)
206 throw new UnsupportedFeatureError('The AMT is not supported');
208 '2': new ComputedLine(() => 0, 'Excess advance premium tax credit repayment'), // Not supported.
209 '3': new ComputedLine((tr): number => {
210 // Should include line 2.
211 return this.getValue(tr, '1');
214 // 4 is not supported (Self-employment tax.)
215 // 5 is not supported (Unreported social security and Medicare tax from)
216 // 6 is not supported (Additional tax on IRAs, other qualified retirement plans, and other tax-favored accounts)
217 // 7 is not supported (Household employment taxes.)
218 '8': new ComputedLine((tr): number => {
219 const f1040 = tr.getForm(Form1040);
220 const wages = f1040.getLine('1').value(tr);
223 const filingStatus = f1040.getInput('filingStatus');
225 const additionalMedicare = wages > Form8959.filingStatusLimit(filingStatus);
227 switch (f1040.getInput('filingStatus')) {
228 case FilingStatus.Single:
229 if (wages > 200000) {
233 case FilingStatus.MarriedFilingJoint:
234 if (wages > 250000) {
238 case FilingStatus.MarriedFilingSeparate:
239 if (wages > 125000) {
247 if (additionalMedicare) {
248 const f8959 = tr.getForm(Form8959);
249 value += f8959.getValue(tr, '18');
253 //const f8960 = tr.getForm('8960');
258 // 9 is not supported (Section 965 net tax liability installment from Form 965-A)
260 '10': new ComputedLine((tr): number => {
261 // Should be lines 4 - 8.
262 return this.getValue(tr, '8');
267 export function computeTax(income: number, filingStatus: FilingStatus): number {
269 throw new UnsupportedFeatureError('Tax-table tax liability not supported');
271 switch (filingStatus) {
272 case FilingStatus.Single:
274 return (income * 0.24) - 5825.50;
275 else if (income < 204100)
276 return (income * 0.32) - 18683.50;
277 else if (income < 510300)
278 return (income * 0.35) - 24806.50;
280 return (income * 0.38) - 35012.50;
281 case FilingStatus.MarriedFilingJoint:
283 return (income * 0.22) - 8283.00;
284 else if (income < 321450)
285 return (income * 0.24) - 11651.00;
286 else if (income < 408200)
287 return (income * 0.32) - 37367.00;
288 else if (income < 612350)
289 return (income * 0.35) - 49613.00;
291 return (income * 0.37) - 61860.00;
292 case FilingStatus.MarriedFilingSeparate:
294 return (income * 0.24) - 5825.50;
295 else if (income < 204100)
296 return (income * 0.32) - 18683.50;
297 else if (income < 306175)
298 return (income * 0.35) - 24806.50;
300 return (income * 0.37) - 30930.00;
302 throw new UnsupportedFeatureError('Unexpected return type');