KissCount/src/model/User.cpp

277 lines
6.4 KiB
C++
Raw Normal View History

2010-07-10 16:34:30 +02:00
/*
Copyright 2010-2011 Grégory Soutadé
2010-07-10 16:34:30 +02:00
This file is part of KissCount.
2010-07-10 16:34:30 +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
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
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
*/
2010-05-15 11:21:42 +02:00
#include "User.h"
2010-10-24 16:04:56 +02:00
User::User(Database* db) : _db(db)
{}
2010-05-15 11:21:42 +02:00
User::~User()
{
InvalidateOperations();
}
void User::InvalidateOperations()
2010-05-15 11:21:42 +02:00
{
std::map<unsigned int, std::map<unsigned int, std::vector<Operation> >* >::iterator it;
2010-05-15 11:21:42 +02:00
for (it = _operations.begin(); it != _operations.end(); it++)
2010-05-15 11:21:42 +02:00
{
if (_operations[it->first])
2010-05-15 11:21:42 +02:00
{
delete it->second;
2010-05-15 11:21:42 +02:00
}
}
_operations.clear();
2010-05-15 11:21:42 +02:00
}
2010-05-24 20:14:15 +02:00
2011-02-09 20:50:21 +01:00
Category User::GetCategory(const wxString& catId)
2010-06-22 12:29:36 +02:00
{
Category cat;
std::vector<Category>::iterator it;
2010-06-22 12:29:36 +02:00
for (it=_categories.begin(); it !=_categories.end(); it++)
if (it->id == catId)
return *it;
2010-06-22 12:29:36 +02:00
2010-10-24 16:04:56 +02:00
if (_db->LoadCategory(catId, wxT(""), cat))
return cat;
cat.id = wxT("0");
cat.parent = wxT("0");
cat.name = _("Unknown");
cat.font = wxT("");
2010-11-17 21:09:41 +01:00
cat.backcolor = OWN_GREEN;
cat.forecolor = wxColour(0x00, 0x00, 0x00);
2010-06-22 12:29:36 +02:00
return cat;
2010-06-22 12:29:36 +02:00
}
2011-02-09 20:50:21 +01:00
wxString User::GetCategoryName(const wxString& catId)
2010-05-24 20:14:15 +02:00
{
2010-10-24 16:04:56 +02:00
Category cat;
std::vector<Category>::iterator it;
2010-10-24 16:04:56 +02:00
for (it=_categories.begin(); it !=_categories.end(); it++)
if (it->id == catId)
return it->name;
2010-06-22 12:29:36 +02:00
2010-10-24 16:04:56 +02:00
if (_db->LoadCategory(catId, wxT(""), cat))
return cat.name;
return _("Unknown") ;
2010-05-24 20:14:15 +02:00
}
2011-02-09 20:50:21 +01:00
wxString User::GetCategoryId(const wxString& catName)
2010-06-03 18:28:38 +02:00
{
std::vector<Category>::iterator it;
2010-10-24 16:04:56 +02:00
Category cat;
for (it=_categories.begin(); it !=_categories.end(); it++)
if (wxGetTranslation(it->name) == catName)
return it->id;
2010-06-03 18:28:38 +02:00
2010-10-24 16:04:56 +02:00
if ( _db->LoadCategory(wxT(""), catName, cat))
return cat.id;
return wxT("0") ;
2010-06-03 18:28:38 +02:00
}
2011-02-09 20:50:21 +01:00
const wxFont User::GetCategoryFont(const wxString& catId)
2010-09-08 11:02:03 +02:00
{
wxFont 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];
2010-10-24 16:04:56 +02:00
if (_db->LoadCategory(catId, wxT(""), cat))
return KissCount::ExtractFont(cat.font);
2010-09-08 11:02:03 +02:00
return f;
}
Account User::GetAccount(const wxString& accountId)
{
std::vector<Account>::iterator it;
for (it=_accounts.begin(); it !=_accounts.end(); it++)
if (it->id == accountId)
return *it;
}
wxString User::GetAccountName(const wxString& accountId)
2010-05-24 20:14:15 +02:00
{
std::vector<Account>::iterator it;
for (it=_accounts.begin(); it !=_accounts.end(); it++)
if (it->id == accountId)
return it->name;
2010-06-21 13:02:02 +02:00
return _("Unknown") ;
2010-05-24 20:14:15 +02:00
}
2010-05-27 21:09:02 +02:00
2011-02-09 20:50:21 +01:00
wxString User::GetAccountId(const wxString& accountName)
2010-06-03 18:28:38 +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
return wxT("0") ;
2010-06-03 18:28:38 +02:00
}
2010-05-27 21:09:02 +02:00
int User::GetCategoriesNumber()
{
return _categories.size();
2010-05-27 21:09:02 +02:00
}
2010-06-02 22:14:11 +02:00
int User::GetAccountsNumber()
{
return _accounts.size();
2010-06-02 22:14:11 +02:00
}
int User::GetOperationsNumber(int month, int year)
{
return (*_operations[year])[month].size();
2010-06-02 22:14:11 +02:00
}
2010-07-06 20:59:02 +02:00
wxLanguage User::GetLanguage()
{
wxString res = _preferences[wxT("language")];
long val;
2010-07-06 20:59:02 +02:00
if (!res.Length())
return wxLANGUAGE_ENGLISH ;
2010-07-06 20:59:02 +02:00
res.ToLong(&val);
2010-07-06 20:59:02 +02:00
return (wxLanguage)val;
2010-07-06 20:59:02 +02:00
}
void User::LinkOrUnlinkOperation(Operation& op)
{
std::vector<Operation>::iterator it;
2011-02-14 20:56:59 +01:00
Account account, account2;
if (!_operations[op.year])
_db->LoadYear(this, op.year);
if (!_operations[op.year])
return;
// Not Linked
if (!op.transfert.Length())
{
for (it = (*_operations[op.year])[op.month].begin(); it != (*_operations[op.year])[op.month].end(); it++)
{
if (it->id != op.id && it->transfert == op.id)
{
it->transfert = wxT("");
2011-02-14 20:56:59 +01:00
it->_virtual = false;
return;
}
}
}
// Linked
else
{
for (it = (*_operations[op.year])[op.month].begin(); it != (*_operations[op.year])[op.month].end(); it++)
{
if (it->id != op.id && it->id == op.transfert)
{
2011-02-14 20:56:59 +01:00
account = GetAccount(it->account);
account2 = GetAccount(op.account);
it->transfert = op.id;
2011-02-14 20:56:59 +01:00
it->_virtual = account._virtual || account2._virtual;
op._virtual = account._virtual || account2._virtual;
return;
}
}
op.transfert = wxT("");
2011-02-14 20:56:59 +01:00
op._virtual = false;
}
}
bool User::Group(std::vector<Operation>* ops, const Operation& op)
{
std::vector<Operation>::iterator it;
std::vector<wxString>::iterator it2;
for (it = ops->begin(); it != ops->end(); it++)
{
if (it->id == op.parent)
{
for (it2 = it->childs.begin(); it2 != it->childs.end(); it2++)
if (*it2 == op.id)
break;
// Already into childs
if (it2 != it->childs.end()) return true;
it->childs.push_back(op.id);
return true;
}
}
2010-10-24 16:04:56 +02:00
return false ;
}
void User::Group(const Operation& op)
{
std::vector<Operation>::iterator it;
std::vector<wxString>::iterator it2;
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);
}
}
void User::UnGroup(const Operation& op)
{
std::vector<Operation>::iterator it;
std::vector<wxString>::iterator it2;
for (it = (*_operations[op.year])[op.month].begin(); it != (*_operations[op.year])[op.month].end(); it++)
{
if (it->id == op.parent)
{
for (it2 = it->childs.begin(); it2 != it->childs.end(); it2++)
if (*it2 == op.id)
{
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++)
if (it->second[i].parent.Length())
Group(((*_operations[year])[it->first])[i]);
}
}