From 7ba2522642a3fe4b5729beddcd1c9a003b49547b Mon Sep 17 00:00:00 2001 From: Giovanni Baratta Date: Thu, 23 May 2024 12:50:01 +0200 Subject: [PATCH] Add miscellaneous cost calculator and add to main state Signed-off-by: Giovanni Baratta --- src/App.tsx | 14 ++++++++++- src/model/miscellaneous.ts | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/model/miscellaneous.ts diff --git a/src/App.tsx b/src/App.tsx index aaf5d91..c1832b5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -34,6 +34,7 @@ import {HouseComponent} from "./components/HouseComponent.tsx" import {Info} from "./components/InfoComponent.tsx" import {MiscellaneousCostsComponent} from "./components/MiscellaneousComponent.tsx" import MiscellaneousServicesTwoToneIcon from "@mui/icons-material/MiscellaneousServicesTwoTone" +import {buildMiscellaneousExpensesCalculator} from "./model/miscellaneous.ts" const ONE_YEAR_IN_MS = 1 * 1000 * 60 * 60 * 24 * 365 @@ -44,6 +45,7 @@ function App() { const carState = useAppSelector(state => state.car) const incomeState = useAppSelector(state => state.income) const houseState = useAppSelector(state => state.house) + const miscellaneousState = useAppSelector(state => state.miscellaneous) const records: Report[] = [] @@ -115,6 +117,10 @@ function App() { } const houseAgencyCalculator = buildHouseAgencyExpensesCalculator(houseAgencyCosts) + const miscellaneousCostsCalculator = buildMiscellaneousExpensesCalculator({ + tari: miscellaneousState.tari, + canoneRai: miscellaneousState.canoneRai + }) while (simulationCurrentDate.getTime() < simulationEndingDate.getTime()) { @@ -128,11 +134,17 @@ function App() { const houseReport = houseCalculator.computeMonthlyReport(period) const furnitureReport = furnitureCalculator.computeMonthlyReport(period) const houseAgencyReport = houseAgencyCalculator.computeMonthlyReport(period) + const miscellaneuousCostsReport = miscellaneousCostsCalculator.computeMonthlyReport(period) const record: Report = { date: simulationCurrentDate, income: incomeReport.totalIncome, - totalMonthExpenses: carReport.totalExpenses + houseReport.totalExpenses + furnitureReport.totalExpenses + houseAgencyReport.totalExpenses + totalMonthExpenses: carReport.totalExpenses + + incomeReport.totalExpenses + + houseReport.totalExpenses + + furnitureReport.totalExpenses + + houseAgencyReport.totalExpenses + + miscellaneuousCostsReport.totalExpenses } records.push(record) diff --git a/src/model/miscellaneous.ts b/src/model/miscellaneous.ts new file mode 100644 index 0000000..b86bbb2 --- /dev/null +++ b/src/model/miscellaneous.ts @@ -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}) + } + } + + } +}