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/>.
|
|
|
|
*/
|
|
|
|
|
2011-08-20 11:43:12 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2011-08-20 14:02:47 +02:00
|
|
|
#include "ImportEngine.hpp"
|
2011-03-20 19:08:24 +01:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
QString ImportEngine::NULL_IMPORT_PATTERN = "(nil)";
|
2011-08-16 18:22:35 +02:00
|
|
|
|
2011-03-20 19:08:24 +01:00
|
|
|
ImportEngine::ImportEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ImportEngine::~ImportEngine()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
bool ImportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
|
|
|
_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
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
return path.endsWith(_shortExt) || path.endsWith(_shortExt.toUpper());
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
QString ImportEngine::GetFileExt()
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
return _(_longExt.toStdString().c_str());
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
std::vector<QString> ExplodeString(QString& s)
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
QString tmp = s, cur = "";
|
|
|
|
std::vector<QString> res;
|
2011-03-20 19:08:24 +01:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
while (tmp.size())
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
if (tmp.startsWith(" " ) ||
|
|
|
|
tmp.startsWith("\t") ||
|
|
|
|
tmp.startsWith("\n") ||
|
|
|
|
tmp.startsWith("\r"))
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
if (cur.size())
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
|
|
|
res.push_back(cur);
|
2011-08-27 18:35:36 +02:00
|
|
|
cur = "";
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
2011-08-27 18:35:36 +02:00
|
|
|
tmp = tmp.right(tmp.size()-1);
|
2011-03-20 19:08:24 +01:00
|
|
|
continue;
|
|
|
|
}
|
2011-08-27 18:35:36 +02:00
|
|
|
cur += tmp.left(1);
|
|
|
|
tmp = tmp.right(tmp.size()-1);
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
if (cur.size())
|
2011-03-20 19:08:24 +01:00
|
|
|
res.push_back(cur);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Remove :
|
|
|
|
- head spaces
|
|
|
|
- tail spaces
|
|
|
|
- every word starting by a number
|
|
|
|
*/
|
2011-08-27 18:35:36 +02:00
|
|
|
QString ImportEngine::RemoveUnused(const QString& s)
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
QString tmp = s, tmp2;
|
|
|
|
QString res;
|
|
|
|
bool number;
|
2011-03-20 19:08:24 +01:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
tmp = tmp.trimmed();
|
2011-03-20 19:08:24 +01:00
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
while (tmp.size())
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
tmp2 = tmp.left(1);
|
|
|
|
tmp2.toInt(&number);
|
|
|
|
if (number)
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
tmp = tmp.right(tmp.size()-1);
|
|
|
|
tmp2 = tmp.left(1);
|
|
|
|
} while (tmp.size() && tmp2 != " " &&
|
|
|
|
tmp2 != "\t" &&
|
|
|
|
tmp2 != "\r" &&
|
|
|
|
tmp2 != "\n");
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
res += tmp2;
|
2011-08-27 18:35:36 +02:00
|
|
|
tmp = tmp.right(tmp.size()-1);
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2011-08-27 18:35:36 +02:00
|
|
|
QString ImportEngine::FindPattern(QString& orig, QString& dest)
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
QString pattern, cur_pat;
|
2011-03-20 19:08:24 +01:00
|
|
|
int i, a;
|
2011-08-27 18:35:36 +02:00
|
|
|
std::vector<QString> tok1;
|
|
|
|
std::vector<QString> tok2;
|
2011-03-23 20:35:29 +01:00
|
|
|
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++)
|
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
cur_pat = "";
|
2011-03-20 19:08:24 +01:00
|
|
|
|
|
|
|
for (a=0; a<size1; a++)
|
|
|
|
{
|
|
|
|
if (tok2[i] == tok1[a])
|
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
cur_pat = "%w";
|
2011-03-20 19:08:24 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-08-27 18:35:36 +02:00
|
|
|
else if (tok2[i] == tok1[a].toLower())
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
cur_pat = "%m";
|
2011-03-20 19:08:24 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-08-27 18:35:36 +02:00
|
|
|
else if (tok2[i] == tok1[a].toUpper())
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
cur_pat = "%m";
|
2011-03-20 19:08:24 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-08-27 18:35:36 +02:00
|
|
|
else if (tok2[i].toLower() == tok1[a].toLower() && tok2[i][0] == tok1[a][0])
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
cur_pat = "%f";
|
2011-03-20 19:08:24 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
if (cur_pat.size())
|
|
|
|
pattern += cur_pat + QString::number(a);
|
2011-03-20 19:08:24 +01:00
|
|
|
else
|
|
|
|
pattern += tok2[i];
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
pattern += " ";
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
pattern = pattern.trimmed();
|
2011-03-20 19:08:24 +01:00
|
|
|
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportEngine::ApplyPattern(ImportPattern& pattern, Operation& op)
|
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
std::vector<QString> tok1;
|
|
|
|
std::vector<QString> tok2;
|
2011-03-23 20:35:29 +01:00
|
|
|
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-08-27 18:35:36 +02:00
|
|
|
op.description = "";
|
2011-03-20 19:08:24 +01:00
|
|
|
|
|
|
|
for(i=0; i<size1; i++)
|
|
|
|
{
|
|
|
|
pos = -1;
|
2011-08-27 18:35:36 +02:00
|
|
|
if (tok1[i].startsWith("%"))
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
pos = tok1[i].right(tok1[i].size()-3).toInt();
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pos != -1)
|
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
if (tok1[i].left(2).right(1) == "w")
|
2011-03-20 19:08:24 +01:00
|
|
|
op.description += tok2[pos];
|
2011-08-27 18:35:36 +02:00
|
|
|
else if (tok1[i].left(2).right(1) == "m")
|
|
|
|
op.description += tok2[pos].toLower();
|
|
|
|
else if (tok1[i].left(2).right(1) == "M")
|
|
|
|
op.description += tok2[pos].toUpper();
|
|
|
|
else if (tok1[i].left(2).right(1) == "f")
|
|
|
|
op.description += tok2[pos].left(1).toUpper() + tok2[pos].right(tok2[pos].size()-1).toLower();
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
op.description += tok1[i];
|
2011-08-27 18:35:36 +02:00
|
|
|
op.description += " ";
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
op.description = op.description.trimmed();
|
2011-03-20 19:08:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ImportEngine::UpdatePattern(int pos)
|
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
QString key1, key2;
|
2011-03-20 19:08:24 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
void ImportEngine::MatchPattern(QString& originalKey, Operation& op)
|
2011-03-20 19:08:24 +01:00
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
QString key1;
|
2011-03-20 19:08:24 +01:00
|
|
|
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-08-27 18:35:36 +02:00
|
|
|
std::vector<Operation>* ImportEngine::GetOperations(std::map<int, int>& accounts, std::map<int, int>& categories)
|
2011-03-23 20:35:29 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0; i<(int)_operations.size(); i++)
|
|
|
|
{
|
2011-08-27 18:35:36 +02:00
|
|
|
if (_operations[i].account < 0)
|
|
|
|
_operations[i].account = accounts[_operations[i].account];
|
|
|
|
if (_operations[i].category < 0)
|
|
|
|
_operations[i].category = categories[_operations[i].category];
|
2011-03-23 20:35:29 +01:00
|
|
|
}
|
|
|
|
|
2011-08-27 18:35:36 +02:00
|
|
|
if (_kiss->GetOperationOrder() == "ASC")
|
2011-03-23 20:35:29 +01:00
|
|
|
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;
|
|
|
|
}
|