Add license information.
[ustaxlib.git] / src / fed2019 / Form8949.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, InputLine, ComputedLine, sumLineOfForms } from '../core/Line';
8
9 import Form1099B, { GainType } from './Form1099B';
10
11 export enum Form8949Box {
12 A = 'A', // Short-term transactions reported on Form(s) 1099-B showing basis was reported to the IRS
13 B = 'B', // Short-term transactions reported on Form(s) 1099-B showing basis wasn’t reported to the IRS
14 C = 'C', // Short-term transactions not reported to you on Form 1099-B
15 D = 'D', // Long-term transactions reported on Form(s) 1099-B showing basis was reported to the IRS
16 E = 'E', // Long-term transactions reported on Form(s) 1099-B showing basis wasn’t reported to the IRS
17 F = 'F', // Long-term transactions not reported to you on Form 1099-B
18 };
19
20 export interface Adjustment {
21 entry: Form1099B;
22 code: string;
23 amount: number;
24 };
25
26 export interface Form8949Input {
27 adjustments?: Adjustment[];
28 };
29
30 export interface Form8949Total {
31 proceeds: number;
32 costBasis: number;
33 adjustments: number;
34 gainOrLoss: number;
35 };
36
37 function matching1099Bs(tr: TaxReturn, box: Form8949Box): Form1099B[] {
38 return tr.findForms(Form1099B).filter(f => {
39 const gainType: GainType = f.getValue(tr, '2');
40 const basisReported: boolean = f.getValue(tr, '12');
41
42 switch (box) {
43 case Form8949Box.A:
44 return gainType == GainType.ShortTerm && basisReported;
45 case Form8949Box.B:
46 return gainType == GainType.ShortTerm && !basisReported;
47 case Form8949Box.D:
48 return gainType == GainType.LongTerm && basisReported;
49 case Form8949Box.E:
50 return gainType == GainType.LongTerm && !basisReported;
51 };
52
53 return false;
54 });
55 }
56
57 class Form8949Line extends Line<Form8949Total> {
58 private _box: Form8949Box;
59 private _line: keyof Form1099B['lines'];
60
61 constructor(box: Form8949Box) {
62 super(`Form 8949 Box ${box} Total`);
63 this._box = box;
64 }
65
66 value(tr: TaxReturn): Form8949Total {
67 const f1099bs = matching1099Bs(tr, this._box);
68 const proceeds = sumLineOfForms(tr, f1099bs, '1d');
69 const costBasis = sumLineOfForms(tr, f1099bs, '1e');
70 const f8949 = tr.getForm(Form8949);
71 const adjustments = !f8949.hasInput('adjustments') ? 0 :
72 f8949.getInput('adjustments')
73 .filter(a => f1099bs.includes(a.entry))
74 .reduce((acc, curr) => acc + curr.amount, 0);
75 return {
76 proceeds,
77 costBasis,
78 adjustments,
79 gainOrLoss: proceeds - costBasis + adjustments,
80 };
81 }
82 };
83
84 export default class Form8949 extends Form<Form8949['_lines'], Form8949Input> {
85 readonly name = '8949';
86
87 readonly supportsMultipleCopies = true;
88
89 protected readonly _lines = {
90 'boxA': new Form8949Line(Form8949Box.A),
91 'boxB': new Form8949Line(Form8949Box.B),
92 'boxC': new Form8949Line(Form8949Box.C),
93 'boxD': new Form8949Line(Form8949Box.D),
94 'boxE': new Form8949Line(Form8949Box.E),
95 'boxF': new Form8949Line(Form8949Box.F),
96 };
97 };