-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add miscellaneous cost calculator and add to main state
Signed-off-by: Giovanni Baratta <[email protected]>
- Loading branch information
1 parent
bb91b06
commit 7ba2522
Showing
2 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {Month, MonthlyReport, Period} from "./monthly-report.ts" | ||
|
||
const CANONE_RAI_MONTH = Month.January | ||
const TARI_MONTHS = [Month.April, Month.August, Month.November] | ||
|
||
const MISCELLANEOUS_COMPONENT = "Miscellaneous" | ||
|
||
interface MiscellaneousCosts { | ||
canoneRai: number | ||
tari: number | ||
} | ||
|
||
export type MiscellaneousCostsCalculator = MiscellaneousCosts & { | ||
computeMonthlyReport: (period: Period) => MonthlyReport | ||
} | ||
|
||
export function buildMiscellaneousExpensesCalculator(config: MiscellaneousCosts): MiscellaneousCostsCalculator { | ||
return { | ||
...config, | ||
computeMonthlyReport: computeMonthlyCosts(config) | ||
} | ||
} | ||
|
||
function computeMonthlyCosts(config: MiscellaneousCosts): (period: Period) => MonthlyReport { | ||
|
||
const {canoneRai, tari} = config | ||
const tariSplitPayment = tari / TARI_MONTHS.length | ||
|
||
return (period: Period): MonthlyReport => { | ||
|
||
const isRaiPeriod = period.month === CANONE_RAI_MONTH | ||
const isTariPeriod = TARI_MONTHS.includes(period.month) | ||
|
||
const raiCosts = isRaiPeriod ? canoneRai : 0 | ||
const tariCosts = isTariPeriod ? tariSplitPayment : 0 | ||
|
||
const totalExpenses = raiCosts + tariCosts | ||
|
||
return { | ||
period, | ||
component: MISCELLANEOUS_COMPONENT, | ||
totalIncome: 0, | ||
totalExpenses, | ||
detailedExpenses: { | ||
...(raiCosts > 0 && {rai: raiCosts}), | ||
...(tariCosts > 0 && {tari: tariCosts}) | ||
} | ||
} | ||
|
||
} | ||
} |