@novha/calc-engines 7.0.0 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capital-gains/brazil/BrazilCapitalGainsService.js +3 -0
- package/dist/capital-gains/brazil/BrazilCapitalGainsServiceImpl.js +56 -0
- package/dist/capital-gains/brazil/domain/types.js +3 -0
- package/dist/capital-gains/index.js +14 -2
- package/dist/capital-gains/india/IndiaCapitalGainsService.js +3 -0
- package/dist/capital-gains/india/IndiaCapitalGainsServiceImpl.js +58 -0
- package/dist/capital-gains/india/domain/types.js +3 -0
- package/dist/capital-gains/japan/JapanCapitalGainsService.js +3 -0
- package/dist/capital-gains/japan/JapanCapitalGainsServiceImpl.js +41 -0
- package/dist/capital-gains/japan/domain/types.js +3 -0
- package/dist/capital-gains/spain/SpainCapitalGainsService.js +3 -0
- package/dist/capital-gains/spain/SpainCapitalGainsServiceImpl.js +56 -0
- package/dist/capital-gains/spain/domain/types.js +3 -0
- package/dist/corporate/brazil/BrazilCorporateTaxService.js +3 -0
- package/dist/corporate/brazil/BrazilCorporateTaxServiceImpl.js +33 -0
- package/dist/corporate/brazil/domain/types.js +3 -0
- package/dist/corporate/index.js +10 -2
- package/dist/corporate/india/IndiaCorporateTaxService.js +3 -0
- package/dist/corporate/india/IndiaCorporateTaxServiceImpl.js +29 -0
- package/dist/corporate/india/domain/types.js +3 -0
- package/dist/corporate/japan/JapanCorporateTaxService.js +3 -0
- package/dist/corporate/japan/JapanCorporateTaxServiceImpl.js +29 -0
- package/dist/corporate/japan/domain/types.js +3 -0
- package/dist/corporate/spain/SpainCorporateTaxService.js +3 -0
- package/dist/corporate/spain/SpainCorporateTaxServiceImpl.js +29 -0
- package/dist/corporate/spain/domain/types.js +3 -0
- package/dist/income-tax/brazil/BrazilIncomeTaxService.js +3 -0
- package/dist/income-tax/brazil/BrazilIncomeTaxServiceImpl.js +78 -0
- package/dist/income-tax/brazil/domain/types.js +3 -0
- package/dist/income-tax/index.js +14 -2
- package/dist/income-tax/india/IndiaIncomeTaxService.js +3 -0
- package/dist/income-tax/india/IndiaIncomeTaxServiceImpl.js +75 -0
- package/dist/income-tax/india/domain/types.js +3 -0
- package/dist/income-tax/japan/JapanIncomeTaxService.js +3 -0
- package/dist/income-tax/japan/JapanIncomeTaxServiceImpl.js +75 -0
- package/dist/income-tax/japan/domain/types.js +3 -0
- package/dist/income-tax/spain/SpainIncomeTaxService.js +3 -0
- package/dist/income-tax/spain/SpainIncomeTaxServiceImpl.js +62 -0
- package/dist/income-tax/spain/domain/types.js +3 -0
- package/dist/inheritance-tax/index.js +8 -2
- package/dist/inheritance-tax/japan/JapanInheritanceTaxService.js +3 -0
- package/dist/inheritance-tax/japan/JapanInheritanceTaxServiceImpl.js +53 -0
- package/dist/inheritance-tax/japan/domain/types.js +3 -0
- package/dist/inheritance-tax/spain/SpainInheritanceTaxService.js +3 -0
- package/dist/inheritance-tax/spain/SpainInheritanceTaxServiceImpl.js +51 -0
- package/dist/inheritance-tax/spain/domain/types.js +3 -0
- package/dist/mortgage/brazil/BrazilMortgageService.js +3 -0
- package/dist/mortgage/brazil/BrazilMortgageServiceImpl.js +76 -0
- package/dist/mortgage/brazil/domain/types.js +3 -0
- package/dist/mortgage/index.js +14 -2
- package/dist/mortgage/india/IndiaMortgageService.js +3 -0
- package/dist/mortgage/india/IndiaMortgageServiceImpl.js +76 -0
- package/dist/mortgage/india/domain/types.js +3 -0
- package/dist/mortgage/japan/JapanMortgageService.js +3 -0
- package/dist/mortgage/japan/JapanMortgageServiceImpl.js +76 -0
- package/dist/mortgage/japan/domain/types.js +3 -0
- package/dist/mortgage/spain/SpainMortgageService.js +3 -0
- package/dist/mortgage/spain/SpainMortgageServiceImpl.js +96 -0
- package/dist/mortgage/spain/domain/types.js +3 -0
- package/dist/types/capital-gains/brazil/BrazilCapitalGainsService.d.ts +4 -0
- package/dist/types/capital-gains/brazil/BrazilCapitalGainsServiceImpl.d.ts +10 -0
- package/dist/types/capital-gains/brazil/domain/types.d.ts +11 -0
- package/dist/types/capital-gains/index.d.ts +9 -1
- package/dist/types/capital-gains/india/IndiaCapitalGainsService.d.ts +4 -0
- package/dist/types/capital-gains/india/IndiaCapitalGainsServiceImpl.d.ts +9 -0
- package/dist/types/capital-gains/india/domain/types.d.ts +9 -0
- package/dist/types/capital-gains/japan/JapanCapitalGainsService.d.ts +4 -0
- package/dist/types/capital-gains/japan/JapanCapitalGainsServiceImpl.d.ts +9 -0
- package/dist/types/capital-gains/japan/domain/types.d.ts +6 -0
- package/dist/types/capital-gains/spain/SpainCapitalGainsService.d.ts +4 -0
- package/dist/types/capital-gains/spain/SpainCapitalGainsServiceImpl.d.ts +10 -0
- package/dist/types/capital-gains/spain/domain/types.d.ts +11 -0
- package/dist/types/corporate/brazil/BrazilCorporateTaxService.d.ts +4 -0
- package/dist/types/corporate/brazil/BrazilCorporateTaxServiceImpl.d.ts +8 -0
- package/dist/types/corporate/brazil/domain/types.d.ts +19 -0
- package/dist/types/corporate/index.d.ts +9 -1
- package/dist/types/corporate/india/IndiaCorporateTaxService.d.ts +4 -0
- package/dist/types/corporate/india/IndiaCorporateTaxServiceImpl.d.ts +8 -0
- package/dist/types/corporate/india/domain/types.d.ts +15 -0
- package/dist/types/corporate/japan/JapanCorporateTaxService.d.ts +4 -0
- package/dist/types/corporate/japan/JapanCorporateTaxServiceImpl.d.ts +8 -0
- package/dist/types/corporate/japan/domain/types.d.ts +15 -0
- package/dist/types/corporate/spain/SpainCorporateTaxService.d.ts +4 -0
- package/dist/types/corporate/spain/SpainCorporateTaxServiceImpl.d.ts +8 -0
- package/dist/types/corporate/spain/domain/types.d.ts +15 -0
- package/dist/types/income-tax/brazil/BrazilIncomeTaxService.d.ts +4 -0
- package/dist/types/income-tax/brazil/BrazilIncomeTaxServiceImpl.d.ts +11 -0
- package/dist/types/income-tax/brazil/domain/types.d.ts +18 -0
- package/dist/types/income-tax/index.d.ts +9 -1
- package/dist/types/income-tax/india/IndiaIncomeTaxService.d.ts +4 -0
- package/dist/types/income-tax/india/IndiaIncomeTaxServiceImpl.d.ts +10 -0
- package/dist/types/income-tax/india/domain/types.d.ts +16 -0
- package/dist/types/income-tax/japan/JapanIncomeTaxService.d.ts +4 -0
- package/dist/types/income-tax/japan/JapanIncomeTaxServiceImpl.d.ts +10 -0
- package/dist/types/income-tax/japan/domain/types.d.ts +16 -0
- package/dist/types/income-tax/spain/SpainIncomeTaxService.d.ts +4 -0
- package/dist/types/income-tax/spain/SpainIncomeTaxServiceImpl.d.ts +10 -0
- package/dist/types/income-tax/spain/domain/types.d.ts +16 -0
- package/dist/types/inheritance-tax/index.d.ts +5 -1
- package/dist/types/inheritance-tax/japan/JapanInheritanceTaxService.d.ts +4 -0
- package/dist/types/inheritance-tax/japan/JapanInheritanceTaxServiceImpl.d.ts +9 -0
- package/dist/types/inheritance-tax/japan/domain/types.d.ts +21 -0
- package/dist/types/inheritance-tax/spain/SpainInheritanceTaxService.d.ts +4 -0
- package/dist/types/inheritance-tax/spain/SpainInheritanceTaxServiceImpl.d.ts +9 -0
- package/dist/types/inheritance-tax/spain/domain/types.d.ts +19 -0
- package/dist/types/mortgage/brazil/BrazilMortgageService.d.ts +4 -0
- package/dist/types/mortgage/brazil/BrazilMortgageServiceImpl.d.ts +7 -0
- package/dist/types/mortgage/brazil/domain/types.d.ts +39 -0
- package/dist/types/mortgage/index.d.ts +9 -1
- package/dist/types/mortgage/india/IndiaMortgageService.d.ts +4 -0
- package/dist/types/mortgage/india/IndiaMortgageServiceImpl.d.ts +7 -0
- package/dist/types/mortgage/india/domain/types.d.ts +39 -0
- package/dist/types/mortgage/japan/JapanMortgageService.d.ts +4 -0
- package/dist/types/mortgage/japan/JapanMortgageServiceImpl.d.ts +7 -0
- package/dist/types/mortgage/japan/domain/types.d.ts +39 -0
- package/dist/types/mortgage/spain/SpainMortgageService.d.ts +4 -0
- package/dist/types/mortgage/spain/SpainMortgageServiceImpl.d.ts +8 -0
- package/dist/types/mortgage/spain/domain/types.d.ts +44 -0
- package/package.json +1 -1
- package/src/capital-gains/brazil/BrazilCapitalGainsService.ts +5 -0
- package/src/capital-gains/brazil/BrazilCapitalGainsServiceImpl.ts +69 -0
- package/src/capital-gains/brazil/domain/types.ts +15 -0
- package/src/capital-gains/index.ts +40 -0
- package/src/capital-gains/india/IndiaCapitalGainsService.ts +5 -0
- package/src/capital-gains/india/IndiaCapitalGainsServiceImpl.ts +64 -0
- package/src/capital-gains/india/domain/types.ts +12 -0
- package/src/capital-gains/japan/JapanCapitalGainsService.ts +5 -0
- package/src/capital-gains/japan/JapanCapitalGainsServiceImpl.ts +47 -0
- package/src/capital-gains/japan/domain/types.ts +9 -0
- package/src/capital-gains/spain/SpainCapitalGainsService.ts +5 -0
- package/src/capital-gains/spain/SpainCapitalGainsServiceImpl.ts +69 -0
- package/src/capital-gains/spain/domain/types.ts +15 -0
- package/src/corporate/brazil/BrazilCorporateTaxService.ts +5 -0
- package/src/corporate/brazil/BrazilCorporateTaxServiceImpl.ts +40 -0
- package/src/corporate/brazil/domain/types.ts +16 -0
- package/src/corporate/index.ts +45 -1
- package/src/corporate/india/IndiaCorporateTaxService.ts +5 -0
- package/src/corporate/india/IndiaCorporateTaxServiceImpl.ts +35 -0
- package/src/corporate/india/domain/types.ts +15 -0
- package/src/corporate/japan/JapanCorporateTaxService.ts +5 -0
- package/src/corporate/japan/JapanCorporateTaxServiceImpl.ts +35 -0
- package/src/corporate/japan/domain/types.ts +15 -0
- package/src/corporate/spain/SpainCorporateTaxService.ts +5 -0
- package/src/corporate/spain/SpainCorporateTaxServiceImpl.ts +35 -0
- package/src/corporate/spain/domain/types.ts +15 -0
- package/src/income-tax/brazil/BrazilIncomeTaxService.ts +5 -0
- package/src/income-tax/brazil/BrazilIncomeTaxServiceImpl.ts +91 -0
- package/src/income-tax/brazil/domain/types.ts +21 -0
- package/src/income-tax/index.ts +40 -0
- package/src/income-tax/india/IndiaIncomeTaxService.ts +5 -0
- package/src/income-tax/india/IndiaIncomeTaxServiceImpl.ts +87 -0
- package/src/income-tax/india/domain/types.ts +16 -0
- package/src/income-tax/japan/JapanIncomeTaxService.ts +5 -0
- package/src/income-tax/japan/JapanIncomeTaxServiceImpl.ts +87 -0
- package/src/income-tax/japan/domain/types.ts +16 -0
- package/src/income-tax/spain/SpainIncomeTaxService.ts +5 -0
- package/src/income-tax/spain/SpainIncomeTaxServiceImpl.ts +75 -0
- package/src/income-tax/spain/domain/types.ts +16 -0
- package/src/inheritance-tax/index.ts +24 -0
- package/src/inheritance-tax/japan/JapanInheritanceTaxService.ts +5 -0
- package/src/inheritance-tax/japan/JapanInheritanceTaxServiceImpl.ts +67 -0
- package/src/inheritance-tax/japan/domain/types.ts +25 -0
- package/src/inheritance-tax/spain/SpainInheritanceTaxService.ts +5 -0
- package/src/inheritance-tax/spain/SpainInheritanceTaxServiceImpl.ts +65 -0
- package/src/inheritance-tax/spain/domain/types.ts +23 -0
- package/src/mortgage/brazil/BrazilMortgageService.ts +5 -0
- package/src/mortgage/brazil/BrazilMortgageServiceImpl.ts +101 -0
- package/src/mortgage/brazil/domain/types.ts +46 -0
- package/src/mortgage/index.ts +49 -1
- package/src/mortgage/india/IndiaMortgageService.ts +5 -0
- package/src/mortgage/india/IndiaMortgageServiceImpl.ts +101 -0
- package/src/mortgage/india/domain/types.ts +46 -0
- package/src/mortgage/japan/JapanMortgageService.ts +5 -0
- package/src/mortgage/japan/JapanMortgageServiceImpl.ts +101 -0
- package/src/mortgage/japan/domain/types.ts +46 -0
- package/src/mortgage/spain/SpainMortgageService.ts +5 -0
- package/src/mortgage/spain/SpainMortgageServiceImpl.ts +130 -0
- package/src/mortgage/spain/domain/types.ts +52 -0
- package/test/brazil-capital-gains.test.ts +65 -0
- package/test/brazil-corporate-tax.test.ts +63 -0
- package/test/brazil-income-tax.test.ts +85 -0
- package/test/brazil-mortgage.test.ts +70 -0
- package/test/india-capital-gains.test.ts +71 -0
- package/test/india-corporate-tax.test.ts +54 -0
- package/test/india-income-tax.test.ts +78 -0
- package/test/india-mortgage.test.ts +70 -0
- package/test/japan-capital-gains.test.ts +55 -0
- package/test/japan-corporate-tax.test.ts +54 -0
- package/test/japan-income-tax.test.ts +76 -0
- package/test/japan-inheritance-tax.test.ts +74 -0
- package/test/japan-mortgage.test.ts +69 -0
- package/test/spain-capital-gains.test.ts +66 -0
- package/test/spain-corporate-tax.test.ts +54 -0
- package/test/spain-income-tax.test.ts +84 -0
- package/test/spain-inheritance-tax.test.ts +78 -0
- package/test/spain-mortgage.test.ts +70 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Breakdown } from "../domain/types";
|
|
2
|
+
import { JapanInheritanceTaxService } from "./JapanInheritanceTaxService";
|
|
3
|
+
import { Input, Result, Rules, TaxBracket } from "./domain/types";
|
|
4
|
+
|
|
5
|
+
export class JapanInheritanceTaxServiceImpl implements JapanInheritanceTaxService {
|
|
6
|
+
private _input: Input;
|
|
7
|
+
private _rules: Rules;
|
|
8
|
+
|
|
9
|
+
constructor(input: Input, rules: Rules) {
|
|
10
|
+
this._input = input;
|
|
11
|
+
this._rules = rules;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
calculate(): Result {
|
|
15
|
+
const estate = this._input.estateValue;
|
|
16
|
+
const heirs = Math.max(1, this._input.numberOfStatutoryHeirs);
|
|
17
|
+
const exemption = this._rules.baseExemption + this._rules.perHeirExemption * heirs;
|
|
18
|
+
|
|
19
|
+
if (estate <= 0 || estate <= exemption) {
|
|
20
|
+
return {
|
|
21
|
+
taxableEstate: 0,
|
|
22
|
+
inheritanceTax: 0,
|
|
23
|
+
effectiveRate: 0,
|
|
24
|
+
breakdowns: [],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const taxableEstate = estate - exemption;
|
|
29
|
+
const { tax, breakdowns } = this.applyBrackets(taxableEstate, this._rules.taxBrackets);
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
taxableEstate,
|
|
33
|
+
inheritanceTax: tax,
|
|
34
|
+
effectiveRate: estate > 0 ? (tax / estate) * 100 : 0,
|
|
35
|
+
breakdowns,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private applyBrackets(
|
|
40
|
+
taxableAmount: number,
|
|
41
|
+
brackets: TaxBracket[],
|
|
42
|
+
): { tax: number; breakdowns: Breakdown[] } {
|
|
43
|
+
let tax = 0;
|
|
44
|
+
const breakdowns: Breakdown[] = [];
|
|
45
|
+
|
|
46
|
+
for (const bracket of brackets) {
|
|
47
|
+
if (taxableAmount <= bracket.from) break;
|
|
48
|
+
|
|
49
|
+
const upper = bracket.to ?? taxableAmount;
|
|
50
|
+
const taxable = Math.min(upper, taxableAmount) - bracket.from;
|
|
51
|
+
|
|
52
|
+
if (taxable > 0) {
|
|
53
|
+
const bracketTax = taxable * bracket.rate;
|
|
54
|
+
tax += bracketTax;
|
|
55
|
+
|
|
56
|
+
breakdowns.push({
|
|
57
|
+
from: `${bracket.from}`,
|
|
58
|
+
to: `${bracket.to ?? 'Above'}`,
|
|
59
|
+
rate: bracket.rate,
|
|
60
|
+
amount: bracketTax,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return { tax, breakdowns };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Breakdown } from "../../domain/types";
|
|
2
|
+
|
|
3
|
+
export interface TaxBracket {
|
|
4
|
+
from: number;
|
|
5
|
+
to: number | null;
|
|
6
|
+
rate: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Rules {
|
|
10
|
+
baseExemption: number;
|
|
11
|
+
perHeirExemption: number;
|
|
12
|
+
taxBrackets: TaxBracket[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Input {
|
|
16
|
+
estateValue: number;
|
|
17
|
+
numberOfStatutoryHeirs: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface Result {
|
|
21
|
+
taxableEstate: number;
|
|
22
|
+
inheritanceTax: number;
|
|
23
|
+
effectiveRate: number;
|
|
24
|
+
breakdowns: Breakdown[];
|
|
25
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Breakdown } from "../domain/types";
|
|
2
|
+
import { SpainInheritanceTaxService } from "./SpainInheritanceTaxService";
|
|
3
|
+
import { Input, Result, Rules, TaxBracket } from "./domain/types";
|
|
4
|
+
|
|
5
|
+
export class SpainInheritanceTaxServiceImpl implements SpainInheritanceTaxService {
|
|
6
|
+
private _input: Input;
|
|
7
|
+
private _rules: Rules;
|
|
8
|
+
|
|
9
|
+
constructor(input: Input, rules: Rules) {
|
|
10
|
+
this._input = input;
|
|
11
|
+
this._rules = rules;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
calculate(): Result {
|
|
15
|
+
const estate = this._input.estateValue;
|
|
16
|
+
|
|
17
|
+
if (estate <= 0 || estate <= this._rules.exemption) {
|
|
18
|
+
return {
|
|
19
|
+
taxableEstate: 0,
|
|
20
|
+
inheritanceTax: 0,
|
|
21
|
+
effectiveRate: 0,
|
|
22
|
+
breakdowns: [],
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const taxableEstate = estate - this._rules.exemption;
|
|
27
|
+
const { tax, breakdowns } = this.applyBrackets(taxableEstate, this._rules.taxBrackets);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
taxableEstate,
|
|
31
|
+
inheritanceTax: tax,
|
|
32
|
+
effectiveRate: estate > 0 ? (tax / estate) * 100 : 0,
|
|
33
|
+
breakdowns,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private applyBrackets(
|
|
38
|
+
taxableAmount: number,
|
|
39
|
+
brackets: TaxBracket[],
|
|
40
|
+
): { tax: number; breakdowns: Breakdown[] } {
|
|
41
|
+
let tax = 0;
|
|
42
|
+
const breakdowns: Breakdown[] = [];
|
|
43
|
+
|
|
44
|
+
for (const bracket of brackets) {
|
|
45
|
+
if (taxableAmount <= bracket.from) break;
|
|
46
|
+
|
|
47
|
+
const upper = bracket.to ?? taxableAmount;
|
|
48
|
+
const taxable = Math.min(upper, taxableAmount) - bracket.from;
|
|
49
|
+
|
|
50
|
+
if (taxable > 0) {
|
|
51
|
+
const bracketTax = taxable * bracket.rate;
|
|
52
|
+
tax += bracketTax;
|
|
53
|
+
|
|
54
|
+
breakdowns.push({
|
|
55
|
+
from: `${bracket.from}`,
|
|
56
|
+
to: `${bracket.to ?? 'Above'}`,
|
|
57
|
+
rate: bracket.rate,
|
|
58
|
+
amount: bracketTax,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return { tax, breakdowns };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Breakdown } from "../../domain/types";
|
|
2
|
+
|
|
3
|
+
export interface TaxBracket {
|
|
4
|
+
from: number;
|
|
5
|
+
to: number | null;
|
|
6
|
+
rate: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Rules {
|
|
10
|
+
exemption: number;
|
|
11
|
+
taxBrackets: TaxBracket[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface Input {
|
|
15
|
+
estateValue: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Result {
|
|
19
|
+
taxableEstate: number;
|
|
20
|
+
inheritanceTax: number;
|
|
21
|
+
effectiveRate: number;
|
|
22
|
+
breakdowns: Breakdown[];
|
|
23
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { BrazilMortgageService } from "./BrazilMortgageService";
|
|
2
|
+
import { AmortizationScheduleItem, MortgageInput, MortgageOutput, MortgageRules } from "./domain/types";
|
|
3
|
+
|
|
4
|
+
export class BrazilMortgageServiceImpl implements BrazilMortgageService {
|
|
5
|
+
|
|
6
|
+
public calculate(input: MortgageInput, rules: MortgageRules): MortgageOutput {
|
|
7
|
+
const loanAmount = input.propertyPrice - input.downPayment;
|
|
8
|
+
|
|
9
|
+
if (loanAmount <= 0) {
|
|
10
|
+
throw new Error('Invalid loan amount: down payment must be less than property price');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const totalPayments = input.amortizationYears * 12;
|
|
14
|
+
const monthlyRate = (input.annualInterestRate / 100) / 12;
|
|
15
|
+
|
|
16
|
+
const monthlyPayment = this.calculatePayment(loanAmount, monthlyRate, totalPayments);
|
|
17
|
+
const totalPaid = monthlyPayment * totalPayments;
|
|
18
|
+
const totalInterestPaid = totalPaid - loanAmount;
|
|
19
|
+
const itbi = input.propertyPrice * rules.itbi.rate;
|
|
20
|
+
|
|
21
|
+
const amortizationSchedule = this.calculateAmortizationSchedule(
|
|
22
|
+
loanAmount,
|
|
23
|
+
monthlyRate,
|
|
24
|
+
monthlyPayment,
|
|
25
|
+
totalPayments,
|
|
26
|
+
input.amortizationYears,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
loanAmount,
|
|
31
|
+
totalMortgage: loanAmount,
|
|
32
|
+
monthlyPayment,
|
|
33
|
+
totalInterestPaid,
|
|
34
|
+
totalPaid,
|
|
35
|
+
itbi,
|
|
36
|
+
amortizationSchedule,
|
|
37
|
+
otherFees: {
|
|
38
|
+
notaryFees: {
|
|
39
|
+
value: itbi,
|
|
40
|
+
label: 'ITBI',
|
|
41
|
+
},
|
|
42
|
+
bankFees: {
|
|
43
|
+
value: 0,
|
|
44
|
+
label: 'BANK_FEES',
|
|
45
|
+
},
|
|
46
|
+
monthlyInsuranceFees: {
|
|
47
|
+
value: 0,
|
|
48
|
+
label: 'MONTHLY_INSURANCE_FEES',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private calculateAmortizationSchedule(
|
|
55
|
+
principal: number,
|
|
56
|
+
monthlyRate: number,
|
|
57
|
+
monthlyPayment: number,
|
|
58
|
+
totalPayments: number,
|
|
59
|
+
amortizationYears: number,
|
|
60
|
+
): AmortizationScheduleItem[] {
|
|
61
|
+
const schedule: AmortizationScheduleItem[] = [];
|
|
62
|
+
let balance = principal;
|
|
63
|
+
|
|
64
|
+
for (let year = 1; year <= amortizationYears; year++) {
|
|
65
|
+
let yearlyPrincipal = 0;
|
|
66
|
+
let yearlyInterest = 0;
|
|
67
|
+
|
|
68
|
+
const paymentsInYear = Math.min(12, totalPayments - (year - 1) * 12);
|
|
69
|
+
|
|
70
|
+
for (let payment = 1; payment <= paymentsInYear; payment++) {
|
|
71
|
+
const interestPayment = balance * monthlyRate;
|
|
72
|
+
const principalPayment = monthlyPayment - interestPayment;
|
|
73
|
+
|
|
74
|
+
yearlyInterest += interestPayment;
|
|
75
|
+
yearlyPrincipal += principalPayment;
|
|
76
|
+
balance -= principalPayment;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
schedule.push({
|
|
80
|
+
year,
|
|
81
|
+
principal: yearlyPrincipal,
|
|
82
|
+
interest: yearlyInterest,
|
|
83
|
+
balance: Math.max(0, balance),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (balance <= 0) break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return schedule;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private calculatePayment(principal: number, rate: number, periods: number): number {
|
|
93
|
+
if (rate === 0) {
|
|
94
|
+
return principal / periods;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return principal *
|
|
98
|
+
(rate * Math.pow(1 + rate, periods)) /
|
|
99
|
+
(Math.pow(1 + rate, periods) - 1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { OtherFees } from "../../domain/types";
|
|
2
|
+
|
|
3
|
+
export interface MortgageRules {
|
|
4
|
+
loanConstraints: LoanConstraints;
|
|
5
|
+
interest: InterestRules;
|
|
6
|
+
itbi: ITBIRules;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface LoanConstraints {
|
|
10
|
+
maxLtvPercent: number;
|
|
11
|
+
maxAmortizationYears: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface InterestRules {
|
|
15
|
+
compounding: 'MONTHLY';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ITBIRules {
|
|
19
|
+
rate: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface AmortizationScheduleItem {
|
|
23
|
+
year: number;
|
|
24
|
+
principal: number;
|
|
25
|
+
interest: number;
|
|
26
|
+
balance: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface MortgageInput {
|
|
30
|
+
propertyPrice: number;
|
|
31
|
+
downPayment: number;
|
|
32
|
+
annualInterestRate: number;
|
|
33
|
+
amortizationYears: number;
|
|
34
|
+
isFirstTimeBuyer: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface MortgageOutput {
|
|
38
|
+
loanAmount: number;
|
|
39
|
+
totalMortgage: number;
|
|
40
|
+
monthlyPayment: number;
|
|
41
|
+
totalInterestPaid: number;
|
|
42
|
+
totalPaid: number;
|
|
43
|
+
itbi: number;
|
|
44
|
+
amortizationSchedule: AmortizationScheduleItem[];
|
|
45
|
+
otherFees: OtherFees;
|
|
46
|
+
}
|
package/src/mortgage/index.ts
CHANGED
|
@@ -55,6 +55,38 @@ import {
|
|
|
55
55
|
MortgageOutput as GermanyMortgageOutput,
|
|
56
56
|
} from './germany/domain/types';
|
|
57
57
|
|
|
58
|
+
// Brazil
|
|
59
|
+
import { BrazilMortgageServiceImpl as BrazilMortgageService } from './brazil/BrazilMortgageServiceImpl';
|
|
60
|
+
import {
|
|
61
|
+
MortgageRules as BrazilMortgageRules,
|
|
62
|
+
MortgageInput as BrazilMortgageInput,
|
|
63
|
+
MortgageOutput as BrazilMortgageOutput,
|
|
64
|
+
} from './brazil/domain/types';
|
|
65
|
+
|
|
66
|
+
// Spain
|
|
67
|
+
import { SpainMortgageServiceImpl as SpainMortgageService } from './spain/SpainMortgageServiceImpl';
|
|
68
|
+
import {
|
|
69
|
+
MortgageRules as SpainMortgageRules,
|
|
70
|
+
MortgageInput as SpainMortgageInput,
|
|
71
|
+
MortgageOutput as SpainMortgageOutput,
|
|
72
|
+
} from './spain/domain/types';
|
|
73
|
+
|
|
74
|
+
// India
|
|
75
|
+
import { IndiaMortgageServiceImpl as IndiaMortgageService } from './india/IndiaMortgageServiceImpl';
|
|
76
|
+
import {
|
|
77
|
+
MortgageRules as IndiaMortgageRules,
|
|
78
|
+
MortgageInput as IndiaMortgageInput,
|
|
79
|
+
MortgageOutput as IndiaMortgageOutput,
|
|
80
|
+
} from './india/domain/types';
|
|
81
|
+
|
|
82
|
+
// Japan
|
|
83
|
+
import { JapanMortgageServiceImpl as JapanMortgageService } from './japan/JapanMortgageServiceImpl';
|
|
84
|
+
import {
|
|
85
|
+
MortgageRules as JapanMortgageRules,
|
|
86
|
+
MortgageInput as JapanMortgageInput,
|
|
87
|
+
MortgageOutput as JapanMortgageOutput,
|
|
88
|
+
} from './japan/domain/types';
|
|
89
|
+
|
|
58
90
|
export {
|
|
59
91
|
CanadaMortgageService,
|
|
60
92
|
CanadaMortgageRules,
|
|
@@ -83,5 +115,21 @@ export {
|
|
|
83
115
|
GermanyMortgageService,
|
|
84
116
|
GermanyMortgageRules,
|
|
85
117
|
GermanyMortgageInput,
|
|
86
|
-
GermanyMortgageOutput
|
|
118
|
+
GermanyMortgageOutput,
|
|
119
|
+
BrazilMortgageService,
|
|
120
|
+
BrazilMortgageRules,
|
|
121
|
+
BrazilMortgageInput,
|
|
122
|
+
BrazilMortgageOutput,
|
|
123
|
+
SpainMortgageService,
|
|
124
|
+
SpainMortgageRules,
|
|
125
|
+
SpainMortgageInput,
|
|
126
|
+
SpainMortgageOutput,
|
|
127
|
+
IndiaMortgageService,
|
|
128
|
+
IndiaMortgageRules,
|
|
129
|
+
IndiaMortgageInput,
|
|
130
|
+
IndiaMortgageOutput,
|
|
131
|
+
JapanMortgageService,
|
|
132
|
+
JapanMortgageRules,
|
|
133
|
+
JapanMortgageInput,
|
|
134
|
+
JapanMortgageOutput
|
|
87
135
|
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { IndiaMortgageService } from "./IndiaMortgageService";
|
|
2
|
+
import { AmortizationScheduleItem, MortgageInput, MortgageOutput, MortgageRules } from "./domain/types";
|
|
3
|
+
|
|
4
|
+
export class IndiaMortgageServiceImpl implements IndiaMortgageService {
|
|
5
|
+
|
|
6
|
+
public calculate(input: MortgageInput, rules: MortgageRules): MortgageOutput {
|
|
7
|
+
const loanAmount = input.propertyPrice - input.downPayment;
|
|
8
|
+
|
|
9
|
+
if (loanAmount <= 0) {
|
|
10
|
+
throw new Error('Invalid loan amount: down payment must be less than property price');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const totalPayments = input.amortizationYears * 12;
|
|
14
|
+
const monthlyRate = (input.annualInterestRate / 100) / 12;
|
|
15
|
+
|
|
16
|
+
const monthlyPayment = this.calculatePayment(loanAmount, monthlyRate, totalPayments);
|
|
17
|
+
const totalPaid = monthlyPayment * totalPayments;
|
|
18
|
+
const totalInterestPaid = totalPaid - loanAmount;
|
|
19
|
+
const stampDuty = input.propertyPrice * rules.stampDuty.rate;
|
|
20
|
+
|
|
21
|
+
const amortizationSchedule = this.calculateAmortizationSchedule(
|
|
22
|
+
loanAmount,
|
|
23
|
+
monthlyRate,
|
|
24
|
+
monthlyPayment,
|
|
25
|
+
totalPayments,
|
|
26
|
+
input.amortizationYears,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
loanAmount,
|
|
31
|
+
totalMortgage: loanAmount,
|
|
32
|
+
monthlyPayment,
|
|
33
|
+
totalInterestPaid,
|
|
34
|
+
totalPaid,
|
|
35
|
+
stampDuty,
|
|
36
|
+
amortizationSchedule,
|
|
37
|
+
otherFees: {
|
|
38
|
+
notaryFees: {
|
|
39
|
+
value: stampDuty,
|
|
40
|
+
label: 'STAMP_DUTY',
|
|
41
|
+
},
|
|
42
|
+
bankFees: {
|
|
43
|
+
value: 0,
|
|
44
|
+
label: 'BANK_FEES',
|
|
45
|
+
},
|
|
46
|
+
monthlyInsuranceFees: {
|
|
47
|
+
value: 0,
|
|
48
|
+
label: 'MONTHLY_INSURANCE_FEES',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private calculateAmortizationSchedule(
|
|
55
|
+
principal: number,
|
|
56
|
+
monthlyRate: number,
|
|
57
|
+
monthlyPayment: number,
|
|
58
|
+
totalPayments: number,
|
|
59
|
+
amortizationYears: number,
|
|
60
|
+
): AmortizationScheduleItem[] {
|
|
61
|
+
const schedule: AmortizationScheduleItem[] = [];
|
|
62
|
+
let balance = principal;
|
|
63
|
+
|
|
64
|
+
for (let year = 1; year <= amortizationYears; year++) {
|
|
65
|
+
let yearlyPrincipal = 0;
|
|
66
|
+
let yearlyInterest = 0;
|
|
67
|
+
|
|
68
|
+
const paymentsInYear = Math.min(12, totalPayments - (year - 1) * 12);
|
|
69
|
+
|
|
70
|
+
for (let payment = 1; payment <= paymentsInYear; payment++) {
|
|
71
|
+
const interestPayment = balance * monthlyRate;
|
|
72
|
+
const principalPayment = monthlyPayment - interestPayment;
|
|
73
|
+
|
|
74
|
+
yearlyInterest += interestPayment;
|
|
75
|
+
yearlyPrincipal += principalPayment;
|
|
76
|
+
balance -= principalPayment;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
schedule.push({
|
|
80
|
+
year,
|
|
81
|
+
principal: yearlyPrincipal,
|
|
82
|
+
interest: yearlyInterest,
|
|
83
|
+
balance: Math.max(0, balance),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (balance <= 0) break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return schedule;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private calculatePayment(principal: number, rate: number, periods: number): number {
|
|
93
|
+
if (rate === 0) {
|
|
94
|
+
return principal / periods;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return principal *
|
|
98
|
+
(rate * Math.pow(1 + rate, periods)) /
|
|
99
|
+
(Math.pow(1 + rate, periods) - 1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { OtherFees } from "../../domain/types";
|
|
2
|
+
|
|
3
|
+
export interface MortgageRules {
|
|
4
|
+
loanConstraints: LoanConstraints;
|
|
5
|
+
interest: InterestRules;
|
|
6
|
+
stampDuty: StampDutyRules;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface LoanConstraints {
|
|
10
|
+
maxLtvPercent: number;
|
|
11
|
+
maxAmortizationYears: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface InterestRules {
|
|
15
|
+
compounding: 'MONTHLY';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface StampDutyRules {
|
|
19
|
+
rate: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface AmortizationScheduleItem {
|
|
23
|
+
year: number;
|
|
24
|
+
principal: number;
|
|
25
|
+
interest: number;
|
|
26
|
+
balance: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface MortgageInput {
|
|
30
|
+
propertyPrice: number;
|
|
31
|
+
downPayment: number;
|
|
32
|
+
annualInterestRate: number;
|
|
33
|
+
amortizationYears: number;
|
|
34
|
+
isFirstTimeBuyer: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface MortgageOutput {
|
|
38
|
+
loanAmount: number;
|
|
39
|
+
totalMortgage: number;
|
|
40
|
+
monthlyPayment: number;
|
|
41
|
+
totalInterestPaid: number;
|
|
42
|
+
totalPaid: number;
|
|
43
|
+
stampDuty: number;
|
|
44
|
+
amortizationSchedule: AmortizationScheduleItem[];
|
|
45
|
+
otherFees: OtherFees;
|
|
46
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { JapanMortgageService } from "./JapanMortgageService";
|
|
2
|
+
import { AmortizationScheduleItem, MortgageInput, MortgageOutput, MortgageRules } from "./domain/types";
|
|
3
|
+
|
|
4
|
+
export class JapanMortgageServiceImpl implements JapanMortgageService {
|
|
5
|
+
|
|
6
|
+
public calculate(input: MortgageInput, rules: MortgageRules): MortgageOutput {
|
|
7
|
+
const loanAmount = input.propertyPrice - input.downPayment;
|
|
8
|
+
|
|
9
|
+
if (loanAmount <= 0) {
|
|
10
|
+
throw new Error('Invalid loan amount: down payment must be less than property price');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const totalPayments = input.amortizationYears * 12;
|
|
14
|
+
const monthlyRate = (input.annualInterestRate / 100) / 12;
|
|
15
|
+
|
|
16
|
+
const monthlyPayment = this.calculatePayment(loanAmount, monthlyRate, totalPayments);
|
|
17
|
+
const totalPaid = monthlyPayment * totalPayments;
|
|
18
|
+
const totalInterestPaid = totalPaid - loanAmount;
|
|
19
|
+
const acquisitionTax = input.propertyPrice * rules.acquisitionTax.rate;
|
|
20
|
+
|
|
21
|
+
const amortizationSchedule = this.calculateAmortizationSchedule(
|
|
22
|
+
loanAmount,
|
|
23
|
+
monthlyRate,
|
|
24
|
+
monthlyPayment,
|
|
25
|
+
totalPayments,
|
|
26
|
+
input.amortizationYears,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
loanAmount,
|
|
31
|
+
totalMortgage: loanAmount,
|
|
32
|
+
monthlyPayment,
|
|
33
|
+
totalInterestPaid,
|
|
34
|
+
totalPaid,
|
|
35
|
+
acquisitionTax,
|
|
36
|
+
amortizationSchedule,
|
|
37
|
+
otherFees: {
|
|
38
|
+
notaryFees: {
|
|
39
|
+
value: acquisitionTax,
|
|
40
|
+
label: 'ACQUISITION_TAX',
|
|
41
|
+
},
|
|
42
|
+
bankFees: {
|
|
43
|
+
value: 0,
|
|
44
|
+
label: 'BANK_FEES',
|
|
45
|
+
},
|
|
46
|
+
monthlyInsuranceFees: {
|
|
47
|
+
value: 0,
|
|
48
|
+
label: 'MONTHLY_INSURANCE_FEES',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private calculateAmortizationSchedule(
|
|
55
|
+
principal: number,
|
|
56
|
+
monthlyRate: number,
|
|
57
|
+
monthlyPayment: number,
|
|
58
|
+
totalPayments: number,
|
|
59
|
+
amortizationYears: number,
|
|
60
|
+
): AmortizationScheduleItem[] {
|
|
61
|
+
const schedule: AmortizationScheduleItem[] = [];
|
|
62
|
+
let balance = principal;
|
|
63
|
+
|
|
64
|
+
for (let year = 1; year <= amortizationYears; year++) {
|
|
65
|
+
let yearlyPrincipal = 0;
|
|
66
|
+
let yearlyInterest = 0;
|
|
67
|
+
|
|
68
|
+
const paymentsInYear = Math.min(12, totalPayments - (year - 1) * 12);
|
|
69
|
+
|
|
70
|
+
for (let payment = 1; payment <= paymentsInYear; payment++) {
|
|
71
|
+
const interestPayment = balance * monthlyRate;
|
|
72
|
+
const principalPayment = monthlyPayment - interestPayment;
|
|
73
|
+
|
|
74
|
+
yearlyInterest += interestPayment;
|
|
75
|
+
yearlyPrincipal += principalPayment;
|
|
76
|
+
balance -= principalPayment;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
schedule.push({
|
|
80
|
+
year,
|
|
81
|
+
principal: yearlyPrincipal,
|
|
82
|
+
interest: yearlyInterest,
|
|
83
|
+
balance: Math.max(0, balance),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (balance <= 0) break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return schedule;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private calculatePayment(principal: number, rate: number, periods: number): number {
|
|
93
|
+
if (rate === 0) {
|
|
94
|
+
return principal / periods;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return principal *
|
|
98
|
+
(rate * Math.pow(1 + rate, periods)) /
|
|
99
|
+
(Math.pow(1 + rate, periods) - 1);
|
|
100
|
+
}
|
|
101
|
+
}
|