From 7d1f9ee120b96a112201499f210a5f87cff160fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Wed, 23 Jun 2010 19:32:42 +0200 Subject: [PATCH] wip --- controller/KissCount.cpp | 24 +++++++++++++ controller/KissCount.h | 2 ++ model/Database.cpp | 57 +++++++++++++++++++++++++++++++ model/Database.h | 1 + view/AccountPanel.cpp | 73 ++++++++++++++++++++++++++++++++++++---- view/AccountPanel.h | 3 +- view/GenerateDialog.cpp | 33 +++++++++++++++++- view/wxUI.cpp | 5 +++ view/wxUI.h | 1 + 9 files changed, 191 insertions(+), 8 deletions(-) diff --git a/controller/KissCount.cpp b/controller/KissCount.cpp index 8ce843f..d757df9 100644 --- a/controller/KissCount.cpp +++ b/controller/KissCount.cpp @@ -142,3 +142,27 @@ std::map > KissCount::GetAllOperations() { return _db->GetAllOperations(_user); } + +void KissCount::GenerateMonth(int monthFrom, int yearFrom, int monthTo, int yearTo) +{ + std::vector::iterator it; + operation op; + + _db->GenerateMonth(_user, monthFrom, yearFrom, monthTo, yearTo); + if (monthFrom != -1 && yearFrom != -1) + { + LoadYear(yearFrom, false); + + for(it = (*_user->_operations[yearFrom])[monthFrom].begin(); + it != (*_user->_operations[yearFrom])[monthFrom].end() + && it->fix_cost; + it++) + { + op = *it; + op.month = monthTo; + op.year = yearTo; + AddOperation(op); + } + } + _wxUI->GenerateMonth(monthTo, yearTo); +} diff --git a/controller/KissCount.h b/controller/KissCount.h index dd059a8..1ebcb50 100644 --- a/controller/KissCount.h +++ b/controller/KissCount.h @@ -37,6 +37,8 @@ class KissCount void DeleteCategory(struct category category); std::map > GetAllOperations(); + + void GenerateMonth(int monthFrom, int yearFrom, int monthTo, int yearTo); private: wxUI* _wxUI; Database* _db; diff --git a/model/Database.cpp b/model/Database.cpp index b4e2e8b..1b17fcb 100644 --- a/model/Database.cpp +++ b/model/Database.cpp @@ -605,3 +605,60 @@ std::map > Database::GetAllOperations(User* user) return res; } + +void Database::GenerateMonth(User* user, int monthFrom, int yearFrom, int monthTo, int yearTo) +{ + std::vector::iterator it; + wxString req; + wxSQLite3ResultSet set; + double amount; + + if (monthFrom == -1 || yearFrom == -1) + { + for (it = user->_accounts.begin(); it != user->_accounts.end(); it++) + { + req = _("INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('") ; + req += it->id + _("'"); + req += _(" ,'") + wxString::Format(_("%d"), yearTo) + _("'"); + req += _(" ,'") + wxString::Format(_("%d"), monthTo) + _("'"); + req += _(" ,'0.0'"); + req += _(")"); + EXECUTE_SQL_UPDATE(req, ); + } + + return; + } + + for (it = user->_accounts.begin(); it != user->_accounts.end(); it++) + { + amount = 0.0; + req = _("SELECT SUM(amount) FROM operation WHERE") ; + req += _(" account='") + it->id + _("'"); + req += _(" AND year='") + wxString::Format(_("%d"), yearFrom) + _("'"); + req += _(" AND month='") + wxString::Format(_("%d"), monthFrom) + _("'"); + + EXECUTE_SQL_QUERY(req, set, ); + + if (set.NextRow()) + amount = set.GetDouble(_("amount")); + + req = _("SELECT amount FROM account_amount WHERE") ; + req += _(" account='") + it->id + _("'"); + req += _(" AND year='") + wxString::Format(_("%d"), yearFrom) + _("'"); + req += _(" AND month='") + wxString::Format(_("%d"), monthFrom) + _("'"); + + EXECUTE_SQL_QUERY(req, set, ); + + if (set.NextRow()) + amount += set.GetDouble(_("amount")); + + req = _("INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('") ; + req += it->id + _("'"); + req += _(" ,'") + wxString::Format(_("%d"), yearTo) + _("'"); + req += _(" ,'") + wxString::Format(_("%d"), monthTo) + _("'"); + req += _(" ,'") + wxString::Format(_("%.2lf"), amount) + _("'"); + req += _(")"); + + EXECUTE_SQL_UPDATE(req, ); + } +} diff --git a/model/Database.h b/model/Database.h index e6e2c6f..52ed276 100644 --- a/model/Database.h +++ b/model/Database.h @@ -39,6 +39,7 @@ class Database void DeleteCategory(User* user, struct category category); std::map > GetAllOperations(User* user); + void GenerateMonth(User* user, int monthFrom, int yearFrom, int monthTo, int yearTo); private: wxSQLite3Database _db; diff --git a/view/AccountPanel.cpp b/view/AccountPanel.cpp index e66792f..e9e50a4 100644 --- a/view/AccountPanel.cpp +++ b/view/AccountPanel.cpp @@ -226,11 +226,12 @@ void AccountPanel::LoadYear(int year, bool showMonth) std::map >::iterator it; wxDateTime curDate; wxTreeItemId parentNode, curMonthNode; - + //std::map > Database::GetAllOperations(User* user) + if (user->_operations[year]) { if (showMonth) - ShowMonth(year, -1); + ShowMonth(-1, year); return; } @@ -256,7 +257,7 @@ void AccountPanel::LoadYear(int year, bool showMonth) if (showMonth) { _tree.SelectItem(curMonthNode, true); - ShowMonth(year, curMonth); + ShowMonth(curMonth, year); } _wxUI->Layout(); @@ -267,7 +268,7 @@ void AccountPanel::LoadYear(int year, bool showMonth) _grid->SetCellBackgroundColour(row, i, color);\ } -void AccountPanel::ShowMonth(int year, int month) +void AccountPanel::ShowMonth(int month, int year) { std::vector operations; std::vector::iterator it; @@ -278,7 +279,6 @@ void AccountPanel::ShowMonth(int year, int month) std::vector::iterator categoryIt; std::map >::iterator monthIt; wxDateTime curDate; - curDate.SetToCurrent(); if (month == -1) @@ -846,7 +846,7 @@ void AccountPanel::OnTreeChange(wxTreeEvent& event) if (year != _curYear || month != _curMonth) { - ShowMonth(year, month); + ShowMonth(month, year); } } @@ -893,3 +893,64 @@ void AccountPanel::OnMenuDelete(wxCommandEvent& event) { } + +void AccountPanel::GenerateMonth(int month, int year) +{ + wxTreeItemId root, years, node ; + wxTreeItemIdValue cookie; + wxString monthString, yearString; + std::map >::iterator it; + int i; + User* user = _kiss->GetUser(); + + root = _tree.GetRootItem(); + yearString = wxString::Format(wxT("%d"), year); + monthString = months[month]; + + if (_tree.GetChildrenCount(root, true) < 1) + { + node = _tree.AppendItem(root, yearString); + node = _tree.AppendItem(node, monthString); + + _tree.SelectItem(node, true); + ShowMonth(month, year); + return ; + } + + years = _tree.GetFirstChild(root, cookie); + while (years.IsOk()) + { + if (_tree.GetItemText(years) == yearString) + break; + if (wxAtoi(_tree.GetItemText(years)) > year) + { + years = _tree.GetPrevSibling(years); + if (!years.IsOk()) + years = _tree.PrependItem(root, yearString); + else + years = _tree.InsertItem(root, years, yearString); + break; + } + years = _tree.GetNextSibling(years); + } + + if (!years.IsOk()) + years = _tree.PrependItem(root, yearString); + + if (!_tree.GetChildrenCount(years, true)) + node = _tree.AppendItem(years, monthString); + else + { + for(i=0, it = user->_operations[year]->begin(); + it != user->_operations[year]->end(); + it++, i++) + { + if ((int)it->first >= month) + break; + } + years = _tree.InsertItem(years, i, monthString); + } + + _tree.SelectItem(node, true); + ShowMonth(month, year); +} diff --git a/view/AccountPanel.h b/view/AccountPanel.h index f81e320..10f2255 100644 --- a/view/AccountPanel.h +++ b/view/AccountPanel.h @@ -35,7 +35,8 @@ public: ~AccountPanel(); void ChangeUser(); void LoadYear(int year, bool showMonth=true); - void ShowMonth(int year, int month); + void ShowMonth(int month, int year); + void GenerateMonth(int month, int year); void OnOperationModified(wxGridEvent& event); void OnAccountModified(wxGridEvent& event); diff --git a/view/GenerateDialog.cpp b/view/GenerateDialog.cpp index e9f85c7..328d3dd 100644 --- a/view/GenerateDialog.cpp +++ b/view/GenerateDialog.cpp @@ -168,8 +168,39 @@ void GenerateDialog::OnYearToChange(wxCommandEvent& event) } void GenerateDialog::OnOK(wxCommandEvent& event) -{ +{ + int monthFrom, yearFrom, monthTo, yearTo, i; + + if (_yearFrom->GetString(_yearTo->GetCurrentSelection()) == _("")) + { + monthFrom = -1; + yearFrom = -1; + } + else + { + for (i=0; i<12; i++) + { + if (months[i] == _monthFrom->GetString(_monthFrom->GetCurrentSelection())) + { + monthFrom = i; + break; + } + } + yearFrom = wxAtoi(_yearFrom->GetString(_yearFrom->GetCurrentSelection())); + } + + for (i=0; i<12; i++) + { + if (months[i] == _monthTo->GetString(_monthTo->GetCurrentSelection())) + { + monthTo = i; + break; + } + } + yearTo = wxAtoi(_yearTo->GetString(_yearTo->GetCurrentSelection())); + Close(); + _kiss->GenerateMonth(monthFrom, yearFrom, monthTo, yearTo); } void GenerateDialog::OnCancel(wxCommandEvent& event) diff --git a/view/wxUI.cpp b/view/wxUI.cpp index 66c6ffe..02c2ba5 100644 --- a/view/wxUI.cpp +++ b/view/wxUI.cpp @@ -101,3 +101,8 @@ void wxUI::ShowPanel(wxPanel* panel) _curPanel->Show(); Layout(); } + +void wxUI::GenerateMonth(int month, int year) +{ + _accountPanel->GenerateMonth(month, year); +} diff --git a/view/wxUI.h b/view/wxUI.h index 6f8161c..2f19ef7 100644 --- a/view/wxUI.h +++ b/view/wxUI.h @@ -30,6 +30,7 @@ public: void ShowAccount(); void ShowPreferences(); + void GenerateMonth(int month, int year); private: KissCount *_kiss;