From c322a01adb61f8dd74da39c3050fe4d668a48388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Mon, 21 Jun 2010 13:02:02 +0200 Subject: [PATCH] A lot of good job done --- controller/KissCount.cpp | 22 ++++++++++--- controller/KissCount.h | 4 +-- model/Database.cpp | 69 +++++++++++++++++++++++++++++++-------- model/Database.h | 4 +-- model/User.cpp | 15 +++++---- model/User.h | 2 +- view/AccountPanel.cpp | 36 ++++++++++---------- view/AccountPanel.h | 2 +- view/PreferencesPanel.cpp | 31 ++++++++++-------- view/PreferencesPanel.h | 2 +- 10 files changed, 125 insertions(+), 62 deletions(-) diff --git a/controller/KissCount.cpp b/controller/KissCount.cpp index 10109cc..d4e6f7a 100644 --- a/controller/KissCount.cpp +++ b/controller/KissCount.cpp @@ -74,9 +74,9 @@ void KissCount::UpdateOperation(struct operation op) _db->UpdateOperation(op); } -void KissCount::AddOperation(struct operation op) +wxString KissCount::AddOperation(struct operation op) { - _db->AddOperation(_user, op); + return _db->AddOperation(_user, op); } void KissCount::DeleteOperation(struct operation op) @@ -89,18 +89,30 @@ void KissCount::SetAccountAmount(int month, int year, wxString accountId, double _db->SetAccountAmount(month, year, accountId, amount); } -void KissCount::InsertAccount(struct Account ac) +void KissCount::AddAccount(struct Account ac) { - _db->InsertAccount(_user, ac); + ac.id = _db->AddAccount(_user, ac); + _user->_accounts.push_back(ac); } void KissCount::UpdateAccount(struct Account ac) { + std::vector::iterator it; + int i; + _db->UpdateAccount(ac); + for (i=0, it=_user->_accounts.begin(); it !=_user->_accounts.end(); it++, i++) + if (it->id == ac.id) + _user->_accounts[i] = ac; } void KissCount::DeleteAccount(struct Account ac) { + std::vector::iterator it; + int i; + _db->DeleteAccount(ac); - _user->_accounts.erase(ac.id); + for (i=0, it=_user->_accounts.begin(); it !=_user->_accounts.end(); it++, i++) + if (it->id == ac.id) + _user->_accounts.erase(_user->_accounts.begin()+i); } diff --git a/controller/KissCount.h b/controller/KissCount.h index b765085..29769ee 100644 --- a/controller/KissCount.h +++ b/controller/KissCount.h @@ -22,10 +22,10 @@ class KissCount User* GetUser(); double GetAccountAmount(wxString id, int month, int year); void UpdateOperation(struct operation op); - void AddOperation(struct operation op); + wxString AddOperation(struct operation op); void DeleteOperation(struct operation op); void SetAccountAmount(int month, int year, wxString accountId, double value); - void InsertAccount(struct Account ac); + void AddAccount(struct Account ac); void UpdateAccount(struct Account ac); void DeleteAccount(struct Account ac); diff --git a/model/Database.cpp b/model/Database.cpp index 3432727..015b052 100644 --- a/model/Database.cpp +++ b/model/Database.cpp @@ -148,7 +148,7 @@ User* Database::LoadUser(wxString name) wxString req; User* user; struct Account account; - std::map::iterator it; + std::vector::iterator it; req = _("SELECT * FROM user WHERE name='") + name + _("'"); @@ -176,18 +176,18 @@ User* Database::LoadUser(wxString name) account.number = set.GetAsString(_("number")); account.shared = set.GetBool(_("shared")); account._default = set.GetBool(_("default_account")); - user->_accounts[account.id] = account; + user->_accounts.push_back(account); } set.Finalize(); if (!user->_accounts.empty()) { it = user->_accounts.begin(); - req = _("SELECT DISTINCT year FROM account_amount WHERE account IN('") + it->first; + req = _("SELECT DISTINCT year FROM account_amount WHERE account IN('") + it->id; it++; for (;it != user->_accounts.end(); it++) { - req += _("', '") + it->first ; + req += _("', '") + it->id ; } req += _("') ORDER BY year ASC"); @@ -223,17 +223,17 @@ void Database::LoadYear(User* user, int year) { wxSQLite3ResultSet set; wxString req; - std::map::iterator it; + std::vector::iterator it; if (user->_operations[year] == NULL) user->_operations[year] = new std::map >(); it = user->_accounts.begin(); - req = _("SELECT * FROM operation WHERE account IN('") + it->first; + req = _("SELECT * FROM operation WHERE account IN('") + it->id; it++; for (;it != user->_accounts.end(); it++) { - req += _("', '") + it->first ; + req += _("', '") + it->id ; } req += _("') ORDER BY fix_cost DESC, year,month,day ASC"); @@ -306,9 +306,11 @@ void Database::UpdateOperation(struct operation op) } -void Database::AddOperation(User* user, struct operation op) +wxString Database::AddOperation(User* user, struct operation op) { - wxString req; + wxString req, res; + wxSQLite3ResultSet set; + req = _("INSERT INTO operation ('user', 'account', 'year', 'month', 'day', 'amount', 'description', 'category', 'fix_cost') VALUES ('") ; req += user->_id + _("'"); req += _(", '") + op.account + _("'"); @@ -324,7 +326,33 @@ void Database::AddOperation(User* user, struct operation op) req += _(", '0'") ; req += _(")"); - EXECUTE_SQL_UPDATE(req, ); + EXECUTE_SQL_UPDATE(req, _("0")); + + req = _("SELECT id FROM operation WHERE "); + req += _("user='") + user->_id + _("'"); + req += _(" AND account='") + op.account + _("'"); + req += _(" AND year='") + wxString::Format(_("%d"), op.year) + _("'"); + req += _(" AND month='") + wxString::Format(_("%d"), op.month) + _("'"); + req += _(" AND day='") + wxString::Format(_("%d"), op.day) + _("'"); + req += _(" AND amount='") + wxString::Format(_("%.2lf"), op.amount) + _("'"); + req += _(" AND description=\"") + op.description + _("\""); + req += _(" AND category='") + op.category + _("'"); + if (op.fix_cost) + req += _(" AND fix_cost='1'") ; + else + req += _(" AND fix_cost='0'") ; + req += _("ORDER BY ID DESC") ; + + EXECUTE_SQL_QUERY(req , set, _("0")); + + if (set.NextRow()) + res = set.GetAsString(_("id")); + else + res = _("0"); + + set.Finalize(); + + return res; } void Database::DeleteOperation(struct operation op) @@ -347,9 +375,11 @@ void Database::SetAccountAmount(int month, int year, wxString accountId, double EXECUTE_SQL_UPDATE(req, ); } -void Database::InsertAccount(User* user, struct Account ac) +wxString Database::AddAccount(User* user, struct Account ac) { - wxString req; + wxString req, res; + wxSQLite3ResultSet set; + req = _("INSERT INTO account ('user', 'name', 'number', 'shared', 'default_account') VALUES ('") ; req += user->_id + _("'"); req += _(", '") + ac.name + _("'"); @@ -364,7 +394,20 @@ void Database::InsertAccount(User* user, struct Account ac) req += _(", '0'") ; req += _(")"); - EXECUTE_SQL_UPDATE(req, ); + EXECUTE_SQL_UPDATE(req, _("0")); + + req = _("SELECT id FROM account WHERE name='") + ac.name + _("'") ; + + EXECUTE_SQL_QUERY(req , set, _("0")); + + if (set.NextRow()) + res = set.GetAsString(_("id")); + else + res = _("0"); + + set.Finalize(); + + return res; } void Database::UpdateAccount(struct Account ac) diff --git a/model/Database.h b/model/Database.h index 186657d..35bc076 100644 --- a/model/Database.h +++ b/model/Database.h @@ -25,12 +25,12 @@ class Database void LoadYear(User* user, int year); void UpdateOperation(struct operation op); - void AddOperation(User* user, struct operation op); + wxString AddOperation(User* user, struct operation op); void DeleteOperation(struct operation op); double GetAccountAmount(wxString id, int month, int year); void SetAccountAmount(int month, int year, wxString accountId, double amount); - void InsertAccount(User* user, struct Account ac); + wxString AddAccount(User* user, struct Account ac); void UpdateAccount(struct Account ac); void DeleteAccount(struct Account ac); diff --git a/model/User.cpp b/model/User.cpp index a1d1c78..c28119b 100644 --- a/model/User.cpp +++ b/model/User.cpp @@ -33,17 +33,20 @@ wxString User::GetCategoryId(wxString catName) wxString User::GetAccountName(wxString accountId) { - if (_accounts.find(accountId) == _accounts.end()) + std::vector::iterator it; + for (it=_accounts.begin(); it !=_accounts.end(); it++) + if (it->id == accountId) + return it->name; + return _("Unknown") ; - return _accounts[accountId].name; } wxString User::GetAccountId(wxString accountName) { - std::map::iterator it; - for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++) - if (it->second == accountName) - return it->first; + std::vector::iterator it; + for (it=_accounts.begin(); it !=_accounts.end(); it++) + if (it->name == accountName) + return it->id; return _("0") ; } diff --git a/model/User.h b/model/User.h index ac88a7c..800b110 100644 --- a/model/User.h +++ b/model/User.h @@ -34,7 +34,7 @@ public: wxString _id; wxString _name; wxString _password; - std::map _accounts; + std::vector _accounts; std::map >* > _operations; Preferences _preferences; diff --git a/view/AccountPanel.cpp b/view/AccountPanel.cpp index 0758af5..a6807da 100644 --- a/view/AccountPanel.cpp +++ b/view/AccountPanel.cpp @@ -21,7 +21,7 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), int i ; DEFAULT_FONT(font); User* user = _kiss->GetUser(); - std::map::iterator accountIt; + std::vector::iterator accountIt; std::map::iterator it; wxColour categoryColors[] = {wxColour(0x00, 0x45, 0x86), wxColour(0xFF, 0x3E, 0x0E), @@ -41,12 +41,12 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _pie = new PiePlot(); - _accounts = new wxString[user->_accounts.size()]; + _accounts = new wxString[user->GetAccountsNumber()]; for (i=0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); accountIt++, i++) - _accounts[i] = user->_accounts[accountIt->first].name; + _accounts[i] = accountIt->name; _categories = new wxString[user->GetCategoriesNumber()] ; for(i=0, it = user->_preferences._categories.begin(); it != user->_preferences._categories.end(); it++, i++) @@ -319,7 +319,7 @@ void AccountPanel::InsertOperation(User* user, operation* op, int line, bool fix _grid->SetCellEditor(line, DEBIT, new wxGridCellFloatEditor(-1, 2)); _grid->SetCellEditor(line, CREDIT, new wxGridCellFloatEditor(-1, 2)); - wxGridCellChoiceEditor* accountEditor = new wxGridCellChoiceEditor(user->_accounts.size(), _accounts, false); + wxGridCellChoiceEditor* accountEditor = new wxGridCellChoiceEditor(user->GetAccountsNumber(), _accounts, false); _grid->SetCellEditor(line, ACCOUNT, accountEditor); // Remove Fix category wxGridCellChoiceEditor* categoryEditor = new wxGridCellChoiceEditor(user->GetCategoriesNumber()-1, _categories+1, false); @@ -404,9 +404,8 @@ void AccountPanel::InsertOperation(User* user, operation* op, int line, bool fix void AccountPanel::InitAccountsGrid(User* user, int month, int year) { - std::map::iterator it; + std::vector::iterator it; int curLine = 0; - Account account ; double value; int i, a; DEFAULT_FONT(font); @@ -416,19 +415,17 @@ void AccountPanel::InitAccountsGrid(User* user, int month, int year) for (i=0, it = user->_accounts.begin(); it != user->_accounts.end(); i++, it++, curLine++) { _accountsGrid->AppendRows(); - account = user->_accounts[it->first]; - _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, account.number); - _accountsGrid->SetCellValue(curLine, ACCOUNT_NAME, account.name); - value = _kiss->GetAccountAmount(it->first, month, year); + _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number); + _accountsGrid->SetCellValue(curLine, ACCOUNT_NAME, it->name); + value = _kiss->GetAccountAmount(it->id, month, year); _accountsGrid->SetCellValue(curLine, ACCOUNT_INIT, wxString::Format(wxT("%.2lf"), value)); _accountsGrid->SetCellEditor(curLine, ACCOUNT_INIT, new wxGridCellFloatEditor(-1, 2)); for (a=0; aSetReadOnly(curLine, a, a != ACCOUNT_INIT); _accountsGrid->SetCellFont(curLine, ACCOUNT_CUR, font); - _accountsIndexes[it->first] = i; - _accountsInitValues[it->first] = value; + _accountsInitValues[it->id] = value; _accountsGrid->SetCellAlignment(curLine, ACCOUNT_INIT, wxALIGN_RIGHT, wxALIGN_CENTRE); _accountsGrid->SetCellAlignment(curLine, ACCOUNT_CUR, wxALIGN_RIGHT, wxALIGN_CENTRE); _accountsGrid->SetCellAlignment(curLine, ACCOUNT_FINAL, wxALIGN_RIGHT, wxALIGN_CENTRE); @@ -447,6 +444,7 @@ void AccountPanel::UpdateStats() std::map curAccountAmount, finalAccountAmount; std::map::iterator doubleIt; std::map::iterator intIt; + std::vector::iterator accountIt; curDate.SetToCurrent(); @@ -498,13 +496,12 @@ void AccountPanel::UpdateStats() for(i=0; iGetCategoriesNumber()+1; i++) _statsGrid->SetCellValue(CATS_STATS+i, 1, wxString::Format(wxT("%.2lf"), _categoriesValues[i])); - for (intIt=_accountsIndexes.begin(); intIt!=_accountsIndexes.end(); intIt++) + for (i=0, accountIt=user->_accounts.begin(); accountIt!=user->_accounts.end(); accountIt++, i++) { - i = _accountsIndexes[intIt->first]; - value = curAccountAmount[intIt->first]; + value = curAccountAmount[accountIt->id]; _accountsGrid->SetCellValue(i, ACCOUNT_CUR, wxString::Format(wxT("%.2lf"), value)); _accountsGrid->SetCellTextColour(i, ACCOUNT_CUR, (value >= 0.0) ? wxColor(0x00, 0x00, 0x00) : wxColor(0xFF, 0x00, 0x00)); - value = finalAccountAmount[intIt->first]; + value = finalAccountAmount[accountIt->id]; _accountsGrid->SetCellValue(i, ACCOUNT_FINAL, wxString::Format(wxT("%.2lf"), value)); } @@ -653,13 +650,14 @@ void AccountPanel::OnOperationModified(wxGridEvent& event) } need_insertion = true; fix_op = true; + new_op.fix_cost = true; for(i=0; iSetCellValue(event.GetRow(), i, _("")); } - _kiss->AddOperation(new_op); + new_op.id = _kiss->AddOperation(new_op); } // Modify an operation else if (row <= user->GetOperationsNumber(_curMonth, _curYear)) @@ -697,12 +695,14 @@ void AccountPanel::OnOperationModified(wxGridEvent& event) return ; } need_insertion = true; + fix_op = false; + new_op.fix_cost = false; for(i=0; iSetCellValue(event.GetRow(), i, _("")); } - _kiss->AddOperation(new_op); + new_op.id = _kiss->AddOperation(new_op); } if (need_insertion) diff --git a/view/AccountPanel.h b/view/AccountPanel.h index 1b506eb..d7be19f 100644 --- a/view/AccountPanel.h +++ b/view/AccountPanel.h @@ -48,7 +48,7 @@ private: wxGrid *_statsGrid, *_accountsGrid; PiePlot* _pie; double *_categoriesValues; - std::map _categoriesIndexes, _accountsIndexes; + std::map _categoriesIndexes; std::vector* _curOperations; int _curMonth, _curYear; wxString* _categories, *_accounts; diff --git a/view/PreferencesPanel.cpp b/view/PreferencesPanel.cpp index 53b126a..d8376e4 100644 --- a/view/PreferencesPanel.cpp +++ b/view/PreferencesPanel.cpp @@ -80,7 +80,7 @@ PreferencesPanel::PreferencesPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*p void PreferencesPanel::InitAccounts(User* user) { - std::map::iterator it; + std::vector::iterator it; int curLine = 0; Account account ; DEFAULT_FONT(font); @@ -97,10 +97,9 @@ void PreferencesPanel::InitAccounts(User* user) for (it = user->_accounts.begin(); it != user->_accounts.end(); it++, curLine++) { _accountsGrid->AppendRows(); - account = user->_accounts[it->first]; - _accountsGrid->SetCellValue(curLine, ACCOUNT_NAME, account.name); - _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, account.number); + _accountsGrid->SetCellValue(curLine, ACCOUNT_NAME, it->name); + _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number); _accountsGrid->SetCellRenderer(curLine, ACCOUNT_SHARED, new wxGridCellBoolRenderer ()); _accountsGrid->SetCellEditor(curLine, ACCOUNT_SHARED, new wxGridCellBoolEditor ()); @@ -108,13 +107,12 @@ void PreferencesPanel::InitAccounts(User* user) _accountsGrid->SetCellEditor(curLine, ACCOUNT_DEFAULT, new wxGridCellBoolEditor ()); _accountsGrid->SetCellRenderer(curLine, ACCOUNT_DELETE, new wxGridCellBoolRenderer ()); _accountsGrid->SetCellEditor(curLine, ACCOUNT_DELETE, new wxGridCellBoolEditor ()); - _accountsGrid->SetCellValue(curLine, ACCOUNT_SHARED, (account.shared)?_("1"):_("0")); - _accountsGrid->SetCellValue(curLine, ACCOUNT_DEFAULT, (account._default)?_("1"):_("0")); + _accountsGrid->SetCellValue(curLine, ACCOUNT_SHARED, (it->shared)?_("1"):_("0")); + _accountsGrid->SetCellValue(curLine, ACCOUNT_DEFAULT, (it->_default)?_("1"):_("0")); _accountsGrid->SetCellAlignment(curLine, ACCOUNT_SHARED, wxALIGN_CENTRE, wxALIGN_CENTRE); _accountsGrid->SetCellAlignment(curLine, ACCOUNT_DEFAULT, wxALIGN_CENTRE, wxALIGN_CENTRE); _accountsGrid->SetCellAlignment(curLine, ACCOUNT_DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE); - _accountsIndexes[curLine] = account.id; } _accountsGrid->AutoSizeColumns(true); @@ -221,18 +219,17 @@ void PreferencesPanel::OnAccountModified(wxGridEvent& event) if (col == ACCOUNT_DEFAULT) { - new_account.id = _accountsIndexes[row]; + new_account.id = user->_accounts[row].id; for (i=0; iGetAccountsNumber(); i++) { - if (i != col) + if (i != row) { - account = user->_accounts[_accountsIndexes[i]]; + account = user->_accounts[i]; if (account._default) { account._default = false; _kiss->UpdateAccount(account); - user->_accounts[_accountsIndexes[i]] = account; _accountsGrid->SetCellValue(i, ACCOUNT_DEFAULT, _("")); break; } @@ -245,7 +242,7 @@ void PreferencesPanel::OnAccountModified(wxGridEvent& event) // Account modification if (user->GetAccountsNumber() && row < user->GetAccountsNumber()) { - new_account.id = _accountsIndexes[row]; + new_account.id = user->_accounts[row].id; if (col == ACCOUNT_DELETE) { @@ -275,6 +272,14 @@ void PreferencesPanel::OnAccountModified(wxGridEvent& event) return ; } + if (user->GetAccountId(new_account.name) != _("0")) + { + wxMessageDialog dialog(_wxUI, _("Account ")+new_account.name+_(" already exists"), _("Error"), wxOK); + dialog.ShowModal(); + inModification = false; + return ; + } + _accountsGrid->SetCellRenderer(row, ACCOUNT_SHARED, new wxGridCellBoolRenderer ()); _accountsGrid->SetCellEditor(row, ACCOUNT_SHARED, new wxGridCellBoolEditor ()); _accountsGrid->SetCellRenderer(row, ACCOUNT_DEFAULT, new wxGridCellBoolRenderer ()); @@ -303,7 +308,7 @@ void PreferencesPanel::OnAccountModified(wxGridEvent& event) _accountsGrid->SetReadOnly(row+1, ACCOUNT_DELETE, true); _wxUI->Layout(); - _kiss->InsertAccount(new_account); + _kiss->AddAccount(new_account); } inModification = false; diff --git a/view/PreferencesPanel.h b/view/PreferencesPanel.h index 6d8ad10..3570dac 100644 --- a/view/PreferencesPanel.h +++ b/view/PreferencesPanel.h @@ -30,7 +30,7 @@ private: wxUI* _wxUI; wxGrid* _accountsGrid; wxGrid* _categoriesGrid; - std::map _categoriesIndexes, _accountsIndexes; + std::map _categoriesIndexes; void InitAccounts(User* user); void InitCategories(User* user);