2010-07-14 16:22:02 +02:00
|
|
|
/*
|
|
|
|
Copyright 2010 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 "AccountPanel.h"
|
|
|
|
|
2010-07-16 18:38:13 +02:00
|
|
|
enum {SEARCH_ID, GRID_ID, CALENDAR_FROM_ID, CALENDAR_TO_ID};
|
2010-07-14 16:22:02 +02:00
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(SearchPanel, wxPanel)
|
|
|
|
EVT_BUTTON(SEARCH_ID, SearchPanel::OnButtonSearch)
|
2010-07-16 18:38:13 +02:00
|
|
|
EVT_CALENDAR_SEL_CHANGED(CALENDAR_FROM_ID, SearchPanel::OnCalendarFromChange)
|
|
|
|
EVT_CALENDAR_SEL_CHANGED(CALENDAR_TO_ID, SearchPanel::OnCalendarToChange)
|
2010-07-16 19:28:36 +02:00
|
|
|
EVT_GRID_CMD_CELL_CHANGE(GRID_ID, SearchPanel::OnOperationModified)
|
|
|
|
EVT_SHOW(SearchPanel::OnShow)
|
2010-07-14 16:22:02 +02:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
#define SET_ROW_COLOR(row, color) for(int i=0; i<NUMBER_COLS_OPS; i++) \
|
|
|
|
{ \
|
|
|
|
_grid->SetCellBackgroundColour(row, i, color); \
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchPanel::SearchPanel(KissCount* kiss, wxUI *parent) : wxScrolledWindow(&(*parent)), _kiss(kiss), _wxUI(parent), _operations(NULL)
|
|
|
|
{
|
|
|
|
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
|
|
|
|
|
|
|
|
DEFAULT_FONT(font);
|
|
|
|
User* user = _kiss->GetUser();
|
|
|
|
std::vector<Account>::iterator accountIt;
|
|
|
|
std::vector<Category>::iterator categoryIt;
|
|
|
|
|
|
|
|
SetSizer(vbox);
|
|
|
|
|
|
|
|
_checkDateFrom = new wxCheckBox(this, wxID_ANY, _("Date from"));
|
|
|
|
_checkDateTo = new wxCheckBox(this, wxID_ANY, _("Date to"));
|
|
|
|
|
|
|
|
wxGridBagSizer *gridBagSizer = new wxGridBagSizer(3, 9);
|
|
|
|
|
2010-07-16 18:38:13 +02:00
|
|
|
_calendarFrom = new wxCalendarCtrl(this, CALENDAR_FROM_ID, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize,
|
2010-07-14 16:22:02 +02:00
|
|
|
wxCAL_MONDAY_FIRST);
|
2010-07-16 18:38:13 +02:00
|
|
|
_calendarTo = new wxCalendarCtrl(this, CALENDAR_TO_ID, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize,
|
2010-07-14 16:22:02 +02:00
|
|
|
wxCAL_MONDAY_FIRST);
|
|
|
|
|
|
|
|
|
|
|
|
_description = new wxTextCtrl(this, wxID_ANY);
|
|
|
|
wxSize size = _description->GetSize();
|
|
|
|
size.SetWidth(size.GetWidth()*2);
|
|
|
|
_description->SetMinSize(size);
|
|
|
|
_amountFrom = new wxTextCtrl(this, wxID_ANY);
|
|
|
|
_amountTo = new wxTextCtrl(this, wxID_ANY);
|
|
|
|
|
|
|
|
_category = new wxCheckListBox(this, wxID_ANY);
|
|
|
|
for(categoryIt = user->_categories.begin(); categoryIt != user->_categories.end(); categoryIt++)
|
|
|
|
_category->Append(categoryIt->name);
|
|
|
|
_account = new wxCheckListBox(this, wxID_ANY);
|
|
|
|
for(accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); accountIt++)
|
|
|
|
_account->Append(accountIt->name);
|
|
|
|
|
|
|
|
_searchButton = new wxButton(this, SEARCH_ID, _("Search"));
|
|
|
|
|
2010-07-14 18:56:07 +02:00
|
|
|
wxStaticText* labelDescription = new wxStaticText(this, wxID_ANY, _("Description"));
|
|
|
|
wxStaticText* labelAmountFrom = new wxStaticText(this, wxID_ANY, _("Amount from"));
|
|
|
|
wxStaticText* labelAmountTo = new wxStaticText(this, wxID_ANY, _("Amount to"));
|
|
|
|
wxStaticText* labelCategory = new wxStaticText(this, wxID_ANY, _("Category"));
|
|
|
|
wxStaticText* labelAccount = new wxStaticText(this, wxID_ANY, _("Account"));
|
|
|
|
|
|
|
|
gridBagSizer->Add(labelDescription, wxGBPosition(0, 0));
|
2010-07-14 16:22:02 +02:00
|
|
|
gridBagSizer->Add(_description, wxGBPosition(1, 0));
|
|
|
|
gridBagSizer->Add(_checkDateFrom, wxGBPosition(0, 1));
|
|
|
|
gridBagSizer->Add(_calendarFrom, wxGBPosition(1, 1));
|
|
|
|
gridBagSizer->Add(_checkDateTo, wxGBPosition(0, 2));
|
|
|
|
gridBagSizer->Add(_calendarTo, wxGBPosition(1, 2));
|
2010-07-14 18:56:07 +02:00
|
|
|
gridBagSizer->Add(labelAmountFrom, wxGBPosition(0, 3));
|
2010-07-14 16:22:02 +02:00
|
|
|
gridBagSizer->Add(_amountFrom, wxGBPosition(1, 3));
|
2010-07-14 18:56:07 +02:00
|
|
|
gridBagSizer->Add(labelAmountTo, wxGBPosition(0, 4));
|
2010-07-14 16:22:02 +02:00
|
|
|
gridBagSizer->Add(_amountTo, wxGBPosition(1, 4));
|
2010-07-14 18:56:07 +02:00
|
|
|
gridBagSizer->Add(labelCategory, wxGBPosition(0, 5));
|
2010-07-14 16:22:02 +02:00
|
|
|
gridBagSizer->Add(_category, wxGBPosition(1, 5));
|
2010-07-14 18:56:07 +02:00
|
|
|
gridBagSizer->Add(labelAccount, wxGBPosition(0, 6));
|
2010-07-14 16:22:02 +02:00
|
|
|
gridBagSizer->Add(_account, wxGBPosition(1, 6));
|
|
|
|
gridBagSizer->Add(_searchButton, wxGBPosition(2, 0));
|
|
|
|
|
|
|
|
vbox->Add(gridBagSizer);
|
|
|
|
vbox->Add(-1, 20);
|
|
|
|
|
|
|
|
_grid = new GridAccount(_kiss, this, GRID_ID);
|
|
|
|
|
|
|
|
vbox->Add(_grid);
|
|
|
|
|
|
|
|
Fit();
|
|
|
|
|
|
|
|
SetMinSize(wxSize(1024, 640));
|
|
|
|
SetScrollbars(10, 10, 100/10, 100/10);
|
|
|
|
|
|
|
|
Hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchPanel::~SearchPanel()
|
|
|
|
{
|
|
|
|
if (_operations) delete _operations;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchPanel::OnButtonSearch(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
wxString *description=NULL, *amountFrom=NULL, *amountTo=NULL;
|
|
|
|
std::vector<wxString> categories, accounts;
|
2010-07-14 18:56:07 +02:00
|
|
|
wxDateTime *dateFrom=NULL, *dateTo=NULL;
|
2010-07-14 16:22:02 +02:00
|
|
|
User* user= _kiss->GetUser();
|
|
|
|
int i;
|
|
|
|
std::vector<Operation>::iterator it;
|
2010-07-14 18:56:07 +02:00
|
|
|
double af, at;
|
|
|
|
|
|
|
|
if (_calendarFrom->GetDate() > _calendarTo->GetDate())
|
|
|
|
{
|
2010-07-16 18:38:13 +02:00
|
|
|
wxMessageBox(_("Invalid date range"), _("Error"), wxICON_ERROR | wxOK);
|
2010-07-14 18:56:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_amountFrom->GetLineText(0) != wxT(""))
|
|
|
|
{
|
|
|
|
amountFrom = new wxString;
|
|
|
|
*amountFrom = _amountFrom->GetLineText(0);
|
|
|
|
if (!amountFrom->ToDouble(&af))
|
|
|
|
{
|
2010-07-16 18:38:13 +02:00
|
|
|
wxMessageBox(_("Invalid amount from"), _("Error"), wxICON_ERROR | wxOK);
|
2010-07-14 18:56:07 +02:00
|
|
|
delete amountFrom;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_amountTo->GetLineText(0) != wxT(""))
|
|
|
|
{
|
|
|
|
amountTo = new wxString;
|
|
|
|
*amountTo = _amountTo->GetLineText(0);
|
|
|
|
if (!amountTo->ToDouble(&at))
|
|
|
|
{
|
2010-07-16 18:38:13 +02:00
|
|
|
wxMessageBox(_("Invalid amount to"), _("Error"), wxICON_ERROR | wxOK);
|
2010-07-14 18:56:07 +02:00
|
|
|
delete amountFrom;
|
|
|
|
delete amountTo;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amountFrom && amountTo && af > at)
|
|
|
|
{
|
2010-07-16 18:38:13 +02:00
|
|
|
wxMessageBox(_("Invalid amount range"), _("Error"), wxICON_ERROR | wxOK);
|
2010-07-14 18:56:07 +02:00
|
|
|
delete amountFrom;
|
|
|
|
delete amountTo;
|
|
|
|
return;
|
|
|
|
}
|
2010-07-14 16:22:02 +02:00
|
|
|
|
|
|
|
_grid->DeleteRows(1, _grid->GetNumberRows()-1);
|
|
|
|
|
2010-07-14 18:56:07 +02:00
|
|
|
if (_description->GetLineText(0) != wxT(""))
|
2010-07-14 16:22:02 +02:00
|
|
|
{
|
|
|
|
description = new wxString;
|
|
|
|
*description = _description->GetLineText(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_checkDateFrom->IsChecked())
|
|
|
|
{
|
|
|
|
dateFrom = new wxDateTime;
|
|
|
|
*dateFrom = _calendarFrom->GetDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_checkDateTo->IsChecked())
|
|
|
|
{
|
|
|
|
dateTo = new wxDateTime;
|
|
|
|
*dateTo = _calendarTo->GetDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateFrom && dateTo && *dateFrom > *dateTo)
|
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2010-07-14 18:56:07 +02:00
|
|
|
for(i=0; i<user->GetCategoriesNumber(); i++)
|
|
|
|
if (_category->IsChecked(i))
|
|
|
|
categories.push_back(user->_categories[i].id);
|
2010-07-14 16:22:02 +02:00
|
|
|
|
2010-07-14 18:56:07 +02:00
|
|
|
for(i=0; i<user->GetAccountsNumber(); i++)
|
|
|
|
if (_account->IsChecked(i))
|
|
|
|
accounts.push_back(user->_accounts[i].id);
|
2010-07-14 16:22:02 +02:00
|
|
|
|
|
|
|
if (_operations)
|
|
|
|
delete _operations;
|
|
|
|
|
|
|
|
_operations = _kiss->Search(description, dateFrom, dateTo, amountFrom, amountTo, categories, accounts);
|
2010-07-14 18:56:07 +02:00
|
|
|
|
|
|
|
if (_operations->size() > 1)
|
|
|
|
wxMessageBox(wxString::Format(wxT("%d"), _operations->size()) + _(" entries found"), wxT("KissCount"), wxICON_INFORMATION | wxOK);
|
|
|
|
else if (_operations->size() == 1)
|
|
|
|
wxMessageBox(_("1 entry found"), wxT("KissCount"), wxICON_INFORMATION | wxOK);
|
|
|
|
else
|
|
|
|
wxMessageBox(_("No entry found"), wxT("KissCount"), wxICON_INFORMATION | wxOK);
|
|
|
|
|
2010-07-14 16:22:02 +02:00
|
|
|
for(i=1, it = _operations->begin(); it != _operations->end(); it++, i++)
|
|
|
|
{
|
|
|
|
_grid->InsertOperation(user, &(*it), i, false, 0, 0);
|
|
|
|
}
|
|
|
|
|
2010-07-14 18:56:07 +02:00
|
|
|
_grid->AutoSizeColumn(CATEGORY, false);
|
|
|
|
_grid->AutoSizeColumn(DATE, false);
|
|
|
|
_grid->AutoSizeColumn(ACCOUNT, false);
|
|
|
|
_grid->AutoSizeColumn(DELETE, false);
|
|
|
|
_grid->AutoSizeColumn(CHECKED, false);
|
|
|
|
|
2010-07-14 16:22:02 +02:00
|
|
|
_wxUI->Layout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchPanel::OnShow(wxShowEvent& event)
|
|
|
|
{
|
|
|
|
_wxUI->SetTitle(_kiss->GetUser()->_name + wxT(" - ") + _("Search"));
|
|
|
|
}
|
|
|
|
|
2010-07-16 18:38:13 +02:00
|
|
|
void SearchPanel::OnCalendarFromChange(wxCalendarEvent& event)
|
|
|
|
{
|
|
|
|
_checkDateFrom->SetValue(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchPanel::OnCalendarToChange(wxCalendarEvent& event)
|
|
|
|
{
|
|
|
|
_checkDateTo->SetValue(true);
|
|
|
|
}
|
2010-07-16 19:28:36 +02:00
|
|
|
|
|
|
|
void SearchPanel::OnOperationModified(wxGridEvent& event)
|
|
|
|
{
|
|
|
|
User* user = _kiss->GetUser();
|
|
|
|
int row = event.GetRow()-1;
|
|
|
|
int col = event.GetCol();
|
|
|
|
Operation new_op, cur_op;
|
|
|
|
int op_complete = 6;
|
|
|
|
wxString value ;
|
|
|
|
wxDateTime date;
|
|
|
|
static bool inModification = false ;
|
|
|
|
wxColour color ;
|
|
|
|
unsigned char r, g, b;
|
|
|
|
|
|
|
|
// Avoid recursives calls
|
|
|
|
if (inModification) return;
|
|
|
|
|
|
|
|
inModification = true ;
|
|
|
|
|
|
|
|
if (event.GetCol() == DEBIT)
|
|
|
|
_grid->SetCellValue(event.GetRow(), CREDIT, wxT(""));
|
|
|
|
else if (event.GetCol() == CREDIT)
|
|
|
|
_grid->SetCellValue(event.GetRow(), DEBIT, wxT(""));
|
|
|
|
|
|
|
|
value = _grid->GetCellValue(event.GetRow(), DESCRIPTION);
|
|
|
|
if (value != wxT(""))
|
|
|
|
{
|
|
|
|
new_op.description = value;
|
|
|
|
op_complete--;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = _grid->GetCellValue(event.GetRow(), DATE);
|
|
|
|
if (value != wxT(""))
|
|
|
|
{
|
|
|
|
date.ParseFormat(value, wxT("%d/%m/%Y"));
|
|
|
|
new_op.day = date.GetDay()-1;
|
|
|
|
new_op.month = date.GetMonth();
|
|
|
|
new_op.year = date.GetYear();
|
|
|
|
op_complete--;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = _grid->GetCellValue(event.GetRow(), DEBIT);
|
|
|
|
if (value != wxT(""))
|
|
|
|
{
|
|
|
|
value.ToDouble(&new_op.amount);
|
|
|
|
new_op.amount *= -1.0;
|
|
|
|
op_complete--;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = _grid->GetCellValue(event.GetRow(), CREDIT);
|
|
|
|
if (value != wxT(""))
|
|
|
|
{
|
|
|
|
value.ToDouble(&new_op.amount);
|
|
|
|
op_complete--;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = _grid->GetCellValue(event.GetRow(), CATEGORY);
|
|
|
|
if (value != wxT(""))
|
|
|
|
{
|
|
|
|
new_op.category = user->GetCategoryId(value);
|
|
|
|
op_complete--;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = _grid->GetCellValue(event.GetRow(), ACCOUNT);
|
|
|
|
if (value != wxT(""))
|
|
|
|
{
|
|
|
|
new_op.account = user->GetAccountId(value);
|
|
|
|
op_complete--;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = _grid->GetCellValue(event.GetRow(), CHECKED);
|
|
|
|
if (value != wxT("") && value != wxT("0"))
|
|
|
|
new_op.checked = true;
|
|
|
|
else
|
|
|
|
new_op.checked = false;
|
|
|
|
op_complete--;
|
|
|
|
|
|
|
|
if (col == CHECKED || col == CATEGORY)
|
|
|
|
{
|
|
|
|
color = user->GetCategory(new_op.category).color;
|
|
|
|
|
|
|
|
if (new_op.checked)
|
|
|
|
{
|
|
|
|
r = ((color.Red()*1.5) >= 0xFF) ? 0xFF : color.Red()*1.5 ;
|
|
|
|
g = ((color.Green()*1.5) >= 0xFF) ? 0xFF : color.Green()*1.5 ;
|
|
|
|
b = ((color.Blue()*1.5) >= 0xFF) ? 0xFF : color.Blue()*1.5 ;
|
|
|
|
color.Set(r, g, b, color.Alpha());
|
|
|
|
}
|
|
|
|
|
|
|
|
SET_ROW_COLOR(event.GetRow(), color);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (col == DELETE)
|
|
|
|
{
|
|
|
|
wxMessageDialog dialog(_wxUI, _("Are you sure want to delete : \n")+new_op.description, wxT("KissCount"), wxYES_NO);
|
|
|
|
if (dialog.ShowModal() == wxID_NO)
|
|
|
|
{
|
|
|
|
_grid->SetCellValue(event.GetRow(), event.GetCol(), wxT("0"));
|
|
|
|
_wxUI->NeedReload();
|
|
|
|
inModification = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify an operation
|
|
|
|
cur_op = (*_operations)[row] ;
|
|
|
|
new_op.id = cur_op.id;
|
|
|
|
new_op.fix_cost = false;
|
|
|
|
|
|
|
|
if (col == DELETE)
|
|
|
|
{
|
|
|
|
_grid->DeleteRows(event.GetRow(), 1);
|
|
|
|
_kiss->DeleteOperation(cur_op);
|
|
|
|
_operations->erase(_operations->begin()+row);
|
|
|
|
_wxUI->NeedReload();
|
|
|
|
inModification = false ;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*_operations)[row] = new_op;
|
|
|
|
_kiss->UpdateOperation(new_op);
|
|
|
|
|
|
|
|
_wxUI->NeedReload();
|
|
|
|
|
|
|
|
inModification = false ;
|
|
|
|
}
|