LibOFX
|
00001 /*************************************************************************** 00002 ofx_container_account.cpp 00003 ------------------- 00004 copyright : (C) 2002 by Benoit Gr�goire 00005 email : benoitg@coeus.ca 00006 ***************************************************************************/ 00011 /*************************************************************************** 00012 * * 00013 * This program is free software; you can redistribute it and/or modify * 00014 * it under the terms of the GNU General Public License as published by * 00015 * the Free Software Foundation; either version 2 of the License, or * 00016 * (at your option) any later version. * 00017 * * 00018 ***************************************************************************/ 00019 00020 #ifdef HAVE_CONFIG_H 00021 #include <config.h> 00022 #endif 00023 00024 #include <cstdlib> 00025 #include <string> 00026 #include "messages.hh" 00027 #include "libofx.h" 00028 #include "ofx_containers.hh" 00029 #include "ofx_utilities.hh" 00030 00031 extern OfxMainContainer * MainContainer; 00032 00033 /*************************************************************************** 00034 * OfxTransactionContainer * 00035 ***************************************************************************/ 00036 00037 OfxTransactionContainer::OfxTransactionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, string para_tag_identifier): 00038 OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier) 00039 { 00040 OfxGenericContainer * tmp_parentcontainer = parentcontainer; 00041 00042 memset(&data, 0, sizeof(data)); 00043 type = "TRANSACTION"; 00044 /* Find the parent statement container*/ 00045 while (tmp_parentcontainer != NULL && tmp_parentcontainer->type != "STATEMENT") 00046 { 00047 tmp_parentcontainer = tmp_parentcontainer->parentcontainer; 00048 } 00049 if (tmp_parentcontainer != NULL) 00050 { 00051 parent_statement = (OfxStatementContainer*)tmp_parentcontainer; 00052 } 00053 else 00054 { 00055 parent_statement = NULL; 00056 message_out(ERROR, "Unable to find the enclosing statement container this transaction"); 00057 } 00058 if (parent_statement != NULL && parent_statement->data.account_id_valid == true) 00059 { 00060 strncpy(data.account_id, parent_statement->data.account_id, OFX_ACCOUNT_ID_LENGTH); 00061 data.account_id_valid = true; 00062 } 00063 } 00064 OfxTransactionContainer::~OfxTransactionContainer() 00065 { 00066 00067 } 00068 00069 int OfxTransactionContainer::gen_event() 00070 { 00071 if (data.unique_id_valid == true && MainContainer != NULL) 00072 { 00073 data.security_data_ptr = MainContainer->find_security(data.unique_id); 00074 if (data.security_data_ptr != NULL) 00075 { 00076 data.security_data_valid = true; 00077 } 00078 } 00079 libofx_context->transactionCallback(data); 00080 return true; 00081 } 00082 00083 int OfxTransactionContainer::add_to_main_tree() 00084 { 00085 00086 if (MainContainer != NULL) 00087 { 00088 return MainContainer->add_container(this); 00089 } 00090 else 00091 { 00092 return false; 00093 } 00094 } 00095 00096 00097 void OfxTransactionContainer::add_attribute(const string identifier, const string value) 00098 { 00099 00100 if (identifier == "DTPOSTED") 00101 { 00102 data.date_posted = ofxdate_to_time_t(value); 00103 data.date_posted_valid = true; 00104 } 00105 else if (identifier == "DTUSER") 00106 { 00107 data.date_initiated = ofxdate_to_time_t(value); 00108 data.date_initiated_valid = true; 00109 } 00110 else if (identifier == "DTAVAIL") 00111 { 00112 data.date_funds_available = ofxdate_to_time_t(value); 00113 data.date_funds_available_valid = true; 00114 } 00115 else if (identifier == "FITID") 00116 { 00117 strncpy(data.fi_id, value.c_str(), sizeof(data.fi_id)); 00118 data.fi_id_valid = true; 00119 } 00120 else if (identifier == "CORRECTFITID") 00121 { 00122 strncpy(data.fi_id_corrected, value.c_str(), sizeof(data.fi_id)); 00123 data.fi_id_corrected_valid = true; 00124 } 00125 else if (identifier == "CORRECTACTION") 00126 { 00127 data.fi_id_correction_action_valid = true; 00128 if (value == "REPLACE") 00129 { 00130 data.fi_id_correction_action = REPLACE; 00131 } 00132 else if (value == "DELETE") 00133 { 00134 data.fi_id_correction_action = DELETE; 00135 } 00136 else 00137 { 00138 data.fi_id_correction_action_valid = false; 00139 } 00140 } 00141 else if ((identifier == "SRVRTID") || (identifier == "SRVRTID2")) 00142 { 00143 strncpy(data.server_transaction_id, value.c_str(), sizeof(data.server_transaction_id)); 00144 data.server_transaction_id_valid = true; 00145 } 00146 else if (identifier == "MEMO" || identifier == "MEMO2") 00147 { 00148 strncpy(data.memo, value.c_str(), sizeof(data.memo)); 00149 data.memo_valid = true; 00150 } 00151 else 00152 { 00153 /* Redirect unknown identifiers to the base class */ 00154 OfxGenericContainer::add_attribute(identifier, value); 00155 } 00156 }// end OfxTransactionContainer::add_attribute() 00157 00158 void OfxTransactionContainer::add_account(OfxAccountData * account_data) 00159 { 00160 if (account_data->account_id_valid == true) 00161 { 00162 data.account_ptr = account_data; 00163 strncpy(data.account_id, account_data->account_id, OFX_ACCOUNT_ID_LENGTH); 00164 data.account_id_valid = true; 00165 } 00166 } 00167 00168 /*************************************************************************** 00169 * OfxBankTransactionContainer * 00170 ***************************************************************************/ 00171 00172 OfxBankTransactionContainer::OfxBankTransactionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, string para_tag_identifier): 00173 OfxTransactionContainer(p_libofx_context, para_parentcontainer, para_tag_identifier) 00174 { 00175 ; 00176 } 00177 void OfxBankTransactionContainer::add_attribute(const string identifier, const string value) 00178 { 00179 if ( identifier == "TRNTYPE") 00180 { 00181 data.transactiontype_valid = true; 00182 if (value == "CREDIT") 00183 { 00184 data.transactiontype = OFX_CREDIT; 00185 } 00186 else if (value == "DEBIT") 00187 { 00188 data.transactiontype = OFX_DEBIT; 00189 } 00190 else if (value == "INT") 00191 { 00192 data.transactiontype = OFX_INT; 00193 } 00194 else if (value == "DIV") 00195 { 00196 data.transactiontype = OFX_DIV; 00197 } 00198 else if (value == "FEE") 00199 { 00200 data.transactiontype = OFX_FEE; 00201 } 00202 else if (value == "SRVCHG") 00203 { 00204 data.transactiontype = OFX_SRVCHG; 00205 } 00206 else if (value == "DEP") 00207 { 00208 data.transactiontype = OFX_DEP; 00209 } 00210 else if (value == "ATM") 00211 { 00212 data.transactiontype = OFX_ATM; 00213 } 00214 else if (value == "POS") 00215 { 00216 data.transactiontype = OFX_POS; 00217 } 00218 else if (value == "XFER") 00219 { 00220 data.transactiontype = OFX_XFER; 00221 } 00222 else if (value == "CHECK") 00223 { 00224 data.transactiontype = OFX_CHECK; 00225 } 00226 else if (value == "PAYMENT") 00227 { 00228 data.transactiontype = OFX_PAYMENT; 00229 } 00230 else if (value == "CASH") 00231 { 00232 data.transactiontype = OFX_CASH; 00233 } 00234 else if (value == "DIRECTDEP") 00235 { 00236 data.transactiontype = OFX_DIRECTDEP; 00237 } 00238 else if (value == "DIRECTDEBIT") 00239 { 00240 data.transactiontype = OFX_DIRECTDEBIT; 00241 } 00242 else if (value == "REPEATPMT") 00243 { 00244 data.transactiontype = OFX_REPEATPMT; 00245 } 00246 else if (value == "OTHER") 00247 { 00248 data.transactiontype = OFX_OTHER; 00249 } 00250 else 00251 { 00252 data.transactiontype_valid = false; 00253 } 00254 }//end TRANSTYPE 00255 else if (identifier == "TRNAMT") 00256 { 00257 data.amount = ofxamount_to_double(value); 00258 data.amount_valid = true; 00259 data.units = -data.amount; 00260 data.units_valid = true; 00261 data.unitprice = 1.00; 00262 data.unitprice_valid = true; 00263 } 00264 else if (identifier == "CHECKNUM") 00265 { 00266 strncpy(data.check_number, value.c_str(), sizeof(data.check_number)); 00267 data.check_number_valid = true; 00268 } 00269 else if (identifier == "REFNUM") 00270 { 00271 strncpy(data.reference_number, value.c_str(), sizeof(data.reference_number)); 00272 data.reference_number_valid = true; 00273 } 00274 else if (identifier == "SIC") 00275 { 00276 data.standard_industrial_code = atoi(value.c_str()); 00277 data.standard_industrial_code_valid = true; 00278 } 00279 else if ((identifier == "PAYEEID") || (identifier == "PAYEEID2")) 00280 { 00281 strncpy(data.payee_id, value.c_str(), sizeof(data.payee_id)); 00282 data.payee_id_valid = true; 00283 } 00284 else if (identifier == "NAME") 00285 { 00286 strncpy(data.name, value.c_str(), sizeof(data.name)); 00287 data.name_valid = true; 00288 } 00289 else 00290 { 00291 /* Redirect unknown identifiers to base class */ 00292 OfxTransactionContainer::add_attribute(identifier, value); 00293 } 00294 }//end OfxBankTransactionContainer::add_attribute 00295 00296 00297 /*************************************************************************** 00298 * OfxInvestmentTransactionContainer * 00299 ***************************************************************************/ 00300 00301 OfxInvestmentTransactionContainer::OfxInvestmentTransactionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, string para_tag_identifier): 00302 OfxTransactionContainer(p_libofx_context, para_parentcontainer, para_tag_identifier) 00303 { 00304 type = "INVESTMENT"; 00305 data.transactiontype = OFX_OTHER; 00306 data.transactiontype_valid = true; 00307 00308 data.invtransactiontype_valid = true; 00309 if (para_tag_identifier == "BUYDEBT") 00310 { 00311 data.invtransactiontype = OFX_BUYDEBT; 00312 } 00313 else if (para_tag_identifier == "BUYMF") 00314 { 00315 data.invtransactiontype = OFX_BUYMF; 00316 } 00317 else if (para_tag_identifier == "BUYOPT") 00318 { 00319 data.invtransactiontype = OFX_BUYOPT; 00320 } 00321 else if (para_tag_identifier == "BUYOTHER") 00322 { 00323 data.invtransactiontype = OFX_BUYOTHER; 00324 } 00325 else if (para_tag_identifier == "BUYSTOCK") 00326 { 00327 data.invtransactiontype = OFX_BUYSTOCK; 00328 } 00329 else if (para_tag_identifier == "CLOSUREOPT") 00330 { 00331 data.invtransactiontype = OFX_CLOSUREOPT; 00332 } 00333 else if (para_tag_identifier == "INCOME") 00334 { 00335 data.invtransactiontype = OFX_INCOME; 00336 } 00337 else if (para_tag_identifier == "INVEXPENSE") 00338 { 00339 data.invtransactiontype = OFX_INVEXPENSE; 00340 } 00341 else if (para_tag_identifier == "JRNLFUND") 00342 { 00343 data.invtransactiontype = OFX_JRNLFUND; 00344 } 00345 else if (para_tag_identifier == "JRNLSEC") 00346 { 00347 data.invtransactiontype = OFX_JRNLSEC; 00348 } 00349 else if (para_tag_identifier == "MARGININTEREST") 00350 { 00351 data.invtransactiontype = OFX_MARGININTEREST; 00352 } 00353 else if (para_tag_identifier == "REINVEST") 00354 { 00355 data.invtransactiontype = OFX_REINVEST; 00356 } 00357 else if (para_tag_identifier == "RETOFCAP") 00358 { 00359 data.invtransactiontype = OFX_RETOFCAP; 00360 } 00361 else if (para_tag_identifier == "SELLDEBT") 00362 { 00363 data.invtransactiontype = OFX_SELLDEBT; 00364 } 00365 else if (para_tag_identifier == "SELLMF") 00366 { 00367 data.invtransactiontype = OFX_SELLMF; 00368 } 00369 else if (para_tag_identifier == "SELLOPT") 00370 { 00371 data.invtransactiontype = OFX_SELLOPT; 00372 } 00373 else if (para_tag_identifier == "SELLOTHER") 00374 { 00375 data.invtransactiontype = OFX_SELLOTHER; 00376 } 00377 else if (para_tag_identifier == "SELLSTOCK") 00378 { 00379 data.invtransactiontype = OFX_SELLSTOCK; 00380 } 00381 else if (para_tag_identifier == "SPLIT") 00382 { 00383 data.invtransactiontype = OFX_SPLIT; 00384 } 00385 else if (para_tag_identifier == "TRANSFER") 00386 { 00387 data.invtransactiontype = OFX_TRANSFER; 00388 } 00389 else 00390 { 00391 message_out(ERROR, "This should not happen, " + para_tag_identifier + " is an unknown investment transaction type"); 00392 data.invtransactiontype_valid = false; 00393 } 00394 } 00395 00396 void OfxInvestmentTransactionContainer::add_attribute(const string identifier, const string value) 00397 { 00398 if (identifier == "UNIQUEID") 00399 { 00400 strncpy(data.unique_id, value.c_str(), sizeof(data.unique_id)); 00401 data.unique_id_valid = true; 00402 } 00403 else if (identifier == "UNIQUEIDTYPE") 00404 { 00405 strncpy(data.unique_id_type, value.c_str(), sizeof(data.unique_id_type)); 00406 data.unique_id_type_valid = true; 00407 } 00408 else if (identifier == "UNITS") 00409 { 00410 data.units = ofxamount_to_double(value); 00411 data.units_valid = true; 00412 } 00413 else if (identifier == "UNITPRICE") 00414 { 00415 data.unitprice = ofxamount_to_double(value); 00416 data.unitprice_valid = true; 00417 } 00418 else if (identifier == "MKTVAL") 00419 { 00420 message_out(DEBUG, "MKTVAL of " + value + " ignored since MKTVAL should always be UNITS*UNITPRICE"); 00421 } 00422 else if (identifier == "TOTAL") 00423 { 00424 data.amount = ofxamount_to_double(value); 00425 data.amount_valid = true; 00426 } 00427 else if (identifier == "DTSETTLE") 00428 { 00429 data.date_posted = ofxdate_to_time_t(value); 00430 data.date_posted_valid = true; 00431 } 00432 else if (identifier == "DTTRADE") 00433 { 00434 data.date_initiated = ofxdate_to_time_t(value); 00435 data.date_initiated_valid = true; 00436 } 00437 else if (identifier == "COMMISSION") 00438 { 00439 data.commission = ofxamount_to_double(value); 00440 data.commission_valid = true; 00441 } 00442 else if (identifier == "FEES") 00443 { 00444 data.fees = ofxamount_to_double(value); 00445 data.fees_valid = true; 00446 } 00447 else if (identifier == "OLDUNITS") 00448 { 00449 data.oldunits = ofxamount_to_double(value); 00450 data.oldunits_valid = true; 00451 } 00452 else if (identifier == "NEWUNITS") 00453 { 00454 data.newunits = ofxamount_to_double(value); 00455 data.newunits_valid = true; 00456 } 00457 else 00458 { 00459 /* Redirect unknown identifiers to the base class */ 00460 OfxTransactionContainer::add_attribute(identifier, value); 00461 } 00462 }//end OfxInvestmentTransactionContainer::add_attribute 00463