KissCount/src/model/import/OFXImportEngine.cpp

139 lines
3.7 KiB
C++

/*
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 "OFXImportEngine.h"
static OFXImportEngine ofxImportEngine;
int OFXImportEngine::account_cb(const struct OfxAccountData data, void * account_data)
{
int i;
OFXImportEngine* _this = (OFXImportEngine*) account_data;
wxString account_number = wxString(data.account_number, wxConvUTF8);
_this->_curAccount = wxT("");
for (i=0; i<(int)_this->_user->_accounts.size(); i++)
{
if (_this->_user->_accounts[i].number == account_number)
{
_this->_accounts[account_number] = _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;
}
}
if (!_this->_curAccount.Len())
{
_this->_curAccount = _this->_accounts[account_number] = wxT("unknown-") + account_number;
_this->_unresolvedAccounts.push_back(account_number);
}
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;
op.id = wxString::Format(wxT("%d"), ++id);
op.parent = wxT("");
op.category = wxT("0");
op.fix_cost = false;
op.account = _this->_curAccount;
op.checked = false;
op.transfert = wxT("");
op.formula = wxT("");
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)
op.description = wxString(data.name, wxConvUTF8);
if (data.memo_valid)
{
if (op.description.Len())
op.description += wxT(" ");
op.description += wxString(data.memo, wxConvUTF8);
}
_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);
_shortExt = wxT("ofx");
_longExt = _("OFX files (*.ofx)|*.ofx");
}
OFXImportEngine::~OFXImportEngine()
{
if (_ctx)
libofx_free_context(_ctx);
}
bool OFXImportEngine::HandleFile(const wxString& path, User* user, Database* db, KissCount* kiss)
{
if (!ImportEngine::HandleFile(path, user, db, kiss)) return false;
return !libofx_proc_file(_ctx, path.mb_str(), AUTODETECT);
}