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, 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, QDCGTaxWorksheet, 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 const limit = filingStatus == FilingStatus.MarriedFilingSeparate ? -1500 : -3000;
79 return Math.max(l16, limit);
80 }, 'Net capital loss'),
82 '22': new ComputedLine((tr): boolean => {
83 return tr.getForm(Form1040).getValue(tr, '3a') > 0;
84 }, 'Need QD/CG Tax Worksheet'),
88 export class ScheduleDTaxWorksheet extends Form {
89 readonly name = 'Schedule D Tax Worksheet';
92 '1': new ReferenceLine(Form1040, '11b', 'Taxable income'),
93 '2': new ReferenceLine(Form1040, '3a', 'Qualified dividends'),
94 '3': new UnsupportedLine('Form 4952@4g'),
95 '4': new UnsupportedLine('Form 4952@4e'),
96 '5': new ComputedLine((tr): number => 0),
97 '6': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '2') - this.getValue(tr, '5'))),
98 '7': new ComputedLine((tr): number => {
99 const schedD = tr.getForm(ScheduleD);
100 return Math.min(schedD.getValue(tr, '15'), schedD.getValue(tr, '16'));
102 '8': new ComputedLine((tr): number => {
103 return Math.min(this.getValue(tr, '3'), this.getValue(tr, '4'));
105 '9': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '7') - this.getValue(tr, '8'))),
106 '10': new ComputedLine((tr): number => this.getValue(tr, '6') + this.getValue(tr, '9')),
107 '11': new ComputedLine((tr): number => {
108 const schedD = tr.getForm(ScheduleD);
109 return schedD.getValue(tr, '18') + schedD.getValue(tr, '19');
110 }, '28% gains and unrecaptured gains'),
111 '12': new ComputedLine((tr): number => Math.min(this.getValue(tr, '9'), this.getValue(tr, '11'))),
112 '13': new ComputedLine((tr): number => this.getValue(tr, '10') - this.getValue(tr, '12')),
113 '14': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '1') - this.getValue(tr, '13'))),
114 '15': new ComputedLine((tr): number => {
115 const fs = tr.getForm(Form1040).filingStatus;
116 return tr.constants.capitalGains.rate0MaxIncome[fs];
118 '16': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '15'))),
119 '17': new ComputedLine((tr): number => Math.min(this.getValue(tr, '14'), this.getValue(tr, '16'))),
120 '18': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '1') - this.getValue(tr, '10'))),
121 '19': new ComputedLine((tr): number => {
122 const fs = tr.getForm(Form1040).filingStatus;
123 const threshold = tr.constants.qualifiedBusinessIncomeDeductionThreshold[fs];
124 return Math.min(this.getValue(tr, '1'), threshold);
126 '20': new ComputedLine((tr): number => Math.min(this.getValue(tr, '14'), this.getValue(tr, '19'))),
127 '21': new ComputedLine((tr): number => Math.max(this.getValue(tr, '18'), this.getValue(tr, '20'))),
128 '22': new ComputedLine((tr): number => this.getValue(tr, '16') - this.getValue(tr, '17'), 'Amount taxed at 0%'),
129 '23': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '13'))),
130 '24': new ReferenceLine(ScheduleDTaxWorksheet as any, '22'),
131 '25': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '23') - this.getValue(tr, '24'))),
132 '26': new ComputedLine((tr): number => {
133 const fs = tr.getForm(Form1040).filingStatus;
134 return tr.constants.capitalGains.rate15MaxIncome[fs];
136 '27': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '26'))),
137 '28': new ComputedLine((tr): number => this.getValue(tr, '21') + this.getValue(tr, '22')),
138 '29': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '27') - this.getValue(tr, '28'))),
139 '30': new ComputedLine((tr): number => Math.min(this.getValue(tr, '25'), this.getValue(tr, '29')), 'Amount taxed at 15%'),
140 '31': new ComputedLine((tr): number => this.getValue(tr, '30') * Literal(0.15), '15% Tax'),
141 '32': new ComputedLine((tr): number => this.getValue(tr, '24') + this.getValue(tr, '30')),
142 '33': new ComputedLine((tr): number => this.getValue(tr, '23') - this.getValue(tr, '32'), 'Amount taxed at 20%'),
143 '34': new ComputedLine((tr): number => this.getValue(tr, '33') * Literal(0.20), '20% Tax'),
144 '35': new ComputedLine((tr): number => {
145 const schedD = tr.getForm(ScheduleD);
146 return Math.min(this.getValue(tr, '9'), schedD.getValue(tr, '19'));
148 '36': new ComputedLine((tr): number => this.getValue(tr, '10') + this.getValue(tr, '21')),
149 '37': new ReferenceLine(ScheduleDTaxWorksheet as any, '1'),
150 '38': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '36') - this.getValue(tr, '37'))),
151 '39': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '35') - this.getValue(tr, '38'))),
152 '40': new ComputedLine((tr): number => this.getValue(tr, '39') * Literal(0.25), 'Tax on unrecaptured gains'),
153 '41': new ComputedLine((tr): number => {
154 const schedD = tr.getForm(ScheduleD);
155 if (schedD.getValue(tr, '18'))
156 throw new UnsupportedFeatureError('28% Gain unsupported');
159 '42': new ComputedLine((tr): number => {
160 if (!tr.getForm(ScheduleD).getValue(tr, '18'))
162 return this.getValue(tr, '1') - this.getValue(tr, '41');
164 '43': new ComputedLine((tr): number => {
165 if (!tr.getForm(ScheduleD).getValue(tr, '18'))
167 return this.getValue(tr, '42') * Literal(0.28);
169 '44': new ComputedLine((tr): number => {
170 const income = this.getValue(tr, '21');
171 return computeTax(income, tr);
172 }, 'Nominal rate tax'),
173 '45': new ComputedLine((tr): number => {
174 return sumFormLines(tr, this, ['31', '34', '40', '43', '44']);
175 }, 'Schedule D tax'),
176 '46': new ComputedLine((tr): number => {
177 const income = this.getValue(tr, '1');
178 return computeTax(income, tr);
180 '47': new ComputedLine((tr): number => Math.min(this.getValue(tr, '45'), this.getValue(tr, '46')), 'Tax on all taxable income'),