Start modeling Line, Form and their relation to TaxReturn.
[ustaxlib.git] / src / TaxReturn.ts
1 import Form from './Form';
2 import { Person, Relation } from './Person';
3 import { NotFoundError, InconsistencyError, UnsupportedFeatureError } from './Errors';
4
5 export default class TaxReturn {
6 private _year: number;
7 private _people: Person[] = [];
8 private _forms: Form[] = [];
9
10 constructor(year: number) {
11 this._year = year;
12 }
13
14 get year(): number {
15 return this._year;
16 }
17
18 addPerson(person: Person) {
19 if (person.relation == Relation.Dependent) {
20 throw new UnsupportedFeatureError('Dependents are not supported');
21 }
22 if (person.relation == Relation.Self || person.relation == Relation.Spouse) {
23 const others = this._people.filter(p => p.relation == person.relation);
24 if (others.length > 0) {
25 throw new InconsistencyError('Cannot have more than one Self or Spouse');
26 }
27 }
28 this._people.push(person);
29 }
30
31 getPerson(name: RegExp | string): Person {
32 const people = this._people.filter(p => p.name.search(name) !== -1);
33 if (people.length != 1) {
34 throw new Error(`Person ${name} not found or too imprecise`);
35 }
36 return people[0];
37 }
38
39 addForm(form: Form) {
40 if (!form.allowMultipleCopies) {
41 const other = this.getForms(form.name);
42 if (other.length > 0) {
43 throw new InconsistencyError(`Cannot have more than one type of form ${form.name}`);
44 }
45 }
46 this._forms.push(form);
47 }
48
49 getForm(name: string): Form {
50 const forms = this.getForms(name);
51 if (forms.length == 0) {
52 throw new NotFoundError(`No form named ${name}`);
53 }
54 if (forms.length > 1) {
55 throw new InconsistencyError(`More than 1 form named ${name}`);
56 }
57 return forms[0];
58 }
59
60 getForms(name: string): Form[] {
61 return this._forms.filter(f => f.name == name);
62 }
63 };