diff --git a/Project.toml b/Project.toml index 6b0bd0c..54d16d0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "RetirementPlanners" uuid = "2683bf95-d0b8-4c71-a7d3-b42f78bf1cf0" authors = ["itsdfish"] -version = "0.4.1" +version = "0.4.2" [deps] ConcreteStructs = "2569d6c7-a4a2-43d3-a901-331e8e4be471" diff --git a/src/core.jl b/src/core.jl index 0695271..c099b21 100644 --- a/src/core.jl +++ b/src/core.jl @@ -224,7 +224,7 @@ function transact( income.amount.adjust ? nothing : (return income.amount.amount) (; Δt, state, start_age) = model (; amount) = income - amount.amount = start_age ≈ t ? (amount.initial_amount) : amount.amount + amount.amount = (start_age + Δt) ≈ t ? (amount.initial_amount) : amount.amount r = (1 + state.inflation_rate)^Δt amount.amount /= r return amount.amount diff --git a/src/update_functions/withdraw.jl b/src/update_functions/withdraw.jl index 9e985e7..fd1446c 100644 --- a/src/update_functions/withdraw.jl +++ b/src/update_functions/withdraw.jl @@ -33,7 +33,11 @@ end function _withdraw!(model::AbstractModel, t, withdraw::AbstractTransaction) (; Δt) = model if can_transact(withdraw, t; Δt) - model.state.withdraw_amount += transact(model, withdraw; t) + withdraw_amount = transact(model, withdraw; t) + if model.state.net_worth < withdraw_amount + withdraw_amount = model.state.net_worth + end + model.state.withdraw_amount += withdraw_amount end return nothing end @@ -63,8 +67,5 @@ function transact( withdraw_amount = max(withdraw_amount, min_withdraw) withdraw_amount = max(withdraw_amount - state.income_amount * income_adjustment, 0) - if state.net_worth < withdraw_amount - withdraw_amount = state.net_worth - end return withdraw_amount end diff --git a/test/transaction.jl b/test/transaction.jl index a5c8062..baf8c18 100644 --- a/test/transaction.jl +++ b/test/transaction.jl @@ -19,8 +19,8 @@ amount = transact(model, transaction; t = 2) @test amount ≈ 100 / 1.03^(2 / 12) atol = 1e-10 - # reset amount - amount = transact(model, transaction; t = 1) + # reset amount (recorded time starts at start_time + Δt) + amount = transact(model, transaction; t = 1 + 1 / 12) @test amount ≈ 100 / 1.03^(1 / 12) atol = 1e-10 end