Move directories, fix a bug into month generation (tree), add tooltips
This commit is contained in:
17
src/model/Account.h
Normal file
17
src/model/Account.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef ACCOUNT_H
|
||||
#define ACCOUNT_H
|
||||
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <wx/wx.h>
|
||||
|
||||
class Account {
|
||||
public:
|
||||
wxString id;
|
||||
wxString name;
|
||||
wxString number;
|
||||
bool shared;
|
||||
bool _default;
|
||||
};
|
||||
|
||||
#endif
|
||||
14
src/model/Category.h
Normal file
14
src/model/Category.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef CATEGORY_H
|
||||
#define CATEGORY_H
|
||||
|
||||
class Category
|
||||
{
|
||||
public:
|
||||
wxString id;
|
||||
wxString parent;
|
||||
wxString name;
|
||||
wxColour color;
|
||||
wxString font;
|
||||
};
|
||||
|
||||
#endif
|
||||
884
src/model/Database.cpp
Normal file
884
src/model/Database.cpp
Normal file
@@ -0,0 +1,884 @@
|
||||
#include "Database.h"
|
||||
|
||||
// if (!_db.CheckSyntax(req))
|
||||
// {
|
||||
// wxString s = req;
|
||||
// std::cout << s.mb_str() << " is invalid !\n" ;
|
||||
// code_if_syntax_fail;
|
||||
// return return_value;
|
||||
// }
|
||||
|
||||
#define EXECUTE_SQL_UPDATE_WITH_CODE(req, return_value, code_if_fail, code_if_syntax_fail) \
|
||||
do{ \
|
||||
try \
|
||||
{ \
|
||||
_db.ExecuteUpdate(req); \
|
||||
} \
|
||||
catch (wxSQLite3Exception e) \
|
||||
{ \
|
||||
std::cerr << req.mb_str() << "\n" ; \
|
||||
std::cerr << e.GetMessage().mb_str() << "\n" ; \
|
||||
code_if_fail; \
|
||||
return return_value; \
|
||||
} \
|
||||
} while(0);
|
||||
|
||||
#define EXECUTE_SQL_QUERY_WITH_CODE(req, res, return_value, code_if_fail, code_if_syntax_fail) \
|
||||
do{ \
|
||||
try \
|
||||
{ \
|
||||
res = _db.ExecuteQuery(req); \
|
||||
} \
|
||||
catch (wxSQLite3Exception e) \
|
||||
{ \
|
||||
std::cerr << req.mb_str() << "\n" ; \
|
||||
std::cerr << e.GetMessage().mb_str() << "\n" ; \
|
||||
code_if_fail; \
|
||||
return return_value; \
|
||||
} \
|
||||
} while(0);
|
||||
|
||||
#define EXECUTE_SQL_QUERY(req, res, return_value) EXECUTE_SQL_QUERY_WITH_CODE(req, res, return_value, {}, {})
|
||||
|
||||
#define EXECUTE_SQL_UPDATE(req, return_value) EXECUTE_SQL_UPDATE_WITH_CODE(req, return_value, {}, {})
|
||||
|
||||
static inline wxString DoubleToString(double d)
|
||||
{
|
||||
wxString res;
|
||||
|
||||
res = wxString::Format(wxT("%.2lf"), d);
|
||||
res.Replace(wxT(","), wxT("."));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Database::Database()
|
||||
{
|
||||
std::ifstream bdd_file;
|
||||
|
||||
bdd_file.open(BDD_FILE);
|
||||
|
||||
if (!bdd_file)
|
||||
{
|
||||
CreateDatabase();
|
||||
}
|
||||
else
|
||||
_db.Open(wxT(BDD_FILE));
|
||||
|
||||
bdd_file.close();
|
||||
}
|
||||
|
||||
void Database::CreateDatabase()
|
||||
{
|
||||
std::ifstream init_script;
|
||||
std::string line;
|
||||
wxString wxline;
|
||||
|
||||
wxMessageBox(_("No database found, creating a new one"), wxT("KissCount"), wxICON_EXCLAMATION | wxOK );
|
||||
|
||||
init_script.open(INIT_SCRIPT);
|
||||
|
||||
if (!init_script)
|
||||
{
|
||||
wxMessageBox(_(INIT_SCRIPT " not found, aborting"), _("Error"), wxICON_ERROR | wxOK );
|
||||
throw "init.sql not found, aborting";
|
||||
}
|
||||
|
||||
_db.Open(wxT(BDD_FILE));
|
||||
|
||||
do
|
||||
{
|
||||
getline(init_script, line);
|
||||
wxline = wxString(line.c_str(), wxConvUTF8);
|
||||
wxline.Trim(false);
|
||||
if (!wxline.Length() || wxline.StartsWith(wxT("--"))) continue;
|
||||
if (!_db.CheckSyntax(wxline))
|
||||
{
|
||||
std::cout << line << " is invalid !\n" ;
|
||||
continue;
|
||||
}
|
||||
try
|
||||
{
|
||||
_db.ExecuteUpdate(wxline);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
wxMessageBox(_("Error creating original database"), _("Error"), wxICON_ERROR | wxOK );
|
||||
remove(BDD_FILE);
|
||||
throw line;
|
||||
}
|
||||
} while (init_script);
|
||||
|
||||
init_script.close();
|
||||
}
|
||||
|
||||
|
||||
wxString Database::HashPassword(wxString password)
|
||||
{
|
||||
blk_SHA_CTX sha_ctx;
|
||||
unsigned char sha[20];
|
||||
wxString wxSHA;
|
||||
|
||||
blk_SHA1_Init(&sha_ctx);
|
||||
blk_SHA1_Update(&sha_ctx, password.c_str(), password.Length());
|
||||
blk_SHA1_Final(sha, &sha_ctx);
|
||||
|
||||
for(int i=0; i<20; i++)
|
||||
wxSHA += wxString::Format(wxT("%02x"), (int)sha[i]);
|
||||
|
||||
return wxSHA;
|
||||
}
|
||||
|
||||
std::list<wxString> Database::GetUsers()
|
||||
{
|
||||
std::list<wxString> res;
|
||||
wxString req;
|
||||
// Check whether value exists in table
|
||||
wxSQLite3ResultSet set ;
|
||||
|
||||
req = wxT("SELECT name FROM user ORDER BY name");
|
||||
EXECUTE_SQL_QUERY(req, set, res);
|
||||
|
||||
while (set.NextRow())
|
||||
{
|
||||
res.push_front(set.GetAsString(0));
|
||||
}
|
||||
set.Finalize();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool Database::IsValidUser(wxString user, wxString password)
|
||||
{
|
||||
bool res;
|
||||
wxString req;
|
||||
wxSQLite3ResultSet set;
|
||||
|
||||
req = wxT("SELECT name FROM user WHERE name='") + user + wxT("' AND password='") + HashPassword(password) + wxT("'");
|
||||
|
||||
EXECUTE_SQL_QUERY(req, set, false);
|
||||
|
||||
res = set.NextRow() ;
|
||||
set.Finalize();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
User* Database::LoadUser(wxString name)
|
||||
{
|
||||
wxSQLite3ResultSet set;
|
||||
wxString req;
|
||||
User* user;
|
||||
Account account;
|
||||
Category category;
|
||||
|
||||
std::vector<Account>::iterator it;
|
||||
|
||||
req = wxT("SELECT * FROM user WHERE name='") + name + wxT("'");
|
||||
|
||||
EXECUTE_SQL_QUERY(req, set, NULL);
|
||||
|
||||
if (!set.NextRow())
|
||||
return NULL;
|
||||
|
||||
user = new User();
|
||||
|
||||
user->_id = set.GetAsString(wxT("id"));
|
||||
user->_name = set.GetAsString(wxT("name"));
|
||||
user->_password = wxT("") ; // Security reasons set.GetAsString("password");
|
||||
|
||||
set.Finalize();
|
||||
|
||||
req = wxT("SELECT * FROM account WHERE user='") + user->_id + wxT("' ORDER BY default_account DESC, 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"));
|
||||
user->_accounts.push_back(account);
|
||||
}
|
||||
set.Finalize();
|
||||
|
||||
if (!user->_accounts.empty())
|
||||
{
|
||||
it = user->_accounts.begin();
|
||||
req = wxT("SELECT DISTINCT year FROM operation WHERE account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += wxT("', '") + it->id ;
|
||||
}
|
||||
req += wxT("')");
|
||||
req += wxT(" OR user='") + user->_id + wxT("'");
|
||||
req += wxT(" ORDER BY year ASC");
|
||||
|
||||
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;});
|
||||
|
||||
while (set.NextRow())
|
||||
{
|
||||
user->_operations[set.GetInt(wxT("year"))] = NULL;
|
||||
}
|
||||
set.Finalize();
|
||||
}
|
||||
|
||||
req = wxT("SELECT * FROM category WHERE user='") + user->_id + wxT("' ORDER BY name ASC");
|
||||
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;});
|
||||
|
||||
while (set.NextRow())
|
||||
{
|
||||
category.id = set.GetAsString(wxT("id"));
|
||||
category.parent = set.GetAsString(wxT("parent"));
|
||||
category.name = set.GetAsString(wxT("name"));
|
||||
category.color = wxColour(set.GetAsString(wxT("color")));
|
||||
category.font = set.GetAsString(wxT("font"));
|
||||
if (category.name != wxT("Fixe"))
|
||||
user->_categories.push_back(category);
|
||||
else
|
||||
user->_categories.insert(user->_categories.begin(), category);
|
||||
}
|
||||
|
||||
set.Finalize();
|
||||
|
||||
req = wxT("SELECT name, value FROM preference WHERE user='") + user->_id + wxT("' ORDER BY value ASC");
|
||||
EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;});
|
||||
|
||||
while (set.NextRow())
|
||||
user->_preferences[set.GetAsString(wxT("name"))] = set.GetAsString(wxT("value"));
|
||||
|
||||
set.Finalize();
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
void Database::LoadYear(User* user, int year)
|
||||
{
|
||||
wxSQLite3ResultSet set;
|
||||
wxString req;
|
||||
std::vector<Account>::iterator it;
|
||||
|
||||
if (user->_operations[year] == NULL)
|
||||
user->_operations[year] = new std::map<unsigned int, std::vector<Operation> >();
|
||||
|
||||
if (!user->_accounts.size()) return;
|
||||
|
||||
it = user->_accounts.begin();
|
||||
req = wxT("SELECT * FROM operation WHERE (account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += wxT("', '") + it->id ;
|
||||
}
|
||||
req += wxT("')");
|
||||
req += wxT(" OR user='") + user->_id + wxT("')");
|
||||
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
|
||||
req += wxT(" ORDER BY fix_cost DESC, year,month,day ASC");
|
||||
|
||||
EXECUTE_SQL_QUERY(req, set, );
|
||||
|
||||
while (set.NextRow())
|
||||
{
|
||||
Operation op;
|
||||
op.id = set.GetAsString(wxT("id"));
|
||||
op.account = set.GetAsString(wxT("account"));
|
||||
op.day = set.GetInt(wxT("day"));
|
||||
op.month = set.GetInt(wxT("month"));
|
||||
op.year = set.GetInt(wxT("year"));
|
||||
op.amount = set.GetDouble(wxT("amount"));
|
||||
op.description = set.GetAsString(wxT("description"));
|
||||
op.category = set.GetAsString(wxT("category"));
|
||||
op.fix_cost = set.GetBool(wxT("fix_cost"));
|
||||
op.checked = set.GetBool(wxT("checked"));
|
||||
(*user->_operations[op.year])[op.month].push_back(op);
|
||||
}
|
||||
|
||||
set.Finalize();
|
||||
}
|
||||
|
||||
double Database::GetAccountAmount(wxString id, int month, int year)
|
||||
{
|
||||
wxSQLite3ResultSet set;
|
||||
wxString req;
|
||||
double res;
|
||||
|
||||
req = wxT("SELECT amount FROM account_amount WHERE account='") + id ;
|
||||
req += wxT("' AND month='") + wxString::Format(wxT("%d"), month);
|
||||
req += wxT("' AND year='") + wxString::Format(wxT("%d"), year);
|
||||
req += wxT("'");
|
||||
|
||||
EXECUTE_SQL_QUERY(req , set, 0.0);
|
||||
|
||||
if (set.NextRow())
|
||||
res = set.GetDouble(wxT("amount"));
|
||||
else
|
||||
{
|
||||
SetAccountAmount(month, year, id, 0.0);
|
||||
res = 0.0;
|
||||
}
|
||||
|
||||
set.Finalize();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void Database::UpdateOperation(Operation op)
|
||||
{
|
||||
wxString req;
|
||||
req = wxT("UPDATE operation SET ") ;
|
||||
req += wxT("account='") + op.account + wxT("'");
|
||||
req += wxT(", year='") + wxString::Format(wxT("%d"), op.year) + wxT("'");
|
||||
req += wxT(", month='") + wxString::Format(wxT("%d"), op.month) + wxT("'");
|
||||
req += wxT(", day='") + wxString::Format(wxT("%d"), op.day) + wxT("'");
|
||||
req += wxT(", amount='") + DoubleToString(op.amount) + wxT("'");
|
||||
req += wxT(", description=\"") + op.description + wxT("\"");
|
||||
req += wxT(", category='") + op.category + wxT("'");
|
||||
if (op.checked)
|
||||
req += wxT(", checked='1'");
|
||||
else
|
||||
req += wxT(", checked='0'");
|
||||
req += wxT(" WHERE id='") + op.id + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
wxString Database::AddOperation(User* user, Operation op)
|
||||
{
|
||||
wxString req, res;
|
||||
wxSQLite3ResultSet set;
|
||||
|
||||
req = wxT("INSERT INTO operation ('user', 'account', 'year', 'month', 'day', 'amount', 'description', 'category', 'fix_cost') VALUES ('") ;
|
||||
req += user->_id + wxT("'");
|
||||
req += wxT(", '") + op.account + wxT("'");
|
||||
req += wxT(", '") + wxString::Format(wxT("%d"), op.year) + wxT("'");
|
||||
req += wxT(", '") + wxString::Format(wxT("%d"), op.month) + wxT("'");
|
||||
req += wxT(", '") + wxString::Format(wxT("%d"), op.day) + wxT("'");
|
||||
req += wxT(", '") + DoubleToString(op.amount) + wxT("'");
|
||||
req += wxT(", \"") + op.description + wxT("\"");
|
||||
req += wxT(", '") + op.category + wxT("'");
|
||||
if (op.fix_cost)
|
||||
req += wxT(", '1'") ;
|
||||
else
|
||||
req += wxT(", '0'") ;
|
||||
req += wxT(")");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, wxT("0"));
|
||||
|
||||
req = wxT("SELECT id FROM operation WHERE ");
|
||||
req += wxT("user='") + user->_id + wxT("'");
|
||||
req += wxT(" AND account='") + op.account + wxT("'");
|
||||
req += wxT(" AND year='") + wxString::Format(wxT("%d"), op.year) + wxT("'");
|
||||
req += wxT(" AND month='") + wxString::Format(wxT("%d"), op.month) + wxT("'");
|
||||
req += wxT(" AND day='") + wxString::Format(wxT("%d"), op.day) + wxT("'");
|
||||
req += wxT(" AND amount='") + DoubleToString(op.amount) + wxT("'");
|
||||
req += wxT(" AND description=\"") + op.description + wxT("\"");
|
||||
req += wxT(" AND category='") + op.category + wxT("'");
|
||||
if (op.fix_cost)
|
||||
req += wxT(" AND fix_cost='1'") ;
|
||||
else
|
||||
req += wxT(" AND fix_cost='0'") ;
|
||||
req += wxT("ORDER BY ID DESC") ;
|
||||
|
||||
EXECUTE_SQL_QUERY(req , set, wxT("0"));
|
||||
|
||||
if (set.NextRow())
|
||||
res = set.GetAsString(wxT("id"));
|
||||
else
|
||||
res = wxT("0");
|
||||
|
||||
set.Finalize();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void Database::DeleteOperation(Operation op)
|
||||
{
|
||||
wxString req;
|
||||
req = wxT("DELETE FROM operation WHERE id='") + op.id + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
void Database::DeleteOperations(User* user, int month, int year)
|
||||
{
|
||||
wxString req;
|
||||
std::vector<Account>::iterator it;
|
||||
|
||||
it = user->_accounts.begin();
|
||||
req = wxT("DELETE FROM account_amount WHERE account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += wxT("', '") + it->id ;
|
||||
}
|
||||
req += wxT("')");
|
||||
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
|
||||
if (month != -1)
|
||||
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
|
||||
it = user->_accounts.begin();
|
||||
req = wxT("DELETE FROM operation WHERE (account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += wxT("', '") + it->id ;
|
||||
}
|
||||
req += wxT("')");
|
||||
req += wxT(" OR user='") + user->_id + wxT("')");
|
||||
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
|
||||
if (month != -1)
|
||||
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
|
||||
}
|
||||
|
||||
void Database::SetAccountAmount(int month, int year, wxString accountId, double amount)
|
||||
{
|
||||
wxString req;
|
||||
req = wxT("UPDATE account_amount SET ") ;
|
||||
req += wxT("amount='") + DoubleToString(amount) + wxT("'");
|
||||
req += wxT(" WHERE account='") + accountId + wxT("'");
|
||||
req += wxT(" AND year='") + wxString::Format(wxT("%d"), year) + wxT("'");
|
||||
req += wxT(" AND month='") + wxString::Format(wxT("%d"), month) + wxT("'");
|
||||
|
||||
try
|
||||
{
|
||||
if (!_db.ExecuteUpdate(req))
|
||||
{
|
||||
req = wxT("INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('") ;
|
||||
req += accountId + wxT("'");
|
||||
req += wxT(" ,'") + wxString::Format(wxT("%d"), year) + wxT("'");
|
||||
req += wxT(" ,'") + wxString::Format(wxT("%d"), month) + wxT("'");
|
||||
req += wxT(" ,'") + DoubleToString(amount) + wxT("'");
|
||||
req += wxT(")");
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
}
|
||||
catch (wxSQLite3Exception e)
|
||||
{
|
||||
std::cerr << req.mb_str() << "\n" ;
|
||||
std::cerr << e.GetMessage().mb_str() << "\n" ;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
wxString Database::AddAccount(User* user, Account ac)
|
||||
{
|
||||
wxString req, res;
|
||||
wxSQLite3ResultSet set;
|
||||
|
||||
req = wxT("INSERT INTO account ('user', 'name', 'number', 'shared', 'default_account') VALUES ('") ;
|
||||
req += user->_id + wxT("'");
|
||||
req += wxT(", '") + ac.name + wxT("'");
|
||||
req += wxT(", '") + ac.number + wxT("'");
|
||||
if (ac.shared)
|
||||
req += wxT(", '1'") ;
|
||||
else
|
||||
req += wxT(", '0'") ;
|
||||
if (ac._default)
|
||||
req += wxT(", '1'") ;
|
||||
else
|
||||
req += wxT(", '0'") ;
|
||||
req += wxT(")");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, wxT("0"));
|
||||
|
||||
req = wxT("SELECT id FROM account WHERE name='") + ac.name + wxT("'") ;
|
||||
req += wxT("AND user='") + user->_id + wxT("'");
|
||||
|
||||
EXECUTE_SQL_QUERY(req , set, wxT("0"));
|
||||
|
||||
if (set.NextRow())
|
||||
res = set.GetAsString(wxT("id"));
|
||||
else
|
||||
res = wxT("0");
|
||||
|
||||
set.Finalize();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void Database::UpdateAccount(Account ac)
|
||||
{
|
||||
wxString req;
|
||||
req = wxT("UPDATE account SET ") ;
|
||||
req += wxT("name='") + ac.name + wxT("'");
|
||||
req += wxT(", number='") + ac.number + wxT("'");
|
||||
if (ac.shared)
|
||||
req += wxT(", shared='1'");
|
||||
else
|
||||
req += wxT(", shared='0'");
|
||||
if (ac._default)
|
||||
req += wxT(", default_account='1'");
|
||||
else
|
||||
req += wxT(", default_account='0'");
|
||||
req += wxT(" WHERE id='") + ac.id + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
void Database::DeleteAccount(Account ac)
|
||||
{
|
||||
wxString req;
|
||||
req = wxT("DELETE FROM account WHERE id='") + ac.id + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
wxString Database::AddCategory(User* user, Category category)
|
||||
{
|
||||
wxString req, res;
|
||||
wxSQLite3ResultSet set;
|
||||
wxString color;
|
||||
|
||||
color = wxT("#") ;
|
||||
color += wxString::Format(wxT("%02X"), category.color.Red());
|
||||
color += wxString::Format(wxT("%02X"), category.color.Green());
|
||||
color += wxString::Format(wxT("%02X"), category.color.Blue());
|
||||
|
||||
req = wxT("INSERT INTO category ('user', 'parent', 'name', 'color', font) VALUES ('") ;
|
||||
req += user->_id + wxT("'");
|
||||
req += wxT(", '") + category.parent + wxT("'");
|
||||
req += wxT(", '") + category.name + wxT("'");
|
||||
req += wxT(", '") + color + wxT("'");
|
||||
req += wxT(", '") + category.font + wxT("'");
|
||||
req += wxT(")");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, wxT("0"));
|
||||
|
||||
req = wxT("SELECT id FROM preference WHERE user='") + user->_id + wxT("'") ;
|
||||
req += wxT(" AND name='") + category.name + wxT("'");
|
||||
|
||||
EXECUTE_SQL_QUERY(req , set, wxT("0"));
|
||||
|
||||
if (set.NextRow())
|
||||
res = set.GetAsString(wxT("id"));
|
||||
else
|
||||
res = wxT("0");
|
||||
|
||||
set.Finalize();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void Database::UpdateCategory(Category category)
|
||||
{
|
||||
wxString req;
|
||||
wxString color;
|
||||
|
||||
color = wxT("#") ;
|
||||
color += wxString::Format(wxT("%02X"), category.color.Red());
|
||||
color += wxString::Format(wxT("%02X"), category.color.Green());
|
||||
color += wxString::Format(wxT("%02X"), category.color.Blue());
|
||||
|
||||
req = wxT("UPDATE category SET") ;
|
||||
req += wxT(" parent='") + category.parent + wxT("'");
|
||||
req += wxT(", name='") + category.name + wxT("'");
|
||||
req += wxT(", color='") + color + wxT("'");
|
||||
req += wxT(", font='") + category.font + wxT("'");
|
||||
req += wxT(" WHERE id='") + category.id + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
void Database::DeleteCategory(User* user, Category category)
|
||||
{
|
||||
wxString req;
|
||||
|
||||
req = wxT("DELETE FROM category WHERE id='") + category.id + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
|
||||
req = wxT("UPDATE category SET") ;
|
||||
req += wxT(" parent='0'");
|
||||
req += wxT(" WHERE parent='") + category.id + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
std::map<int, std::vector<int> > Database::GetAllOperations(User* user)
|
||||
{
|
||||
wxString req, req2, reqUnion;
|
||||
wxSQLite3ResultSet set, set2;
|
||||
std::vector<Account>::iterator it;
|
||||
std::map<int, std::vector<int> > res;
|
||||
int year;
|
||||
|
||||
if (!user->_accounts.empty())
|
||||
{
|
||||
it = user->_accounts.begin();
|
||||
req = wxT("SELECT DISTINCT year FROM account_amount WHERE account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += wxT("', '") + it->id ;
|
||||
}
|
||||
req += wxT("')");
|
||||
|
||||
it = user->_accounts.begin();
|
||||
req2 = wxT("SELECT DISTINCT year FROM operation WHERE account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req2 += wxT("', '") + it->id ;
|
||||
}
|
||||
req2 += wxT("')");
|
||||
req2 += wxT(" OR user='") + user->_id + wxT("'");
|
||||
req2 += wxT(" ORDER BY year ASC");
|
||||
|
||||
reqUnion = req + wxT(" UNION ") + req2;
|
||||
EXECUTE_SQL_QUERY(reqUnion, set, res);
|
||||
|
||||
while (set.NextRow())
|
||||
{
|
||||
year = set.GetInt(wxT("year"));
|
||||
|
||||
it = user->_accounts.begin();
|
||||
req = wxT("SELECT DISTINCT month FROM account_amount WHERE account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += wxT("', '") + it->id ;
|
||||
}
|
||||
req += wxT("')");
|
||||
req += wxT(" AND year='") + set.GetAsString(wxT("year")) + wxT("'");
|
||||
|
||||
it = user->_accounts.begin();
|
||||
req2 = wxT("SELECT DISTINCT month FROM operation WHERE (account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req2 += wxT("', '") + it->id ;
|
||||
}
|
||||
req2 += wxT("')");
|
||||
req2 += wxT(" OR user='") + user->_id + wxT("')");
|
||||
req2 += wxT(" AND year='") + set.GetAsString(wxT("year")) + wxT("'");
|
||||
req2 += wxT(" ORDER BY month ASC");
|
||||
|
||||
reqUnion = req + wxT(" UNION ") + req2;
|
||||
EXECUTE_SQL_QUERY(reqUnion, set2, res);
|
||||
|
||||
while (set2.NextRow())
|
||||
{
|
||||
res[year].push_back(set2.GetInt(wxT("month")));
|
||||
}
|
||||
set2.Finalize();
|
||||
}
|
||||
set.Finalize();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void Database::GenerateMonth(User* user, int monthFrom, int yearFrom, int monthTo, int yearTo)
|
||||
{
|
||||
std::vector<Account>::iterator it;
|
||||
wxString req;
|
||||
wxSQLite3ResultSet set;
|
||||
double amount;
|
||||
|
||||
if (monthFrom == -1 || yearFrom == -1)
|
||||
{
|
||||
for (it = user->_accounts.begin(); it != user->_accounts.end(); it++)
|
||||
{
|
||||
req = wxT("INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('") ;
|
||||
req += it->id + wxT("'");
|
||||
req += wxT(" ,'") + wxString::Format(wxT("%d"), yearTo) + wxT("'");
|
||||
req += wxT(" ,'") + wxString::Format(wxT("%d"), monthTo) + wxT("'");
|
||||
req += wxT(" ,'0.0'");
|
||||
req += wxT(")");
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (it = user->_accounts.begin(); it != user->_accounts.end(); it++)
|
||||
{
|
||||
amount = 0.0;
|
||||
req = wxT("SELECT SUM(amount) AS total FROM operation WHERE") ;
|
||||
req += wxT(" account='") + it->id + wxT("'");
|
||||
req += wxT(" AND year='") + wxString::Format(wxT("%d"), yearFrom) + wxT("'");
|
||||
req += wxT(" AND month='") + wxString::Format(wxT("%d"), monthFrom) + wxT("'");
|
||||
|
||||
EXECUTE_SQL_QUERY(req, set, );
|
||||
|
||||
if (set.NextRow())
|
||||
amount += set.GetDouble(wxT("total"));
|
||||
|
||||
req = wxT("SELECT amount FROM account_amount WHERE") ;
|
||||
req += wxT(" account='") + it->id + wxT("'");
|
||||
req += wxT(" AND year='") + wxString::Format(wxT("%d"), yearFrom) + wxT("'");
|
||||
req += wxT(" AND month='") + wxString::Format(wxT("%d"), monthFrom) + wxT("'");
|
||||
|
||||
EXECUTE_SQL_QUERY(req, set, );
|
||||
|
||||
if (set.NextRow())
|
||||
amount += set.GetDouble(wxT("amount"));
|
||||
|
||||
req = wxT("INSERT INTO account_amount ('account', 'year', 'month', 'amount') VALUES ('") ;
|
||||
req += it->id + wxT("'");
|
||||
req += wxT(" ,'") + wxString::Format(wxT("%d"), yearTo) + wxT("'");
|
||||
req += wxT(" ,'") + wxString::Format(wxT("%d"), monthTo) + wxT("'");
|
||||
req += wxT(" ,'") + DoubleToString(amount) + wxT("'");
|
||||
req += wxT(")");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
}
|
||||
|
||||
void Database::ChangePassword(User* user, wxString password)
|
||||
{
|
||||
wxString req;
|
||||
|
||||
req = wxT("UPDATE user SET ") ;
|
||||
req += wxT("password='") + HashPassword(password) + wxT("'");
|
||||
req += wxT(" WHERE name='") + user->_name + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
bool Database::UserExists(wxString name)
|
||||
{
|
||||
wxSQLite3ResultSet set;
|
||||
wxString req;
|
||||
bool res=false;
|
||||
|
||||
req = wxT("SELECT name FROM user WHERE name='") + name + wxT("'") ;
|
||||
|
||||
EXECUTE_SQL_QUERY(req , set, false);
|
||||
|
||||
if (set.NextRow())
|
||||
res = true;
|
||||
else
|
||||
res = false;
|
||||
|
||||
set.Finalize();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void Database::ChangeName(User* user, wxString name)
|
||||
{
|
||||
wxString req;
|
||||
|
||||
req = wxT("UPDATE user SET ") ;
|
||||
req += wxT("name='") + name + wxT("'");
|
||||
req += wxT(" WHERE name='") + user->_name + wxT("'");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
void Database::NewUser(wxString name)
|
||||
{
|
||||
wxString req, id;
|
||||
wxSQLite3ResultSet set;
|
||||
|
||||
req = wxT("INSERT INTO user ('name', 'password') VALUES ('") ;
|
||||
req += name + wxT("'");
|
||||
req += wxT(", '") + HashPassword(wxT("")) + wxT("'");
|
||||
req += wxT(")");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
|
||||
req = wxT("SELECT id FROM user WHERE ");
|
||||
req += wxT("name='") + name + wxT("'");
|
||||
|
||||
EXECUTE_SQL_QUERY(req , set, );
|
||||
|
||||
set.NextRow();
|
||||
id = set.GetAsString(wxT("id"));
|
||||
|
||||
set.Finalize();
|
||||
|
||||
req = wxT("SELECT * FROM default_preference");
|
||||
|
||||
EXECUTE_SQL_QUERY(req, set,);
|
||||
|
||||
while (set.NextRow())
|
||||
{
|
||||
req = wxT("INSERT INTO preference ('user', 'name', 'value') VALUES ('") ;
|
||||
req += id + wxT("'");
|
||||
req += wxT(", '") + set.GetAsString(wxT("type")) + wxT("'");
|
||||
req += wxT(", '") + set.GetAsString(wxT("value")) + wxT("'");
|
||||
req += wxT(")");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
set.Finalize();
|
||||
|
||||
req = wxT("SELECT * FROM default_category");
|
||||
|
||||
EXECUTE_SQL_QUERY(req, set,);
|
||||
|
||||
while (set.NextRow())
|
||||
{
|
||||
req = wxT("INSERT INTO category ('user', 'parent', 'name', 'color', 'font') VALUES ('") ;
|
||||
req += id + wxT("'");
|
||||
req += wxT(", '") + set.GetAsString(wxT("parent")) + wxT("'");
|
||||
req += wxT(", '") + set.GetAsString(wxT("name")) + wxT("'");
|
||||
req += wxT(", '") + set.GetAsString(wxT("color")) + wxT("'");
|
||||
req += wxT(", '") + set.GetAsString(wxT("font")) + wxT("'");
|
||||
req += wxT(")");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
set.Finalize();
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
/*
|
||||
Shared accounts not currently supported
|
||||
*/
|
||||
void Database::KillMe(User* user)
|
||||
{
|
||||
wxString req;
|
||||
std::vector<Account>::iterator it;
|
||||
|
||||
req = wxT("DELETE FROM preference WHERE user='") + user->_id + wxT("'");
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
|
||||
if (!user->_accounts.empty())
|
||||
{
|
||||
it = user->_accounts.begin();
|
||||
req = wxT("DELETE FROM account_amount WHERE account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += wxT("', '") + it->id ;
|
||||
}
|
||||
req += wxT("')");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
|
||||
it = user->_accounts.begin();
|
||||
req = wxT("DELETE FROM operation WHERE account IN('") + it->id;
|
||||
it++;
|
||||
for (;it != user->_accounts.end(); it++)
|
||||
{
|
||||
req += wxT("', '") + it->id ;
|
||||
}
|
||||
req += wxT("')");
|
||||
req += wxT(" OR user='") + user->_id + wxT("')");
|
||||
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
|
||||
req = wxT("DELETE FROM account WHERE user='") + user->_id + wxT("'");
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
|
||||
req = wxT("DELETE FROM category WHERE user='") + user->_id + wxT("'");
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
|
||||
req = wxT("DELETE FROM user WHERE id='") + user->_id + wxT("'");
|
||||
EXECUTE_SQL_UPDATE(req, );
|
||||
}
|
||||
58
src/model/Database.h
Normal file
58
src/model/Database.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef DATABASE_H
|
||||
#define DATABASE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <fstream>
|
||||
#include <list>
|
||||
#include <wx/wxsqlite3.h>
|
||||
#include <wx/wx.h>
|
||||
#include <sha1.h>
|
||||
|
||||
#include "model.h"
|
||||
|
||||
#define BDD_FILE "kc.bdd"
|
||||
#define INIT_SCRIPT "init.sql"
|
||||
|
||||
class Database
|
||||
{
|
||||
public:
|
||||
Database();
|
||||
|
||||
std::list<wxString> GetUsers();
|
||||
bool IsValidUser(wxString user, wxString password);
|
||||
|
||||
User* LoadUser(wxString name);
|
||||
void LoadYear(User* user, int year);
|
||||
|
||||
void UpdateOperation(Operation op);
|
||||
wxString AddOperation(User* user, Operation op);
|
||||
void DeleteOperation(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);
|
||||
|
||||
wxString AddAccount(User* user, Account ac);
|
||||
void UpdateAccount(Account ac);
|
||||
void DeleteAccount(Account ac);
|
||||
|
||||
wxString AddCategory(User* user, Category category);
|
||||
void UpdateCategory(Category category);
|
||||
void DeleteCategory(User* user, Category category);
|
||||
|
||||
std::map<int, std::vector<int> > GetAllOperations(User* user);
|
||||
void GenerateMonth(User* user, int monthFrom, int yearFrom, int monthTo, int yearTo);
|
||||
|
||||
void ChangePassword(User* user, wxString password);
|
||||
bool UserExists(wxString name);
|
||||
void ChangeName(User* user, wxString name);
|
||||
void NewUser(wxString name);
|
||||
|
||||
void KillMe(User* user);
|
||||
private:
|
||||
wxSQLite3Database _db;
|
||||
|
||||
void CreateDatabase();
|
||||
wxString HashPassword(wxString password);
|
||||
};
|
||||
|
||||
#endif
|
||||
18
src/model/Operation.h
Normal file
18
src/model/Operation.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef OPERATION_H
|
||||
#define OPERATION_H
|
||||
|
||||
class Operation {
|
||||
public:
|
||||
wxString id;
|
||||
unsigned int day;
|
||||
unsigned int month;
|
||||
unsigned int year;
|
||||
double amount;
|
||||
wxString description;
|
||||
wxString category;
|
||||
bool fix_cost;
|
||||
wxString account;
|
||||
bool checked;
|
||||
} ;
|
||||
|
||||
#endif
|
||||
87
src/model/User.cpp
Normal file
87
src/model/User.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "User.h"
|
||||
|
||||
User::~User()
|
||||
{
|
||||
std::map<unsigned int, std::map<unsigned int, std::vector<Operation> >* >::iterator it;
|
||||
|
||||
for (it = _operations.begin(); it != _operations.end(); it++)
|
||||
{
|
||||
if (_operations[it->first])
|
||||
{
|
||||
delete it->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Category User::GetCategory(wxString catId)
|
||||
{
|
||||
Category cat;
|
||||
std::vector<Category>::iterator it;
|
||||
|
||||
for (it=_categories.begin(); it !=_categories.end(); it++)
|
||||
if (it->id == catId)
|
||||
return *it;
|
||||
|
||||
cat.id = wxT("0");
|
||||
cat.parent = wxT("0");
|
||||
cat.name = _("Unknown");
|
||||
cat.font = wxT("");
|
||||
cat.color = wxColour(0xFF, 0xFF, 0xFF);
|
||||
|
||||
return cat;
|
||||
}
|
||||
|
||||
wxString User::GetCategoryName(wxString catId)
|
||||
{
|
||||
std::vector<Category>::iterator it;
|
||||
for (it=_categories.begin(); it !=_categories.end(); it++)
|
||||
if (it->id == catId)
|
||||
return it->name;
|
||||
|
||||
return _("Unknown") ;
|
||||
}
|
||||
|
||||
wxString User::GetCategoryId(wxString catName)
|
||||
{
|
||||
std::vector<Category>::iterator it;
|
||||
for (it=_categories.begin(); it !=_categories.end(); it++)
|
||||
if (it->name == catName)
|
||||
return it->id;
|
||||
|
||||
return wxT("0") ;
|
||||
}
|
||||
|
||||
wxString User::GetAccountName(wxString accountId)
|
||||
{
|
||||
std::vector<Account>::iterator it;
|
||||
for (it=_accounts.begin(); it !=_accounts.end(); it++)
|
||||
if (it->id == accountId)
|
||||
return it->name;
|
||||
|
||||
return _("Unknown") ;
|
||||
}
|
||||
|
||||
wxString User::GetAccountId(wxString accountName)
|
||||
{
|
||||
std::vector<Account>::iterator it;
|
||||
for (it=_accounts.begin(); it !=_accounts.end(); it++)
|
||||
if (it->name == accountName)
|
||||
return it->id;
|
||||
|
||||
return wxT("0") ;
|
||||
}
|
||||
|
||||
int User::GetCategoriesNumber()
|
||||
{
|
||||
return _categories.size();
|
||||
}
|
||||
|
||||
int User::GetAccountsNumber()
|
||||
{
|
||||
return _accounts.size();
|
||||
}
|
||||
|
||||
int User::GetOperationsNumber(int month, int year)
|
||||
{
|
||||
return (*_operations[year])[month].size();
|
||||
}
|
||||
36
src/model/User.h
Normal file
36
src/model/User.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef USER_H
|
||||
#define USER_H
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <wx/wx.h>
|
||||
#include <wx/colour.h>
|
||||
|
||||
#include "Category.h"
|
||||
#include "Account.h"
|
||||
#include "Operation.h"
|
||||
|
||||
class User
|
||||
{
|
||||
public:
|
||||
~User();
|
||||
|
||||
wxString _id;
|
||||
wxString _name;
|
||||
wxString _password;
|
||||
std::vector<Account> _accounts;
|
||||
std::map<unsigned int, std::map<unsigned int, std::vector<Operation> >* > _operations;
|
||||
std::vector<Category> _categories;
|
||||
std::map<wxString, wxString> _preferences;
|
||||
|
||||
Category GetCategory(wxString catId);
|
||||
wxString GetCategoryName(wxString catId);
|
||||
wxString GetCategoryId(wxString catName);
|
||||
wxString GetAccountName(wxString accountId);
|
||||
wxString GetAccountId(wxString accountName);
|
||||
int GetCategoriesNumber();
|
||||
int GetAccountsNumber();
|
||||
int GetOperationsNumber(int month, int year);
|
||||
};
|
||||
|
||||
#endif
|
||||
7
src/model/model.h
Normal file
7
src/model/model.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef MODEL_H
|
||||
#define MODEL_H
|
||||
|
||||
#include "User.h"
|
||||
#include "Database.h"
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user