diff --git a/init.sql b/init.sql index ceab0d5..4a00ddc 100644 --- a/init.sql +++ b/init.sql @@ -1,7 +1,7 @@ CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR(255), password VARCHAR(255)); CREATE TABLE account(id INTEGER PRIMARY KEY, user REFERENCES user(id), name VARCHAR(255), number VARCHAR(255), shared CHAR(1), default_account CHAR(1)); -CREATE TABLE account_amount(id INTEGER PRIMARY KEY, account REFERENCES account(id), year INTEGER, month INTEGER, amount INTEGER); -CREATE TABLE operation(id INTEGER PRIMARY KEY, user REFERENCES user(id), account REFERENCES account(id), year INTEGER, month INTEGER, day INTEGER, amount INTEGER, description VARCHAR(255), category REFERENCES preference(id), fix_cost CHAR(1)); +CREATE TABLE account_amount(id INTEGER PRIMARY KEY, account REFERENCES account(id), year INTEGER, month INTEGER, amount FLOAT); +CREATE TABLE operation(id INTEGER PRIMARY KEY, user REFERENCES user(id), account REFERENCES account(id), year INTEGER, month INTEGER, day INTEGER, amount FLOAT, description VARCHAR(255), category REFERENCES preference(id), fix_cost CHAR(1)); CREATE TABLE preference(id INTEGER PRIMARY KEY, user REFERENCES user(id), type VARCHAR(255), name VARCHAR(255), value VARCHAR(255)); CREATE TABLE default_preference(id INTEGER PRIMARY KEY, type VARCHAR(255), name VARCHAR(255), value VARCHAR(255)); INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Fixe"); diff --git a/model/Database.cpp b/model/Database.cpp index 3633d7a..442e125 100644 --- a/model/Database.cpp +++ b/model/Database.cpp @@ -223,7 +223,7 @@ void Database::LoadYear(User* user, int year) op.day = set.GetInt(_("day")); op.month = set.GetInt(_("month")); op.year = set.GetInt(_("year")); - op.amount = set.GetInt(_("amount")); + op.amount = set.GetDouble(_("amount")); op.description = set.GetAsString(_("description")); op.category = set.GetAsString(_("category")); op.fix_cost = set.GetBool(_("fix_cost")); diff --git a/model/User.cpp b/model/User.cpp index b32615c..c9114c0 100644 --- a/model/User.cpp +++ b/model/User.cpp @@ -27,3 +27,8 @@ wxString User::GetAccountName(wxString accountId) return _("Unknown") ; return _accounts[accountId].name; } + +int User::GetCategoriesNumber() +{ + return _preferences._categories.size(); +} diff --git a/model/User.h b/model/User.h index b803b7c..883f720 100644 --- a/model/User.h +++ b/model/User.h @@ -10,7 +10,7 @@ struct operation { unsigned int day; unsigned int month; unsigned int year; - int amount; + double amount; wxString description; wxString category; bool fix_cost; @@ -40,6 +40,7 @@ public: wxString GetCategoryName(wxString catId); wxString GetAccountName(wxString accountId); + int GetCategoriesNumber(); }; #endif diff --git a/view/AccountPanel.cpp b/view/AccountPanel.cpp index e4d0abd..5fc74fa 100644 --- a/view/AccountPanel.cpp +++ b/view/AccountPanel.cpp @@ -6,31 +6,57 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), { wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); wxChartPanel* chart ; - double data[] = {1.0, 2.0, 3.0} ; - wxString cats[] = {_("cat1"), _("cat2"), _("cat3")}; - wxColour colours[] = {OWN_CYAN, OWN_YELLOW, OWN_GREEN} ; - double value; + int i ; + User* user = _kiss->GetUser(); + std::map::iterator accountIt; + std::map::iterator it; + wxColour categoryColors[] = {wxColour(0x00, 0x45, 0x86), + wxColour(0xFF, 0x3E, 0x0E), + wxColour(0xFF, 0xD3, 0x20), + wxColour(0x58, 0x9D, 0x1B), + wxColour(0x7E, 0x00, 0x21), + wxColour(0x83, 0xCC, 0xFF), + wxColour(0x31, 0x40, 0x04), + wxColour(0xB0, 0xCF, 0x00), + wxColour(0x4B, 0x1F, 0x6F), + wxColour(0xFF, 0x93, 0x0E), + wxColour(0xC5, 0x00, 0x0D), + wxColour(0x00, 0x84, 0xD1)}; SetSizer(hbox); - _pie = new PiePlot(); - CategorySimpleDataset* set = new CategorySimpleDataset(cats, 3); - value = 1.0; - //set->AddSerie(new CategorySerie(_("cat1"), &value, 1)) ; - set->AddSerie(_("série 1"), data, 3); - // value = 2.0; - // set->AddSerie(new CategorySerie(_("cat2"), &value, 1)) ; - // value = 3.0; - // set->AddSerie(new CategorySerie(_("cat3"), &value, 1)) ; - // set->SetRenderer(new XYLineRenderer()); - // set serie names to be displayed on legend - //dataset->SetSerieName(0, wxT("Serie 0")); - //dataset->SetSerieName(1, wxT("Serie 1")); - - _pie->SetDataset(set); - _pie->SetColorScheme(new ColorScheme(colours, 3)); + ColorScheme* colorScheme = new ColorScheme(categoryColors, WXSIZEOF(categoryColors)); - // set legend - _pie->SetLegend(new Legend(wxCENTER, wxTOP)); + _pie = new PiePlot(); + + _accounts = new wxString[user->_accounts.size()+1]; + _accounts[0] = _("Unknown"); + for (i=1, + accountIt = user->_accounts.begin(); + accountIt != user->_accounts.end(); + accountIt++, i++) + _accounts[i] = user->_accounts[accountIt->first].name; + + _categories = new wxString[user->GetCategoriesNumber()+1] ; + _categories[0] = _("Unknown"); + _categoriesIndexes[_("Unknown")] = 0; + for(i=1, it = user->_preferences._categories.begin(); it != user->_preferences._categories.end(); it++, i++) + { + _categories[i] = it->second ; + _categoriesIndexes[it->second] = i; + } + + _dataset = new CategorySimpleDataset(_categories, user->GetCategoriesNumber()+1); + + _categoriesValues = new double[user->GetCategoriesNumber()+1]; + for(i=0; iGetCategoriesNumber()+1; i++) + _categoriesValues[i] = 1.0; + + _dataset->AddSerie(_("Serie 1"), _categoriesValues, user->GetCategoriesNumber()+1); + _dataset->SetRenderer(new CategoryRenderer(*colorScheme)); + _pie->SetDataset(_dataset); + _pie->SetColorScheme(colorScheme); + + _pie->SetLegend(new Legend(wxBOTTOM, wxCENTER)); _grid = new GridAccount(this, -1); _grid->CreateGrid(1, NUMBER_COLS); @@ -41,7 +67,7 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), chart->Fit(); chart->Layout(); chart->SetMinSize(// chart->GetSize() - wxSize(200,200)); + wxSize(200,500)); hbox->Add(&_tree, 0); hbox->Add(_grid, 0); hbox->Add(chart, 0); @@ -52,6 +78,13 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), SetMinSize(GetSize()); } +AccountPanel::~AccountPanel() +{ + delete[] _categoriesValues; + delete[] _categories; + delete[] _accounts; +} + void AccountPanel::ChangeUser() { User* user = _kiss->GetUser(); @@ -79,7 +112,8 @@ void AccountPanel::LoadYear(int year) wxDateTime curDate; wxTreeItemId parentNode, curMonthNode; - if (user->_operations[year] != NULL) + _curYear = year ; + if (user->_operations[year] != NULL) { ShowMonth(year, 0); return; @@ -108,7 +142,7 @@ void AccountPanel::LoadYear(int year) _tree.Expand(parentNode); _tree.SelectItem(curMonthNode, true); - + ShowMonth(year, curMonth); } @@ -126,15 +160,14 @@ void AccountPanel::ShowMonth(int year, int month) User* user = _kiss->GetUser(); wxFont font(DEFAULT_FONT_SIZE, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, DEFAULT_FONT); std::map::iterator categoryIt; - std::map::iterator accountIt; wxGridCellChoiceEditor* categoryEditor, *accountEditor; - wxString* categories, *accounts; int i; + _curMonth = month; _wxUI->SetTitle(user->_name + _(" - ") + months[month]); // Operations are ordered - operations = (*user->_operations[year])[month]; + _curOperations = &((*user->_operations[year])[month]); _grid->CreateGrid(1, NUMBER_COLS); // Creating headers @@ -158,24 +191,7 @@ void AccountPanel::ShowMonth(int year, int month) // InsertRows (int pos=0, int numRows=1, bool updateLabels=true); // SetReadOnly(row, col, bool) - categories = new wxString[user->_preferences._categories.size()+1]; - categories[0] = _("Unknown"); - for (i=1, - categoryIt = user->_preferences._categories.begin(); - categoryIt != user->_preferences._categories.end(); - categoryIt++, i++) - categories[i] = user->_preferences._categories[categoryIt->first]; - - accounts = new wxString[user->_accounts.size()+1]; - accounts[0] = _("Unknown"); - for (i=1, - accountIt = user->_accounts.begin(); - accountIt != user->_accounts.end(); - accountIt++, i++) - accounts[i] = user->_accounts[accountIt->first].name; - - - it = operations.begin(); + it = _curOperations->begin(); /* struct operation { @@ -199,16 +215,15 @@ enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS}; _grid->SetCellValue(curLine, DESCRIPTION, it->description); _grid->SetCellValue(curLine, DATE, wxString::Format(wxT("%02d/%02d/%d"), it->day+1, it->month+1, it->year)); if (it->amount < 0) - _grid->SetCellValue(curLine, DEBIT, wxString::Format(wxT("%d"), -it->amount)); + _grid->SetCellValue(curLine, DEBIT, wxString::Format(wxT("%.2lf"), -it->amount)); else - _grid->SetCellValue(curLine, CREDIT, wxString::Format(wxT("%d"), it->amount)); - _grid->SetCellValue(curLine, CATEGORY, it->category); - _grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor()); - _grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor()); - categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false); + _grid->SetCellValue(curLine, CREDIT, wxString::Format(wxT("%.2lf"), it->amount)); + _grid->SetCellEditor(curLine, DEBIT, new wxGridCellFloatEditor(-1, 2)); + _grid->SetCellEditor(curLine, CREDIT, new wxGridCellFloatEditor(-1, 2)); + categoryEditor = new wxGridCellChoiceEditor(user->GetCategoriesNumber()+1, _categories, false); _grid->SetCellEditor(curLine, CATEGORY, categoryEditor); _grid->SetCellValue(curLine, CATEGORY, user->GetCategoryName(it->category)); - accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, accounts, false); + accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, _accounts, false); _grid->SetCellEditor(curLine, ACCOUNT, accountEditor); _grid->SetCellValue(curLine, ACCOUNT, user->GetAccountName(it->account)); it++; @@ -217,16 +232,16 @@ enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS}; _grid->AppendRows(); curLine++; SET_ROW_COLOR(curLine, OWN_YELLOW); - categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false); + categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, _categories, false); _grid->SetCellEditor(curLine, CATEGORY, categoryEditor); - _grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor()); - _grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor()); - accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, accounts, false); + _grid->SetCellEditor(curLine, DEBIT, new wxGridCellFloatEditor(-1, 2)); + _grid->SetCellEditor(curLine, CREDIT, new wxGridCellFloatEditor(-1, 2)); + accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, _accounts, false); _grid->SetCellEditor(curLine, ACCOUNT, accountEditor); _grid->_fixCosts = ++fixCosts; - for (; it != operations.begin(); it++) + for (; it != _curOperations->begin(); it++) { _grid->AppendRows(); curLine++; @@ -234,16 +249,15 @@ enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS}; _grid->SetCellValue(curLine, DESCRIPTION, it->description); _grid->SetCellValue(curLine, DATE, wxString::Format(wxT("%02d/%02d/%d"), it->day+1, it->month+1, it->year)); if (it->amount < 0) - _grid->SetCellValue(curLine, DEBIT, wxString::Format(wxT("%d"), -it->amount)); + _grid->SetCellValue(curLine, DEBIT, wxString::Format(wxT("%.2lf"), -it->amount)); else - _grid->SetCellValue(curLine, CREDIT, wxString::Format(wxT("%d"), it->amount)); - _grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor()); - _grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor()); - _grid->SetCellValue(curLine, CATEGORY, it->category); - categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false); + _grid->SetCellValue(curLine, CREDIT, wxString::Format(wxT("%.2lf"), it->amount)); + _grid->SetCellEditor(curLine, DEBIT, new wxGridCellFloatEditor(-1, 2)); + _grid->SetCellEditor(curLine, CREDIT, new wxGridCellFloatEditor(-1, 2)); + categoryEditor = new wxGridCellChoiceEditor(user->GetCategoriesNumber()+1, _categories, false); _grid->SetCellEditor(curLine, CATEGORY, categoryEditor); _grid->SetCellValue(curLine, CATEGORY, user->GetCategoryName(it->category)); - accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, accounts, false); + accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, _accounts, false); _grid->SetCellEditor(curLine, ACCOUNT, accountEditor); _grid->SetCellValue(curLine, ACCOUNT, user->GetAccountName(it->account)); it++; @@ -252,20 +266,41 @@ enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS}; _grid->AppendRows(); curLine++; SET_ROW_COLOR(curLine, OWN_GREEN); - categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false); + categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, _categories, false); _grid->SetCellEditor(curLine, CATEGORY, categoryEditor); - _grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor()); - _grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor()); - accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, accounts, false); + _grid->SetCellEditor(curLine, DEBIT, new wxGridCellFloatEditor(-1, 2)); + _grid->SetCellEditor(curLine, CREDIT, new wxGridCellFloatEditor(-1, 2)); + accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, _accounts, false); _grid->SetCellEditor(curLine, ACCOUNT, accountEditor); - delete[] categories; - delete[] accounts; - _grid->AutoSizeColumn(CATEGORY, false); _grid->AutoSizeColumn(ACCOUNT, false); + UpdateChart(); + Fit(); SetMinSize(GetSize()); } +void AccountPanel::UpdateChart() +{ + int i; + User* user = _kiss->GetUser(); + std::list::iterator it; + + //_dataset->BeginUpdate(); + for (i=0; iGetCategoriesNumber(); i++) + _categoriesValues[i] = 0.0; + + // _categoriesValues[0] += 1.0; + // _categoriesValues[1] += 2.0; + // _categoriesValues[2] += 3.0; + for (it=_curOperations->begin(); it!=_curOperations->end(); it++) + { + if (it->amount > 0) + _categoriesValues[_categoriesIndexes[user->GetCategoryName(it->category)]] += it->amount ; + //std::cout << _categoriesValues[_categoriesIndexes[it->category]] << "\n"; + } + //_dataset->EndUpdate(); + _pie->DatasetChanged(_dataset); +} diff --git a/view/AccountPanel.h b/view/AccountPanel.h index 504a60c..552c908 100644 --- a/view/AccountPanel.h +++ b/view/AccountPanel.h @@ -29,6 +29,7 @@ class AccountPanel: public wxPanel { public: AccountPanel(KissCount* kiss, wxUI *parent); + ~AccountPanel(); void ChangeUser(); void LoadYear(int year); void ShowMonth(int year, int month); @@ -39,6 +40,14 @@ private: wxTreeCtrl _tree; GridAccount* _grid; PiePlot* _pie; + double *_categoriesValues; + std::map _categoriesIndexes; + std::list* _curOperations; + int _curMonth, _curYear; + wxString* _categories, *_accounts; + CategorySimpleDataset* _dataset; + + void UpdateChart(); }; #endif diff --git a/view/ButtonPanel.cpp b/view/ButtonPanel.cpp index 209acb7..c8a460c 100644 --- a/view/ButtonPanel.cpp +++ b/view/ButtonPanel.cpp @@ -11,10 +11,10 @@ END_EVENT_TABLE() ButtonPanel::ButtonPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _kiss(kiss), _wxUI(parent) { wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); - _account = new wxBitmapButton(this, ID_BUTTON_ACCOUNT, wxBitmap(_(ACCOUNT_ICON))); - _stats = new wxBitmapButton(this, ID_BUTTON_STATS, wxBitmap(_(STATS_ICON))); - _prefs = new wxBitmapButton(this, ID_BUTTON_PREFS, wxBitmap(_(PREFS_ICON))); - _changeUser = new wxBitmapButton(this, ID_BUTTON_CHANGE_USER, wxBitmap(_(CHANGE_USER_ICON))); + _account = new wxBitmapButton(this, ID_BUTTON_ACCOUNT, wxBitmap(_(ACCOUNT_ICON)), wxDefaultPosition, wxSize(128, 128)); + _stats = new wxBitmapButton(this, ID_BUTTON_STATS, wxBitmap(_(STATS_ICON)), wxDefaultPosition, wxSize(128, 128)); + _prefs = new wxBitmapButton(this, ID_BUTTON_PREFS, wxBitmap(_(PREFS_ICON)), wxDefaultPosition, wxSize(128, 128)); + _changeUser = new wxBitmapButton(this, ID_BUTTON_CHANGE_USER, wxBitmap(_(CHANGE_USER_ICON)), wxDefaultPosition, wxSize(128, 128)); SetSizer(hbox);