diff --git a/include/accounts.hpp b/include/accounts.hpp index 4d91e01..a8a2548 100644 --- a/include/accounts.hpp +++ b/include/accounts.hpp @@ -62,6 +62,7 @@ std::vector current_accounts(data_cache & cache); budget::account get_account(size_t id); budget::account get_account(std::string_view name, year year, month month); +std::string get_account_name(size_t id); void set_accounts_changed(); void set_accounts_next_id(size_t next_id); diff --git a/include/views.hpp b/include/views.hpp index 6ba9ba4..80cb125 100644 --- a/include/views.hpp +++ b/include/views.hpp @@ -18,6 +18,8 @@ namespace ranges = std::ranges; namespace budget { +std::string get_account_name(size_t id); + // We define views without parameters as an adaptor to avoid having to use () namespace detail { @@ -222,6 +224,10 @@ inline auto filter_by_account(size_t id) { return std::views::filter([id] (const auto & expense) { return expense.account == id; }); } +inline auto filter_by_account_name(std::string_view name) { + return std::views::filter([name] (const auto & expense) { return get_account_name(expense.account) == name; }); +} + inline auto filter_by_asset(size_t id) { return std::views::filter([id] (const auto & share) { return share.asset_id == id; }); } diff --git a/src/accounts.cpp b/src/accounts.cpp index 2aba23f..76e2f64 100644 --- a/src/accounts.cpp +++ b/src/accounts.cpp @@ -375,6 +375,10 @@ budget::account budget::get_account(size_t id){ return accounts[id]; } +std::string budget::get_account_name(size_t id){ + return accounts[id].name; +} + budget::account budget::get_account(std::string_view name, budget::year year, budget::month month) { if (auto range = accounts.data() | active_at_date({year, month, 5}) | filter_by_name(name); range) { return *std::ranges::begin(range); diff --git a/src/overview.cpp b/src/overview.cpp index ac25ec8..b86c024 100644 --- a/src/overview.cpp +++ b/src/overview.cpp @@ -571,16 +571,10 @@ void display_values(budget::writer& w, budget::year year, const std::string& tit for(auto& account : all_accounts(w.cache, year, m)){ budget::money month_total; - for(auto& value : values){ - if(relaxed){ - if(get_account(value.account).name == account.name && value.date.year() == year && value.date.month() == m){ - month_total += value.amount; - } - } else { - if(value.account == account.id && value.date.year() == year && value.date.month() == m){ - month_total += value.amount; - } - } + if (relaxed) { + month_total = fold_left_auto(values | filter_by_account_name(account.name) | filter_by_date(year, m) | to_amount); + } else { + month_total = fold_left_auto(values | filter_by_account(account.id) | filter_by_date(year, m) | to_amount); } contents[row_mapping[account.name]].push_back(to_string(month_total));