2010-07-10 16:34:30 +02:00
|
|
|
/*
|
2011-02-13 19:30:12 +01:00
|
|
|
Copyright 2010-2011 Grégory Soutadé
|
2010-07-10 16:34:30 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
This file is part of KissCount.
|
2010-07-10 16:34:30 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
KissCount is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2010-07-10 16:34:30 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
KissCount is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2010-07-10 16:34:30 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
|
2010-07-10 16:34:30 +02:00
|
|
|
*/
|
|
|
|
|
2011-08-20 11:43:12 +02:00
|
|
|
#include <algorithm>
|
2011-08-27 18:35:36 +02:00
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
2011-08-20 14:02:47 +02:00
|
|
|
#include <view/view.hpp>
|
|
|
|
#include "User.hpp"
|
2010-05-15 11:21:42 +02:00
|
|
|
|
2010-10-24 16:04:56 +02:00
|
|
|
User::User(Database* db) : _db(db)
|
|
|
|
{}
|
|
|
|
|
2010-05-15 11:21:42 +02:00
|
|
|
User::~User()
|
2011-02-13 19:30:12 +01:00
|
|
|
{
|
|
|
|
InvalidateOperations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void User::InvalidateOperations()
|
2010-05-15 11:21:42 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
std::map<unsigned int, std::map<unsigned int, std::vector<Operation> >* >::iterator it;
|
2010-05-15 11:21:42 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
for (it = _operations.begin(); it != _operations.end(); it++)
|
2010-05-15 11:21:42 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
if (_operations[it->first])
|
2010-05-15 11:21:42 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
delete it->second;
|
2010-05-15 11:21:42 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-13 19:30:12 +01:00
|
|
|
|
|
|
|
_operations.clear();
|
2010-05-15 11:21:42 +02:00
|
|
|
}
|
2010-05-24 20:14:15 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
Category User::GetCategory(int catId)
|
2010-06-22 12:29:36 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
Category cat;
|
2011-08-14 19:26:54 +02:00
|
|
|
std::vector<Category>::iterator it = std::find(_categories.begin(), _categories.end(), catId);
|
2010-06-22 12:29:36 +02:00
|
|
|
|
2011-08-14 19:26:54 +02:00
|
|
|
if (it !=_categories.end()) return *it;
|
2010-06-22 12:29:36 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
if (_db->LoadCategory(catId, "", cat))
|
2010-10-24 16:04:56 +02:00
|
|
|
return cat;
|
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
cat.id = 0;
|
|
|
|
cat.parent = 0;
|
2010-08-26 21:28:15 +02:00
|
|
|
cat.name = _("Unknown");
|
2011-08-25 17:45:41 +02:00
|
|
|
cat.font = "";
|
2011-08-16 18:22:35 +02:00
|
|
|
cat.backcolor = view::OWN_GREEN;
|
2011-08-27 18:35:36 +02:00
|
|
|
cat.forecolor = Qt::black;
|
2010-06-22 12:29:36 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
return cat;
|
2010-06-22 12:29:36 +02:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
QString User::GetCategoryName(int catId)
|
2010-05-24 20:14:15 +02:00
|
|
|
{
|
2010-10-24 16:04:56 +02:00
|
|
|
Category cat;
|
2011-08-14 19:26:54 +02:00
|
|
|
std::vector<Category>::iterator it = std::find(_categories.begin(), _categories.end(), catId);
|
2010-10-24 16:04:56 +02:00
|
|
|
|
2011-08-14 19:26:54 +02:00
|
|
|
if (it !=_categories.end()) return it->name;
|
2010-06-22 12:29:36 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
if (_db->LoadCategory(catId, "", cat))
|
2010-10-24 16:04:56 +02:00
|
|
|
return cat.name;
|
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
return _("Unknown") ;
|
2010-05-24 20:14:15 +02:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
int User::GetCategoryId(const QString& catName)
|
2010-06-03 18:28:38 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
std::vector<Category>::iterator it;
|
2010-10-24 16:04:56 +02:00
|
|
|
Category cat;
|
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
for (it=_categories.begin(); it !=_categories.end(); it++)
|
2011-08-27 18:35:36 +02:00
|
|
|
if (_(it->name.toStdString().c_str()) == catName)
|
2010-08-26 21:28:15 +02:00
|
|
|
return it->id;
|
2010-06-03 18:28:38 +02:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
if ( _db->LoadCategory(0, catName, cat))
|
2010-10-24 16:04:56 +02:00
|
|
|
return cat.id;
|
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
return 0 ;
|
2010-06-03 18:28:38 +02:00
|
|
|
}
|
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
const QFont User::GetCategoryFont(int catId)
|
2010-09-08 11:02:03 +02:00
|
|
|
{
|
2011-08-25 17:45:41 +02:00
|
|
|
QFont f;
|
2010-10-24 16:04:56 +02:00
|
|
|
Category cat;
|
2010-09-08 11:02:03 +02:00
|
|
|
|
|
|
|
for (unsigned int i=0; i<_categories.size(); i++)
|
|
|
|
if (_categories[i].id == catId)
|
|
|
|
return _categoriesFonts[i];
|
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
if (_db->LoadCategory(catId, "", cat))
|
2010-10-24 16:04:56 +02:00
|
|
|
return KissCount::ExtractFont(cat.font);
|
|
|
|
|
2010-09-08 11:02:03 +02:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2011-08-14 21:03:48 +02:00
|
|
|
void User::AddCategory(const Category& cat)
|
|
|
|
{
|
|
|
|
_categories.push_back(cat);
|
2011-08-25 17:45:41 +02:00
|
|
|
_categoriesFonts.push_back(KissCount::ExtractFont(""));
|
2011-08-14 21:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void User::UpdateCategory(const Category& cat)
|
|
|
|
{
|
|
|
|
std::vector<Category>::iterator it = std::find(_categories.begin(), _categories.end(), cat.id);
|
|
|
|
|
|
|
|
if (it !=_categories.end())
|
|
|
|
{
|
|
|
|
*it = cat;
|
|
|
|
_categoriesFonts[it-_categories.begin()] = KissCount::ExtractFont(cat.font);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void User::DeleteCategory(const Category& cat)
|
|
|
|
{
|
|
|
|
std::vector<Category>::iterator it = std::find(_categories.begin(), _categories.end(), cat.id);
|
|
|
|
|
|
|
|
if (it !=_categories.end())
|
|
|
|
{
|
|
|
|
int pos = it - _categories.begin();
|
|
|
|
_categories.erase(_categories.begin()+pos);
|
|
|
|
_categoriesFonts.erase(_categoriesFonts.begin()+pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
Account User::GetAccount(int accountId) throw (AccountNotFound)
|
2010-11-01 11:34:39 +01:00
|
|
|
{
|
2011-08-14 19:26:54 +02:00
|
|
|
std::vector<Account>::iterator it = std::find(_accounts.begin(), _accounts.end(), accountId);
|
|
|
|
|
|
|
|
if (it != _accounts.end())
|
|
|
|
return *it;
|
|
|
|
|
|
|
|
throw AccountNotFound();
|
2010-11-01 11:34:39 +01:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
QString User::GetAccountName(int accountId)
|
2010-05-24 20:14:15 +02:00
|
|
|
{
|
2011-08-14 19:26:54 +02:00
|
|
|
std::vector<Account>::iterator it = std::find(_accounts.begin(), _accounts.end(), accountId);
|
|
|
|
|
|
|
|
if (it != _accounts.end()) return it->name;
|
2010-06-21 13:02:02 +02:00
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
return _("Unknown") ;
|
2010-05-24 20:14:15 +02:00
|
|
|
}
|
2010-05-27 21:09:02 +02:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
int User::GetAccountId(const QString& accountName)
|
2010-06-03 18:28:38 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
std::vector<Account>::iterator it;
|
|
|
|
for (it=_accounts.begin(); it !=_accounts.end(); it++)
|
|
|
|
if (it->name == accountName)
|
|
|
|
return it->id;
|
2010-06-03 18:28:38 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
return 0 ;
|
2010-06-03 18:28:38 +02:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
int User::GetAccountIdFromAccountNumber(const QString& accountNumber)
|
|
|
|
{
|
|
|
|
std::vector<Account>::iterator it;
|
|
|
|
for (it=_accounts.begin(); it !=_accounts.end(); it++)
|
|
|
|
if (it->number == accountNumber)
|
|
|
|
return it->id;
|
|
|
|
|
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
2011-08-14 21:03:48 +02:00
|
|
|
void User::AddAccount(Account& ac)
|
|
|
|
{
|
|
|
|
_accounts.push_back(ac);
|
|
|
|
}
|
|
|
|
|
|
|
|
void User::UpdateAccount(Account& ac)
|
|
|
|
{
|
|
|
|
std::vector<Account>::iterator it = std::find(_accounts.begin(), _accounts.end(), ac.id);
|
|
|
|
|
|
|
|
if (it != _accounts.end())
|
|
|
|
*it = ac;
|
|
|
|
}
|
|
|
|
|
|
|
|
void User::DeleteAccount(Account& ac)
|
|
|
|
{
|
|
|
|
std::vector<Account>::iterator it = std::find(_accounts.begin(), _accounts.end(), ac.id);
|
|
|
|
|
|
|
|
if (it != _accounts.end())
|
|
|
|
_accounts.erase(it);
|
|
|
|
}
|
|
|
|
|
2010-05-27 21:09:02 +02:00
|
|
|
int User::GetCategoriesNumber()
|
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
return _categories.size();
|
2010-05-27 21:09:02 +02:00
|
|
|
}
|
2010-06-02 22:14:11 +02:00
|
|
|
|
|
|
|
int User::GetAccountsNumber()
|
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
return _accounts.size();
|
2010-06-02 22:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int User::GetOperationsNumber(int month, int year)
|
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
return (*_operations[year])[month].size();
|
2010-06-02 22:14:11 +02:00
|
|
|
}
|
2010-07-06 20:59:02 +02:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
QString User::GetLanguage()
|
2010-07-06 20:59:02 +02:00
|
|
|
{
|
2011-08-25 17:45:41 +02:00
|
|
|
return _preferences["language"];
|
|
|
|
// long val;
|
2010-07-06 20:59:02 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
// if (!res.Length())
|
|
|
|
// return wxLANGUAGE_ENGLISH ;
|
2010-07-06 20:59:02 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
// res.ToLong(&val);
|
2010-07-06 20:59:02 +02:00
|
|
|
|
2011-08-25 17:45:41 +02:00
|
|
|
// return (wxLanguage)val;
|
2010-07-06 20:59:02 +02:00
|
|
|
}
|
2010-08-21 09:25:35 +02:00
|
|
|
|
|
|
|
void User::LinkOrUnlinkOperation(Operation& op)
|
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
std::vector<Operation>::iterator it;
|
2011-02-14 20:56:59 +01:00
|
|
|
Account account, account2;
|
2010-08-21 09:25:35 +02:00
|
|
|
|
2011-04-03 19:11:34 +02:00
|
|
|
if (!_operations[op.year])
|
|
|
|
_db->LoadYear(this, op.year);
|
|
|
|
|
|
|
|
if (!_operations[op.year])
|
|
|
|
return;
|
|
|
|
|
2010-08-26 21:28:15 +02:00
|
|
|
// Not Linked
|
2011-08-25 17:45:41 +02:00
|
|
|
if (!op.transfert)
|
2010-08-21 09:25:35 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
for (it = (*_operations[op.year])[op.month].begin(); it != (*_operations[op.year])[op.month].end(); it++)
|
2010-08-21 09:25:35 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
if (it->id != op.id && it->transfert == op.id)
|
2010-08-21 09:25:35 +02:00
|
|
|
{
|
2011-08-25 17:45:41 +02:00
|
|
|
it->transfert = 0;
|
2011-02-14 20:56:59 +01:00
|
|
|
it->_virtual = false;
|
2010-08-26 21:28:15 +02:00
|
|
|
return;
|
2010-08-21 09:25:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-26 21:28:15 +02:00
|
|
|
// Linked
|
|
|
|
else
|
2010-08-21 09:25:35 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
for (it = (*_operations[op.year])[op.month].begin(); it != (*_operations[op.year])[op.month].end(); it++)
|
2010-08-21 09:25:35 +02:00
|
|
|
{
|
2010-08-26 21:28:15 +02:00
|
|
|
if (it->id != op.id && it->id == op.transfert)
|
2010-08-21 09:25:35 +02:00
|
|
|
{
|
2011-02-14 20:56:59 +01:00
|
|
|
account = GetAccount(it->account);
|
|
|
|
account2 = GetAccount(op.account);
|
2010-08-26 21:28:15 +02:00
|
|
|
it->transfert = op.id;
|
2011-02-14 20:56:59 +01:00
|
|
|
it->_virtual = account._virtual || account2._virtual;
|
|
|
|
op._virtual = account._virtual || account2._virtual;
|
2010-08-26 21:28:15 +02:00
|
|
|
return;
|
2010-08-21 09:25:35 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-25 17:45:41 +02:00
|
|
|
op.transfert = 0;
|
2011-02-14 20:56:59 +01:00
|
|
|
op._virtual = false;
|
2010-08-21 09:25:35 +02:00
|
|
|
}
|
|
|
|
}
|
2010-09-22 21:02:29 +02:00
|
|
|
|
2010-10-25 21:33:09 +02:00
|
|
|
bool User::Group(std::vector<Operation>* ops, const Operation& op)
|
2010-09-22 21:02:29 +02:00
|
|
|
{
|
|
|
|
std::vector<Operation>::iterator it;
|
2011-08-25 17:45:41 +02:00
|
|
|
std::vector<int>::iterator it2;
|
2010-09-22 21:02:29 +02:00
|
|
|
|
2010-10-25 21:33:09 +02:00
|
|
|
for (it = ops->begin(); it != ops->end(); it++)
|
2010-09-22 21:02:29 +02:00
|
|
|
{
|
2010-10-03 20:31:06 +02:00
|
|
|
if (it->id == op.parent)
|
2010-09-22 21:02:29 +02:00
|
|
|
{
|
2011-08-14 19:26:54 +02:00
|
|
|
it2 = std::find(it->childs.begin(), it->childs.end(), op.id);
|
2010-09-22 21:02:29 +02:00
|
|
|
// Already into childs
|
2010-10-25 21:33:09 +02:00
|
|
|
if (it2 != it->childs.end()) return true;
|
2010-10-03 20:31:06 +02:00
|
|
|
it->childs.push_back(op.id);
|
2010-10-25 21:33:09 +02:00
|
|
|
return true;
|
2010-09-22 21:02:29 +02:00
|
|
|
}
|
|
|
|
}
|
2010-10-24 16:04:56 +02:00
|
|
|
|
2010-10-25 21:33:09 +02:00
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void User::Group(const Operation& op)
|
|
|
|
{
|
|
|
|
if (!Group(&(*_operations[op.year])[op.month], op)
|
|
|
|
&& _db->LoadOperation(this, op.parent))
|
2010-10-24 16:04:56 +02:00
|
|
|
{
|
|
|
|
(*_operations[op.year])[op.month][(*_operations[op.year])[op.month].size()-1].childs.push_back(op.id);
|
|
|
|
}
|
2010-09-22 21:02:29 +02:00
|
|
|
}
|
|
|
|
|
2010-10-03 20:31:06 +02:00
|
|
|
void User::UnGroup(const Operation& op)
|
2010-09-22 21:02:29 +02:00
|
|
|
{
|
|
|
|
std::vector<Operation>::iterator it;
|
2011-08-25 17:45:41 +02:00
|
|
|
std::vector<int>::iterator it2;
|
2010-09-22 21:02:29 +02:00
|
|
|
|
2010-10-03 20:31:06 +02:00
|
|
|
for (it = (*_operations[op.year])[op.month].begin(); it != (*_operations[op.year])[op.month].end(); it++)
|
2010-09-22 21:02:29 +02:00
|
|
|
{
|
2010-10-03 20:31:06 +02:00
|
|
|
if (it->id == op.parent)
|
2010-09-22 21:02:29 +02:00
|
|
|
{
|
|
|
|
for (it2 = it->childs.begin(); it2 != it->childs.end(); it2++)
|
2010-10-03 20:31:06 +02:00
|
|
|
if (*it2 == op.id)
|
2010-09-22 21:02:29 +02:00
|
|
|
{
|
|
|
|
it->childs.erase(it2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void User::ResolveGroups(int year)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
std::map<unsigned int, std::vector<Operation> >::iterator it;
|
|
|
|
|
|
|
|
for (it = _operations[year]->begin(); it != _operations[year]->end(); it++)
|
|
|
|
{
|
|
|
|
for (i=0; i<it->second.size(); i++)
|
2011-08-27 18:35:36 +02:00
|
|
|
if (it->second[i].parent)
|
2010-10-03 20:31:06 +02:00
|
|
|
Group(((*_operations[year])[it->first])[i]);
|
2010-09-22 21:02:29 +02:00
|
|
|
}
|
|
|
|
}
|