KissCount/controller/KissCount.cpp

246 lines
4.8 KiB
C++

#include "KissCount.h"
KissCount::KissCount() : _user(NULL)
{
_wxUI = new wxUI(this, wxT("KissCount"), wxPoint(50, 50), wxSize(1024, 768));
_wxUI->Show(true);
_wxUI->Centre();
_wxUI->Disable();
try
{
_db = new Database();
}
catch (std::string s)
{
_wxUI->Close(true);
throw s;
}
_wxUI->ChangeUser();
_wxUI->Enable();
}
KissCount::~KissCount()
{
delete _db;
delete _wxUI;
if (_user) delete _user;
}
std::list<wxString> KissCount::GetUsers()
{
return _db->GetUsers();
}
bool KissCount::IsValidUser(wxString user, wxString password)
{
return _db->IsValidUser(user, password) ;
}
void KissCount::LoadUser(wxString user)
{
if (_user) delete _user;
_user = _db->LoadUser(user) ;
if (_user)
_wxUI->LoadUser();
}
void KissCount::LoadYear(int year, bool force)
{
if (!force && _user->_operations[year] != NULL) return;
if (_user->_operations[year] != NULL)
{
delete _user->_operations[year];
}
_db->LoadYear(_user, year);
}
User* KissCount::GetUser()
{
return _user;
}
double KissCount::GetAccountAmount(wxString id, int month, int year)
{
return _db->GetAccountAmount(id, month, year);
}
void KissCount::UpdateOperation(Operation op)
{
_db->UpdateOperation(op);
}
wxString KissCount::AddOperation(Operation op)
{
return _db->AddOperation(_user, op);
}
void KissCount::DeleteOperation(Operation op)
{
_db->DeleteOperation(op);
}
void KissCount::DeleteOperations(int month, int year)
{
_db->DeleteOperations(_user, month, year);
if (month != -1)
(*_user->_operations[year]).erase(month);
if (month == -1 || !_user->_operations[year]->size())
{
delete _user->_operations[year];
_user->_operations.erase(year);
}
}
void KissCount::SetAccountAmount(int month, int year, wxString accountId, double amount)
{
_db->SetAccountAmount(month, year, accountId, amount);
}
wxString KissCount::AddAccount(Account ac)
{
ac.id = _db->AddAccount(_user, ac);
_user->_accounts.push_back(ac);
return ac.id;
}
void KissCount::UpdateAccount(Account ac)
{
std::vector<Account>::iterator it;
int i;
_db->UpdateAccount(ac);
for (i=0, it=_user->_accounts.begin(); it !=_user->_accounts.end(); it++, i++)
if (it->id == ac.id)
_user->_accounts[i] = ac;
}
void KissCount::DeleteAccount(Account ac)
{
std::vector<Account>::iterator it;
int i;
_db->DeleteAccount(ac);
for (i=0, it=_user->_accounts.begin(); it !=_user->_accounts.end(); it++, i++)
if (it->id == ac.id)
{
_user->_accounts.erase(_user->_accounts.begin()+i);
break;
}
}
wxString KissCount::AddCategory(Category category)
{
wxString id;
id = _db->AddCategory(_user, category);
category.id = id;
_user->_categories.push_back(category);
return id;
}
void KissCount::UpdateCategory(Category category)
{
_db->UpdateCategory(category);
for (int i=0; i<_user->GetCategoriesNumber();i++)
if (_user->_categories[i].id == category.id)
{
_user->_categories[i] = category;
break;
}
}
void KissCount::DeleteCategory(Category category)
{
_db->DeleteCategory(_user, category);
for (int i=0; i<_user->GetCategoriesNumber();i++)
if (_user->_categories[i].id == category.id)
{
_user->_categories.erase(_user->_categories.begin()+i);
break;
}
}
std::map<int, std::vector<int> > KissCount::GetAllOperations()
{
return _db->GetAllOperations(_user);
}
void KissCount::GenerateMonth(int monthFrom, int yearFrom, int monthTo, int yearTo)
{
std::vector<Operation>::iterator it;
Operation op;
_db->GenerateMonth(_user, monthFrom, yearFrom, monthTo, yearTo);
if (!_user->_operations[yearTo])
_user->_operations[yearTo] = new std::map<unsigned int, std::vector<Operation> >();
if (monthFrom != -1 && yearFrom != -1)
{
LoadYear(yearFrom, false);
for(it = (*_user->_operations[yearFrom])[monthFrom].begin();
it != (*_user->_operations[yearFrom])[monthFrom].end()
&& it->fix_cost;
it++)
{
op = *it;
op.month = monthTo;
op.year = yearTo;
op.id = AddOperation(op);
(*_user->_operations[yearTo])[monthTo].push_back(op);
}
}
_wxUI->GenerateMonth(monthTo, yearTo);
}
void KissCount::ChangePassword(wxString password)
{
_db->ChangePassword(_user, password);
}
bool KissCount::UserExists(wxString name)
{
return _db->UserExists(name);
}
void KissCount::ChangeName(wxString name)
{
_db->ChangeName(_user, name);
_user->_name = name;
}
void KissCount::NewUser(wxString name)
{
wxDateTime curDate;
Account ac = {wxT(""), wxT("Account 1"), wxT(""), false, true};
_db->NewUser(name);
if (_user) delete _user;
_user = _db->LoadUser(name) ;
curDate.SetToCurrent();
AddAccount(ac);
_db->GenerateMonth(_user, -1, -1, (int)curDate.GetMonth(), curDate.GetYear());
}
void KissCount::KillMe()
{
_wxUI->KillMe();
_db->KillMe(_user);
delete _user;
_user = NULL;
_wxUI->ChangeUser();
}