Generate & delete month/year OK

This commit is contained in:
Grégory Soutadé 2010-06-24 21:02:42 +02:00
parent 271b3ef1e7
commit e72e36f27d
6 changed files with 133 additions and 11 deletions

View File

@ -84,6 +84,19 @@ void KissCount::DeleteOperation(struct 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);
@ -149,10 +162,14 @@ void KissCount::GenerateMonth(int monthFrom, int yearFrom, int monthTo, int year
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;

View File

@ -25,6 +25,7 @@ class KissCount
wxString AddOperation(struct operation op);
void UpdateOperation(struct operation op);
void DeleteOperation(struct operation op);
void DeleteOperations(int month, int year);
double GetAccountAmount(wxString id, int month, int year);
void SetAccountAmount(int month, int year, wxString accountId, double value);

View File

@ -16,6 +16,7 @@
}\
catch (wxSQLite3Exception e)\
{\
std::cerr << req.mb_str() << "\n" ;\
std::cerr << e.GetMessage().mb_str() << "\n" ;\
code_if_fail; \
return return_value;\
@ -30,6 +31,7 @@
}\
catch (wxSQLite3Exception e)\
{\
std::cerr << req.mb_str() << "\n" ;\
std::cerr << e.GetMessage().mb_str() << "\n" ;\
code_if_fail; \
return return_value;\
@ -103,11 +105,12 @@ void Database::CreateDatabase()
std::list<wxString> Database::GetUsers()
{
std::list<wxString> res;
wxString req;
// Check whether value exists in table
wxSQLite3ResultSet set ;
EXECUTE_SQL_QUERY(_("SELECT name FROM user ORDER BY name"), set, res);
req = _("SELECT name FROM user ORDER BY name");
EXECUTE_SQL_QUERY(req, set, res);
while (set.NextRow())
{
@ -382,6 +385,41 @@ void Database::DeleteOperation(struct operation op)
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 = _("DELETE FROM account_amount WHERE account IN('") + it->id;
it++;
for (;it != user->_accounts.end(); it++)
{
req += _("', '") + it->id ;
}
req += _("')");
req += _(" AND year='") + wxString::Format(_("%d"), year) + _("'");
if (month != -1)
req += _(" AND month='") + wxString::Format(_("%d"), month) + _("'");
EXECUTE_SQL_UPDATE(req, );
it = user->_accounts.begin();
req = _("DELETE FROM operation WHERE account IN('") + it->id;
it++;
for (;it != user->_accounts.end(); it++)
{
req += _("', '") + it->id ;
}
req += _("')");
req += _(" AND year='") + wxString::Format(_("%d"), year) + _("'");
if (month != -1)
req += _(" AND month='") + wxString::Format(_("%d"), month) + _("'");
EXECUTE_SQL_UPDATE(req, );
}
void Database::SetAccountAmount(int month, int year, wxString accountId, double amount)
{
wxString req;
@ -542,6 +580,7 @@ void Database::UpdateCategory(User* user, wxString oldName, wxString name, wxStr
void Database::DeleteCategory(User* user, struct category category)
{
wxString req;
req = _("DELETE FROM preference WHERE user='") + user->_id + _("'");
req += _(" AND type='category'");
req += _(" AND name='name'");

View File

@ -27,6 +27,7 @@ class Database
void UpdateOperation(struct operation op);
wxString AddOperation(User* user, struct operation op);
void DeleteOperation(struct 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);

View File

@ -854,37 +854,52 @@ void AccountPanel::OnTreeChange(wxTreeEvent& event)
inModification = false;
}
void AccountPanel::OnMenuGenerate(wxCommandEvent& event)
void AccountPanel::GetTreeSelection(int* month, int* year)
{
wxString monthString;
int month, year, i;
int i;
month = year = -1;
*month = -1; *year = -1;
monthString = _tree.GetItemText(_tree.GetSelection());
for (i=0; i<12; i++)
if (monthString == months[i])
{
month = i;
*month = i;
break;
}
if (month == -1)
if (*month == -1)
{
year = wxAtoi(monthString);
*year = wxAtoi(monthString);
// Error
if (year == 0)
{
*month = -1;
*year = -1;
return;
}
}
else
{
year = wxAtoi(_tree.GetItemText(_tree.GetItemParent(_tree.GetSelection())));
*year = wxAtoi(_tree.GetItemText(_tree.GetItemParent(_tree.GetSelection())));
// Error
if (year == 0)
{
*month = -1;
*year = -1;
return;
}
}
}
void AccountPanel::OnMenuGenerate(wxCommandEvent& event)
{
int month, year;
GetTreeSelection(&month, &year);
GenerateDialog g(_kiss, _wxUI, month, year);
g.ShowModal();
@ -892,7 +907,49 @@ void AccountPanel::OnMenuGenerate(wxCommandEvent& event)
void AccountPanel::OnMenuDelete(wxCommandEvent& event)
{
int month, year;
wxString message;
wxTreeItemId curNode, node ;
std::map<int, std::vector<int> > ops ;
GetTreeSelection(&month, &year);
message = _("Are you sure want to delete ");
if (month != -1)
message += months[month] + _(" ");
message += wxString::Format(_("%d"), year);
message += _(" operations ?");
wxMessageDialog dialog(_wxUI, message, _("KissCount"), wxYES_NO);
if (dialog.ShowModal() == wxID_NO)
return;
curNode = _tree.GetSelection();
ops = _kiss->GetAllOperations();
if (ops[year].size() == 1 && month != -1)
curNode = _tree.GetItemParent(curNode);
_kiss->DeleteOperations(month, year);
node = _tree.GetNextSibling(curNode);
if (!node.IsOk())
node = _tree.GetPrevSibling(curNode);
_tree.Delete(curNode);
if (!node.IsOk())
ChangeUser();
else
{
_tree.SelectItem(node);
GetTreeSelection(&month, &year);
if (month == -1)
month = ops[year][0];
ShowMonth(month, year);
}
}
void AccountPanel::GenerateMonth(int month, int year)
@ -936,7 +993,13 @@ void AccountPanel::GenerateMonth(int month, int year)
}
if (!years.IsOk())
years = _tree.PrependItem(root, yearString);
{
years = _tree.GetFirstChild(root, cookie);
if (wxAtoi(_tree.GetItemText(years)) > year)
years = _tree.PrependItem(root, yearString);
else
years = _tree.AppendItem(root, yearString);
}
if (!_tree.GetChildrenCount(years, true))
node = _tree.AppendItem(years, monthString);

View File

@ -65,6 +65,7 @@ private:
void InitAccountsGrid(User* user, int month, int year);
void UpdateStats();
void InsertOperation(User* user, operation* op, int line, bool fix);
void GetTreeSelection(int* month, int* year);
DECLARE_EVENT_TABLE();
};