From e72e36f27dd9d9c17841fb29c1dd608c5e10df76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Thu, 24 Jun 2010 21:02:42 +0200 Subject: [PATCH] Generate & delete month/year OK --- controller/KissCount.cpp | 19 +++++++++- controller/KissCount.h | 1 + model/Database.cpp | 43 +++++++++++++++++++++- model/Database.h | 1 + view/AccountPanel.cpp | 79 ++++++++++++++++++++++++++++++++++++---- view/AccountPanel.h | 1 + 6 files changed, 133 insertions(+), 11 deletions(-) diff --git a/controller/KissCount.cpp b/controller/KissCount.cpp index 87fccc8..cf28aeb 100644 --- a/controller/KissCount.cpp +++ b/controller/KissCount.cpp @@ -84,6 +84,19 @@ void KissCount::DeleteOperation(struct operation op) _db->DeleteOperation(op); } +void KissCount::DeleteOperations(int month, int year) +{ + _db->DeleteOperations(_user, month, year); + if (month != -1) + (*_user->_operations[year]).erase(month); + + if (month == -1 || !_user->_operations[year]->size()) + { + delete _user->_operations[year]; + _user->_operations.erase(year); + } +} + void KissCount::SetAccountAmount(int month, int year, wxString accountId, double amount) { _db->SetAccountAmount(month, year, accountId, amount); @@ -149,10 +162,14 @@ void KissCount::GenerateMonth(int monthFrom, int yearFrom, int monthTo, int year operation op; _db->GenerateMonth(_user, monthFrom, yearFrom, monthTo, yearTo); + + if (!_user->_operations[yearTo]) + _user->_operations[yearTo] = new std::map >(); + if (monthFrom != -1 && yearFrom != -1) { LoadYear(yearFrom, false); - + for(it = (*_user->_operations[yearFrom])[monthFrom].begin(); it != (*_user->_operations[yearFrom])[monthFrom].end() && it->fix_cost; diff --git a/controller/KissCount.h b/controller/KissCount.h index 1ebcb50..f35d53c 100644 --- a/controller/KissCount.h +++ b/controller/KissCount.h @@ -25,6 +25,7 @@ class KissCount wxString AddOperation(struct operation op); void UpdateOperation(struct operation op); void DeleteOperation(struct operation op); + void DeleteOperations(int month, int year); double GetAccountAmount(wxString id, int month, int year); void SetAccountAmount(int month, int year, wxString accountId, double value); diff --git a/model/Database.cpp b/model/Database.cpp index b4cd5fb..e4e0cb5 100644 --- a/model/Database.cpp +++ b/model/Database.cpp @@ -16,6 +16,7 @@ }\ catch (wxSQLite3Exception e)\ {\ + std::cerr << req.mb_str() << "\n" ;\ std::cerr << e.GetMessage().mb_str() << "\n" ;\ code_if_fail; \ return return_value;\ @@ -30,6 +31,7 @@ }\ catch (wxSQLite3Exception e)\ {\ + std::cerr << req.mb_str() << "\n" ;\ std::cerr << e.GetMessage().mb_str() << "\n" ;\ code_if_fail; \ return return_value;\ @@ -103,11 +105,12 @@ void Database::CreateDatabase() std::list Database::GetUsers() { std::list res; - + wxString req; // Check whether value exists in table wxSQLite3ResultSet set ; - EXECUTE_SQL_QUERY(_("SELECT name FROM user ORDER BY name"), set, res); + req = _("SELECT name FROM user ORDER BY name"); + EXECUTE_SQL_QUERY(req, set, res); while (set.NextRow()) { @@ -382,6 +385,41 @@ void Database::DeleteOperation(struct operation op) EXECUTE_SQL_UPDATE(req, ); } +void Database::DeleteOperations(User* user, int month, int year) +{ + wxString req; + std::vector::iterator it; + + it = user->_accounts.begin(); + req = _("DELETE FROM account_amount WHERE account IN('") + it->id; + it++; + for (;it != user->_accounts.end(); it++) + { + req += _("', '") + it->id ; + } + req += _("')"); + req += _(" AND year='") + wxString::Format(_("%d"), year) + _("'"); + if (month != -1) + req += _(" AND month='") + wxString::Format(_("%d"), month) + _("'"); + + EXECUTE_SQL_UPDATE(req, ); + + it = user->_accounts.begin(); + req = _("DELETE FROM operation WHERE account IN('") + it->id; + it++; + for (;it != user->_accounts.end(); it++) + { + req += _("', '") + it->id ; + } + req += _("')"); + req += _(" AND year='") + wxString::Format(_("%d"), year) + _("'"); + if (month != -1) + req += _(" AND month='") + wxString::Format(_("%d"), month) + _("'"); + + EXECUTE_SQL_UPDATE(req, ); + +} + void Database::SetAccountAmount(int month, int year, wxString accountId, double amount) { wxString req; @@ -542,6 +580,7 @@ void Database::UpdateCategory(User* user, wxString oldName, wxString name, wxStr void Database::DeleteCategory(User* user, struct category category) { wxString req; + req = _("DELETE FROM preference WHERE user='") + user->_id + _("'"); req += _(" AND type='category'"); req += _(" AND name='name'"); diff --git a/model/Database.h b/model/Database.h index 52ed276..b4529a0 100644 --- a/model/Database.h +++ b/model/Database.h @@ -27,6 +27,7 @@ class Database void UpdateOperation(struct operation op); wxString AddOperation(User* user, struct operation op); void DeleteOperation(struct operation op); + void DeleteOperations(User* user, int month, int year); double GetAccountAmount(wxString id, int month, int year); void SetAccountAmount(int month, int year, wxString accountId, double amount); diff --git a/view/AccountPanel.cpp b/view/AccountPanel.cpp index 61f3f20..2436e13 100644 --- a/view/AccountPanel.cpp +++ b/view/AccountPanel.cpp @@ -854,37 +854,52 @@ void AccountPanel::OnTreeChange(wxTreeEvent& event) inModification = false; } -void AccountPanel::OnMenuGenerate(wxCommandEvent& event) +void AccountPanel::GetTreeSelection(int* month, int* year) { wxString monthString; - int month, year, i; + int i; - month = year = -1; + *month = -1; *year = -1; monthString = _tree.GetItemText(_tree.GetSelection()); for (i=0; i<12; i++) if (monthString == months[i]) { - month = i; + *month = i; break; } - if (month == -1) + if (*month == -1) { - year = wxAtoi(monthString); + *year = wxAtoi(monthString); // Error if (year == 0) + { + *month = -1; + *year = -1; return; + } } else { - year = wxAtoi(_tree.GetItemText(_tree.GetItemParent(_tree.GetSelection()))); + *year = wxAtoi(_tree.GetItemText(_tree.GetItemParent(_tree.GetSelection()))); // Error if (year == 0) + { + *month = -1; + *year = -1; return; + } } +} + +void AccountPanel::OnMenuGenerate(wxCommandEvent& event) +{ + int month, year; + + GetTreeSelection(&month, &year); GenerateDialog g(_kiss, _wxUI, month, year); g.ShowModal(); @@ -892,7 +907,49 @@ void AccountPanel::OnMenuGenerate(wxCommandEvent& event) void AccountPanel::OnMenuDelete(wxCommandEvent& event) { + int month, year; + wxString message; + wxTreeItemId curNode, node ; + std::map > ops ; + GetTreeSelection(&month, &year); + + message = _("Are you sure want to delete "); + if (month != -1) + message += months[month] + _(" "); + message += wxString::Format(_("%d"), year); + + message += _(" operations ?"); + + wxMessageDialog dialog(_wxUI, message, _("KissCount"), wxYES_NO); + if (dialog.ShowModal() == wxID_NO) + return; + + curNode = _tree.GetSelection(); + ops = _kiss->GetAllOperations(); + + if (ops[year].size() == 1 && month != -1) + curNode = _tree.GetItemParent(curNode); + + _kiss->DeleteOperations(month, year); + + node = _tree.GetNextSibling(curNode); + + if (!node.IsOk()) + node = _tree.GetPrevSibling(curNode); + + _tree.Delete(curNode); + + if (!node.IsOk()) + ChangeUser(); + else + { + _tree.SelectItem(node); + GetTreeSelection(&month, &year); + if (month == -1) + month = ops[year][0]; + ShowMonth(month, year); + } } void AccountPanel::GenerateMonth(int month, int year) @@ -936,7 +993,13 @@ void AccountPanel::GenerateMonth(int month, int year) } if (!years.IsOk()) - years = _tree.PrependItem(root, yearString); + { + years = _tree.GetFirstChild(root, cookie); + if (wxAtoi(_tree.GetItemText(years)) > year) + years = _tree.PrependItem(root, yearString); + else + years = _tree.AppendItem(root, yearString); + } if (!_tree.GetChildrenCount(years, true)) node = _tree.AppendItem(years, monthString); diff --git a/view/AccountPanel.h b/view/AccountPanel.h index 10f2255..b769e80 100644 --- a/view/AccountPanel.h +++ b/view/AccountPanel.h @@ -65,6 +65,7 @@ private: void InitAccountsGrid(User* user, int month, int year); void UpdateStats(); void InsertOperation(User* user, operation* op, int line, bool fix); + void GetTreeSelection(int* month, int* year); DECLARE_EVENT_TABLE(); };