LibOFX

ofx_container_position.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 ofx_container_position.cpp
3 -------------------
4 copyright : (C) 2016 by Jeff Lundblad
5 email : jeffjl.kde@outlook.com
6 ***************************************************************************/
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string>
25 #include "messages.hh"
26 #include "libofx.h"
27 #include "ofx_containers.hh"
28 #include "ofx_utilities.hh"
29
30 extern OfxMainContainer * MainContainer;
31
32 /***************************************************************************
33 * OfxPositionContainer *
34 ***************************************************************************/
35
36 OfxPositionContainer::OfxPositionContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
37 OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
38 {
39 memset(&data, 0, sizeof(data));
40 type = "POSITION";
41 }
42 OfxPositionContainer::~OfxPositionContainer()
43 {
44 }
45 void OfxPositionContainer::add_attribute(const std::string identifier, const std::string value)
46 {
47 if (identifier == "UNIQUEID")
48 {
49 ASSIGN_STRNCPY(data.unique_id, value);
50 }
51 else if (identifier == "UNIQUEIDTYPE")
52 {
53 ASSIGN_STRNCPY(data.unique_id_type, value);
54 }
55 else if (identifier == "HELDINACCT")
56 {
57 if (value == "CASH")
58 {
59 ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_CASH);
60 }
61 else if (value == "MARGIN")
62 {
63 ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_MARGIN);
64 }
65 else if (value == "SHORT")
66 {
67 ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_SHORT);
68 }
69 else if (value == "OTHER")
70 {
71 ASSIGN(data.heldinaccount_type, data.OFX_HELDINACCT_OTHER);
72 }
73 }
74 else if (identifier == "POSTYPE")
75 {
76 if (value == "SHORT")
77 {
78 ASSIGN(data.position_type, data.OFX_POSITION_SHORT);
79 }
80 else if (value == "LONG")
81 {
82 ASSIGN(data.position_type, data.OFX_POSITION_LONG);
83 }
84 }
85 else if (identifier == "UNITS")
86 {
87 ASSIGN(data.units, ofxamount_to_double(value));
88 }
89 else if (identifier == "UNITPRICE")
90 {
91 ASSIGN(data.unit_price, ofxamount_to_double(value));
92 }
93 else if (identifier == "MKTVAL")
94 {
95 ASSIGN(data.market_value, ofxamount_to_double(value));
96 }
97 else if (identifier == "DTPRICEASOF")
98 {
99 ASSIGN(data.date_unit_price, ofxdate_to_time_t(value));
100 }
101 else if (identifier == "CURRATE")
102 {
103 ASSIGN(data.currency_ratio, ofxamount_to_double(value));
104 }
105 else if (identifier == "CURSYM")
106 {
107 ASSIGN_STRNCPY(data.currency, value);
108 }
109 else if (identifier == "CURRENCY")
110 {
111 ASSIGN(data.amounts_are_foreign_currency, false);
112 }
113 else if (identifier == "ORIGCURRENCY")
114 {
115 ASSIGN(data.amounts_are_foreign_currency, true);
116 }
117 else if (identifier == "MEMO")
118 {
119 ASSIGN_STRNCPY(data.memo, value);
120 }
121 else if (identifier == "INV401KSOURCE")
122 {
123 if (value == "PRETAX")
124 {
125 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_PRETAX);
126 }
127 else if (value == "AFTERTAX")
128 {
129 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_AFTERTAX);
130 }
131 else if (value == "MATCH")
132 {
133 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_MATCH);
134 }
135 else if (value == "PROFITSHARING")
136 {
137 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_PROFITSHARING);
138 }
139 else if (value == "ROLLOVER")
140 {
141 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_ROLLOVER);
142 }
143 else if (value == "OTHERVEST")
144 {
145 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_OTHERVEST);
146 }
147 else if (value == "OTHERNONVEST")
148 {
149 ASSIGN(data.inv_401k_source, data.OFX_401K_POSN_SOURCE_OTHERNONVEST);
150 }
151 }
152 else
153 {
154 /* Redirect unknown identifiers to the base class */
155 OfxGenericContainer::add_attribute(identifier, value);
156 }
157 }
158 int OfxPositionContainer::gen_event()
159 {
160 if (data.unique_id_valid == true && MainContainer != NULL)
161 {
162 data.security_data_ptr = MainContainer->find_security(data.unique_id);
163 if (data.security_data_ptr != NULL)
164 {
165 data.security_data_valid = true;
166 }
167 }
168 libofx_context->positionCallback(data);
169 return true;
170 }
171
172 void OfxPositionContainer::add_account(OfxAccountData * account_data)
173 {
174 if (account_data->account_id_valid == true)
175 {
176 data.account_ptr = account_data;
177 ASSIGN_STRNCPY(data.account_id, std::string(account_data->account_id));
178 }
179 }
180
181 int OfxPositionContainer::add_to_main_tree()
182 {
183 if (MainContainer != NULL)
184 {
185 return MainContainer->add_container(this);
186 }
187 else
188 {
189 return false;
190 }
191 }
192
OfxGenericContainer::type
std::string type
Definition: ofx_containers.hh:41
OfxPositionData::OFX_HELDINACCT_OTHER
@ OFX_HELDINACCT_OTHER
Definition: inc/libofx.h:1203
ASSIGN
#define ASSIGN(DEST, VALUE)
Definition: ofx_utilities.hh:45
OfxPositionContainer::add_to_main_tree
virtual int add_to_main_tree()
Add this container to the main tree.
Definition: ofx_container_position.cpp:181
OfxPositionData::units
double units
Definition: inc/libofx.h:1216
OfxPositionData::OFX_HELDINACCT_MARGIN
@ OFX_HELDINACCT_MARGIN
Definition: inc/libofx.h:1201
OfxPositionContainer::gen_event
virtual int gen_event()
Generate libofx.h events.
Definition: ofx_container_position.cpp:158
ASSIGN_STRNCPY
#define ASSIGN_STRNCPY(DEST, VALUE)
Definition: ofx_utilities.hh:52
OfxAccountData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:301
OfxAccountData
An abstraction of an account.
Definition: inc/libofx.h:287
OfxMainContainer
The root container. Created by the <OFX> OFX element or by the export functions.
Definition: ofx_containers.hh:289
OfxPositionData::market_value
double market_value
Definition: inc/libofx.h:1224
OfxPositionData::currency_ratio
double currency_ratio
Definition: inc/libofx.h:1259
OfxPositionData::account_ptr
struct OfxAccountData * account_ptr
Definition: inc/libofx.h:1178
ofx_utilities.hh
Various simple functions for type conversion & al.
OfxPositionData::OFX_HELDINACCT_SHORT
@ OFX_HELDINACCT_SHORT
Definition: inc/libofx.h:1202
OfxGenericContainer
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
Definition: ofx_containers.hh:31
OfxGenericContainer::add_attribute
virtual void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_container_generic.cpp:57
ofxamount_to_double
double ofxamount_to_double(const std::string ofxamount)
Convert OFX amount of money to double float.
Definition: ofx_utilities.cpp:130
LibofxContext
Definition: context.hh:23
ofx_containers.hh
LibOFX internal object code.
OfxPositionData::OFX_HELDINACCT_CASH
@ OFX_HELDINACCT_CASH
Definition: inc/libofx.h:1200
OfxPositionData::currency
char currency[OFX_CURRENCY_LENGTH]
Definition: inc/libofx.h:1255
OfxPositionData::unique_id_type
char unique_id_type[OFX_UNIQUE_ID_TYPE_LENGTH]
Definition: inc/libofx.h:1192
OfxPositionContainer::add_attribute
void add_attribute(const std::string identifier, const std::string value)
Add data to a container object.
Definition: ofx_container_position.cpp:45
ofxdate_to_time_t
time_t ofxdate_to_time_t(const std::string &ofxdate)
Convert a C++ string containing a time in OFX format to a C time_t.
Definition: ofx_utilities.cpp:68
OfxPositionData::amounts_are_foreign_currency
int amounts_are_foreign_currency
Definition: inc/libofx.h:1265
OfxPositionData::account_id
char account_id[OFX_ACCOUNT_ID_LENGTH]
Definition: inc/libofx.h:1175
OfxPositionData::unit_price
double unit_price
Definition: inc/libofx.h:1220
OfxPositionData::security_data_ptr
struct OfxSecurityData * security_data_ptr
Definition: inc/libofx.h:1269
OfxPositionData::unique_id
char unique_id[OFX_UNIQUE_ID_LENGTH]
Definition: inc/libofx.h:1190
OfxPositionData::date_unit_price
time_t date_unit_price
Definition: inc/libofx.h:1228
messages.hh
Message IO functionality.
OfxPositionData::memo
char memo[OFX_MEMO_LENGTH]
Definition: inc/libofx.h:1238