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
6 import { Form, Person, TaxReturn } from '../core';
7 import { Line, AccumulatorLine, ComputedLine, ReferenceLine, SymbolicLine, UnsupportedLine, sumFormLines, sumLineOfForms } from '../core/Line';
8 import { Literal, clampToZero } from '../core/Math';
9 import { NotFoundError, UnsupportedFeatureError } from '../core/Errors';
11 import Form8949, { Form8949Box } from './Form8949';
12 import Form1099DIV from './Form1099DIV';
13 import Form1040, { FilingStatus, computeTax } from './Form1040';
15 export default class ScheduleD extends Form {
16 readonly name = 'Schedule D';
19 // 1a not supported (Totals for all short-term transactions reported on Form 1099-B for which basis was reported to the IRS and for which you have no adjustments)
20 '4': new UnsupportedLine(), // Short-term gain from Form 6252 and short-term gain or (loss) from Forms 4684, 6781, and 8824
21 '5': new UnsupportedLine(), // Net short-term gain or (loss) from partnerships, S corporations, estates, and trusts from Schedule(s) K-1
22 '6': new UnsupportedLine(), // Short-term capital loss carryover. Enter the amount, if any, from line 8 of your Capital Loss Carryover Worksheet in the instructions
24 '7': new ComputedLine((tr): number => {
25 // 1-3 are computed by Form8949.
26 let value = sumFormLines(tr, this, ['4', '5', '6']);
27 const f8949 = tr.getForm(Form8949);
28 value += f8949.getValue(tr, 'boxA').gainOrLoss;
29 value += f8949.getValue(tr, 'boxB').gainOrLoss;
30 value += f8949.getValue(tr, 'boxC').gainOrLoss;
32 }, 'Net short-term capital gain or loss'),
34 // 8a is not supported.
36 '11': new UnsupportedLine(), // Gain from Form 4797, Part I; long-term gain from Forms 2439 and 6252; and long-term gain or (loss) from Forms 4684, 6781, and 8824
37 '12': new UnsupportedLine(), // Net long-term gain or (loss) from partnerships, S corporations, estates, and trusts from Schedule(s) K-1
38 '13': new AccumulatorLine(Form1099DIV, '2a', 'Capital gain distributions'),
39 '14': new UnsupportedLine('Long-term capital loss carryover'),
41 '15': new ComputedLine((tr): number => {
42 let value = sumFormLines(tr, this, ['11', '12', '13', '14']);
43 const f8949 = tr.getForm(Form8949);
44 value += f8949.getValue(tr, 'boxD').gainOrLoss;
45 value += f8949.getValue(tr, 'boxE').gainOrLoss;
46 value += f8949.getValue(tr, 'boxF').gainOrLoss;
48 }, 'Net long-term capital gain or loss'),
50 '16': new ComputedLine((tr): number => {
51 return this.getValue(tr, '7') + this.getValue(tr, '15');
52 // If value is a gain, enter on 1040/6 and goto 17.
53 // If value is a loss, goto 21 and 22.
54 // If value is zero, enter 0 on 1040/6 and goto 22.
55 }, 'Total capital gain or loss'),
57 '17': new ComputedLine((tr): boolean => {
58 return this.getValue(tr, '15') > 0 && this.getValue(tr, '16') > 0;
61 }, 'Both ST and LT are gains'),
63 '18': new UnsupportedLine('28% Rate Gain Worksheet Value (Qualified Small Business Stock or collectibles.)'),
65 '19': new UnsupportedLine('Unrecaptured Section 1250 Gain Worksheet'),
67 '20': new ComputedLine((tr): boolean | undefined => {
68 const l18 = this.getValue(tr, '18');
69 const l19 = this.getValue(tr, '19');
70 return (l18 === 0 || l18 === undefined) || (l19 === 0 || l19 === undefined);
71 }, 'Line 18 and 19 both 0 or blank?'),
73 '21': new ComputedLine((tr): number | undefined => {
74 const l16 = this.getValue(tr, '16');
77 const filingStatus = tr.getForm(Form1040).filingStatus;
78 return Math.max(l16, tr.constants.capitalLossLimit[filingStatus]);
79 }, 'Net capital loss'),
81 '22': new ComputedLine((tr): boolean => {
82 return tr.getForm(Form1040).qualifiedDividends(tr) > 0;
83 }, 'Need QD/CG Tax Worksheet'),
87 export class ScheduleDTaxWorksheet extends Form {
88 readonly name = 'Schedule D Tax Worksheet';
91 '1': new SymbolicLine(Form1040, 'taxableIncome', 'Taxable income'),
92 '2': new SymbolicLine(Form1040, 'qualifiedDividends', 'Qualified dividends'),
93 '3': new UnsupportedLine('Form 4952@4g'),
94 '4': new UnsupportedLine('Form 4952@4e'),
95 '5': new ComputedLine((tr): number => 0),
96 '6': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '2') - this.getValue(tr, '5'))),
97 '7': new ComputedLine((tr): number => {
98 const schedD = tr.getForm(ScheduleD);
99 return Math.min(schedD.getValue(tr, '15'), schedD.getValue(tr, '16'));
101 '8': new ComputedLine((tr): number => {
102 return Math.min(this.getValue(tr, '3'), this.getValue(tr, '4'));
104 '9': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '7') - this.getValue(tr, '8'))),
105 '10': new ComputedLine((tr): number => this.getValue(tr, '6') + this.getValue(tr, '9')),
106 '11': new ComputedLine((tr): number => {
107 const schedD = tr.getForm(ScheduleD);
108 return schedD.getValue(tr, '18') + schedD.getValue(tr, '19');
109 }, '28% gains and unrecaptured gains'),
110 '12': new ComputedLine((tr): number => Math.min(this.getValue(tr, '9'), this.getValue(tr, '11'))),
111 '13': new ComputedLine((tr): number => this.getValue(tr, '10') - this.getValue(tr, '12')),
112 '14': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '1') - this.getValue(tr, '13'))),
113 '15': new ComputedLine((tr): number => {
114 const fs = tr.getForm(Form1040).filingStatus;
115 return tr.constants.capitalGains.rate0MaxIncome[fs];
117 '16': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '15'))),
118 '17': new ComputedLine((tr): number => Math.min(this.getValue(tr, '14'), this.getValue(tr, '16'))),
119 '18': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '1') - this.getValue(tr, '10'))),
120 '19': new ComputedLine((tr): number => {
121 const fs = tr.getForm(Form1040).filingStatus;
122 const threshold = tr.constants.qualifiedBusinessIncomeDeductionThreshold[fs];
123 return Math.min(this.getValue(tr, '1'), threshold);
125 '20': new ComputedLine((tr): number => Math.min(this.getValue(tr, '14'), this.getValue(tr, '19'))),
126 '21': new ComputedLine((tr): number => Math.max(this.getValue(tr, '18'), this.getValue(tr, '20'))),
127 '22': new ComputedLine((tr): number => this.getValue(tr, '16') - this.getValue(tr, '17'), 'Amount taxed at 0%'),
128 '23': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '13'))),
129 '24': new ReferenceLine(ScheduleDTaxWorksheet as any, '22'),
130 '25': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '23') - this.getValue(tr, '24'))),
131 '26': new ComputedLine((tr): number => {
132 const fs = tr.getForm(Form1040).filingStatus;
133 return tr.constants.capitalGains.rate15MaxIncome[fs];
135 '27': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '26'))),
136 '28': new ComputedLine((tr): number => this.getValue(tr, '21') + this.getValue(tr, '22')),
137 '29': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '27') - this.getValue(tr, '28'))),
138 '30': new ComputedLine((tr): number => Math.min(this.getValue(tr, '25'), this.getValue(tr, '29')), 'Amount taxed at 15%'),
139 '31': new ComputedLine((tr): number => this.getValue(tr, '30') * Literal(0.15), '15% Tax'),
140 '32': new ComputedLine((tr): number => this.getValue(tr, '24') + this.getValue(tr, '30')),
141 '33': new ComputedLine((tr): number => this.getValue(tr, '23') - this.getValue(tr, '32'), 'Amount taxed at 20%'),
142 '34': new ComputedLine((tr): number => this.getValue(tr, '33') * Literal(0.20), '20% Tax'),
143 '35': new ComputedLine((tr): number => {
144 const schedD = tr.getForm(ScheduleD);
145 return Math.min(this.getValue(tr, '9'), schedD.getValue(tr, '19'));
147 '36': new ComputedLine((tr): number => this.getValue(tr, '10') + this.getValue(tr, '21')),
148 '37': new ReferenceLine(ScheduleDTaxWorksheet as any, '1'),
149 '38': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '36') - this.getValue(tr, '37'))),
150 '39': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '35') - this.getValue(tr, '38'))),
151 '40': new ComputedLine((tr): number => this.getValue(tr, '39') * Literal(0.25), 'Tax on unrecaptured gains'),
152 '41': new ComputedLine((tr): number => {
153 const schedD = tr.getForm(ScheduleD);
154 if (schedD.getValue(tr, '18'))
155 throw new UnsupportedFeatureError('28% Gain unsupported');
158 '42': new ComputedLine((tr): number => {
159 if (!tr.getForm(ScheduleD).getValue(tr, '18'))
161 return this.getValue(tr, '1') - this.getValue(tr, '41');
163 '43': new ComputedLine((tr): number => {
164 if (!tr.getForm(ScheduleD).getValue(tr, '18'))
166 return this.getValue(tr, '42') * Literal(0.28);
168 '44': new ComputedLine((tr): number => {
169 const income = this.getValue(tr, '21');
170 return computeTax(income, tr);
171 }, 'Nominal rate tax'),
172 '45': new ComputedLine((tr): number => {
173 return sumFormLines(tr, this, ['31', '34', '40', '43', '44']);
174 }, 'Schedule D tax'),
175 '46': new ComputedLine((tr): number => {
176 const income = this.getValue(tr, '1');
177 return computeTax(income, tr);
179 '47': new ComputedLine((tr): number => Math.min(this.getValue(tr, '45'), this.getValue(tr, '46')), 'Tax on all taxable income'),