KissCount/view/AccountPanel.cpp
2010-05-16 20:09:18 +02:00

225 lines
7.1 KiB
C++

#include "AccountPanel.h"
static wxString colsName[] = {_("Description"), _("Date"), _("Debit"), _("Credit"), _("Category"), _("Account"), _("")};
AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _kiss(kiss), _wxUI(parent), _tree(this, -1)
{
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxChartPanel* chart ;
SetSizer(hbox);
_pie = new PiePlot();
_grid = new GridAccount(this, -1);
_grid->CreateGrid(1, NUMBER_COLS);
_grid->SetColLabelSize(0);
_grid->SetRowLabelSize(0);
chart = new wxChartPanel(this);
chart->SetChart(new Chart(_pie, _("Cost repartition")));
hbox->Add(&_tree, 0);
hbox->Add(_grid, 0);
hbox->Add(chart, 0);
ChangeUser();
Fit();
SetMinSize(GetSize());
}
void AccountPanel::ChangeUser()
{
User* user = _kiss->GetUser();
int curYear;
std::map<unsigned int, std::map<unsigned int, std::list<operation> >* >::iterator it;
wxDateTime curDate;
curDate.SetToCurrent();
for(it = user->_operations.begin(); it != user->_operations.end(); it++)
{
if ((int)it->first <= curDate.GetYear())
curYear = it->first;
_tree.AddRoot(wxString::Format(wxT("%d"), it->first));
}
LoadYear(curYear);
}
void AccountPanel::LoadYear(int year)
{
User* user = _kiss->GetUser();
int curMonth = -1;
std::map<unsigned int, std::list<operation> >::iterator it;
wxDateTime curDate;
wxTreeItemId parentNode, curMonthNode;
if (user->_operations[year] != NULL)
{
ShowMonth(year, 0);
return;
}
_kiss->LoadYear(year);
curDate.SetToCurrent();
parentNode = _tree.GetSelection();
for (it = user->_operations[year]->begin(); it != user->_operations[year]->end(); it++)
{
if (year == curDate.GetYear() && (int)it->first <= curDate.GetMonth())
{
curMonth = it->first;
curMonthNode = _tree.AppendItem(parentNode, months[it->first]);
}
else if(curMonth == -1)
{
curMonthNode = _tree.AppendItem(parentNode, months[it->first]);
curMonth++;
}
else
_tree.AppendItem(parentNode, months[it->first]);
}
_tree.Expand(parentNode);
_tree.SelectItem(curMonthNode, true);
ShowMonth(year, curMonth);
}
#define SET_ROW_COLOR(row, color) for(int i=0; i<NUMBER_COLS; i++) \
{\
_grid->SetCellBackgroundColour(row, i, color);\
}
void AccountPanel::ShowMonth(int year, int month)
{
std::list<operation> operations;
std::list<operation>::iterator it;
int fixCosts = 0;
int curLine = 0;
User* user = _kiss->GetUser();
wxFont font(DEFAULT_FONT_SIZE, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, DEFAULT_FONT);
std::list<wxString>::iterator categoryIt;
wxGridCellChoiceEditor* categoryEditor;
wxString* categories;
int i;
_wxUI->SetTitle(user->_name + _(" - ") + months[month]);
// Operations are ordered
operations = (*user->_operations[year])[month];
_grid->CreateGrid(1, NUMBER_COLS);
// Creating headers
_grid->SetColSize (0, _grid->GetColSize(0)*3);
_grid->SetDefaultCellFont(font);
font.SetWeight(wxFONTWEIGHT_BOLD);
for(i=0; i<NUMBER_COLS; i++)
{
_grid->SetCellValue(0, i, colsName[i]);
_grid->SetCellBackgroundColour(0, i, OWN_CYAN);
_grid->SetCellFont(0, i, font);
_grid->SetReadOnly(0, i, true);
}
// SetCellBackgroundColour (int row, int col, const wxColour &colour);
// SetCellFont (int row, int col, const wxFont &font);
// SetCellValue (int row, int col, const wxString &s);
// GetColSize (int col) const ;
// SetColSize (int col, int width);
// AppendRows (int numRows=1, bool updateLabels=true);
// InsertRows (int pos=0, int numRows=1, bool updateLabels=true);
// SetReadOnly(row, col, bool)
categories = new wxString[user->_preferences._categories.size()+1];
categories[0] = _("unknown");
for (i=1,
categoryIt = user->_preferences._categories.begin();
categoryIt != user->_preferences._categories.end();
categoryIt++,
i++)
categories[i] = *categoryIt;
categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false);
it = operations.begin();
/*
struct operation {
wxString id;
unsigned int day;
unsigned int month;
unsigned int year;
int amount;
wxString description;
wxString category;
bool fix_cost;
enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS};
} ;
*/
while (it->fix_cost)
{
_grid->AppendRows();
curLine++;
fixCosts++;
SET_ROW_COLOR(curLine, OWN_YELLOW);
_grid->SetCellValue(curLine, DESCRIPTION, it->description);
_grid->SetCellValue(curLine, DATE, wxString::Format(wxT("%02d/%02d/%d"), it->day+1, it->month+1, it->year));
if (it->amount < 0)
_grid->SetCellValue(curLine, DEBIT, wxString::Format(wxT("%d"), -it->amount));
else
_grid->SetCellValue(curLine, CREDIT, wxString::Format(wxT("%d"), it->amount));
_grid->SetCellValue(curLine, CATEGORY, it->category);
_grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor());
_grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor());
categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false);
_grid->SetCellEditor(curLine, CATEGORY, categoryEditor);
_grid->SetCellValue(curLine, ACCOUNT, it->account);
it++;
}
_grid->AppendRows();
curLine++;
SET_ROW_COLOR(curLine, OWN_YELLOW);
categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false);
_grid->SetCellEditor(curLine, CATEGORY, categoryEditor);
_grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor());
_grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor());
_grid->_fixCosts = ++fixCosts;
for (; it != operations.begin(); it++)
{
_grid->AppendRows();
curLine++;
SET_ROW_COLOR(curLine, OWN_GREEN);
_grid->SetCellValue(curLine, DESCRIPTION, it->description);
_grid->SetCellValue(curLine, DATE, wxString::Format(wxT("%02d/%02d/%d"), it->day+1, it->month+1, it->year));
if (it->amount < 0)
_grid->SetCellValue(curLine, DEBIT, wxString::Format(wxT("%d"), -it->amount));
else
_grid->SetCellValue(curLine, CREDIT, wxString::Format(wxT("%d"), it->amount));
_grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor());
_grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor());
_grid->SetCellValue(curLine, CATEGORY, it->category);
categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false);
_grid->SetCellEditor(curLine, CATEGORY, categoryEditor);
_grid->SetCellValue(curLine, ACCOUNT, it->account);
it++;
}
_grid->AppendRows();
curLine++;
SET_ROW_COLOR(curLine, OWN_GREEN);
categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false);
_grid->SetCellEditor(curLine, CATEGORY, categoryEditor);
_grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor());
_grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor());
delete[] categories;
Fit();
SetMinSize(GetSize());
}