@@ -54,7 +54,11 @@ std::map<mmDate, double> BalanceReport::loadAccountBalance_mDate(const AccountDa
5454
5555double BalanceReport::getCheckingBalance (const AccountData* account_n, const mmDate& date)
5656{
57- std::map<mmDate, double > date_balance_m = m_account_balance_mDate_mId[account_n->m_id ];
57+ const auto account_it = m_account_balance_mDate_mId.find (account_n->m_id );
58+ if (account_it == m_account_balance_mDate_mId.end ())
59+ return account_n->m_open_balance ;
60+
61+ const auto & date_balance_m = account_it->second ;
5862 auto const & it = std::upper_bound (date_balance_m.rbegin (), date_balance_m.rend (),
5963 std::pair<mmDate, double >(date, 0 ),
6064 [](const std::pair<mmDate, double > x, std::pair<mmDate, double > y) {
@@ -121,19 +125,39 @@ wxString BalanceReport::getHTMLText()
121125 _t (" Accounts Balance - %s" ),
122126 m_period_id == PERIOD_ID::MONTH ? _t (" Monthly Report" ) : _t (" Yearly Report" )
123127 );
124- hb.addReportHeader (name);
128+ hb.addReportHeader (name,
129+ m_date_range ? m_date_range->startDay () : 0 ,
130+ m_date_range ? m_date_range->isFutureIgnored () : false
131+ );
132+ if (m_date_range)
133+ hb.displayDateHeading (m_date_range);
125134
126135 m_currencyDateRateCache.clear ();
127136 m_stock_xa.clear ();
128137
129- mmDate start_date = mmDate::today ();
138+ mmDate selected_start_date = m_date_range
139+ ? mmDate (m_date_range->start_date ())
140+ : mmDate::today ();
141+ mmDate selected_end_date = m_date_range
142+ ? mmDate (m_date_range->end_date ())
143+ : mmDate::today ();
144+
145+ if (selected_end_date > mmDate::today ())
146+ selected_end_date = mmDate::today ();
147+
148+ mmDate start_date = selected_start_date;
149+ bool has_included_accounts = false ;
150+ mmDate earliest_open_date = selected_end_date;
130151 // Calculate the report date
131152 for (const auto & account_d : AccountModel::instance ().find_all ()) {
132153 if (m_account_a && wxNOT_FOUND == m_account_a->Index (account_d.m_name ))
133154 continue ;
134155
135- if (account_d.m_open_date < start_date)
136- start_date = account_d.m_open_date ;
156+ if (!has_included_accounts || account_d.m_open_date < earliest_open_date) {
157+ earliest_open_date = account_d.m_open_date ;
158+ has_included_accounts = true ;
159+ }
160+
137161 m_account_balance_mDate_mId[account_d.m_id ] = loadAccountBalance_mDate (account_d);
138162 if (AccountModel::type_id (account_d) != mmNavigatorItem::TYPE_ID_INVESTMENT)
139163 continue ;
@@ -152,7 +176,11 @@ wxString BalanceReport::getHTMLText()
152176 }
153177 }
154178
155- wxDateTime end_datetime = mmDate::today ().dateTime ();
179+ // Skip leading periods where selected accounts cannot have any balance yet.
180+ if (has_included_accounts && start_date < earliest_open_date)
181+ start_date = earliest_open_date;
182+
183+ wxDateTime end_datetime = selected_end_date.dateTime ();
156184 wxDateSpan span;
157185 if (m_period_id == PERIOD_ID::MONTH) {
158186 end_datetime.SetToLastMonthDay (end_datetime.GetMonth (), end_datetime.GetYear ());
0 commit comments