KissCount/src/model/import/OFXImportEngine.cpp

154 lines
3.9 KiB
C++
Raw Normal View History

/*
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-27 18:35:36 +02:00
#include "../Database.hpp"
2011-08-20 14:02:47 +02:00
#include "OFXImportEngine.hpp"
static OFXImportEngine ofxImportEngine;
int OFXImportEngine::account_cb(const struct OfxAccountData data, void * account_data)
{
2011-08-27 18:35:36 +02:00
int i, id;
OFXImportEngine* _this = (OFXImportEngine*) account_data;
2011-08-27 18:35:36 +02:00
QString account_number = QString(data.account_number);
Account ac;
2011-08-27 18:35:36 +02:00
static int unknownAccount = 0;
2011-08-27 18:35:36 +02:00
_this->_curAccount = 0;
UNESCAPE_CHARS(account_number);
2011-08-27 18:35:36 +02:00
id = Database::HashPassword(data.account_id).toInt(0, 16);
for (i=0; i<(int)_this->_user->_accounts.size(); i++)
{
if (_this->_user->_accounts[i].number == account_number)
{
2011-08-27 18:35:36 +02:00
_this->_accounts[id] = _this->_user->_accounts[i].id;
_this->_curAccount = _this->_user->_accounts[i].id;
// std::cout << "Account " << data.account_number << " is " << i << std::endl;
//_this->_unresolvedAccounts.push_back(account_number);
break;
}
}
2011-08-27 18:35:36 +02:00
if (!_this->_curAccount)
{
2011-08-27 18:35:36 +02:00
_this->_curAccount = _this->_accounts[id] = --unknownAccount;
ac.number = account_number;
ac.shared = false;
ac.blocked = false;
ac._default = false;
ac.is_owner = true;
ac._virtual = false;
_this->_unresolvedAccounts.push_back(ac);
}
return 0;
}
int OFXImportEngine::transaction_cb(const struct OfxTransactionData data, void * transaction_data)
{
static int id=0;
OFXImportEngine* _this = (OFXImportEngine*) transaction_data;
Operation op;
struct tm t;
if (!data.amount_valid ||
(!data.date_posted_valid && !data.date_initiated_valid)) return 1;
2011-08-27 18:35:36 +02:00
op.id = ++id;
op.parent = 0;
op.category = 0;
op.fix_cost = false;
op.account = _this->_curAccount;
op.checked = false;
2011-08-27 18:35:36 +02:00
op.transfert = 0;
op.formula = "";
op.meta = false;
op._virtual = false;
op.amount = data.amount;
if (data.date_initiated_valid)
gmtime_r(&data.date_initiated, &t);
else
gmtime_r(&data.date_posted, &t);
op.day = t.tm_mday;
op.month = t.tm_mon;
op.year = t.tm_year+1900;
if (data.name_valid)
2011-08-27 18:35:36 +02:00
op.description = data.name;
if (data.memo_valid)
{
2011-08-27 18:35:36 +02:00
if (op.description.size())
op.description += " ";
op.description += data.memo;
}
UNESCAPE_CHARS(op.description);
_this->_operations.push_back(op);
_this->_descriptions[op.id] = op.description;
_this->MatchPattern(op.description, op);
return 0;
}
int OFXImportEngine::account_balance_cb(const struct OfxStatementData data, void * statement_data)
{
// OFXImportEngine* _this = (OFXImportEngine*) statement_data;
return 0;
}
OFXImportEngine::OFXImportEngine()
{
KissCount::RegisterImportEngine(this);
_ctx = libofx_get_new_context();
if (!_ctx)
throw std::string("Unable to initialize libofx");
ofx_set_account_cb(_ctx, account_cb, this);
ofx_set_transaction_cb(_ctx, transaction_cb, this);
ofx_set_statement_cb(_ctx, account_balance_cb, this);
2011-08-27 18:35:36 +02:00
_shortExt = "ofx";
_longExt = _("OFX files (*.ofx)|*.ofx");
}
OFXImportEngine::~OFXImportEngine()
{
if (_ctx)
libofx_free_context(_ctx);
}
2011-08-27 18:35:36 +02:00
bool OFXImportEngine::HandleFile(const QString& path, User* user, Database* db, KissCount* kiss)
{
if (!ImportEngine::HandleFile(path, user, db, kiss)) return false;
2011-08-27 18:35:36 +02:00
return !libofx_proc_file(_ctx, path.toStdString().c_str(), AUTODETECT);
}