Skip to content

Commit e338e62

Browse files
authored
Merge pull request moneymanagerex#8320 from JoshuaLampert/summary-of-accounts-report-accounts
Add selection between accounts and account types in summary of accounts report
2 parents 02f2958 + ca689e9 commit e338e62

File tree

4 files changed

+110
-30
lines changed

4 files changed

+110
-30
lines changed

src/panel/ReportPanel.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ void ReportPanel::createControls()
482482
int idx = w_selection_choice->FindString(removeQuotes(map["default"]));
483483
w_selection_choice->SetSelection(idx != wxNOT_FOUND ? idx : 0);
484484
}
485+
int sel = m_rb->getGenericSelection();
486+
if (sel >= 0 && static_cast<unsigned int>(sel) < w_selection_choice->GetCount()) {
487+
w_selection_choice->SetSelection(sel);
488+
}
485489
itemBoxSizerHeader->Add(w_selection_choice, 0, wxALL | wxALIGN_CENTER_VERTICAL, 1);
486490
itemBoxSizerHeader->AddSpacer(30);
487491
}
@@ -721,6 +725,10 @@ bool ReportPanel::saveReportText()
721725
m_rb->setDateSelection(id);
722726
}
723727

728+
if ((m_rb->getParameters() & ReportBase::M_GENERIC_SELECTION) && w_selection_choice) {
729+
m_rb->setGenericSelection(w_selection_choice->GetSelection());
730+
}
731+
724732
StringBuffer json_buffer;
725733
Writer<StringBuffer> json_writer(json_buffer);
726734

src/report/BalanceReport.cpp

Lines changed: 78 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ BalanceReport::BalanceReport(BalanceReport::PERIOD_ID period_id) :
3737
? REPORT_ID::MonthlySummaryofAccounts
3838
: REPORT_ID::YearlySummaryofAccounts
3939
);
40+
m_selection_map["name"] = _t("View");
41+
m_selection_map["values"] = _t("Account Types") + "," + _t("Accounts");
42+
m_selection_map["default"] = _t("Account Types");
4043
}
4144

4245
std::map<mmDate, double> BalanceReport::loadAccountBalance_mDate(const AccountData& account_d)
@@ -114,9 +117,10 @@ wxString BalanceReport::getHTMLText()
114117
std::vector<BalanceEntry> date_balanceA_a;
115118

116119
GraphData gd;
120+
const bool view_accounts = (getGenericSelection() == 1);
117121
int acc_size = mmNavigatorList::instance().getNumberOfAccountTypes();
118-
// +1 as we add balance to the end;
119-
std::vector<GraphSeries> gs_data(acc_size + 1);
122+
const auto account_data_a = AccountModel::instance().find_all();
123+
const auto asset_data_a = AssetModel::instance().find_all();
120124

121125
std::vector<mmDate> end_date_a;
122126

@@ -148,11 +152,16 @@ wxString BalanceReport::getHTMLText()
148152
mmDate start_date = selected_start_date;
149153
bool has_included_accounts = false;
150154
mmDate earliest_open_date = selected_end_date;
155+
std::vector<wxString> series_name_a;
156+
151157
// Calculate the report date
152-
for (const auto& account_d : AccountModel::instance().find_all()) {
158+
for (const auto& account_d : account_data_a) {
153159
if (m_account_a && wxNOT_FOUND == m_account_a->Index(account_d.m_name))
154160
continue;
155161

162+
if (view_accounts)
163+
series_name_a.push_back(account_d.m_name);
164+
156165
if (!has_included_accounts || account_d.m_open_date < earliest_open_date) {
157166
earliest_open_date = account_d.m_open_date;
158167
has_included_accounts = true;
@@ -176,6 +185,25 @@ wxString BalanceReport::getHTMLText()
176185
}
177186
}
178187

188+
const bool include_asset_series = view_accounts && !asset_data_a.empty();
189+
if (include_asset_series)
190+
series_name_a.push_back(_t("Assets"));
191+
192+
if (!view_accounts) {
193+
for (int i = 0; i < acc_size; ++i)
194+
series_name_a.push_back(mmNavigatorList::instance().getAccountTypeName(i));
195+
}
196+
197+
const int series_count = static_cast<int>(series_name_a.size());
198+
// +1 as we add total balance series to the end.
199+
std::vector<GraphSeries> gs_data(series_count + 1);
200+
for (int i = 0; i < series_count; ++i) {
201+
gs_data[i].name = series_name_a[i];
202+
gs_data[i].type = "column";
203+
}
204+
gs_data[series_count].name = _t("Balance");
205+
gs_data[series_count].type = "line";
206+
179207
// Skip leading periods where selected accounts cannot have any balance yet.
180208
if (has_included_accounts && start_date < earliest_open_date)
181209
start_date = earliest_open_date;
@@ -209,54 +237,76 @@ wxString BalanceReport::getHTMLText()
209237
date_balanceA.date = end_date;
210238
double total = 0.0;
211239

212-
std::vector<double> balance_a(acc_size +1);
240+
std::vector<double> balance_a(series_count);
213241
std::fill(balance_a.begin(), balance_a.end(), 0.0);
214-
int idx;
215-
for (const auto& account_d : AccountModel::instance().find_all()) {
242+
int idx = 0;
243+
int type_idx;
244+
for (const auto& account_d : account_data_a) {
216245
if (m_account_a && wxNOT_FOUND == m_account_a->Index(account_d.m_name))
217246
continue;
218247

219-
idx = mmNavigatorList::instance().getAccountTypeIdx(account_d.m_type_);
220-
if (idx == -1) {
221-
idx = mmNavigatorList::instance().getAccountTypeIdx(mmNavigatorItem::TYPE_ID_CHECKING);
222-
}
223-
if (idx > -1) {
248+
if (view_accounts) {
224249
double rate = getCurrencyDateRate(account_d.m_currency_id, end_date);
225250
std::pair<double, double> dailybal = getBalance(&account_d, end_date);
226-
balance_a[idx] += dailybal.first * rate;
251+
balance_a[idx] = dailybal.first * rate;
227252
if (AccountModel::type_id(account_d) == mmNavigatorItem::TYPE_ID_INVESTMENT) {
228253
balance_a[idx] += dailybal.second * rate;
229254
}
255+
idx++;
256+
}
257+
else {
258+
type_idx = mmNavigatorList::instance().getAccountTypeIdx(account_d.m_type_);
259+
if (type_idx == -1) {
260+
type_idx = mmNavigatorList::instance().getAccountTypeIdx(mmNavigatorItem::TYPE_ID_CHECKING);
261+
}
262+
if (type_idx > -1) {
263+
double rate = getCurrencyDateRate(account_d.m_currency_id, end_date);
264+
std::pair<double, double> dailybal = getBalance(&account_d, end_date);
265+
balance_a[type_idx] += dailybal.first * rate;
266+
if (AccountModel::type_id(account_d) == mmNavigatorItem::TYPE_ID_INVESTMENT) {
267+
balance_a[type_idx] += dailybal.second * rate;
268+
}
269+
}
230270
}
231271
}
232272

233-
idx = mmNavigatorList::instance().getAccountTypeIdx(mmNavigatorItem::TYPE_ID_ASSET);
234-
if (idx > -1) {
235-
for (const auto& asset_d : AssetModel::instance().find_all()) {
236-
double rate = getCurrencyDateRate(asset_d.m_currency_id_n, end_date);
237-
balance_a[idx] += AssetModel::instance().get_data_value_date(
238-
asset_d, end_date
239-
).second * rate;
273+
if (view_accounts) {
274+
if (include_asset_series) {
275+
double asset_balance = 0.0;
276+
for (const auto& asset_d : asset_data_a) {
277+
double rate = getCurrencyDateRate(asset_d.m_currency_id_n, end_date);
278+
asset_balance += AssetModel::instance().get_data_value_date(
279+
asset_d, end_date
280+
).second * rate;
281+
}
282+
balance_a[idx] = asset_balance;
283+
}
284+
}
285+
else {
286+
type_idx = mmNavigatorList::instance().getAccountTypeIdx(mmNavigatorItem::TYPE_ID_ASSET);
287+
if (type_idx > -1) {
288+
for (const auto& asset_d : asset_data_a) {
289+
double rate = getCurrencyDateRate(asset_d.m_currency_id_n, end_date);
290+
balance_a[type_idx] += AssetModel::instance().get_data_value_date(
291+
asset_d, end_date
292+
).second * rate;
293+
}
240294
}
241295
}
242296

243297
int k = -1;
244-
for (int i = 0; i < acc_size; ++i) {
298+
for (int i = 0; i < series_count; ++i) {
245299
date_balanceA.balance_a.push_back(balance_a[i]);
246300
gs_data[++k].values.push_back(balance_a[i]);
247301
total += balance_a[i];
248-
gs_data[k].name = mmNavigatorList::instance().getAccountTypeName(i);
249-
gs_data[k].type = "column";
250302
}
251303
date_balanceA.balance_a.push_back(total);
252304
date_balanceA_a.push_back(date_balanceA);
253305
gs_data[++k].values.push_back(total);
254-
gs_data[k].name = _t("Balance");
255-
gs_data[k].type = "line";
256306
}
257307

258308
std::vector<bool> is_visible_a;
259-
for (int i = 0; i < acc_size; i++) {
309+
for (int i = 0; i < series_count; i++) {
260310
bool av;
261311
for (double value : gs_data[i].values) {
262312
av = false;
@@ -272,7 +322,7 @@ wxString BalanceReport::getHTMLText()
272322

273323
// Chart
274324
if (getChartSelection() == 0) {
275-
for (int i = 0; i < (acc_size + 1); ++i) {
325+
for (int i = 0; i < (series_count + 1); ++i) {
276326
if (is_visible_a[i]) {
277327
gd.series.push_back(gs_data[i]);
278328
}
@@ -304,7 +354,7 @@ wxString BalanceReport::getHTMLText()
304354
hb.startTableRow();
305355
{
306356
hb.addTableHeaderCell(_t("Date"));
307-
for (int i = 0; i < acc_size + 1; i++) {
357+
for (int i = 0; i < series_count + 1; i++) {
308358
if (is_visible_a[i])
309359
hb.addTableHeaderCell(gs_data[i].name, "text-right");
310360
}
@@ -322,7 +372,7 @@ wxString BalanceReport::getHTMLText()
322372
hb.addTableCellMonth(dateTime.GetMonth(), dateTime.GetYear());
323373
else
324374
hb.addTableCell(wxString::Format("%d", dateTime.GetYear()));
325-
for (int i = 0; i < acc_size + 1; i++) {
375+
for (int i = 0; i < series_count + 1; i++) {
326376
if (is_visible_a[i])
327377
hb.addMoneyCell(date_balanceA.balance_a[i]);
328378
}

src/report/_ReportBase.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ void ReportBase::setReportParameters(ReportBase::REPORT_ID report_id)
5353

5454
switch (report_id) {
5555
case MyUsage: m_parameters = M_DATE_RANGE | M_CHART; break;
56-
case MonthlySummaryofAccounts: m_parameters = M_DATE_RANGE | M_ACCOUNT | M_CHART; break;
57-
case YearlySummaryofAccounts: m_parameters = M_DATE_RANGE | M_ACCOUNT | M_CHART; break;
56+
case MonthlySummaryofAccounts: m_parameters = M_DATE_RANGE | M_ACCOUNT | M_CHART | M_GENERIC_SELECTION; break;
57+
case YearlySummaryofAccounts: m_parameters = M_DATE_RANGE | M_ACCOUNT | M_CHART | M_GENERIC_SELECTION; break;
5858
case WheretheMoneyGoes: m_parameters = M_DATE_RANGE | M_ACCOUNT | M_CHART; break;
5959
case WheretheMoneyComesFrom: m_parameters = M_DATE_RANGE | M_ACCOUNT | M_CHART; break;
6060
case CategoriesSummary: m_parameters = M_DATE_RANGE | M_ACCOUNT | M_CHART; break;
@@ -92,12 +92,18 @@ void ReportBase::setAccounts(int selection, const wxString& type_name)
9292
{
9393
wxArrayString account_name_a;
9494
auto account_a = AccountModel::instance().find_all();
95+
const bool exclude_share_accounts =
96+
m_report_id == REPORT_ID::MonthlySummaryofAccounts ||
97+
m_report_id == REPORT_ID::YearlySummaryofAccounts;
9598
std::stable_sort(account_a.begin(), account_a.end(),
9699
AccountData::SorterByName()
97100
);
98101
for (const auto& account_d : account_a) {
99102
if (m_only_active && !account_d.is_open())
100103
continue;
104+
if (exclude_share_accounts &&
105+
AccountModel::type_id(account_d) == mmNavigatorItem::TYPE_ID_SHARES)
106+
continue;
101107
account_name_a.Add(account_d.m_name);
102108
}
103109

@@ -222,6 +228,13 @@ void ReportBase::saveReportSettings()
222228
json_writer.String(m_generic_filter.utf8_str());
223229
}
224230

231+
if (m_parameters & M_GENERIC_SELECTION)
232+
{
233+
isActive = true;
234+
json_writer.Key("GENERIC_SELECTION");
235+
json_writer.Int(m_generic_selection);
236+
}
237+
225238
json_writer.EndObject();
226239

227240
if (isActive) {
@@ -262,6 +275,10 @@ void ReportBase::restoreReportSettings()
262275
m_generic_filter = j_doc["GENERIC_FILTER"].GetString();
263276
}
264277

278+
if (j_doc.HasMember("GENERIC_SELECTION") && j_doc["GENERIC_SELECTION"].IsInt()) {
279+
m_generic_selection = j_doc["GENERIC_SELECTION"].GetInt();
280+
}
281+
265282
m_account_selection = -1;
266283
int selection = 0;
267284
int acc_size = mmNavigatorList::instance().getNumberOfAccountTypes();

src/report/_ReportBase.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class ReportBase
9090
bool m_only_active = false;
9191
int m_chart_selection = 0;
9292
int m_stock_selection = 0;
93+
int m_generic_selection = 0;
9394
wxString m_stock_name = "";
9495
wxString m_generic_filter = "";
9596
std::map<wxString, wxString> m_filter_map;
@@ -120,6 +121,7 @@ class ReportBase
120121
void setAccounts(int selection, const wxString& type_name);
121122
void setChartSelection(int selection);
122123
void setStockSelection(int selection);
124+
void setGenericSelection(int selection);
123125

124126
REPORT_ID getReportId() const;
125127
const wxString getReportSettings() const;
@@ -130,6 +132,7 @@ class ReportBase
130132
wxString getFilterValue() const;
131133
int getChartSelection() const;
132134
int getStockSelection() const;
135+
int getGenericSelection() const;
133136
void setStockName(const wxString& name);
134137

135138
void saveReportSettings();
@@ -159,6 +162,7 @@ inline void ReportBase::setDateSelection(int64 sel) { m_date_selection = sel; }
159162
inline void ReportBase::setForwardMonths(int sel) { m_forward_months = sel; }
160163
inline void ReportBase::setChartSelection(int selection) { m_chart_selection = selection; }
161164
inline void ReportBase::setStockSelection(int selection) { m_stock_selection = selection; }
165+
inline void ReportBase::setGenericSelection(int selection) { m_generic_selection = selection; }
162166
inline void ReportBase::setStockName(const wxString& name) { m_stock_name = name; }
163167

164168

@@ -171,6 +175,7 @@ inline int ReportBase::getForwardMonths() const { return this->m_forward_months;
171175
inline int ReportBase::getAccountSelection() const { return this->m_account_selection; }
172176
inline int ReportBase::getChartSelection() const { return this->m_chart_selection; }
173177
inline int ReportBase::getStockSelection() const { return this->m_stock_selection; }
178+
inline int ReportBase::getGenericSelection() const { return this->m_generic_selection; }
174179
inline wxString ReportBase::getFilterValue() const { return this->m_generic_filter; }
175180
inline std::map<wxString, wxString> ReportBase::getFilterMap() const { return this->m_filter_map; }
176181
inline std::map<wxString, wxString> ReportBase::getSelectionMap() const { return this->m_selection_map; }

0 commit comments

Comments
 (0)