From d800f2302979cfe1cca17ddc3775f9a5e50db840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Wed, 20 Oct 2010 20:54:23 +0200 Subject: [PATCH] * Add early shared account support (Database schema has changed) * Update TODO --- TODO | 2 +- init.sql | 1 + src/controller/KissCount.cpp | 7 +++- src/controller/KissCount.h | 1 + src/model/Account.h | 1 + src/model/Database.cpp | 64 +++++++++++++++++++++++++++++++++-- src/model/Database.h | 3 +- src/view/AccountPanel.cpp | 6 +++- src/view/PreferencesPanel.cpp | 11 +++++- 9 files changed, 89 insertions(+), 7 deletions(-) diff --git a/TODO b/TODO index 5878689..490082e 100644 --- a/TODO +++ b/TODO @@ -24,7 +24,7 @@ More translations Import/Export module Printing (maybe in html) Refactor web view code - +Plugins ? =============================================================== Will not be implemented diff --git a/init.sql b/init.sql index eb96374..7475882 100644 --- a/init.sql +++ b/init.sql @@ -1,6 +1,7 @@ CREATE TABLE kisscount(db_version VARCHAR(20)); 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 shared_account(id REFERENCES account(id), user REFERENCES user(id)); 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, parent REFERENCES operation(id), 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), formula VARCHAR(255), transfert REFERENCES operation(id), meta CHAR(1)); CREATE TABLE category(id INTEGER PRIMARY KEY, user REFERENCES user(id), parent REFERENCES category(id), name VARCHAR(255), backcolor VARCHAR(10), forecolor VARCHAR(10), font VARCHAR(255)); diff --git a/src/controller/KissCount.cpp b/src/controller/KissCount.cpp index 8c88ff7..64c505b 100644 --- a/src/controller/KissCount.cpp +++ b/src/controller/KissCount.cpp @@ -167,7 +167,7 @@ void KissCount::DeleteAccount(Account& ac) std::vector::iterator it; int i; - _db->DeleteAccount(ac); + _db->DeleteAccount(_user, ac); for (i=0, it=_user->_accounts.begin(); it !=_user->_accounts.end(); it++, i++) if (it->id == ac.id) { @@ -176,6 +176,11 @@ void KissCount::DeleteAccount(Account& ac) } } +void KissCount::AddSharedAccount(Account& ac, const wxString& granted) +{ + _db->AddSharedAccount(ac, granted); +} + wxString KissCount::AddCategory(Category& category) { wxString id; diff --git a/src/controller/KissCount.h b/src/controller/KissCount.h index 7357e28..b40fda1 100644 --- a/src/controller/KissCount.h +++ b/src/controller/KissCount.h @@ -61,6 +61,7 @@ public: wxString AddAccount(Account& ac); void UpdateAccount(Account& ac); void DeleteAccount(Account& ac); + void AddSharedAccount(Account& ac, const wxString& granted); wxString AddCategory(Category& category); void UpdateCategory(Category& category); diff --git a/src/model/Account.h b/src/model/Account.h index c9d34b6..ae2f438 100644 --- a/src/model/Account.h +++ b/src/model/Account.h @@ -31,6 +31,7 @@ public: wxString number; bool shared; bool _default; + bool is_owner; }; #endif diff --git a/src/model/Database.cpp b/src/model/Database.cpp index e982dbd..e4e19d8 100644 --- a/src/model/Database.cpp +++ b/src/model/Database.cpp @@ -272,6 +272,23 @@ User* Database::LoadUser(const wxString& name) account.number = set.GetAsString(wxT("number")); account.shared = set.GetBool(wxT("shared")); account._default = set.GetBool(wxT("default_account")); + account.is_owner = true; + user->_accounts.push_back(account); + } + set.Finalize(); + + req = wxT("SELECT * FROM account WHERE id IN (SELECT id FROM shared_account WHERE user='") + user->_id + wxT("') ORDER BY name ASC"); + + EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); + + while (set.NextRow()) + { + account.id = set.GetAsString(wxT("id")); + account.name = set.GetAsString(wxT("name")); + account.number = set.GetAsString(wxT("number")); + account.shared = set.GetBool(wxT("shared")); + account._default = set.GetBool(wxT("default_account")); + account.is_owner = false; user->_accounts.push_back(account); } set.Finalize(); @@ -677,12 +694,55 @@ void Database::UpdateAccount(Account& ac) req += wxT(" WHERE id='") + ac.id + wxT("'"); EXECUTE_SQL_UPDATE(req, ); + + if (!ac.shared && ac.is_owner) + { + req = wxT("DELETE FROM shared_account WHERE id='") + ac.id + wxT("'"); + + EXECUTE_SQL_UPDATE(req, ); + } } -void Database::DeleteAccount(Account& ac) +void Database::DeleteAccount(User* user, Account& ac) { wxString req; - req = wxT("DELETE FROM account WHERE id='") + ac.id + wxT("'"); + wxSQLite3ResultSet set; + + if (ac.is_owner) + { + if (ac.shared) + { + req = wxT("DELETE FROM shared_account WHERE id='") + ac.id + wxT("'"); + + EXECUTE_SQL_UPDATE(req, ); + } + req = wxT("DELETE FROM account WHERE id='") + ac.id + wxT("'"); + + EXECUTE_SQL_UPDATE(req, ); + } + else + { + req = wxT("DELETE FROM shared_account WHERE user='") + user->_id + wxT("'"); + + EXECUTE_SQL_UPDATE(req, ); + + req = wxT("SELECT COUNT(user) AS cnt FROM shared_account WHERE id='") + ac.id + wxT("'"); + + EXECUTE_SQL_QUERY(req, set, ); + + if (!set.GetInt(wxT("cnt"))) + { + ac.shared = false; + UpdateAccount(ac); + } + } +} + +void Database::AddSharedAccount(Account& ac, const wxString& granted) +{ + wxString req; + + req = wxT("INSERT INTO shared_account ('id', 'user') VALUES ('") + ac.id + wxT("', '") + granted + wxT("'"); EXECUTE_SQL_UPDATE(req, ); } diff --git a/src/model/Database.h b/src/model/Database.h index 760f597..4660327 100644 --- a/src/model/Database.h +++ b/src/model/Database.h @@ -55,7 +55,8 @@ public: wxString AddAccount(User* user, Account& ac); void UpdateAccount(Account& ac); - void DeleteAccount(Account& ac); + void DeleteAccount(User* user, Account& ac); + void AddSharedAccount(Account& ac, const wxString& granted); wxString AddCategory(User* user, Category& category); void UpdateCategory(Category& category); diff --git a/src/view/AccountPanel.cpp b/src/view/AccountPanel.cpp index 4a2fe73..499ebf8 100644 --- a/src/view/AccountPanel.cpp +++ b/src/view/AccountPanel.cpp @@ -377,7 +377,11 @@ void AccountPanel::InitAccountsGrid(User* user, int month, int year) { _accountsGrid->AppendRows(); - _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number); + if (it->shared) + _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number + wxT("*")); + else + _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number); + _accountsGrid->SetCellValue(curLine, ACCOUNT_NAME, it->name); value = _kiss->GetAccountAmount(it->id, month, year); _accountsGrid->SetCellEditor(curLine, ACCOUNT_INIT, new wxGridCellFloatEditor(-1, 2)); diff --git a/src/view/PreferencesPanel.cpp b/src/view/PreferencesPanel.cpp index 7f3fe4d..406496a 100644 --- a/src/view/PreferencesPanel.cpp +++ b/src/view/PreferencesPanel.cpp @@ -172,7 +172,10 @@ void PreferencesPanel::InitAccounts(User* user) _accountsGrid->AppendRows(); _accountsGrid->SetCellValue(curLine, ACCOUNT_NAME, it->name); - _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number); + if (it->shared) + _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number + wxT("*")); + else + _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number); _accountsGrid->SetCellRenderer(curLine, ACCOUNT_SHARED, new wxGridCellBoolRenderer ()); _accountsGrid->SetCellEditor(curLine, ACCOUNT_SHARED, new wxGridCellFastBoolEditor ()); @@ -186,6 +189,12 @@ void PreferencesPanel::InitAccounts(User* user) _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); + + if (!it->is_owner) + { + _accountsGrid->SetReadOnly(curLine, ACCOUNT_SHARED, true); + _accountsGrid->SetReadOnly(curLine, ACCOUNT_DEFAULT, true); + } } _accountsGrid->AutoSizeColumns(true);