Skip to content

Commit

Permalink
Add miscellaneous cost calculator and add to main state
Browse files Browse the repository at this point in the history
Signed-off-by: Giovanni Baratta <[email protected]>
  • Loading branch information
giovannibaratta committed May 23, 2024
1 parent bb91b06 commit 7ba2522
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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[] = []

Expand Down Expand Up @@ -115,6 +117,10 @@ function App() {
}

const houseAgencyCalculator = buildHouseAgencyExpensesCalculator(houseAgencyCosts)
const miscellaneousCostsCalculator = buildMiscellaneousExpensesCalculator({
tari: miscellaneousState.tari,
canoneRai: miscellaneousState.canoneRai
})

while (simulationCurrentDate.getTime() < simulationEndingDate.getTime()) {

Expand All @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions src/model/miscellaneous.ts
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})
}
}

}
}

0 comments on commit 7ba2522

Please sign in to comment.