2011-03-20 19:08:24 +01:00
|
|
|
/*
|
|
|
|
Copyright 2010-2011 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 "ImportEngine.h"
|
|
|
|
|
|
|
|
ImportEngine::ImportEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ImportEngine::~ImportEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ImportEngine::HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss)
|
|
|
|
{
|
|
|
|
_path = path;
|
|
|
|
_user = user;
|
|
|
|
_db = db;
|
|
|
|
_kiss = kiss;
|
|
|
|
|
|
|
|
_accounts.clear();
|
|
|
|
_unresolvedAccounts.clear();
|
|
|
|
_operations.clear();
|
|
|
|
_descriptions.clear();
|
2011-07-04 20:23:00 +02:00
|
|
|
_accountAmounts.clear();
|
2011-03-20 19:08:24 +01:00
|
|
|
|
|
|
|
return path.EndsWith(_shortExt) || path.EndsWith(_shortExt.Upper());
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString ImportEngine::GetFileExt()
|
|
|
|
{
|
|
|
|
return wxGetTranslation(_longExt);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<wxString> ExplodeString(wxString& s)
|
|
|
|
{
|
|
|
|
wxString tmp = s, cur = wxT("");
|
|
|
|
std::vector<wxString> res;
|
|
|
|
|
|
|
|
while (tmp.Len())
|
|
|
|
{
|
|
|
|
if (tmp.StartsWith(wxT(" ")) ||
|
|
|
|
tmp.StartsWith(wxT("\t")) ||
|
|
|
|
tmp.StartsWith(wxT("\n")) ||
|
|
|
|
tmp.StartsWith(wxT("\r")))
|
|
|
|
{
|
|
|
|
if (cur.Len())
|
|
|
|
{
|
|
|
|
res.push_back(cur);
|
|
|
|
cur = wxT("");
|
|
|
|
}
|
|
|
|
tmp = tmp.Mid(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cur += tmp.SubString(0, 0);
|
|
|
|
tmp = tmp.Mid(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur.Len())
|
|
|
|
res.push_back(cur);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Remove :
|
|
|
|
- head spaces
|
|
|
|
- tail spaces
|
|
|
|
- every word starting by a number
|
|
|
|
*/
|
2011-03-23 20:35:29 +01:00
|
|
|
wxString ImportEngine::RemoveUnused(const wxString& s)
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
|
|
|
wxString tmp = s, tmp2;
|
|
|
|
wxString res;
|
|
|
|
|
|
|
|
tmp = tmp.Trim(true);
|
|
|
|
tmp = tmp.Trim(false);
|
|
|
|
|
|
|
|
while (tmp.Len())
|
|
|
|
{
|
|
|
|
tmp2 = tmp.SubString(0, 0);
|
|
|
|
if (tmp2.IsNumber())
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
tmp = tmp.Mid(1);
|
|
|
|
tmp2 = tmp.SubString(0, 0);
|
|
|
|
} while (tmp.Len() && tmp2 != wxT(" ") &&
|
|
|
|
tmp2 != wxT("\t") &&
|
|
|
|
tmp2 != wxT("\r") &&
|
|
|
|
tmp2 != wxT("\n"));
|
|
|
|
}
|
|
|
|
res += tmp2;
|
|
|
|
tmp = tmp.Mid(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Find a pattern between the two strings:
|
|
|
|
%mX : lower string of orig[X]
|
|
|
|
%MX : upper string of orig[X]
|
|
|
|
%fX : First case in upper case of orig[X]
|
|
|
|
%wX : word orig[X]
|
|
|
|
other : constants
|
|
|
|
*/
|
|
|
|
wxString ImportEngine::FindPattern(wxString& orig, wxString& dest)
|
|
|
|
{
|
|
|
|
wxString pattern, cur_pat;
|
|
|
|
int i, a;
|
2011-03-23 20:35:29 +01:00
|
|
|
std::vector<wxString> tok1;
|
|
|
|
std::vector<wxString> tok2;
|
|
|
|
int size1, size2;
|
|
|
|
|
|
|
|
if (orig == dest) return NULL_IMPORT_PATTERN;
|
|
|
|
|
|
|
|
tok1 = ExplodeString(orig);
|
|
|
|
tok2 = ExplodeString(dest);
|
|
|
|
size1 = tok1.size();
|
|
|
|
size2 = tok2.size();
|
2011-03-20 19:08:24 +01:00
|
|
|
|
|
|
|
for(i=0; i<size2; i++)
|
|
|
|
{
|
|
|
|
cur_pat = wxT("");
|
|
|
|
|
|
|
|
for (a=0; a<size1; a++)
|
|
|
|
{
|
|
|
|
if (tok2[i] == tok1[a])
|
|
|
|
{
|
|
|
|
cur_pat = wxT("%w");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (tok2[i] == tok1[a].Lower())
|
|
|
|
{
|
|
|
|
cur_pat = wxT("%m");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (tok2[i] == tok1[a].Upper())
|
|
|
|
{
|
|
|
|
cur_pat = wxT("%m");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (tok2[i].Lower() == tok1[a].Lower() && tok2[i][0] == tok1[a][0])
|
|
|
|
{
|
|
|
|
cur_pat = wxT("%f");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur_pat.Len())
|
|
|
|
pattern += cur_pat + wxString::Format(wxT("%d"), a);
|
|
|
|
else
|
|
|
|
pattern += tok2[i];
|
|
|
|
|
|
|
|
pattern += wxT(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern = pattern.Trim();
|
|
|
|
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportEngine::ApplyPattern(ImportPattern& pattern, Operation& op)
|
|
|
|
{
|
2011-03-23 20:35:29 +01:00
|
|
|
std::vector<wxString> tok1;
|
|
|
|
std::vector<wxString> tok2;
|
|
|
|
int size1, i;
|
2011-03-20 19:08:24 +01:00
|
|
|
long pos;
|
|
|
|
|
2011-03-23 20:35:29 +01:00
|
|
|
op.account = pattern.account;
|
|
|
|
op.category = pattern.category;
|
|
|
|
|
|
|
|
if (pattern.pattern == NULL_IMPORT_PATTERN) return;
|
|
|
|
|
|
|
|
tok1 = ExplodeString(pattern.pattern);
|
|
|
|
tok2 = ExplodeString(op.description);
|
|
|
|
size1 = tok1.size();
|
|
|
|
|
2011-03-20 19:08:24 +01:00
|
|
|
op.description = wxT("");
|
|
|
|
|
|
|
|
for(i=0; i<size1; i++)
|
|
|
|
{
|
|
|
|
pos = -1;
|
|
|
|
if (tok1[i].StartsWith(wxT("%")))
|
|
|
|
{
|
|
|
|
tok1[i].Mid(3).ToLong(&pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos != -1)
|
|
|
|
{
|
|
|
|
if (tok1[i].SubString(1, 1) == wxT("w"))
|
|
|
|
op.description += tok2[pos];
|
|
|
|
else if (tok1[i].SubString(1, 1) == wxT("m"))
|
|
|
|
op.description += tok2[pos].Lower();
|
|
|
|
else if (tok1[i].SubString(1, 1) == wxT("M"))
|
|
|
|
op.description += tok2[pos].Upper();
|
|
|
|
else if (tok1[i].SubString(1, 1) == wxT("f"))
|
|
|
|
op.description += tok2[pos].SubString(0, 0).Upper() + tok2[pos].Mid(1).Lower();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
op.description += tok1[i];
|
|
|
|
op.description += wxT(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
op.description.Trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ImportEngine::UpdatePattern(int pos)
|
|
|
|
{
|
|
|
|
wxString key1, key2;
|
|
|
|
ImportPattern pattern;
|
|
|
|
Operation op;
|
|
|
|
int i, nbOpUpdated = 0;
|
|
|
|
|
|
|
|
if (!_user) return 0;
|
|
|
|
|
|
|
|
nbOpUpdated = 1;
|
|
|
|
|
|
|
|
op = _operations[pos];
|
|
|
|
|
|
|
|
key1 = RemoveUnused(_descriptions[op.id]);
|
|
|
|
|
2011-03-23 20:35:29 +01:00
|
|
|
pattern.pattern = FindPattern(_descriptions[op.id], op.description);
|
2011-03-20 19:08:24 +01:00
|
|
|
pattern.account = op.account;
|
|
|
|
pattern.category = op.category;
|
|
|
|
|
|
|
|
_user->_importPatterns[key1] = pattern;
|
|
|
|
|
|
|
|
for(i=pos+1; i<(int)_operations.size(); i++)
|
|
|
|
{
|
|
|
|
key2 = RemoveUnused(_descriptions[_operations[i].id]);
|
|
|
|
|
|
|
|
if (key1 == key2)
|
|
|
|
{
|
|
|
|
ApplyPattern(_user->_importPatterns[key2], _operations[i]);
|
|
|
|
nbOpUpdated++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbOpUpdated;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportEngine::MatchPattern(wxString& originalKey, Operation& op)
|
|
|
|
{
|
|
|
|
wxString key1;
|
|
|
|
ImportPattern pattern;
|
|
|
|
|
|
|
|
if (!_user) return;
|
|
|
|
|
|
|
|
key1 = RemoveUnused(originalKey);
|
|
|
|
|
|
|
|
if (!_user->_importPatterns.count(key1))
|
|
|
|
{
|
2011-03-23 20:35:29 +01:00
|
|
|
pattern.pattern = FindPattern(originalKey, op.description);
|
2011-03-20 19:08:24 +01:00
|
|
|
pattern.account = op.account;
|
|
|
|
pattern.category = op.category;
|
|
|
|
|
|
|
|
_user->_importPatterns[key1] = pattern;
|
|
|
|
|
2011-03-23 20:35:29 +01:00
|
|
|
// std::cout << "New pattern " << key1.mb_str() << "\t" << pattern.pattern.mb_str() << std::endl;
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
op.description = _descriptions[op.id];
|
|
|
|
ApplyPattern(_user->_importPatterns[key1], op);
|
|
|
|
}
|
|
|
|
}
|
2011-03-23 20:35:29 +01:00
|
|
|
|
2011-04-25 19:55:31 +02:00
|
|
|
void ImportEngine::ParseFile(std::vector<Account>& accounts, std::vector<Category>& categories)
|
2011-03-23 20:35:29 +01:00
|
|
|
{
|
2011-04-25 19:55:31 +02:00
|
|
|
accounts = _unresolvedAccounts;
|
|
|
|
categories = _unresolvedCategories;
|
2011-03-23 20:35:29 +01:00
|
|
|
}
|
|
|
|
|
2011-04-25 19:55:31 +02:00
|
|
|
std::vector<Operation>* ImportEngine::GetOperations(std::map<wxString, wxString>& accounts, std::map<wxString, wxString>& categories)
|
2011-03-23 20:35:29 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i<(int)_operations.size(); i++)
|
|
|
|
{
|
|
|
|
if (_operations[i].account.StartsWith(wxT("unknown-")))
|
|
|
|
_operations[i].account = accounts[_operations[i].account.Mid(8)];
|
2011-04-25 19:55:31 +02:00
|
|
|
if (_operations[i].category.StartsWith(wxT("unknown-")))
|
|
|
|
_operations[i].category = categories[_operations[i].category.Mid(8)];
|
2011-03-23 20:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_kiss->GetOperationOrder() == wxT("ASC"))
|
|
|
|
std::sort(_operations.begin(), _operations.end(), sortOperations);
|
|
|
|
else
|
|
|
|
std::sort(_operations.begin(), _operations.end(), reverseSortOperations);
|
|
|
|
|
|
|
|
return &_operations;
|
|
|
|
}
|
2011-07-04 20:23:00 +02:00
|
|
|
|
|
|
|
const std::map<AccountAmount, double, AccountAmount>& ImportEngine::GetAccountAmounts()
|
|
|
|
{
|
|
|
|
return _accountAmounts;
|
|
|
|
}
|