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, { FormClass, isFormT } from './Form';
7 import Person, { Relation } from './Person';
8 import { NotFoundError, InconsistencyError, UnsupportedFeatureError } from './Errors';
10 export default abstract class TaxReturn {
11 private _people: Person[] = [];
12 private _forms: Form[] = [];
14 abstract readonly constants;
16 abstract get year(): number;
18 abstract get includeJointPersonForms(): boolean;
21 return [...this._forms];
24 addPerson(person: Person) {
25 if (person.relation == Relation.Dependent) {
26 throw new UnsupportedFeatureError('Dependents are not supported');
28 if (person.relation == Relation.Self || person.relation == Relation.Spouse) {
29 const others = this._people.filter(p => p.relation == person.relation);
30 if (others.length > 0) {
31 throw new InconsistencyError('Cannot have more than one Self or Spouse');
34 this._people.push(person);
37 getPerson(name: RegExp | string): Person {
38 const people = this._people.filter(p => p.name.search(name) !== -1);
39 if (people.length != 1) {
40 throw new Error(`Person ${name} not found or too imprecise`);
46 if (!form.supportsMultipleCopies) {
47 const other = this.findForms(form.constructor as FormClass<Form>);
48 if (other.length > 0) {
49 throw new InconsistencyError(`Cannot have more than one type of form ${form.name}`);
53 this._forms.push(form);
56 findForm<T extends Form>(cls: FormClass<T>): T | null {
57 const forms = this.findForms(cls);
58 if (forms.length == 0)
61 throw new InconsistencyError(`Form ${forms[0].name} has multiple copies`);
65 findForms<T extends Form>(cls: FormClass<T>): T[] {
66 const forms: T[] = this._forms
67 .filter((form: Form): form is T => isFormT(form, cls))
68 .filter((form: T) => {
69 const person = form.person();
70 if (person === undefined)
73 if (person == Person.joint && this.includeJointPersonForms)
76 return this._people.includes(form.person());
81 getForm<T extends Form>(cls: FormClass<T>): T {
82 const form = this.findForm(cls);
84 throw new NotFoundError(`No form ${cls.name}`);