Grégory Soutadé
d6ade299e3
Fix a bug in LonkOrUnlinkOperation (when year was not previously loaded) Translate categories in PreferencesPanel
277 lines
6.4 KiB
C++
277 lines
6.4 KiB
C++
/*
|
|
Copyright 2010-2011 Grégory Soutadé
|
|
|
|
This file is part of KissCount.
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "User.h"
|
|
|
|
User::User(Database* db) : _db(db)
|
|
{}
|
|
|
|
User::~User()
|
|
{
|
|
InvalidateOperations();
|
|
}
|
|
|
|
void User::InvalidateOperations()
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
_operations.clear();
|
|
}
|
|
|
|
Category User::GetCategory(const wxString& catId)
|
|
{
|
|
Category cat;
|
|
std::vector<Category>::iterator it;
|
|
|
|
for (it=_categories.begin(); it !=_categories.end(); it++)
|
|
if (it->id == catId)
|
|
return *it;
|
|
|
|
if (_db->LoadCategory(catId, wxT(""), cat))
|
|
return cat;
|
|
|
|
cat.id = wxT("0");
|
|
cat.parent = wxT("0");
|
|
cat.name = _("Unknown");
|
|
cat.font = wxT("");
|
|
cat.backcolor = OWN_GREEN;
|
|
cat.forecolor = wxColour(0x00, 0x00, 0x00);
|
|
|
|
return cat;
|
|
}
|
|
|
|
wxString User::GetCategoryName(const wxString& catId)
|
|
{
|
|
Category cat;
|
|
std::vector<Category>::iterator it;
|
|
|
|
for (it=_categories.begin(); it !=_categories.end(); it++)
|
|
if (it->id == catId)
|
|
return it->name;
|
|
|
|
if (_db->LoadCategory(catId, wxT(""), cat))
|
|
return cat.name;
|
|
|
|
return _("Unknown") ;
|
|
}
|
|
|
|
wxString User::GetCategoryId(const wxString& catName)
|
|
{
|
|
std::vector<Category>::iterator it;
|
|
Category cat;
|
|
|
|
for (it=_categories.begin(); it !=_categories.end(); it++)
|
|
if (wxGetTranslation(it->name) == catName)
|
|
return it->id;
|
|
|
|
if ( _db->LoadCategory(wxT(""), catName, cat))
|
|
return cat.id;
|
|
|
|
return wxT("0") ;
|
|
}
|
|
|
|
const wxFont User::GetCategoryFont(const wxString& catId)
|
|
{
|
|
wxFont f;
|
|
Category cat;
|
|
|
|
for (unsigned int i=0; i<_categories.size(); i++)
|
|
if (_categories[i].id == catId)
|
|
return _categoriesFonts[i];
|
|
|
|
if (_db->LoadCategory(catId, wxT(""), cat))
|
|
return KissCount::ExtractFont(cat.font);
|
|
|
|
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)
|
|
{
|
|
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(const 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();
|
|
}
|
|
|
|
wxLanguage User::GetLanguage()
|
|
{
|
|
wxString res = _preferences[wxT("language")];
|
|
long val;
|
|
|
|
if (!res.Length())
|
|
return wxLANGUAGE_ENGLISH ;
|
|
|
|
res.ToLong(&val);
|
|
|
|
return (wxLanguage)val;
|
|
}
|
|
|
|
void User::LinkOrUnlinkOperation(Operation& op)
|
|
{
|
|
std::vector<Operation>::iterator it;
|
|
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("");
|
|
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)
|
|
{
|
|
account = GetAccount(it->account);
|
|
account2 = GetAccount(op.account);
|
|
it->transfert = op.id;
|
|
it->_virtual = account._virtual || account2._virtual;
|
|
op._virtual = account._virtual || account2._virtual;
|
|
return;
|
|
}
|
|
}
|
|
op.transfert = wxT("");
|
|
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;
|
|
}
|
|
}
|
|
|
|
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))
|
|
{
|
|
(*_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]);
|
|
}
|
|
}
|