Move some computations for Form1040 into methods.
[ustaxlib.git] / src / fed2019 / Form1040.ts
1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
5
6 import { Form, TaxReturn } from '../core';
7 import { Line, AccumulatorLine, ComputedLine, ReferenceLine, SymbolicLine, UnsupportedLine, sumFormLines, sumLineOfForms } from '../core/Line';
8 import { UnsupportedFeatureError } from '../core/Errors';
9 import { clampToZero, reduceBySum, undefinedToZero } from '../core/Math';
10
11 import Form8606 from './Form8606';
12 import Form8959 from './Form8959';
13 import Form1099INT from './Form1099INT';
14 import Form1099DIV from './Form1099DIV';
15 import Form1099R, { Box7Code } from './Form1099R';
16 import Form8995REIT from './Form8995';
17 import W2 from './W2';
18 import Schedule1 from './Schedule1';
19 import Schedule2 from './Schedule2';
20 import Schedule3 from './Schedule3';
21 import ScheduleA from './ScheduleA';
22 import ScheduleD, { ScheduleDTaxWorksheet } from './ScheduleD';
23
24 export enum FilingStatus {
25 Single = 'S',
26 MarriedFilingSeparate = 'MFS',
27 MarriedFilingJoint = 'MFJ',
28 };
29
30 export interface Form1040Input {
31 filingStatus: FilingStatus;
32 };
33
34 export default class Form1040 extends Form<Form1040Input> {
35 readonly name = '1040';
36
37 taxableInterest(tr: TaxReturn): number {
38 const value = (new AccumulatorLine(Form1099INT, '1')).value(tr) +
39 (new AccumulatorLine(Form1099INT, '3')).value(tr);
40 return value;
41 }
42
43 qualifiedDividends(tr: TaxReturn): number {
44 return this.getValue(tr, '3a');
45 }
46
47 adjustedGrossIncome(tr: TaxReturn): number {
48 return this.getValue(tr, '7b') - this.getValue(tr, '8a');
49 }
50
51 deduction(tr: TaxReturn): number {
52 let deduction = 0;
53 const schedA = tr.findForm(ScheduleA);
54 if (schedA) {
55 deduction = schedA.getValue(tr, '17');
56 if (schedA.getValue(tr, '18')) {
57 return deduction;
58 }
59 }
60
61 return Math.max(deduction, tr.constants.standardDeduction[this.filingStatus]);
62 }
63
64 qualifiedBusinessIncomeDeduction(tr: TaxReturn): number {
65 const f8995 = tr.findForm(Form8995REIT);
66 if (f8995)
67 return f8995.getValue(tr, '39');
68 return 0;
69 }
70
71 capitalGainOrLoss(tr: TaxReturn): number {
72 const schedD = tr.findForm(ScheduleD);
73 if (!schedD)
74 return 0;
75
76 const l6 = schedD.getValue(tr, '16');
77 if (l6 >= 0)
78 return l6;
79 return schedD.getValue(tr, '21');
80 }
81
82 totalIncome(tr: TaxReturn): number {
83 return sumFormLines(tr, this, ['1', '2b', '3b', '4b', '4d', '5b', '6', '7a']);
84 }
85
86 taxableIncome(tr: TaxReturn): number {
87 return clampToZero(this.getValue(tr, '8b') - this.getValue(tr, '11a'));
88 }
89
90 tax(tr: TaxReturn): number {
91 // Not supported:
92 // Form 8814 (election to report child's interest or dividends)
93 // Form 4972 (relating to lump-sum distributions)
94
95 const schedD = tr.findForm(ScheduleD);
96 // Line 18 and 19 are not undefined or 0.
97 if (schedD && !schedD.getValue(tr, '20')) {
98 // Use ScheD tax worksheet;
99 const schedDtw = tr.findForm(ScheduleDTaxWorksheet);
100 if (schedDtw)
101 return schedDtw.getValue(tr, '47');
102 }
103
104 // If there are qualified dividends, use the QDCGTW.
105 if (this.qualifiedDividends(tr) > 0) {
106 const qdcgtw = tr.getForm(QDCGTaxWorksheet);
107 return qdcgtw.getValue(tr, '27');
108 }
109
110 // Otherwise, compute just on taxable income.
111 return computeTax(this.taxableIncome(tr), tr);
112 }
113
114 readonly lines = {
115 '1': new AccumulatorLine(W2, '1', 'Wages, salaries, tips, etc.'),
116 '2a': new ComputedLine((tr): number => {
117 const value = (new AccumulatorLine(Form1099INT, '8')).value(tr) +
118 (new AccumulatorLine(Form1099DIV, '11')).value(tr);
119 return value;
120 }, 'Tax-exempt interest'),
121 '2b': new ComputedLine((tr) => this.taxableInterest(tr), 'Taxable interest'),
122 '3a': new AccumulatorLine(Form1099DIV, '1b', 'Qualified dividends'),
123 '3b': new AccumulatorLine(Form1099DIV, '1a', 'Ordinary dividends'),
124 '4a': new ComputedLine((tr): number => {
125 const f1099Rs = tr.findForms(Form1099R).filter(f => !f.getValue(tr, '7').includes(Box7Code.G));
126 return sumLineOfForms(tr, f1099Rs, '1');
127 }),
128 '4b': new ComputedLine((tr): number => {
129 const f8606s = tr.findForms(Form8606);
130 return sumLineOfForms(tr, f8606s, '15c') + sumLineOfForms(tr, f8606s, '18');
131 }, 'IRA distributions, taxable amount'),
132 '4c': new UnsupportedLine('Pensions and annuities'),
133 '4d': new UnsupportedLine('Pensions and annuities, taxable amount'),
134 '5a': new UnsupportedLine('Social security benefits'),
135 '5b': new UnsupportedLine('Social security benefits, taxable amount'),
136 '6': new ComputedLine((tr) => this.capitalGainOrLoss(tr), 'Capital gain/loss'),
137 '7a': new ReferenceLine(Schedule1, '9', 'Other income from Schedule 1', 0),
138
139 '7b': new ComputedLine((tr) => this.totalIncome(tr), 'Total income'),
140
141 '8a': new ReferenceLine(Schedule1, '22', 'Adjustments to income', 0),
142
143 '8b': new ComputedLine((tr) => this.adjustedGrossIncome(tr), 'Adjusted gross income'),
144
145 '9': new ComputedLine((tr) => this.deduction(tr), 'Deduction'),
146
147 '10': new ComputedLine((tr) => this.qualifiedBusinessIncomeDeduction(tr), 'Qualified business income deduction'),
148
149 '11a': new ComputedLine((tr): number => {
150 return this.getValue(tr, '9') + this.getValue(tr, '10');
151 }),
152 '11b': new ComputedLine((tr) => this.taxableIncome(tr), 'Taxable income'),
153
154 '12a': new ComputedLine((tr) => this.tax(tr), 'Tax'),
155
156 '12b': new ComputedLine((tr): number => {
157 return this.getValue(tr, '12a') + tr.getForm(Schedule2).getValue(tr, '3');
158 }, 'Additional tax'),
159
160 '13a': new UnsupportedLine('Child tax credit'),
161
162 '13b': new ComputedLine((tr): number => {
163 let value: number = this.getValue(tr, '13a');
164 const sched3 = tr.findForm(Schedule3);
165 if (sched3)
166 value += undefinedToZero(sched3.getValue(tr, '7'));
167 return value;
168 }, 'Additional credits'),
169
170 '14': new ComputedLine((tr): number => {
171 return clampToZero(this.getValue(tr, '12b') - this.getValue(tr, '13b'));
172 }),
173
174 '15': new ReferenceLine(Schedule2, '10', undefined, 0),
175
176 '16': new ComputedLine((tr): number => {
177 return this.getValue(tr, '14') + this.getValue(tr, '15');
178 }, 'Total tax'),
179
180 '17': new ComputedLine((tr): number => {
181 const fedTaxWithheldBoxes = [
182 new AccumulatorLine(W2, '2'),
183 new AccumulatorLine(Form1099R, '4'),
184 new AccumulatorLine(Form1099DIV, '4'),
185 new AccumulatorLine(Form1099INT, '4'),
186 ];
187 const withholding: number[] = fedTaxWithheldBoxes.map(b => b.value(tr));
188
189 let additionalMedicare = 0;
190 const f8959 = tr.findForm(Form8959)
191 if (f8959) {
192 additionalMedicare = f8959.getValue(tr, '24');
193 }
194
195 return reduceBySum(withholding) + additionalMedicare;
196 }, 'Federal income tax withheld'),
197
198 '18a': new UnsupportedLine('Earned income credit (EIC)'),
199 '18b': new UnsupportedLine('Additional child tax credit. Attach Schedule 8812'),
200 '18c': new UnsupportedLine('American opportunity credit from Form 8863, line 8'),
201 '18d': new ReferenceLine(Schedule3, '14', undefined, 0),
202 '18e': new ComputedLine((tr): number => {
203 return sumFormLines(tr, this, ['18a', '18b', '18c', '18d']);
204 }),
205
206 '19': new ComputedLine((tr): number => {
207 return this.getValue(tr, '17') + this.getValue(tr, '18e');
208 }, 'Total payments'),
209
210 '20': new ComputedLine((tr): number => {
211 return clampToZero(this.getValue(tr, '19') - this.getValue(tr, '16'));
212 }, 'Amount overpaid'),
213
214 '23': new ComputedLine((tr): number => {
215 return clampToZero(this.getValue(tr, '16') - this.getValue(tr, '19'));
216 }, 'Amount you owe'),
217 }
218
219 get filingStatus(): FilingStatus {
220 return this.getInput('filingStatus');
221 }
222 };
223
224 export function computeTax(income: number, tr: TaxReturn): number {
225 const f1040 = tr.getForm(Form1040);
226 const taxBrackets = tr.constants.taxBrackets[f1040.filingStatus];
227
228 let i = 0;
229 while (taxBrackets[i][0] < income)
230 ++i;
231
232 const bracket = taxBrackets[i];
233 const bracketStart = i == 0 ? 0 : taxBrackets[i - 1][0];
234
235 return ((income - bracketStart) * bracket[1]) + bracket[2];
236 };
237
238 export class QDCGTaxWorksheet extends Form {
239 readonly name = 'QDCG Tax Worksheet';
240
241 readonly lines = {
242 '1': new SymbolicLine(Form1040, 'taxableIncome', 'Taxable income'),
243 '2': new ReferenceLine(Form1040, '3a', 'Qualified dividends'),
244 '3': new ComputedLine((tr): number => {
245 const schedD = tr.findForm(ScheduleD);
246 if (schedD)
247 return clampToZero(Math.min(schedD.getValue(tr, '15'), schedD.getValue(tr, '16')));
248 return tr.getForm(Form1040).capitalGainOrLoss(tr);
249 }),
250 '4': new ComputedLine((tr): number => this.getValue(tr, '2') + this.getValue(tr, '3')),
251 '5': new UnsupportedLine('Form 4952@4g (Investment interest expense deduction)'),
252 '6': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '4') - this.getValue(tr, '5'))),
253 '7': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '1') - this.getValue(tr, '6'))),
254 '8': new ComputedLine((tr): number => {
255 const fs = tr.getForm(Form1040).filingStatus;
256 return tr.constants.capitalGains.rate0MaxIncome[fs];
257 }),
258 '9': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '8'))),
259 '10': new ComputedLine((tr): number => Math.min(this.getValue(tr, '7'), this.getValue(tr, '9'))),
260 '11': new ComputedLine((tr): number => {
261 return this.getValue(tr, '9') - this.getValue(tr, '10');
262 }, 'Amount taxed at 0%'),
263 '12': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '6'))),
264 '13': new ReferenceLine(QDCGTaxWorksheet as any, '11'),
265 '14': new ComputedLine((tr): number => this.getValue(tr, '12') - this.getValue(tr, '13')),
266 '15': new ComputedLine((tr): number => {
267 const fs = tr.getForm(Form1040).filingStatus;
268 return tr.constants.capitalGains.rate15MaxIncome[fs];
269 }),
270 '16': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '15'))),
271 '17': new ComputedLine((tr): number => this.getValue(tr, '7') + this.getValue(tr, '11')),
272 '18': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '16') - this.getValue(tr, '17'))),
273 '19': new ComputedLine((tr): number => Math.min(this.getValue(tr, '14'), this.getValue(tr, '18'))),
274 '20': new ComputedLine((tr): number => {
275 return this.getValue(tr, '19') * 0.15;
276 }, 'Amount taxed at 15%'),
277 '21': new ComputedLine((tr): number => this.getValue(tr, '11') + this.getValue(tr, '19')),
278 '22': new ComputedLine((tr): number => this.getValue(tr, '12') - this.getValue(tr, '21')),
279 '23': new ComputedLine((tr): number => {
280 return this.getValue(tr, '22') * 0.20;
281 }, 'Amount taxed at 20%'),
282 '24': new ComputedLine((tr): number => {
283 return computeTax(this.getValue(tr, '7'), tr);
284 }, 'Tax on line 7'),
285 '25': new ComputedLine((tr): number => {
286 return this.getValue(tr, '20') +
287 this.getValue(tr, '23') +
288 this.getValue(tr, '24');
289 }),
290 '26': new ComputedLine((tr): number => {
291 return computeTax(this.getValue(tr, '1'), tr);
292 }, 'Tax on line 1'),
293 '27': new ComputedLine((tr): number => {
294 return Math.min(this.getValue(tr, '25'), this.getValue(tr, '26'));
295 }, 'Tax on all taxable income')
296 };
297 };