First version of SearchPanel (+ code migration for GridAccount)

This commit is contained in:
2010-07-14 16:22:02 +02:00
parent 2c1c8b7b76
commit 14a74b84f8
8 changed files with 596 additions and 0 deletions

View File

@@ -910,3 +910,133 @@ void Database::SetLanguage(User* user, wxLanguage language)
return ;
}
}
std::vector<Operation>* Database::Search(User* user, wxString* description, wxDateTime* dateFrom, wxDateTime* dateTo,
wxString* amountFrom, wxString* amountTo,
std::vector<wxString> categories, std::vector<wxString> accounts)
{
wxSQLite3ResultSet set;
wxString req;
bool firstCond = false;
std::vector<wxString>::iterator it;
std::vector<Account>::iterator accountIt;
std::vector<Operation>* res = new std::vector<Operation>;
req = wxT("SELECT * from operation WHERE ");
if (description)
{
req += wxT("UPPER(description) LIKE '%") + description->MakeUpper() + wxT("%'");
firstCond = true;
}
if (dateFrom)
{
if (firstCond) req += wxT(" AND ") ; else firstCond = true;
req += wxT("(");
req += wxT(" year >= '") + wxString::Format(wxT("%d"), dateFrom->GetYear()) + wxT("'");
req += wxT(" AND month >= '") + wxString::Format(wxT("%d"), dateFrom->GetMonth()) + wxT("'");
// req += wxT(" AND day >= '") + wxString::Format(wxT("%d"), dateFrom->GetDay()) + wxT("'");
req += wxT(")");
}
if (dateTo)
{
if (firstCond) req += wxT(" AND ") ; else firstCond = true;
req += wxT("(");
req += wxT(" year <= '") + wxString::Format(wxT("%d"), dateTo->GetYear()) + wxT("'");
req += wxT(" AND month <= '") + wxString::Format(wxT("%d"), dateTo->GetMonth()) + wxT("'");
// req += wxT(" AND day <= '") + wxString::Format(wxT("%d"), dateTo->GetDay()) + wxT("'");
req += wxT(")");
}
if (amountFrom)
{
if (firstCond) req += wxT(" AND ") ; else firstCond = true;
req += wxT("amount >= '") + *amountFrom + wxT("'");
}
if (amountTo)
{
if (firstCond) req += wxT(" AND ") ; else firstCond = true;
req += wxT("amount <= '") + *amountTo + wxT("'");
}
if (categories.size())
{
if (firstCond) req += wxT(" AND ") ; else firstCond = true;
req += wxT("category IN ('");
it = categories.begin();
req += *it;
it++;
for (; it != categories.end(); it++)
req += wxT("', '") + *it ;
req += wxT("')");
}
if (firstCond) req += wxT(" AND ") ; else firstCond = true;
if (accounts.size())
{
req += wxT("account IN ('");
it = accounts.begin();
req += *it;
it++;
for (; it != categories.end(); it++)
req += wxT("', '") + *it ;
req += wxT("')");
}
else
{
req += wxT("account IN ('");
accountIt = user->_accounts.begin();
req += accountIt->id;
accountIt++;
for (;accountIt != user->_accounts.end(); accountIt++)
{
req += wxT("', '") + accountIt->id ;
}
req += wxT("')");
}
req += wxT(" ORDER BY year,month,day ASC");
// std::cout << req.mb_str() << "\n";
EXECUTE_SQL_QUERY(req, set, res);
while (set.NextRow())
{
Operation op;
op.id = set.GetAsString(wxT("id"));
op.account = set.GetAsString(wxT("account"));
op.day = set.GetInt(wxT("day"));
op.month = set.GetInt(wxT("month"));
op.year = set.GetInt(wxT("year"));
op.amount = set.GetDouble(wxT("amount"));
op.description = set.GetAsString(wxT("description"));
op.category = set.GetAsString(wxT("category"));
op.fix_cost = set.GetBool(wxT("fix_cost"));
op.checked = set.GetBool(wxT("checked"));
if (dateFrom &&
op.month == dateFrom->GetMonth() &&
op.year == dateFrom->GetYear() &&
op.day < dateFrom->GetDay()-1)
continue;
if (dateTo &&
op.month == dateTo->GetMonth() &&
op.year == dateTo->GetYear() &&
op.day > dateTo->GetDay()-1)
continue;
res->push_back(op);
}
return res;
}

View File

@@ -68,6 +68,10 @@ class Database
void SetLanguage(User* user, wxLanguage language);
std::vector<Operation>* Search(User* user, wxString* description, wxDateTime* dateFrom, wxDateTime* dateTo,
wxString* amountFrom, wxString* amountTo,
std::vector<wxString> categories, std::vector<wxString> accounts);
void KillMe(User* user);
private:
wxSQLite3Database _db;