diff --git a/controller/KissCount.cpp b/controller/KissCount.cpp index 1b11d3a..97805ed 100644 --- a/controller/KissCount.cpp +++ b/controller/KissCount.cpp @@ -139,27 +139,19 @@ wxString KissCount::AddCategory(struct category category) id = _db->AddCategory(_user, category); category.id = id; - _user->_preferences._categories.push_back(category); + _user->_categories.push_back(category); return id; } -void KissCount::UpdateCategory(wxString oldName, struct category category) +void KissCount::UpdateCategory(struct category category) { - wxString color; - std::vector::iterator it; - - color = _("#") ; - color += wxString::Format(_("%02X"), category.color.Red()); - color += wxString::Format(_("%02X"), category.color.Green()); - color += wxString::Format(_("%02X"), category.color.Blue()); - - _db->UpdateCategory(_user, oldName, category.name, color); + _db->UpdateCategory(category); - for (int i=0; i<(int)_user->_preferences._categories.size();i++) - if (_user->_preferences._categories[i].name == oldName) + for (int i=0; i<_user->GetCategoriesNumber();i++) + if (_user->_categories[i].id == category.id) { - _user->_preferences._categories[i] = category; + _user->_categories[i] = category; break; } } @@ -168,10 +160,10 @@ void KissCount::DeleteCategory(struct category category) { _db->DeleteCategory(_user, category); - for (int i=0; i<(int)_user->_preferences._categories.size();i++) - if (_user->_preferences._categories[i].name == category.name) + for (int i=0; i<_user->GetCategoriesNumber();i++) + if (_user->_categories[i].id == category.id) { - _user->_preferences._categories.erase(_user->_preferences._categories.begin()+i); + _user->_categories.erase(_user->_categories.begin()+i); break; } } diff --git a/controller/KissCount.h b/controller/KissCount.h index 2e5e93c..f03af26 100644 --- a/controller/KissCount.h +++ b/controller/KissCount.h @@ -38,7 +38,7 @@ class KissCount void DeleteAccount(struct Account ac); wxString AddCategory(struct category category); - void UpdateCategory(wxString oldName, struct category category); + void UpdateCategory(struct category category); void DeleteCategory(struct category category); std::map > GetAllOperations(); diff --git a/init.sql b/init.sql index 550007f..c3ee447 100644 --- a/init.sql +++ b/init.sql @@ -1,19 +1,19 @@ 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 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), checked CHAR(1)); -CREATE TABLE preference(id INTEGER PRIMARY KEY, user REFERENCES user(id), type VARCHAR(255), name VARCHAR(255), value VARCHAR(255)); +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 category(id), fix_cost CHAR(1), checked CHAR(1)); +CREATE TABLE category(id INTEGER PRIMARY KEY, user REFERENCES user(id), parent REFERENCES category(id), name VARCHAR(255), color VARCHAR(255), font VARCHAR(255)); +CREATE TABLE preference(id INTEGER PRIMARY KEY, user REFERENCES user(id), 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"); -INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Courses"); -INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Loisirs"); -INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Frais de fonctionnement"); -INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Exceptionnel"); -INSERT INTO default_preference ("type", "name", "value") VALUES ("category_color", "Fixe", "#FFFF99"); -INSERT INTO default_preference ("type", "name", "value") VALUES ("category_color", "Courses", "#3DEB3D"); -INSERT INTO default_preference ("type", "name", "value") VALUES ("category_color", "Loisirs", "#3DEB3D"); -INSERT INTO default_preference ("type", "name", "value") VALUES ("category_color", "Frais de fonctionnement", "#3DEB3D"); -INSERT INTO default_preference ("type", "name", "value") VALUES ("category_color", "Exceptionnel", "#3DEB3D"); +CREATE TABLE default_category(id INTEGER PRIMARY KEY, parent REFERENCES category(id), name VARCHAR(255), color VARCHAR(255), font VARCHAR(255)); +-- INSERT INTO default_preference ("name", "value") VALUES ("category", "name", "Fixe"); +-- INSERT INTO default_preference ("name", "value") VALUES ("category", "name", "Courses"); +INSERT INTO default_category ("parent", "name", "color", "font") VALUES ("0", "Fixe", "#FFFF99", ""); +INSERT INTO default_category ("parent", "name", "color", "font") VALUES ("0", "Courses", "#3DEB3D", ""); +INSERT INTO default_category ("parent", "name", "color", "font") VALUES ("0", "Loisirs", "#3DEB3D", ""); +INSERT INTO default_category ("parent", "name", "color", "font") VALUES ("0", "Frais de fonctionnement", "#3DEB3D", ""); +INSERT INTO default_category ("parent", "name", "color", "font") VALUES ("0", "Exceptionnel", "#3DEB3D", ""); +INSERT INTO default_category ("parent", "name", "color", "font") VALUES ("0", "Autre", "#3DEB3D", ""); -- No password INSERT INTO user ("id", "name", "password") VALUES ("0", "Greg", "da39a3ee5e6b4b0d3255bfef95601890afd80709"); INSERT INTO account ("id", "user", "name", "number", "shared", "default_account") VALUES ("0", "0", "Compte Courant", "000" , "0", "1"); @@ -30,13 +30,9 @@ INSERT INTO operation ("id", "user", "account", "year", "month", "day", "amount" INSERT INTO operation ("id", "user", "account", "year", "month", "day", "amount", "description", "category", "fix_cost", "checked") VALUES ("5", "0", "0", "2010", "5", "1", "-56", "Opé 2", "2", "0", "0"); INSERT INTO operation ("id", "user", "account", "year", "month", "day", "amount", "description", "category", "fix_cost", "checked") VALUES ("6", "0", "0", "2010", "5", "8", "12", "Opé 3", "3", "0", "1"); INSERT INTO operation ("id", "user", "account", "year", "month", "day", "amount", "description", "category", "fix_cost", "checked") VALUES ("7", "0", "0", "2010", "5", "29", "-2056", "Opé 4", "4", "0", "0"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Fixe"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Courses"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Loisirs"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Frais de fonctionnement"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Exceptionnel"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category_color", "Fixe", "#FFFF99"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category_color", "Courses", "#3DEB3D"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category_color", "Loisirs", "#3DEB3D"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category_color", "Frais de fonctionnement", "#3DEB3D"); -INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category_color", "Exceptionnel", "#3DEB3D"); +INSERT INTO category ("user", "parent", "name", "color", "font") VALUES ("0", "0", "Fixe", "#FFFF99", ""); +INSERT INTO category ("user", "parent", "name", "color", "font") VALUES ("0", "0", "Courses", "#3DEB3D", ""); +INSERT INTO category ("user", "parent", "name", "color", "font") VALUES ("0", "0", "Loisirs", "#3DEB3D", ""); +INSERT INTO category ("user", "parent", "name", "color", "font") VALUES ("0", "0", "Frais de fonctionnement", "#3DEB3D", ""); +INSERT INTO category ("user", "parent", "name", "color", "font") VALUES ("0", "0", "Exceptionnel", "#3DEB3D", ""); +INSERT INTO category ("user", "parent", "name", "color", "font") VALUES ("0", "0", "Autre", "#3DEB3D", ""); diff --git a/model/Database.cpp b/model/Database.cpp index 896b9da..019d0e0 100644 --- a/model/Database.cpp +++ b/model/Database.cpp @@ -224,34 +224,29 @@ User* Database::LoadUser(wxString name) set.Finalize(); } - req = _("SELECT id, value FROM preference WHERE type='category' AND name='name' AND user='") + user->_id + _("' ORDER BY value ASC"); + req = _("SELECT * FROM category WHERE user='") + user->_id + _("' ORDER BY name ASC"); EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); while (set.NextRow()) { category.id = set.GetAsString(_("id")); - category.name = set.GetAsString(_("value")); + category.parent = set.GetAsString(_("parent")); + category.name = set.GetAsString(_("name")); + category.color = wxColour(set.GetAsString(_("color"))); + category.font = set.GetAsString(_("font")); if (category.name != _("Fixe")) - user->_preferences._categories.push_back(category); + user->_categories.push_back(category); else - user->_preferences._categories.insert(user->_preferences._categories.begin(), category); + user->_categories.insert(user->_categories.begin(), category); } set.Finalize(); - req = _("SELECT name, value FROM preference WHERE type='category_color' AND user='") + user->_id + _("' ORDER BY value ASC"); + req = _("SELECT name, value FROM preference WHERE user='") + user->_id + _("' ORDER BY value ASC"); EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); while (set.NextRow()) - { - std::vector::iterator it; - for (it=user->_preferences._categories.begin(); it !=user->_preferences._categories.end(); it++) - if (it->name == set.GetAsString(_("name"))) - { - it->color = wxColour(set.GetAsString(_("value"))); - break; - } - } + user->_preferences[set.GetAsString(_("name"))] = set.GetAsString(_("value")); set.Finalize(); @@ -543,28 +538,18 @@ wxString Database::AddCategory(User* user, struct category category) color += wxString::Format(_("%02X"), category.color.Green()); color += wxString::Format(_("%02X"), category.color.Blue()); - req = _("INSERT INTO preference ('user', 'type', 'name', 'value') VALUES ('") ; + req = _("INSERT INTO category ('user', 'parent', 'name', 'color', font) VALUES ('") ; req += user->_id + _("'"); - req += _(", 'category'"); - req += _(", 'name'"); - req += _(", '") + category.name + _("'"); - req += _(")"); - - EXECUTE_SQL_UPDATE(req, _("0")); - - req = _("INSERT INTO preference ('user', 'type', 'name', 'value') VALUES ('") ; - req += user->_id + _("'"); - req += _(", 'category_color'"); + req += _(", '") + category.parent + _("'"); req += _(", '") + category.name + _("'"); req += _(", '") + color + _("'"); + req += _(", '") + category.font + _("'"); req += _(")"); EXECUTE_SQL_UPDATE(req, _("0")); req = _("SELECT id FROM preference WHERE user='") + user->_id + _("'") ; - req += _(" AND type='category'"); - req += _(" AND name='name'"); - req += _(" AND value='") + category.name + _("'"); + req += _(" AND name='") + category.name + _("'"); EXECUTE_SQL_QUERY(req , set, _("0")); @@ -578,57 +563,37 @@ wxString Database::AddCategory(User* user, struct category category) return res; } -void Database::UpdateCategory(User* user, wxString oldName, wxString name, wxString color) +void Database::UpdateCategory(struct category category) { - wxString req; + wxString req; + wxString color; - if (oldName != name) - { - req = _("UPDATE preference SET ") ; - req += _("value='") + name + _("'"); - req += _(" WHERE user='") + user->_id + _("'"); - req += _(" AND type='category'"); - req += _(" AND name='name'"); - req += _(" AND value='") + oldName + _("'"); + color = _("#") ; + color += wxString::Format(_("%02X"), category.color.Red()); + color += wxString::Format(_("%02X"), category.color.Green()); + color += wxString::Format(_("%02X"), category.color.Blue()); - EXECUTE_SQL_UPDATE(req, ); - } + req = _("UPDATE category SET") ; + req += _(" parent='") + category.parent + _("'"); + req += _(", name='") + category.name + _("'"); + req += _(", color='") + color + _("'"); + req += _(", font='") + category.font + _("'"); + req += _(" WHERE id='") + category.id + _("'"); - req = _("UPDATE preference SET ") ; - req += _("value='") + color + _("'"); - req += _(" WHERE user='") + user->_id + _("'"); - req += _(" AND type='category_color'"); - req += _(" AND name='") + oldName + _("'"); - - EXECUTE_SQL_UPDATE(req, ); - - if (oldName != name) - { - req = _("UPDATE preference SET ") ; - req += _("name='") + name + _("'"); - req += _(" WHERE user='") + user->_id + _("'"); - req += _(" AND type='category_color'"); - req += _(" AND name='") + oldName + _("'"); - req += _(" AND value='") + color + _("'"); - - EXECUTE_SQL_UPDATE(req, ); - } + EXECUTE_SQL_UPDATE(req, ); } 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'"); - req += _(" AND value='") + category.name + _("'"); + req = _("DELETE FROM category WHERE id='") + category.id + _("'"); EXECUTE_SQL_UPDATE(req, ); - req = _("DELETE FROM preference WHERE user='") + user->_id + _("'"); - req += _(" AND type='category_color'"); - req += _(" AND name='") + category.name + _("'"); + req = _("UPDATE category SET") ; + req += _(" parent='0'"); + req += _(" WHERE parent='") + category.id + _("'"); EXECUTE_SQL_UPDATE(req, ); } @@ -810,10 +775,9 @@ void Database::NewUser(wxString name) while (set.NextRow()) { - req = _("INSERT INTO preference ('user', 'type', 'name', 'value') VALUES ('") ; + req = _("INSERT INTO preference ('user', 'name', 'value') VALUES ('") ; req += id + _("'"); req += _(", '") + set.GetAsString(_("type")) + _("'"); - req += _(", '") + set.GetAsString(_("name")) + _("'"); req += _(", '") + set.GetAsString(_("value")) + _("'"); req += _(")"); @@ -822,6 +786,25 @@ void Database::NewUser(wxString name) set.Finalize(); + req = _("SELECT * FROM default_category"); + + EXECUTE_SQL_QUERY(req, set,); + + while (set.NextRow()) + { + req = _("INSERT INTO category ('user', 'parent', 'name', 'color', 'font') VALUES ('") ; + req += id + _("'"); + req += _(", '") + set.GetAsString(_("parent")) + _("'"); + req += _(", '") + set.GetAsString(_("name")) + _("'"); + req += _(", '") + set.GetAsString(_("color")) + _("'"); + req += _(", '") + set.GetAsString(_("font")) + _("'"); + req += _(")"); + + EXECUTE_SQL_UPDATE(req, ); + } + + set.Finalize(); + return ; } @@ -861,6 +844,9 @@ void Database::KillMe(User* user) req = _("DELETE FROM account WHERE user='") + user->_id + _("'"); EXECUTE_SQL_UPDATE(req, ); + req = _("DELETE FROM category WHERE user='") + user->_id + _("'"); + EXECUTE_SQL_UPDATE(req, ); + req = _("DELETE FROM user WHERE id='") + user->_id + _("'"); EXECUTE_SQL_UPDATE(req, ); } diff --git a/model/Database.h b/model/Database.h index 25989e7..b24fa4d 100644 --- a/model/Database.h +++ b/model/Database.h @@ -36,7 +36,7 @@ class Database void DeleteAccount(struct Account ac); wxString AddCategory(User* user, struct category category); - void UpdateCategory(User* user, wxString oldName, wxString name, wxString color); + void UpdateCategory(struct category category); void DeleteCategory(User* user, struct category category); std::map > GetAllOperations(User* user); diff --git a/model/Preferences.cpp b/model/Preferences.cpp deleted file mode 100644 index 9092b96..0000000 --- a/model/Preferences.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "Preferences.h" - diff --git a/model/Preferences.h b/model/Preferences.h deleted file mode 100644 index 08534d5..0000000 --- a/model/Preferences.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef PREFERENCES_H -#define PREFERENCES_H - -#include -#include - -struct category -{ - wxString id; - wxString name; - wxColour color; -}; - -class Preferences -{ -public: - std::vector _categories; -}; - -#endif diff --git a/model/User.cpp b/model/User.cpp index 31fa164..a5cdb58 100644 --- a/model/User.cpp +++ b/model/User.cpp @@ -19,19 +19,23 @@ struct category User::GetCategory(wxString catId) struct category cat; std::vector::iterator it; - for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++) + for (it=_categories.begin(); it !=_categories.end(); it++) if (it->id == catId) return *it; cat.id = _("0"); - return cat; + cat.parent = _("0"); + cat.name = _("Unknown"); + cat.font = _(""); + cat.color = wxColour(0xFF, 0xFF, 0xFF); + return cat; } wxString User::GetCategoryName(wxString catId) { std::vector::iterator it; - for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++) + for (it=_categories.begin(); it !=_categories.end(); it++) if (it->id == catId) return it->name; @@ -41,7 +45,7 @@ wxString User::GetCategoryName(wxString catId) wxString User::GetCategoryId(wxString catName) { std::vector::iterator it; - for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++) + for (it=_categories.begin(); it !=_categories.end(); it++) if (it->name == catName) return it->id; @@ -70,7 +74,7 @@ wxString User::GetAccountId(wxString accountName) int User::GetCategoriesNumber() { - return _preferences._categories.size(); + return _categories.size(); } int User::GetAccountsNumber() diff --git a/model/User.h b/model/User.h index 3487eee..188680a 100644 --- a/model/User.h +++ b/model/User.h @@ -4,27 +4,36 @@ #include #include #include -#include "Preferences.h" +#include struct operation { wxString id; - unsigned int day; - unsigned int month; - unsigned int year; - double amount; - wxString description; - wxString category; - bool fix_cost; - wxString account; - bool checked; + unsigned int day; + unsigned int month; + unsigned int year; + double amount; + wxString description; + wxString category; + bool fix_cost; + wxString account; + bool checked; } ; struct Account { wxString id; - wxString name; - wxString number; - bool shared; - bool _default; + wxString name; + wxString number; + bool shared; + bool _default; +}; + +struct category +{ + wxString id; + wxString parent; + wxString name; + wxColour color; + wxString font; }; class User @@ -37,7 +46,8 @@ public: wxString _password; std::vector _accounts; std::map >* > _operations; - Preferences _preferences; + std::vector _categories; + std::map _preferences; struct category GetCategory(wxString catId); wxString GetCategoryName(wxString catId); diff --git a/view/AccountPanel.cpp b/view/AccountPanel.cpp index 82cc8c9..0f34639 100644 --- a/view/AccountPanel.cpp +++ b/view/AccountPanel.cpp @@ -65,8 +65,8 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxScrolledWindow(&(* _accounts[i] = accountIt->name; _categories = new wxString[user->GetCategoriesNumber()] ; - for(i=0, categoryIt = user->_preferences._categories.begin(); - categoryIt != user->_preferences._categories.end(); + for(i=0, categoryIt = user->_categories.begin(); + categoryIt != user->_categories.end(); categoryIt++, i++) { _categories[i] = categoryIt->name ; @@ -319,7 +319,10 @@ void AccountPanel::ShowMonth(int month, int year) if (month == -1) { monthIt = user->_operations[year]->begin(); - month = monthIt->first; + if (user->_operations[year]->size() == 0 && year == curDate.GetYear()) + month = curDate.GetMonth(); + else + month = monthIt->first; } } diff --git a/view/PreferencesPanel.cpp b/view/PreferencesPanel.cpp index 6e509df..33078bf 100644 --- a/view/PreferencesPanel.cpp +++ b/view/PreferencesPanel.cpp @@ -148,7 +148,7 @@ void PreferencesPanel::InitCategories(User* user) _categoriesGrid->SetColLabelValue(CATEGORY_FONT, _("Font")); _categoriesGrid->SetColLabelValue(CATEGORY_DELETE, _("Delete")); - for (it=user->_preferences._categories.begin(); it!=user->_preferences._categories.end(); it++, curLine++) + for (it=user->_categories.begin(); it!=user->_categories.end(); it++, curLine++) { _categoriesGrid->AppendRows(); @@ -337,11 +337,13 @@ void PreferencesPanel::OnCategoryModified(wxGridEvent& event) } new_cat.color = _categoriesGrid->GetCellBackgroundColour(row, col); + new_cat.font = _(""); + new_cat.parent = _("0"); // Categories modification if (user->GetCategoriesNumber() && row < user->GetCategoriesNumber()) { - new_cat.id = user->_preferences._categories[row].id; + new_cat.id = user->_categories[row].id; if (col == CATEGORY_DELETE) { wxMessageDialog dialog(_wxUI, _("Are you sure want to delete : \n")+new_cat.name, _("KissCount"), wxYES_NO); @@ -352,7 +354,7 @@ void PreferencesPanel::OnCategoryModified(wxGridEvent& event) else { _categoriesGrid->DeleteRows(row, 1); - _kiss->DeleteCategory(user->_preferences._categories[row]); + _kiss->DeleteCategory(user->_categories[row]); } _wxUI->Layout(); @@ -360,7 +362,7 @@ void PreferencesPanel::OnCategoryModified(wxGridEvent& event) inModification = false; return; } - _kiss->UpdateCategory(user->_preferences._categories[row].name, new_cat); + _kiss->UpdateCategory(new_cat); } // New category else