|
| 1 | +//======================================================================= |
| 2 | +// Copyright (c) 2013-2020 Baptiste Wicht. |
| 3 | +// Distributed under the terms of the MIT License. |
| 4 | +// (See accompanying file LICENSE or copy at |
| 5 | +// http://opensource.org/licenses/MIT) |
| 6 | +//======================================================================= |
| 7 | + |
| 8 | +#include "api/server_api.hpp" |
| 9 | +#include "api/retirement_api.hpp" |
| 10 | + |
| 11 | +#include "assets.hpp" |
| 12 | +#include "liabilities.hpp" |
| 13 | +#include "accounts.hpp" |
| 14 | +#include "data.hpp" |
| 15 | +#include "data_cache.hpp" |
| 16 | +#include "retirement.hpp" |
| 17 | + |
| 18 | +#include "pages/web_config.hpp" |
| 19 | + |
| 20 | +using namespace budget; |
| 21 | + |
| 22 | +void budget::retirement_countdown_api(const httplib::Request& req, httplib::Response& res) { |
| 23 | + if (auto fi_expenses = budget::get_fi_expenses()){ |
| 24 | + data_cache cache; |
| 25 | + |
| 26 | + const auto nw = get_fi_net_worth(cache); |
| 27 | + const auto savings_rate = running_savings_rate(cache); |
| 28 | + const auto income = running_income(cache); |
| 29 | + |
| 30 | + const auto currency = get_default_currency(); |
| 31 | + const auto wrate = to_number<double>(internal_config_value("withdrawal_rate")); |
| 32 | + const auto roi = to_number<double>(internal_config_value("expected_roi")); |
| 33 | + const auto years = double(100.0 / wrate); |
| 34 | + const auto fi_goal = years * fi_expenses; |
| 35 | + |
| 36 | + date_type fi_base_months = 0; |
| 37 | + { |
| 38 | + auto current_nw = nw; |
| 39 | + while (current_nw < fi_goal) { |
| 40 | + current_nw *= 1.0 + (roi / 100.0) / 12; |
| 41 | + current_nw += (savings_rate * income) / 12; |
| 42 | + |
| 43 | + ++fi_base_months; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (nw > fi_goal) { |
| 48 | + api_success_content(req, res, "You are FI!"); |
| 49 | + } else { |
| 50 | + // TODO Add days granularity |
| 51 | + |
| 52 | + const auto missing_years = unsigned(fi_base_months / 12); |
| 53 | + const auto missing_months = unsigned(fi_base_months - (missing_years * 12)); |
| 54 | + |
| 55 | + auto result = std::format("{} years, {} months", missing_years, missing_months); |
| 56 | + |
| 57 | + api_success_content(req, res, result); |
| 58 | + } |
| 59 | + } else { |
| 60 | + api_success_content(req, res, "No expenses"); |
| 61 | + } |
| 62 | +} |
0 commit comments