diff --git a/include/views.hpp b/include/views.hpp index 80cb125..cc971a7 100644 --- a/include/views.hpp +++ b/include/views.hpp @@ -24,6 +24,20 @@ std::string get_account_name(size_t id); namespace detail { +struct monthly_only_adaptor { + template + friend auto operator|(R&& r, monthly_only_adaptor) { + return std::forward(r) | std::views::filter([](const auto& objective) { return objective.type == "monthly"; }); + } +}; + +struct yearly_only_adaptor { + template + friend auto operator|(R&& r, yearly_only_adaptor) { + return std::forward(r) | std::views::filter([](const auto& objective) { return objective.type == "yearly"; }); + } +}; + struct share_based_only_adaptor { template friend auto operator|(R&& r, share_based_only_adaptor) { @@ -348,6 +362,8 @@ inline constexpr detail::is_active_adaptor is_active; inline constexpr detail::active_today_adaptor active_today; inline constexpr detail::only_open_ended_adaptor only_open_ended; inline constexpr detail::not_open_ended_adaptor not_open_ended; +inline constexpr detail::monthly_only_adaptor monthly_only; +inline constexpr detail::yearly_only_adaptor yearly_only; inline constexpr detail::share_based_only_adaptor share_based_only; inline constexpr detail::not_share_based_adaptor not_share_based; inline constexpr detail::to_name_adaptor to_name; diff --git a/src/wishes.cpp b/src/wishes.cpp index adcb444..fd13b93 100644 --- a/src/wishes.cpp +++ b/src/wishes.cpp @@ -11,6 +11,7 @@ #include "cpp_utils/string.hpp" +#include "views.hpp" #include "wishes.hpp" #include "objectives.hpp" #include "expenses.hpp" @@ -413,33 +414,27 @@ void budget::estimate_wishes(budget::writer& w) { bool month_objective = true; bool year_objective = true; - for (auto& objective : w.cache.objectives()) { - if (objective.type == "monthly") { - auto success_after = budget::compute_success(month_status.add_expense(wish.amount), objective); + for (auto& objective : w.cache.objectives() | monthly_only) { + auto success_after = budget::compute_success(month_status.add_expense(wish.amount), objective); - if (success_after < 100) { - month_objective = false; - } - } else if (objective.type == "yearly") { - auto success_after = budget::compute_success(year_status.add_expense(wish.amount), objective); + if (success_after < 100) { + month_objective = false; + } + } - if (success_after < 100) { - year_objective = false; - } + for (auto& objective : w.cache.objectives() | yearly_only) { + auto success_after = budget::compute_success(year_status.add_expense(wish.amount), objective); + + if (success_after < 100) { + year_objective = false; } } if (fortune_amount >= wish.amount && month_objective && year_objective) { - if (wish.amount >= month_status.budget) { - if (year_status.balance > wish.amount) { - status = day.month().as_short_string() + " " + to_string(day.year()); - ok = true; - } - } else { - if (month_status.balance > wish.amount) { - status = day.month().as_short_string() + " " + to_string(day.year()); - ok = true; - } + if ((wish.amount >= month_status.budget && year_status.balance > wish.amount) + || (wish.amount < month_status.budget && month_status.balance > wish.amount)) { + status = day.month().as_short_string() + " " + to_string(day.year()); + ok = true; } } } @@ -472,16 +467,10 @@ void budget::estimate_wishes(budget::writer& w) { } if (fortune_amount >= wish.amount && month_objective) { - if (wish.amount >= month_status.budget) { - if (year_status.balance > wish.amount) { - status = day.month().as_short_string() + " " + to_string(day.year()); - ok = true; - } - } else { - if (month_status.balance > wish.amount) { - status = day.month().as_short_string() + " " + to_string(day.year()); - ok = true; - } + if ((wish.amount >= month_status.budget && year_status.balance > wish.amount) + || (wish.amount < month_status.budget && month_status.balance > wish.amount)) { + status = day.month().as_short_string() + " " + to_string(day.year()); + ok = true; } } }