ofx_container_transaction.cpp
Go to the documentation of this file.
/***************************************************************************
ofx_container_account.cpp
-------------------
copyright : (C) 2002 by Benoit Gr�goire
email : benoitg@coeus.ca
***************************************************************************/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstdlib>
#include <string>
#include "messages.hh"
#include "libofx.h"
#include "ofx_containers.hh"
#include "ofx_utilities.hh"
extern OfxMainContainer * MainContainer;
/***************************************************************************
* OfxTransactionContainer *
***************************************************************************/
OfxTransactionContainer::OfxTransactionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, string para_tag_identifier):
OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
{
OfxGenericContainer * tmp_parentcontainer = parentcontainer;
memset(&data, 0, sizeof(data));
type = "TRANSACTION";
/* Find the parent statement container*/
while (tmp_parentcontainer != NULL && tmp_parentcontainer->type != "STATEMENT")
{
tmp_parentcontainer = tmp_parentcontainer->parentcontainer;
}
if (tmp_parentcontainer != NULL)
{
parent_statement = (OfxStatementContainer*)tmp_parentcontainer;
}
else
{
parent_statement = NULL;
message_out(ERROR, "Unable to find the enclosing statement container this transaction");
}
if (parent_statement != NULL && parent_statement->data.account_id_valid == true)
{
strncpy(data.account_id, parent_statement->data.account_id, OFX_ACCOUNT_ID_LENGTH);
data.account_id_valid = true;
}
}
OfxTransactionContainer::~OfxTransactionContainer()
{
}
int OfxTransactionContainer::gen_event()
{
if (data.unique_id_valid == true && MainContainer != NULL)
{
data.security_data_ptr = MainContainer->find_security(data.unique_id);
if (data.security_data_ptr != NULL)
{
data.security_data_valid = true;
}
}
libofx_context->transactionCallback(data);
return true;
}
int OfxTransactionContainer::add_to_main_tree()
{
if (MainContainer != NULL)
{
return MainContainer->add_container(this);
}
else
{
return false;
}
}
void OfxTransactionContainer::add_attribute(const string identifier, const string value)
{
if (identifier == "DTPOSTED")
{
data.date_posted = ofxdate_to_time_t(value);
data.date_posted_valid = true;
}
else if (identifier == "DTUSER")
{
data.date_initiated = ofxdate_to_time_t(value);
data.date_initiated_valid = true;
}
else if (identifier == "DTAVAIL")
{
data.date_funds_available = ofxdate_to_time_t(value);
data.date_funds_available_valid = true;
}
else if (identifier == "FITID")
{
strncpy(data.fi_id, value.c_str(), sizeof(data.fi_id));
data.fi_id_valid = true;
}
else if (identifier == "CORRECTFITID")
{
strncpy(data.fi_id_corrected, value.c_str(), sizeof(data.fi_id));
data.fi_id_corrected_valid = true;
}
else if (identifier == "CORRECTACTION")
{
data.fi_id_correction_action_valid = true;
if (value == "REPLACE")
{
data.fi_id_correction_action = REPLACE;
}
else if (value == "DELETE")
{
data.fi_id_correction_action = DELETE;
}
else
{
data.fi_id_correction_action_valid = false;
}
}
else if ((identifier == "SRVRTID") || (identifier == "SRVRTID2"))
{
strncpy(data.server_transaction_id, value.c_str(), sizeof(data.server_transaction_id));
data.server_transaction_id_valid = true;
}
else if (identifier == "MEMO" || identifier == "MEMO2")
{
strncpy(data.memo, value.c_str(), sizeof(data.memo));
data.memo_valid = true;
}
else
{
/* Redirect unknown identifiers to the base class */
OfxGenericContainer::add_attribute(identifier, value);
}
}// end OfxTransactionContainer::add_attribute()
void OfxTransactionContainer::add_account(OfxAccountData * account_data)
{
if (account_data->account_id_valid == true)
{
data.account_ptr = account_data;
strncpy(data.account_id, account_data->account_id, OFX_ACCOUNT_ID_LENGTH);
data.account_id_valid = true;
}
}
/***************************************************************************
* OfxBankTransactionContainer *
***************************************************************************/
OfxBankTransactionContainer::OfxBankTransactionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, string para_tag_identifier):
OfxTransactionContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
{
;
}
void OfxBankTransactionContainer::add_attribute(const string identifier, const string value)
{
if ( identifier == "TRNTYPE")
{
data.transactiontype_valid = true;
if (value == "CREDIT")
{
data.transactiontype = OFX_CREDIT;
}
else if (value == "DEBIT")
{
data.transactiontype = OFX_DEBIT;
}
else if (value == "INT")
{
data.transactiontype = OFX_INT;
}
else if (value == "DIV")
{
data.transactiontype = OFX_DIV;
}
else if (value == "FEE")
{
data.transactiontype = OFX_FEE;
}
else if (value == "SRVCHG")
{
data.transactiontype = OFX_SRVCHG;
}
else if (value == "DEP")
{
data.transactiontype = OFX_DEP;
}
else if (value == "ATM")
{
data.transactiontype = OFX_ATM;
}
else if (value == "POS")
{
data.transactiontype = OFX_POS;
}
else if (value == "XFER")
{
data.transactiontype = OFX_XFER;
}
else if (value == "CHECK")
{
data.transactiontype = OFX_CHECK;
}
else if (value == "PAYMENT")
{
data.transactiontype = OFX_PAYMENT;
}
else if (value == "CASH")
{
data.transactiontype = OFX_CASH;
}
else if (value == "DIRECTDEP")
{
data.transactiontype = OFX_DIRECTDEP;
}
else if (value == "DIRECTDEBIT")
{
data.transactiontype = OFX_DIRECTDEBIT;
}
else if (value == "REPEATPMT")
{
data.transactiontype = OFX_REPEATPMT;
}
else if (value == "OTHER")
{
data.transactiontype = OFX_OTHER;
}
else
{
data.transactiontype_valid = false;
}
}//end TRANSTYPE
else if (identifier == "TRNAMT")
{
data.amount = ofxamount_to_double(value);
data.amount_valid = true;
data.units = -data.amount;
data.units_valid = true;
data.unitprice = 1.00;
data.unitprice_valid = true;
}
else if (identifier == "CHECKNUM")
{
strncpy(data.check_number, value.c_str(), sizeof(data.check_number));
data.check_number_valid = true;
}
else if (identifier == "REFNUM")
{
strncpy(data.reference_number, value.c_str(), sizeof(data.reference_number));
data.reference_number_valid = true;
}
else if (identifier == "SIC")
{
data.standard_industrial_code = atoi(value.c_str());
data.standard_industrial_code_valid = true;
}
else if ((identifier == "PAYEEID") || (identifier == "PAYEEID2"))
{
strncpy(data.payee_id, value.c_str(), sizeof(data.payee_id));
data.payee_id_valid = true;
}
else if (identifier == "NAME")
{
strncpy(data.name, value.c_str(), sizeof(data.name));
data.name_valid = true;
}
else
{
/* Redirect unknown identifiers to base class */
OfxTransactionContainer::add_attribute(identifier, value);
}
}//end OfxBankTransactionContainer::add_attribute
/***************************************************************************
* OfxInvestmentTransactionContainer *
***************************************************************************/
OfxInvestmentTransactionContainer::OfxInvestmentTransactionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, string para_tag_identifier):
OfxTransactionContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
{
type = "INVESTMENT";
data.transactiontype = OFX_OTHER;
data.transactiontype_valid = true;
data.invtransactiontype_valid = true;
if (para_tag_identifier == "BUYDEBT")
{
data.invtransactiontype = OFX_BUYDEBT;
}
else if (para_tag_identifier == "BUYMF")
{
data.invtransactiontype = OFX_BUYMF;
}
else if (para_tag_identifier == "BUYOPT")
{
data.invtransactiontype = OFX_BUYOPT;
}
else if (para_tag_identifier == "BUYOTHER")
{
data.invtransactiontype = OFX_BUYOTHER;
}
else if (para_tag_identifier == "BUYSTOCK")
{
data.invtransactiontype = OFX_BUYSTOCK;
}
else if (para_tag_identifier == "CLOSUREOPT")
{
data.invtransactiontype = OFX_CLOSUREOPT;
}
else if (para_tag_identifier == "INCOME")
{
data.invtransactiontype = OFX_INCOME;
}
else if (para_tag_identifier == "INVEXPENSE")
{
data.invtransactiontype = OFX_INVEXPENSE;
}
else if (para_tag_identifier == "JRNLFUND")
{
data.invtransactiontype = OFX_JRNLFUND;
}
else if (para_tag_identifier == "JRNLSEC")
{
data.invtransactiontype = OFX_JRNLSEC;
}
else if (para_tag_identifier == "MARGININTEREST")
{
data.invtransactiontype = OFX_MARGININTEREST;
}
else if (para_tag_identifier == "REINVEST")
{
data.invtransactiontype = OFX_REINVEST;
}
else if (para_tag_identifier == "RETOFCAP")
{
data.invtransactiontype = OFX_RETOFCAP;
}
else if (para_tag_identifier == "SELLDEBT")
{
data.invtransactiontype = OFX_SELLDEBT;
}
else if (para_tag_identifier == "SELLMF")
{
data.invtransactiontype = OFX_SELLMF;
}
else if (para_tag_identifier == "SELLOPT")
{
data.invtransactiontype = OFX_SELLOPT;
}
else if (para_tag_identifier == "SELLOTHER")
{
data.invtransactiontype = OFX_SELLOTHER;
}
else if (para_tag_identifier == "SELLSTOCK")
{
data.invtransactiontype = OFX_SELLSTOCK;
}
else if (para_tag_identifier == "SPLIT")
{
data.invtransactiontype = OFX_SPLIT;
}
else if (para_tag_identifier == "TRANSFER")
{
data.invtransactiontype = OFX_TRANSFER;
}
else
{
message_out(ERROR, "This should not happen, " + para_tag_identifier + " is an unknown investment transaction type");
data.invtransactiontype_valid = false;
}
}
void OfxInvestmentTransactionContainer::add_attribute(const string identifier, const string value)
{
if (identifier == "UNIQUEID")
{
strncpy(data.unique_id, value.c_str(), sizeof(data.unique_id));
data.unique_id_valid = true;
}
else if (identifier == "UNIQUEIDTYPE")
{
strncpy(data.unique_id_type, value.c_str(), sizeof(data.unique_id_type));
data.unique_id_type_valid = true;
}
else if (identifier == "UNITS")
{
data.units = ofxamount_to_double(value);
data.units_valid = true;
}
else if (identifier == "UNITPRICE")
{
data.unitprice = ofxamount_to_double(value);
data.unitprice_valid = true;
}
else if (identifier == "MKTVAL")
{
message_out(DEBUG, "MKTVAL of " + value + " ignored since MKTVAL should always be UNITS*UNITPRICE");
}
else if (identifier == "TOTAL")
{
data.amount = ofxamount_to_double(value);
data.amount_valid = true;
}
else if (identifier == "DTSETTLE")
{
data.date_posted = ofxdate_to_time_t(value);
data.date_posted_valid = true;
}
else if (identifier == "DTTRADE")
{
data.date_initiated = ofxdate_to_time_t(value);
data.date_initiated_valid = true;
}
else if (identifier == "COMMISSION")
{
data.commission = ofxamount_to_double(value);
data.commission_valid = true;
}
else if (identifier == "FEES")
{
data.fees = ofxamount_to_double(value);
data.fees_valid = true;
}
else if (identifier == "OLDUNITS")
{
data.oldunits = ofxamount_to_double(value);
data.oldunits_valid = true;
}
else if (identifier == "NEWUNITS")
{
data.newunits = ofxamount_to_double(value);
data.newunits_valid = true;
}
else
{
/* Redirect unknown identifiers to the base class */
OfxTransactionContainer::add_attribute(identifier, value);
}
}//end OfxInvestmentTransactionContainer::add_attribute
OFX_INT
Definition: inc/libofx.h:419
OFX_OTHER
Definition: inc/libofx.h:433
OfxTransactionData::date_posted
time_t date_posted
Definition: inc/libofx.h:539
OFX_RETOFCAP
Definition: inc/libofx.h:450
OfxAccountData
An abstraction of an account.
Definition: inc/libofx.h:270
OFX_JRNLSEC
Definition: inc/libofx.h:447
OFX_POS
Definition: inc/libofx.h:425
OFX_REPEATPMT
Definition: inc/libofx.h:432
DELETE
Definition: inc/libofx.h:462
OfxTransactionContainer::add_to_main_tree
virtual int add_to_main_tree()
Add this container to the main tree.
Definition: ofx_container_transaction.cpp:83
OFX_ATM
Definition: inc/libofx.h:424
OfxTransactionData::reference_number
char reference_number[OFX_REFERENCE_NUMBER_LENGTH]
Definition: inc/libofx.h:583
OfxGenericContainer
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
Definition: ofx_containers.hh:33
OFX_MARGININTEREST
Definition: inc/libofx.h:448
OfxTransactionData::units
double units
Definition: inc/libofx.h:505
OfxTransactionData::unique_id
char unique_id[OFX_UNIQUE_ID_LENGTH]
Definition: inc/libofx.h:530
OfxAccountData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:284
OfxTransactionContainer
Represents a generic transaction.
Definition: ofx_containers.hh:201
OfxTransactionData::fi_id_corrected
char fi_id_corrected[256]
Definition: inc/libofx.h:562
OfxTransactionData::check_number
char check_number[OFX_CHECK_NUMBER_LENGTH]
Definition: inc/libofx.h:578
OFX_BUYOTHER
Definition: inc/libofx.h:441
OfxGenericContainer::add_attribute
virtual void add_attribute(const string identifier, const string value)
Add data to a container object.
Definition: ofx_container_generic.cpp:57
OfxTransactionData::transactiontype_valid
int transactiontype_valid
Definition: inc/libofx.h:490
OfxTransactionData::name
char name[OFX_TRANSACTION_NAME_LENGTH]
Definition: inc/libofx.h:593
OfxTransactionData::fi_id
char fi_id[256]
Definition: inc/libofx.h:518
OFX_SELLDEBT
Definition: inc/libofx.h:451
OFX_CHECK
Definition: inc/libofx.h:427
OFX_FEE
Definition: inc/libofx.h:421
OfxTransactionData::commission
double commission
Definition: inc/libofx.h:600
OFX_CASH
Definition: inc/libofx.h:429
LibofxContext
Definition: context.hh:25
OfxTransactionData::memo
char memo[OFX_MEMO2_LENGTH]
Definition: inc/libofx.h:597
OfxTransactionData::fi_id_correction_action
FiIdCorrectionAction fi_id_correction_action
Definition: inc/libofx.h:567
OFX_BUYDEBT
Definition: inc/libofx.h:438
OFX_SPLIT
Definition: inc/libofx.h:456
OFX_CLOSUREOPT
Definition: inc/libofx.h:443
OFX_TRANSFER
Definition: inc/libofx.h:457
REPLACE
Definition: inc/libofx.h:464
OfxTransactionContainer::add_attribute
virtual void add_attribute(const string identifier, const string value)
Add data to a container object.
Definition: ofx_container_transaction.cpp:97
OfxTransactionData::standard_industrial_code
long int standard_industrial_code
Definition: inc/libofx.h:586
OfxStatementData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:649
OFX_BUYMF
Definition: inc/libofx.h:439
OFX_SELLSTOCK
Definition: inc/libofx.h:455
OfxTransactionData::date_initiated
time_t date_initiated
Definition: inc/libofx.h:546
message_out
int message_out(OfxMsgType error_type, const string message)
Message output function.
Definition: messages.cpp:60
OfxTransactionData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:482
OFX_CREDIT
Definition: inc/libofx.h:417
OFX_DEP
Definition: inc/libofx.h:423
OFX_JRNLFUND
Definition: inc/libofx.h:446
OFX_SELLMF
Definition: inc/libofx.h:452
OfxBankTransactionContainer::add_attribute
void add_attribute(const string identifier, const string value)
Add data to a container object.
Definition: ofx_container_transaction.cpp:177
OfxTransactionData::unitprice
double unitprice
Definition: inc/libofx.h:508
ofxdate_to_time_t
time_t ofxdate_to_time_t(const string ofxdate)
Convert a C++ string containing a time in OFX format to a C time_t.
Definition: ofx_utilities.cpp:108
DEBUG
Definition: messages.hh:25
ofxamount_to_double
double ofxamount_to_double(const string ofxamount)
Convert OFX amount of money to double float.
Definition: ofx_utilities.cpp:205
OfxTransactionData::account_ptr
struct OfxAccountData * account_ptr
Definition: inc/libofx.h:485
OfxTransactionData::server_transaction_id
char server_transaction_id[OFX_SVRTID2_LENGTH]
Definition: inc/libofx.h:572
OFX_SRVCHG
Definition: inc/libofx.h:422
OFX_BUYSTOCK
Definition: inc/libofx.h:442
OfxInvestmentTransactionContainer::add_attribute
void add_attribute(const string identifier, const string value)
Add data to a container object.
Definition: ofx_container_transaction.cpp:396
OFX_DIRECTDEBIT
Definition: inc/libofx.h:431
ofx_utilities.hh
Various simple functions for type conversion & al.
OFX_SELLOTHER
Definition: inc/libofx.h:454
ERROR
Definition: messages.hh:34
OFX_PAYMENT
Definition: inc/libofx.h:428
OFX_BUYOPT
Definition: inc/libofx.h:440
OfxTransactionData::amount
double amount
Definition: inc/libofx.h:512
OfxStatementContainer
Represents a statement for either a bank account or a credit card account.
Definition: ofx_containers.hh:134
OFX_INCOME
Definition: inc/libofx.h:444
OFX_SELLOPT
Definition: inc/libofx.h:453
OfxGenericContainer::type
string type
Definition: ofx_containers.hh:36
OFX_REINVEST
Definition: inc/libofx.h:449
OfxTransactionData::security_data_valid
int security_data_valid
Definition: inc/libofx.h:537
OfxTransactionData::payee_id
char payee_id[OFX_SVRTID2_LENGTH]
Definition: inc/libofx.h:590
ofx_containers.hh
LibOFX internal object code.
OfxTransactionData::fees
double fees
Definition: inc/libofx.h:603
OFX_DEBIT
Definition: inc/libofx.h:418
OfxTransactionData::date_funds_available
time_t date_funds_available
Definition: inc/libofx.h:554
messages.hh
Message IO functionality.
OFX_XFER
Definition: inc/libofx.h:426
OFX_DIV
Definition: inc/libofx.h:420
OfxTransactionData::unique_id_type
char unique_id_type[OFX_UNIQUE_ID_TYPE_LENGTH]
Definition: inc/libofx.h:532
OFX_INVEXPENSE
Definition: inc/libofx.h:445
OfxMainContainer
The root container. Created by the <OFX> OFX element or by the export functions.
Definition: ofx_containers.hh:247
OFX_DIRECTDEP
Definition: inc/libofx.h:430
OfxTransactionContainer::gen_event
virtual int gen_event()
Generate libofx.h events.
Definition: ofx_container_transaction.cpp:69