Add license information.
[ustaxlib.git] / src / fed2019 / ScheduleD.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, Person, TaxReturn } from '../core';
7 import { Line, AccumulatorLine, ComputedLine, ReferenceLine, sumLineOfForms } from '../core/Line';
8 import { clampToZero } from '../core/Math';
9 import { UnsupportedFeatureError } from '../core/Errors';
10
11 import Form8949, { Form8949Box } from './Form8949';
12 import Form1099DIV from './Form1099DIV';
13 import Form1040, { FilingStatus, computeTax } from './Form1040';
14
15 export default class ScheduleD extends Form<ScheduleD['_lines']> {
16 readonly name = 'Schedule D';
17
18 protected readonly _lines = {
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 is not supported (Short-term gain from Form 6252 and short-term gain or (loss) from Forms 4684, 6781, and 8824)
21 // 5 is not supported (Net short-term gain or (loss) from partnerships, S corporations, estates, and trusts from Schedule(s) K-1)
22 // 6 is not supported (Short-term capital loss carryover. Enter the amount, if any, from line 8 of your Capital Loss Carryover Worksheet in the instructions)
23
24 '7': new ComputedLine((tr): number => {
25 // 1-3 are computed by Form8949.
26 // 4-6 should be included.
27 const f8949 = tr.getForm(Form8949);
28 return f8949.getValue(tr, 'boxA').gainOrLoss +
29 f8949.getValue(tr, 'boxB').gainOrLoss +
30 f8949.getValue(tr, 'boxC').gainOrLoss;
31 }, 'Net short-term capital gain or (loss)'),
32
33 // 8a is not supported.
34
35 // 11 is not supported (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)
36 // 12 is not supported (Net long-term gain or (loss) from partnerships, S corporations, estates, and trusts from Schedule(s) K-1)
37
38 '13': new AccumulatorLine(Form1099DIV, '2a', 'Capital gain distributions'),
39
40 // 14 is not supported (Long-term capital loss carryover. Enter the amount, if any, from line 13 of your Capital Loss Carryover Worksheet in the instructions)
41
42 '15': new ComputedLine((tr): number => {
43 // 11-14 should be included.
44 const f8949 = tr.getForm(Form8949);
45 return f8949.getValue(tr, 'boxD').gainOrLoss +
46 f8949.getValue(tr, 'boxE').gainOrLoss +
47 f8949.getValue(tr, 'boxF').gainOrLoss +
48 this.getValue(tr, '13');
49 }, 'Net long-term capital gain or (loss)'),
50
51 '16': new ComputedLine((tr): number => {
52 return this.getValue(tr, '7') + this.getValue(tr, '15');
53 }),
54
55 '17': new ComputedLine((tr): boolean => {
56 return this.getValue(tr, '15') > 0 && this.getValue(tr, '16') > 0;
57 }, 'Both ST and LT are gains'),
58
59 '18': new ComputedLine((tr): number | undefined => {
60 if (!this.getValue(tr, '17') || this.getValue(tr, '16') <= 0)
61 return undefined;
62 // Not supported - only for gains on Qualified Small Business Stock or collectibles.
63 return 0;
64 }, '28% Rate Gain Worksheet Value'),
65
66 // 19 is not supported (Unrecaptured Section 1250 Gain Worksheet)
67
68 '20': new ComputedLine((tr): boolean | undefined => {
69 if (!this.getValue(tr, '17') || this.getValue(tr, '16') <= 0)
70 return undefined;
71 const l18 = this.getValue(tr, '18');
72 const l19 = undefined; //this.getValue(tr, '19');
73 return (l18 === 0 || l18 === undefined) || (l19 === 0 || l19 === undefined);
74 }, 'Line 18 and 19 both 0 or blank?'),
75
76 '21': new ComputedLine((tr): number | undefined => {
77 if (!this.getValue(tr, '17') || !this.getValue(tr, '20'))
78 return undefined;
79 const filingStatus = tr.getForm(Form1040).filingStatus;
80 const limit = filingStatus == FilingStatus.MarriedFilingSeparate ? -1500 : -3000;
81 return Math.min(this.getValue(tr, '16'), limit);
82 }, 'Net capital loss'),
83 };
84 };
85
86 export class ScheduleDTaxWorksheet extends Form<ScheduleDTaxWorksheet['_lines']> {
87 readonly name = 'Schedule D Tax Worksheet';
88
89 protected readonly _lines = {
90 '1': new ReferenceLine(Form1040, '11b'),
91 '2': new ReferenceLine(Form1040, '3a'),
92 // TODO 3 - form 4952
93 // TODO 4 - 4952
94 '5': new ComputedLine((tr): number => 0),
95 '6': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '2') - this.getValue(tr, '5'))),
96 '7': new ComputedLine((tr): number => {
97 const schedD = tr.getForm(ScheduleD);
98 return Math.min(schedD.getValue(tr, '15'), schedD.getValue(tr, '16'));
99 }),
100 '8': new ComputedLine((tr): number => {
101 return 0;
102 // return Math.min(this.getValue(tr, '3'), this.getValue(tr, '4'));
103 }),
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 // TODO - line 19 is not supported.
109 return Math.min(schedD.getValue(tr, '18'), Infinity); //schedD.getValue(tr, '19'));
110 }),
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 switch (tr.getForm(Form1040).filingStatus) {
116 case FilingStatus.Single:
117 case FilingStatus.MarriedFilingSeparate:
118 return 39375;
119 case FilingStatus.MarriedFilingJoint:
120 return 78750;
121 }
122 }),
123 '16': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '15'))),
124 '17': new ComputedLine((tr): number => Math.min(this.getValue(tr, '14'), this.getValue(tr, '16'))),
125 '18': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '1') - this.getValue(tr, '10'))),
126 '19': new ComputedLine((tr): number => {
127 let threshold: number;
128 switch (tr.getForm(Form1040).filingStatus) {
129 case FilingStatus.Single:
130 case FilingStatus.MarriedFilingSeparate:
131 threshold = 160725;
132 break;
133 case FilingStatus.MarriedFilingJoint:
134 threshold = 321450;
135 break;
136 }
137 return Math.min(this.getValue(tr, '1'), threshold);
138 }),
139 '20': new ComputedLine((tr): number => Math.min(this.getValue(tr, '14'), this.getValue(tr, '19'))),
140 '21': new ComputedLine((tr): number => Math.max(this.getValue(tr, '18'), this.getValue(tr, '20'))),
141 '22': new ComputedLine((tr): number => this.getValue(tr, '16') - this.getValue(tr, '17')),
142 '23': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '13'))),
143 '24': new ReferenceLine(ScheduleDTaxWorksheet as any, '22'),
144 '25': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '23') - this.getValue(tr, '24'))),
145 '26': new ComputedLine((tr): number => {
146 switch (tr.getForm(Form1040).filingStatus) {
147 case FilingStatus.Single:
148 return 434550;
149 case FilingStatus.MarriedFilingSeparate:
150 return 244425;
151 case FilingStatus.MarriedFilingJoint:
152 return 488850;
153 }
154 }),
155 '27': new ComputedLine((tr): number => Math.min(this.getValue(tr, '1'), this.getValue(tr, '26'))),
156 '28': new ComputedLine((tr): number => this.getValue(tr, '21') + this.getValue(tr, '22')),
157 '29': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '27') - this.getValue(tr, '28'))),
158 '30': new ComputedLine((tr): number => Math.min(this.getValue(tr, '25'), this.getValue(tr, '29'))),
159 '31': new ComputedLine((tr): number => this.getValue(tr, '30') * 0.15),
160 '32': new ComputedLine((tr): number => this.getValue(tr, '24') + this.getValue(tr, '30')),
161 '33': new ComputedLine((tr): number => this.getValue(tr, '23') - this.getValue(tr, '32')),
162 '34': new ComputedLine((tr): number => this.getValue(tr, '33') * 0.20),
163 '35': new ComputedLine((tr): number => {
164 const schedD = tr.getForm(ScheduleD);
165 // TODO - line 19 is not supported.
166 return Math.min(this.getValue(tr, '9'), Infinity); //schedD.getValue(tr, '19'));
167 }),
168 '36': new ComputedLine((tr): number => this.getValue(tr, '10') + this.getValue(tr, '21')),
169 '37': new ReferenceLine(ScheduleDTaxWorksheet as any, '1'),
170 '38': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '36') - this.getValue(tr, '37'))),
171 '39': new ComputedLine((tr): number => clampToZero(this.getValue(tr, '35') - this.getValue(tr, '38'))),
172 '40': new ComputedLine((tr): number => this.getValue(tr, '39') * 0.25),
173 '41': new ComputedLine((tr): number => {
174 const schedD = tr.getForm(ScheduleD);
175 if (schedD.getValue(tr, '18'))
176 throw new UnsupportedFeatureError('28% Gain unsupported');
177 return 0;
178 }),
179 '42': new ComputedLine((tr): number => {
180 if (!tr.getForm(ScheduleD).getValue(tr, '18'))
181 return 0;
182 return this.getValue(tr, '1') - this.getValue(tr, '41');
183 }),
184 '43': new ComputedLine((tr): number => {
185 if (!tr.getForm(ScheduleD).getValue(tr, '18'))
186 return 0;
187 return this.getValue(tr, '42') * 0.28;
188 }),
189 '44': new ComputedLine((tr): number => {
190 const income = this.getValue(tr, '21');
191 return computeTax(income, tr.getForm(Form1040).filingStatus);
192 }),
193 '45': new ComputedLine((tr): number => {
194 return this.getValue(tr, '31') +
195 this.getValue(tr, '34') +
196 this.getValue(tr, '40') +
197 this.getValue(tr, '43') +
198 this.getValue(tr, '44');
199 }),
200 '46': new ComputedLine((tr): number => {
201 const income = this.getValue(tr, '1');
202 return computeTax(income, tr.getForm(Form1040).filingStatus);
203 }),
204 '47': new ComputedLine((tr): number => Math.min(this.getValue(tr, '45'), this.getValue(tr, '46'))),
205 };
206 };