Skip to content

Commit

Permalink
Revert "Revert "fix(moneymanagerex#4214): fix""
Browse files Browse the repository at this point in the history
This reverts commit faa821f.
  • Loading branch information
Mark Whalley committed Sep 14, 2022
1 parent faa821f commit fef5014
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/model/Model_Budget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void Model_Budget::getBudgetStats(
{
const wxDateTime d = start_date.Add(wxDateSpan::Months(m));
const int idx = d.GetYear() * 100 + d.GetMonth();
budgetStats[category.second.first][category.second.second][groupByMonth ? idx : 0] = value;
budgetStats[category.second.first][category.second.second][idx] = value;
}
}

Expand All @@ -135,7 +135,7 @@ void Model_Budget::getBudgetStats(
const wxString year = wxString::Format("%i", start_date.GetYear());
int budgetYearID = Model_Budgetyear::instance().Get(year);
for (const auto& budget : instance().find(BUDGETYEARID(budgetYearID)))
yearBudgetValue[budget.CATEGID][budget.SUBCATEGID] = getEstimate(true, period(budget), budget.AMOUNT);
yearBudgetValue[budget.CATEGID][budget.SUBCATEGID] = getEstimate(true, period(budget), budget.AMOUNT);

for (int m = 0; m < 12; m++)
{
Expand All @@ -144,7 +144,7 @@ void Model_Budget::getBudgetStats(
{
const wxDateTime d = start_date.Add(wxDateSpan::Months(m));
const int idx = d.GetYear() * 100 + d.GetMonth();
budgetStats[cat.first][id.first][groupByMonth ? idx : 0] += id.second;
budgetStats[cat.first][id.first][idx] += id.second;
}

const wxString budgetYearMonth = wxString::Format("%s-%02d", year, m + 1);
Expand All @@ -154,10 +154,22 @@ void Model_Budget::getBudgetStats(
const wxDateTime d = start_date.Add(wxDateSpan::Months(m));
const int idx = d.GetYear() * 100 + d.GetMonth();
if (Option::instance().BudgetOverride())
budgetStats[budget.CATEGID][budget.SUBCATEGID][groupByMonth ? idx : 0] = getEstimate(true, period(budget), budget.AMOUNT);
budgetStats[budget.CATEGID][budget.SUBCATEGID][idx] = getEstimate(true, period(budget), budget.AMOUNT);
else
budgetStats[budget.CATEGID][budget.SUBCATEGID][groupByMonth ? idx : 0] += getEstimate(true, period(budget), budget.AMOUNT);
}
budgetStats[budget.CATEGID][budget.SUBCATEGID][idx] += getEstimate(true, period(budget), budget.AMOUNT); }
}
if (!groupByMonth)
{
std::map<int, std::map<int, std::map<int, double> > > yearlyBudgetStats;
for (const auto& category : Model_Category::all_categories())
yearlyBudgetStats[category.second.first][category.second.second][0] = 0.0;

for (const auto& cat : budgetStats)
for (const auto& subcat : cat.second)
for (const auto& month : subcat.second)
yearlyBudgetStats[cat.first][subcat.first][0] += month.second;

budgetStats = yearlyBudgetStats;
}
}

Expand Down
37 changes: 36 additions & 1 deletion src/reports/incexpenses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ wxString mmReportIncomeExpensesMonthly::getHTMLText()
if (account) convRate = Model_CurrencyHistory::getDayRate(Model_Account::currency(account)->CURRENCYID, transaction.TRANSDATE);
int year = Model_Checking::TRANSDATE(transaction).GetYear();

int idx = (year * 100 + Model_Checking::TRANSDATE(transaction).GetMonth());
int idx = year * 100 + Model_Checking::TRANSDATE(transaction).GetMonth();

if (Model_Checking::type(transaction) == Model_Checking::DEPOSIT) {
incomeExpensesStats[idx].first += transaction.TRANSAMOUNT * convRate;
Expand All @@ -174,6 +174,19 @@ wxString mmReportIncomeExpensesMonthly::getHTMLText()
}
}

// Grab related budget data
std::map<int, double> goalStats;
for (const auto &stats : incomeExpensesStats)
{
int year = stats.first / 100;
int month = stats.first % 100 + 1;
const wxString yearMonthName = wxString::Format("%4d-%02d", year, month);
const double est = Model_Budget::getMonthlyBudget(yearMonthName);
if (est != 0)
goalStats[stats.first] = est;
}
bool displayGoals = goalStats.size() > 0;

// Build the report
mmHTMLBuilder hb;
hb.init();
Expand Down Expand Up @@ -245,6 +258,11 @@ wxString mmReportIncomeExpensesMonthly::getHTMLText()
hb.addTableHeaderCell(_("Income"), "text-right");
hb.addTableHeaderCell(_("Expenses"), "text-right");
hb.addTableHeaderCell(_("Difference"), "text-right");
if (displayGoals)
{
hb.addTableHeaderCell(_("Budget Goal"), "text-right");
hb.addTableHeaderCell(_("Budget Goal%"), "text-right");
}
hb.addTableHeaderCell(_("Cumulative"), "text-right");
hb.endTableRow();
}
Expand All @@ -253,6 +271,7 @@ wxString mmReportIncomeExpensesMonthly::getHTMLText()

double total_expenses = 0.0;
double total_income = 0.0;
double total_goal = 0.0;
hb.startTbody();
for (const auto &stats : incomeExpensesStats)
{
Expand All @@ -265,6 +284,16 @@ wxString mmReportIncomeExpensesMonthly::getHTMLText()
hb.addMoneyCell(stats.second.first);
hb.addMoneyCell(stats.second.second);
hb.addMoneyCell(stats.second.first - stats.second.second);
if (displayGoals)
if ((goalStats.count(stats.first) > 0) && (goalStats.at(stats.first) > 0))
{
const double goal = goalStats.at(stats.first);
hb.addMoneyCell(goal);
const double percentage = 100.0 * goal / (stats.second.first - stats.second.second);
hb.addTableCell(wxString::Format("%.2f", percentage), true);
total_goal += goal;
} else
hb.addEmptyTableCell(2);
hb.addMoneyCell(total_income - total_expenses);
}
hb.endTableRow();
Expand All @@ -275,6 +304,12 @@ wxString mmReportIncomeExpensesMonthly::getHTMLText()
totals.push_back(total_income);
totals.push_back(total_expenses);
totals.push_back(total_income - total_expenses);
if (displayGoals)
{
totals.push_back(total_goal);
const double percentage = std::ceil(10000.0 * total_goal / (total_income - total_expenses)) / 100.0;
totals.push_back(percentage);
}
totals.push_back(total_income - total_expenses);

hb.addMoneyTotalRow(_("Total:"), 5, totals);
Expand Down

0 comments on commit fef5014

Please sign in to comment.