Skip to content

Commit

Permalink
Merge pull request #5075 from whalley/master
Browse files Browse the repository at this point in the history
fix(#4214): merge budgets
  • Loading branch information
vomikan committed Sep 14, 2022
2 parents ad7e0b9 + 617ba61 commit 08ff621
Show file tree
Hide file tree
Showing 14 changed files with 269 additions and 182 deletions.
2 changes: 2 additions & 0 deletions src/constants.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************
Copyright (C) 2009 VaDiM
Copyright (C) 2022 Mark Whalley (mark@ipx.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -198,6 +199,7 @@ const wxString INIDB_BUDGET_FINANCIAL_YEARS = "BUDGET_FINANCIAL_YEARS";
const wxString INIDB_BUDGET_INCLUDE_TRANSFERS = "BUDGET_INCLUDE_TRANSFERS";
const wxString INIDB_BUDGET_SETUP_WITHOUT_SUMMARY = "BUDGET_SETUP_WITHOUT_SUMMARY";
const wxString INIDB_BUDGET_SUMMARY_WITHOUT_CATEG = "BUDGET_SUMMARY_WITHOUT_CATEGORIES";
const wxString INIDB_BUDGET_OVERRIDE = "BUDGET_OVERRIDE";
const wxString INIDB_IGNORE_FUTURE_TRANSACTIONS = "IGNORE_FUTURE_TRANSACTIONS";
const wxString INIDB_SHOW_TOOLTIPS = "IGNORE_SHOW_TOOLTIPS";
const wxString INIDB_SHOW_MONEYTIPS = "IGNORE_SHOW_MONEYTIPS";
Expand Down
3 changes: 2 additions & 1 deletion src/constants.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************
Copyright (C) 2009 VaDiM
Copyright (C) 2021 Mark Whalley (mark@ipx.co.uk)
Copyright (C) 2021-2022 Mark Whalley (mark@ipx.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -123,6 +123,7 @@ extern const wxString INIDB_HIDE_SHARE_ACCOUNTS;
extern const wxString INIDB_BUDGET_FINANCIAL_YEARS;
extern const wxString INIDB_BUDGET_INCLUDE_TRANSFERS;
extern const wxString INIDB_BUDGET_SUMMARY_WITHOUT_CATEG;
extern const wxString INIDB_BUDGET_OVERRIDE;
extern const wxString INIDB_IGNORE_FUTURE_TRANSACTIONS;
extern const wxString INIDB_SHOW_TOOLTIPS;
extern const wxString INIDB_SHOW_MONEYTIPS;
Expand Down
12 changes: 6 additions & 6 deletions src/mmframereport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ void mmGUIFrame::DoUpdateReportNavigation(wxTreeItemId& parent_item)
size_t i = Model_Budgetyear::instance().all().size();
if (i > 0)
{
if (hidden_reports.Index("Budget Performance") == wxNOT_FOUND)
if (hidden_reports.Index("Budgets") == wxNOT_FOUND)
{
wxTreeItemId budgetPerformance = m_nav_tree_ctrl->AppendItem(parent_item, _("Budget Performance"), img::PIECHART_PNG, img::PIECHART_PNG);
wxTreeItemId budgetReports = m_nav_tree_ctrl->AppendItem(parent_item, _("Budgets"), img::PIECHART_PNG, img::PIECHART_PNG);
m_nav_tree_ctrl->SetItemData(budgetReports, new mmTreeItemData(mmTreeItemData::MENU_REPORT, "Budgets"));

wxTreeItemId budgetPerformance = m_nav_tree_ctrl->AppendItem(budgetReports, _("Budget Performance"), img::PIECHART_PNG, img::PIECHART_PNG);
m_nav_tree_ctrl->SetItemData(budgetPerformance, new mmTreeItemData("Budget Performance", new mmReportBudgetingPerformance()));
}

if (hidden_reports.Index("Budget Category Summary") == wxNOT_FOUND)
{
wxTreeItemId budgetSetupPerformance = m_nav_tree_ctrl->AppendItem(parent_item, _("Budget Category Summary"), img::PIECHART_PNG, img::PIECHART_PNG);
wxTreeItemId budgetSetupPerformance = m_nav_tree_ctrl->AppendItem(budgetReports, _("Budget Category Summary"), img::PIECHART_PNG, img::PIECHART_PNG);
m_nav_tree_ctrl->SetItemData(budgetSetupPerformance, new mmTreeItemData("Budget Category Summary", new mmReportBudgetCategorySummary()));
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/mmreportspanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,12 @@ void mmReportsPanel::CreateControls()
for (const auto& e : Model_Budgetyear::instance().all(Model_Budgetyear::COL_BUDGETYEARNAME))
{
const wxString& name = e.BUDGETYEARNAME;
m_date_ranges->Append(name, new wxStringClientData(wxString::Format("%i", e.BUDGETYEARID)));
if (sel_id == e.BUDGETYEARID)
sel_name = e.BUDGETYEARNAME;
if (name.length() == 4) // Only years
{
m_date_ranges->Append(name, new wxStringClientData(wxString::Format("%i", e.BUDGETYEARID)));
if (sel_id == e.BUDGETYEARID)
sel_name = e.BUDGETYEARNAME;
}
}
m_date_ranges->SetStringSelection(sel_name);

Expand Down
65 changes: 65 additions & 0 deletions src/model/Model_Budget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************
Copyright (C) 2013,2014 James Higley
Copyright (C) 2022 Mark Whalley (mark@ipx.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -17,9 +18,11 @@
********************************************************/

#include "Model_Budget.h"
#include "Model_Budgetyear.h"
#include <wx/intl.h>
#include "model/Model_Category.h"
#include "db/DB_Table_Budgettable_V1.h"
#include "option.h"

Model_Budget::Model_Budget()
: Model<DB_Table_BUDGETTABLE_V1>()
Expand Down Expand Up @@ -108,6 +111,68 @@ void Model_Budget::getBudgetEntry(int budgetYearID
}
}

void Model_Budget::getBudgetStats(
std::map<int, std::map<int, std::map<int, double> > > &budgetStats
, mmDateRange* date_range
, bool groupByMonth)
{
//Initialization
//Set std::map with zeros
double value = 0;
const wxDateTime start_date(1, date_range->start_date().GetMonth(), date_range->start_date().GetYear());
for (const auto& category : Model_Category::all_categories())
{
for (int m = 0; m < 12; m++)
{
const wxDateTime d = start_date.Add(wxDateSpan::Months(m));
const int idx = d.GetYear() * 100 + d.GetMonth();
budgetStats[category.second.first][category.second.second][idx] = value;
}
}

//Calculations
std::map<int, std::map<int, double> > yearBudgetValue;
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);

for (int m = 0; m < 12; m++)
{
for (const auto& cat : yearBudgetValue)
for (const auto& id : cat.second)
{
const wxDateTime d = start_date.Add(wxDateSpan::Months(m));
const int idx = d.GetYear() * 100 + d.GetMonth();
budgetStats[cat.first][id.first][idx] += id.second;
}

const wxString budgetYearMonth = wxString::Format("%s-%02d", year, m + 1);
budgetYearID = Model_Budgetyear::instance().Get(budgetYearMonth);
for (const auto& budget : instance().find(BUDGETYEARID(budgetYearID)))
{
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][idx] = getEstimate(true, period(budget), budget.AMOUNT);
else
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;
}
}

void Model_Budget::copyBudgetYear(int newYearID, int baseYearID)
{
for (const Data& data : instance().find(BUDGETYEARID(baseYearID)))
Expand Down
7 changes: 7 additions & 0 deletions src/model/Model_Budget.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************
Copyright (C) 2013,2014 James Higley
Copyright (C) 2022 Mark Whalley (mark@ipx.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -21,6 +22,8 @@

#include "Model.h"
#include "db/DB_Table_Budgettable_V1.h"
#include "reports/mmDateRange.h"
#include <float.h>

class Model_Budget : public Model<DB_Table_BUDGETTABLE_V1>
{
Expand Down Expand Up @@ -55,6 +58,10 @@ class Model_Budget : public Model<DB_Table_BUDGETTABLE_V1>
static void getBudgetEntry(int budgetYearID, std::map<int,
std::map<int, PERIOD_ENUM> > &budgetPeriod,
std::map<int, std::map<int, double> > &budgetAmt);
static void getBudgetStats(
std::map<int, std::map<int, std::map<int, double> > > &budgetStats
, mmDateRange* date_range
, bool groupByMonth);
static void copyBudgetYear(int newYearID, int baseYearID);
static double getEstimate(bool is_monthly, const PERIOD_ENUM period, const double amount);
};
Expand Down
17 changes: 16 additions & 1 deletion src/option.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************
Copyright (C) 2006 Madhan Kanagavel
Copyright (C) 2016 - 2021 Nikolay Akimov
Copyright (C) 2021 Mark Whalley (mark@ipx.co.uk)
Copyright (C) 2021-2022 Mark Whalley (mark@ipx.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -38,6 +38,7 @@ Option::Option()
, m_budgetFinancialYears(false)
, m_budgetIncludeTransfers(false)
, m_budgetReportWithSummaries(true)
, m_budgetOverride(false)
, m_ignoreFutureTransactions(false)
, m_showToolTips(true)
, m_showMoneyTips(true)
Expand Down Expand Up @@ -100,6 +101,7 @@ void Option::LoadOptions(bool include_infotable)
m_budgetFinancialYears = Model_Setting::instance().GetBoolSetting(INIDB_BUDGET_FINANCIAL_YEARS, false);
m_budgetIncludeTransfers = Model_Setting::instance().GetBoolSetting(INIDB_BUDGET_INCLUDE_TRANSFERS, false);
m_budgetReportWithSummaries = Model_Setting::instance().GetBoolSetting(INIDB_BUDGET_SUMMARY_WITHOUT_CATEG, true);
m_budgetOverride = Model_Setting::instance().GetBoolSetting(INIDB_BUDGET_OVERRIDE, false);
m_ignoreFutureTransactions = Model_Setting::instance().GetBoolSetting(INIDB_IGNORE_FUTURE_TRANSACTIONS, false);
m_showToolTips = Model_Setting::instance().GetBoolSetting(INIDB_SHOW_TOOLTIPS, true);
m_showMoneyTips = Model_Setting::instance().GetBoolSetting(INIDB_SHOW_MONEYTIPS, true);
Expand Down Expand Up @@ -263,6 +265,19 @@ bool Option::BudgetReportWithSummaries()
return m_budgetReportWithSummaries;
}

void Option::BudgetOverride(bool value)
{
Model_Setting::instance().Set(INIDB_BUDGET_OVERRIDE, value);
m_budgetOverride = value;

}

bool Option::BudgetOverride()
{
return m_budgetOverride;
}


void Option::IgnoreFutureTransactions(bool value)
{
Model_Setting::instance().Set(INIDB_IGNORE_FUTURE_TRANSACTIONS, value);
Expand Down
6 changes: 5 additions & 1 deletion src/option.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************
Copyright (C) 2006 Madhan Kanagavel
Copyright (C) 2021 Mark Whalley (mark@ipx.co.uk)
Copyright (C) 2021-2022 Mark Whalley (mark@ipx.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -84,6 +84,9 @@ class Option
void BudgetReportWithSummaries(bool value);
bool BudgetReportWithSummaries();

void BudgetOverride(bool value);
bool BudgetOverride();

void TransPayeeSelection(int value);
int TransPayeeSelection();

Expand Down Expand Up @@ -174,6 +177,7 @@ class Option
bool m_budgetFinancialYears; //INIDB_BUDGET_FINANCIAL_YEARS
bool m_budgetIncludeTransfers; //INIDB_BUDGET_INCLUDE_TRANSFERS
bool m_budgetReportWithSummaries; //INIDB_BUDGET_SUMMARY_WITHOUT_CATEG
bool m_budgetOverride; //INIDB_BUDGET_OVERRIDE
bool m_ignoreFutureTransactions; //INIDB_IGNORE_FUTURE_TRANSACTIONS
bool m_showToolTips; //INIDB_SHOW_TOOLTIPS
bool m_showMoneyTips; //INIDB_SHOW_MONEYTIPS
Expand Down
12 changes: 10 additions & 2 deletions src/optionsettingsview.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************
Copyright (C) 2014 Stefano Giorgio
Copyright (C) 2021 Mark Whalley (mark@ipx.co.uk)
Copyright (C) 2021-2022 Mark Whalley (mark@ipx.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -118,7 +118,6 @@ void OptionSettingsView::Create()
wxStaticBoxSizer* trxStaticBoxSizer = new wxStaticBoxSizer(trxStaticBox, wxVERTICAL);
viewsPanelSizer->Add(trxStaticBoxSizer, wxSizerFlags(g_flagsExpand).Proportion(0));


m_budget_financial_years = new wxCheckBox(view_panel, wxID_STATIC, _("View Budgets as Financial Years"), wxDefaultPosition, wxDefaultSize, wxCHK_2STATE);
m_budget_financial_years->SetValue(Option::instance().BudgetFinancialYears());
trxStaticBoxSizer->Add(m_budget_financial_years, g_flagsV);
Expand All @@ -135,6 +134,14 @@ void OptionSettingsView::Create()
m_budget_summary_without_category->SetValue(Option::instance().BudgetReportWithSummaries());
trxStaticBoxSizer->Add(m_budget_summary_without_category, g_flagsV);

// Budget Yearly/Monthly relationship if both exist
m_budget_override = new wxCheckBox(view_panel, wxID_STATIC
, _("Override yearly budget with monthly budget")
, wxDefaultPosition, wxDefaultSize, wxCHK_2STATE);
mmToolTip(m_budget_override, _("If monthly budget exists then use this to override the yearly budget; otherwise combine them"));
m_budget_override->SetValue(Option::instance().BudgetOverride());
trxStaticBoxSizer->Add(m_budget_override, g_flagsV);

// Allows a year or financial year to start before or after the 1st of the month.
wxBoxSizer* budget_offset_sizer = new wxBoxSizer(wxHORIZONTAL);
trxStaticBoxSizer->Add(budget_offset_sizer);
Expand Down Expand Up @@ -400,6 +407,7 @@ bool OptionSettingsView::SaveSettings()
Option::instance().BudgetFinancialYears(m_budget_financial_years->GetValue());
Option::instance().BudgetIncludeTransfers(m_budget_include_transfers->GetValue());
Option::instance().BudgetReportWithSummaries(m_budget_summary_without_category->GetValue());
Option::instance().BudgetOverride(m_budget_override->GetValue());
Option::instance().setBudgetDaysOffset(m_budget_days_offset->GetValue());
Option::instance().setReportingFirstDay(m_reporting_firstday->GetValue());
Option::instance().IgnoreFutureTransactions(m_ignore_future_transactions->GetValue());
Expand Down
3 changes: 2 additions & 1 deletion src/optionsettingsview.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************
Copyright (C) 2014 Stefano Giorgio
Copyright (C) 2021 Mark Whalley (mark@ipx.co.uk)
Copyright (C) 2021-2022 Mark Whalley (mark@ipx.co.uk)
This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -74,6 +74,7 @@ class OptionSettingsView : public OptionSettingsBase
wxCheckBox* m_budget_financial_years;
wxCheckBox* m_budget_include_transfers;
wxCheckBox* m_budget_summary_without_category;
wxCheckBox* m_budget_override;
wxCheckBox* m_ignore_future_transactions;
wxSpinCtrl* m_budget_days_offset;
wxSpinCtrl* m_reporting_firstday;
Expand Down

0 comments on commit 08ff621

Please sign in to comment.