Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wichtounet committed Oct 14, 2023
1 parent 72448d7 commit a2dbe6d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
16 changes: 16 additions & 0 deletions include/views.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ std::string get_account_name(size_t id);

namespace detail {

struct monthly_only_adaptor {
template <std::ranges::range R>
friend auto operator|(R&& r, monthly_only_adaptor) {
return std::forward<R>(r) | std::views::filter([](const auto& objective) { return objective.type == "monthly"; });
}
};

struct yearly_only_adaptor {
template <std::ranges::range R>
friend auto operator|(R&& r, yearly_only_adaptor) {
return std::forward<R>(r) | std::views::filter([](const auto& objective) { return objective.type == "yearly"; });
}
};

struct share_based_only_adaptor {
template <std::ranges::range R>
friend auto operator|(R&& r, share_based_only_adaptor) {
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 20 additions & 31 deletions src/wishes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "cpp_utils/string.hpp"

#include "views.hpp"
#include "wishes.hpp"
#include "objectives.hpp"
#include "expenses.hpp"
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit a2dbe6d

Please sign in to comment.