Skip to content

Commit

Permalink
Merge pull request #5112 from whalley/master
Browse files Browse the repository at this point in the history
fix(#5111): asset widget detail
  • Loading branch information
whalley committed Sep 22, 2022
2 parents 652aa20 + 243223a commit e2d0ace
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
2 changes: 2 additions & 0 deletions resources/home_page.htt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
<!--Stocks-->
<TMPL_VAR STOCKS_INFO>
<!--Assets-->
<TMPL_VAR ASSETS_INFO>
<!--Currency-->
<TMPL_VAR CURRENCY_RATES>
</td>
<td>
Expand Down
73 changes: 59 additions & 14 deletions src/mmhomepage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,24 +575,69 @@ htmlWidgetGrandTotals::~htmlWidgetGrandTotals()
{
}

const wxString htmlWidgetAssets::getHTMLText(double& tBalance)
const wxString htmlWidgetAssets::getHTMLText()
{
double asset_balance = Model_Asset::instance().balance();
tBalance += asset_balance;
const int MAX_ASSETS = 10;
wxString output = "";
output = R"(<div class="shadow">)";
output += "<table class ='sortable table'><col style='width: 50%'><col style='width: 25%'><col style='width: 25%'><thead><tr class='active'>\n";
output += "<th>" + _("Assets") + "</th>";
output += "<th class='text-right'>" + _("Initial Value") + "</th>\n";
output += "<th class='text-right'>" + _("Current Value") + "</th>\n";
output += wxString::Format("<th nowrap class='text-right sorttable_nosort'><a id='%s_label' onclick='toggleTable(\"%s\");' href='#%s' oncontextmenu='return false;'>[-]</a></th>\n"
, "ASSETS", "ASSETS", "ASSETS");
output += "</tr></thead><tbody id='ASSETS'>\n";

Model_Asset::Data_Set assets = Model_Asset::instance().all();
std::stable_sort(assets.begin(), assets.end(), SorterByVALUE());
std::reverse(assets.begin(), assets.end());

int rows = 0;
double initialDisplayed = 0.0;
double initialTotal = 0.0;
double currentDisplayed = 0.0;
double currentTotal = 0.0;
for (const auto& asset : assets)
{
double initial = asset.VALUE;
double current = Model_Asset::value(asset);
initialTotal += initial;
currentTotal += current;
if (rows++ < MAX_ASSETS)
{
initialDisplayed += initial;
currentDisplayed += current;
output += "<tr>";
output += wxString::Format("<td sorttable_customkey='*%s*'>%s</td>\n"
, asset.ASSETNAME, asset.ASSETNAME);
output += wxString::Format("<td class='money' sorttable_customkey='%f'>%s</td>\n"
, initial, Model_Currency::toCurrency(initial));
output += wxString::Format("<td colspan='2' class='money' sorttable_customkey='%f'>%s</td>\n"
, current, Model_Currency::toCurrency(current));
output += "</tr>";
}
}
if (rows > MAX_ASSETS)
{
output += "<tr>";
output += wxString::Format("<td sorttable_customkey='*%s*'>%s (%i)</td>\n"
, _("Other Assets"), _("Other Assets"), rows - MAX_ASSETS);
output += wxString::Format("<td class='money' sorttable_customkey='%f'>%s</td>\n"
, initialTotal - initialDisplayed, Model_Currency::toCurrency(initialTotal - initialDisplayed));
output += wxString::Format("<td class='money' sorttable_customkey='%f'>%s</td>\n"
, currentTotal - currentDisplayed, Model_Currency::toCurrency(currentTotal - currentDisplayed));
output += "</tr>";
}

StringBuffer json_buffer;
PrettyWriter<StringBuffer> json_writer(json_buffer);
json_writer.StartObject();
json_writer.Key("NAME");
json_writer.String(_("Assets").utf8_str());
json_writer.Key("VALUE");
json_writer.String(Model_Currency::toCurrency(asset_balance).utf8_str());
json_writer.EndObject();
output += "</tbody><tfoot><tr class = 'total'><td>" + _("Total:") + "</td>";
output += wxString::Format("<td class='money'>%s</td>\n"
, Model_Currency::toCurrency(initialTotal));
output += wxString::Format("<td colspan='2' class='money'>%s</td></tr></tfoot></table>\n"
, Model_Currency::toCurrency(currentTotal));

wxLogDebug("======= mmHomePagePanel::getAssetsJSON =======");
wxLogDebug("RapidJson\n%s", wxString::FromUTF8(json_buffer.GetString()));
output += "</div>";

return wxString::FromUTF8(json_buffer.GetString());
return output;
}

htmlWidgetAssets::~htmlWidgetAssets()
Expand Down
2 changes: 1 addition & 1 deletion src/mmhomepage.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class htmlWidgetAssets
{
public:
~htmlWidgetAssets();
const wxString getHTMLText(double& tBalance);
const wxString getHTMLText();
};

class htmlWidgetAccounts
Expand Down
3 changes: 2 additions & 1 deletion src/mmhomepagepanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ void mmHomePagePanel::insertDataIntoTemplate()
tBalance += stocks_widget.get_total();

htmlWidgetAssets assets;
m_frames["ASSETS_INFO"] = assets.getHTMLText(tBalance);
m_frames["ASSETS_INFO"] = assets.getHTMLText();
tBalance += Model_Asset::instance().balance();

htmlWidgetGrandTotals grand_totals;
m_frames["GRAND_TOTAL"] = grand_totals.getHTMLText(tBalance, tReconciled
Expand Down

0 comments on commit e2d0ace

Please sign in to comment.