eFinancialPlanner  V1.0 (proof of concept)
Personal Financial Planning based on Maslowian Portfolio Theory
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
asset.class.cpp
Go to the documentation of this file.
1 /*
2  *
3  * class asset
4  *
5  * copyright: (c) Philippe De Brouwer 2014
6  *
7  * last modification:
8  *
9  */
10 
11 class asset
12 {
13 public:
14  int asset_id;
15  int investor;
17  string description;
18  string currency;
19  float amount;
20 
21  bool set_asset_id (const std::string& s);
22  bool set_investor (const std::string& s);
23  bool set_asset_class(const std::string& s);
24  bool set_description(const std::string& s);
25  bool set_currency (const std::string& s);
26  bool set_amount (const std::string& s);
27  void list_assets (int iid);
28  void show_edit_form (int iid);
29  bool load();
30  bool save();
31  bool asset_delete ();
32 
33 private:
34  bool exists (int aid);
35 };
36 
37 
38 /**********************************************
39  * load
40  *
41  * ****************************************** */
43 {
44  bool rc = false;
45  string s = "";
46  s = "SELECT * FROM " + oConfig.tbl_prefix + "_assets WHERE asset_id = " + std::to_string(asset_id) + ";";
47  db.res = db.stmt->executeQuery(s);
48  while (db.res->next())
49  {
50  if (set_investor (db.res->getString("investor")) &&
51  set_asset_class(db.res->getString("asset_class")) &&
52  set_description(db.res->getString("description")) &&
53  set_currency (db.res->getString("currency")) &&
54  set_amount (db.res->getString("amount"))) rc = true;
55  }
56  return rc;
57 }
58 
59 /**********************************************
60  * show_edit
61  *
62  * ****************************************** */
63 void asset::show_edit_form(int iid)
64 {
65 
66  cout << "<form method=\"post\" action=\"" + oConfig.soft_url + "?a=as\">";
67  cout << "<table class=\"asset\">\n";
68  cout << "<tr>";
69  cout << "<th>Description</th><th>Asset Class</th><th>Amount</th><th>Currency</th><th>Actions</th>";
70 /* cout << "</tr>\n";
71  oAsset.list_assets(investor_id);
72  */
73  cout << "</tr>\n<tr>\n";
74  cout << "<td><input type=\"text\" name=\"description\" required value=\"" << description << "\"></td>" ;
75  cout << "<td>" << oAssetClass.dropDown_assetClass(asset_class) << "</td>" ;
76  cout << "<td><input type=\"number\" min=\"0\" name=\"amount\" required title=\"Please enter a positive number\" value=\"" << amount << "\"></td>" ;
77  cout << "<td>" << oCurrency.dropDown_currency(currency) << "</td>" ;
78 
79  switch (oConfig.layout)
80  {
81  case '1' :
82  cout << "<td><button type=\"submit\" value=\"Submit\"><img src=\"" << oConfig.css_dir << "check.svg\" alt=\"add\" width=\"16\" height=\"16\" alt=\"Save\"></button><a href=\"" + oConfig.soft_url + "?a=if&amp;iid=" << iid << "\" class=\"button\"><img src=\"" << oConfig.css_dir << "arrow_left.svg\" alt=\"Go back without saving the changes!\" width=\"16\" height=\"16\"></a></td>" ;
83  break;
84  case '2' :
85  cout << "<td><button type=\"submit\" value=\"Submit\" data-toggle=\"tooltip\" title=\"" << t_save << "\"><span class=\"glyphicon glyphicon-ok\" style=\"color:green\"></span></button>";
86  cout << "<a data-toggle=\"tooltip\" title=\"" << t_cancel << "\" href=\"" + oConfig.soft_url + "?a=if&amp;iid=" << iid << "\"><span class=\"glyphicon glyphicon-chevron-left\" style=\"color:blue\"></span></a></td>" ;
87  break;
88  default :
89  cout << "<td><button type=\"submit\" value=\"Submit\"><img src=\"" << oConfig.css_dir << "check.svg\" alt=\"add\" width=\"16\" height=\"16\" alt=\"Save\"></button><a href=\"" + oConfig.soft_url + "?a=if&amp;iid=" << iid << "\" class=\"button\"><img src=\"" << oConfig.css_dir << "arrow_left.svg\" alt=\"Go back without saving the changes!\" width=\"16\" height=\"16\"></a></td>" ;
90  break;
91  }
92 
93  cout << "</tr></table>\n";
94  cout << "<input type=\"hidden\" name=\"iid\" value=\"" << iid << "\" />\n";
95  cout << "<input type=\"hidden\" name=\"aid\" value=\"" << asset_id << "\" />\n";
96  cout << "<p>All fields are required.</p>";
97  cout << "</form>";
98 }
99 
100 /**********************************************
101  * void list_assets(investor_id);
102  *
103  * ****************************************** */
104 void asset::list_assets(int iid)
105 {
106  string s = "";
107  s = "SELECT A.*, AC.asset_class_name FROM " + oConfig.tbl_prefix + "_assets AS A, " + oConfig.tbl_prefix + "_asset_classes AS AC WHERE A.asset_class = AC.asset_class_id && A.investor = " + std::to_string(iid) + ";";
108  //cout << s;
109  db.res = db.stmt->executeQuery(s);
110 
111  s = "";
112  while (db.res->next())
113  {
114  s = s + "<tr>";
115  s = s + "<td>" + db.res->getString("description") + "</td>";
116  s = s + "<td>" + db.res->getString("asset_class_name") + "</td>";
117  s = s + "<td>" + db.res->getString("amount") + "</td>";
118  s = s + "<td>" + db.res->getString("currency") + "</td>";
119 
120  switch (oConfig.layout)
121  {
122  case '1' :
123  s = s + "<td><a href=\"" + oConfig.soft_url + "?a=ae&amp;iid=" + std::to_string(iid) + "&amp;aid=" + db.res->getString("asset_id") + "\"><img src=\"" + oConfig.css_dir + "pen.svg\" alt=\"edit\" width=\"16\" height=\"16\"></a>&nbsp;";
124  s =s + "<a href=\"" + oConfig.soft_url + "?a=ad&amp;iid=" + std::to_string(iid) + "&amp;aid=" + db.res->getString("asset_id") + "\"><img src=\"" + oConfig.css_dir + "x.svg\" alt=\"delete\" width=\"16\" height=\"16\"></a></td>";
125  break;
126  case '2' :
127  s = s + "<td><a class=\"btn btn-default\" href=\"" + oConfig.soft_url + "?a=ae&amp;iid=" + std::to_string(iid) + "&amp;aid=" + db.res->getString("asset_id") + "\" data-toggle=\"tooltip\" title=\"" + t_edit + "\"><span class=\"glyphicon glyphicon-pencil\" style=\"color:blue\"></span></a>&nbsp;";
128  s = s + "<a href=\"" + oConfig.soft_url + "?a=ad&amp;iid=" + std::to_string(iid) + "&amp;aid=" + db.res->getString("asset_id") + "\" data-toggle=\"tooltip\" title=\"" + t_delete + "\"><span class=\"glyphicon glyphicon-remove\" style=\"color:red\"></span></a></td>";
129  break;
130  default :
131  s = s + "<td><a href=\"" + oConfig.soft_url + "?a=ae&amp;iid=" + std::to_string(iid) + "&amp;aid=" + db.res->getString("asset_id") + "\"><img src=\"" + oConfig.css_dir + "pen.svg\" alt=\"edit\" width=\"16\" height=\"16\"></a>&nbsp;";
132  s =s + "<a href=\"" + oConfig.soft_url + "?a=ad&amp;iid=" + std::to_string(iid) + "&amp;aid=" + db.res->getString("asset_id") + "\"><img src=\"" + oConfig.css_dir + "x.svg\" alt=\"delete\" width=\"16\" height=\"16\"></a></td>";
133  break;
134  }
135  s = s + "</tr>";
136  }
137  cout << s;
138 }
139 
140 /******************************************
141  * set functions
142  * ****************************************/
143 
148 bool asset::set_asset_id(const std::string& s)
149 {
150  int a = atoi(s.c_str());
151  bool rc = true;
152 
153  if ((a < 0) || (a > 1000000)) //TODO : ad real check if investor_id exists
154  // and if not, block IP and report!!!
155  {
156  rc = false;
157  error_message = error_message + "<li>Sorry, an error occured. The asset cannot be found.</li>";
158  }
159  else {asset_id = a;}
160  return rc;
161 }
162 
163 bool asset::set_investor(const std::string& s)
164 {
165  int a = atoi(s.c_str());
166  bool rc = true;
167 
168  if ((a < 0) || (a > 1000000)) //TODO : ad real check if investor_id exists
169  // and if not, block IP and report!!!
170  {
171  rc = false;
172  error_message = error_message + "<li>...please contact me, I know ...</li>";
173  }
174  else {investor = a;}
175  return rc;
176 }
177 
178 bool asset::set_description(const std::string& s)
179 {
180  bool rc = true;
181  if (s.length() < 2)
182  {
183  rc = false;
184  error_message = error_message + "<li>a description of the asset is required</li>";
185  }
186  else {description = s;}
187  return rc;
188 }
189 
190 bool asset::set_asset_class(const std::string& s)
191 {
192  int a = atoi(s.c_str());
193  bool rc = true;
194 
195  if ((a < 0) || (a > 100)) //TODO : ad real check if asset_class exists
196  {
197  rc = false;
198  error_message = error_message + "<li>...please contact me, I know ...</li>";
199  }
200  else {asset_class = a;}
201  return rc;
202 }
203 
204 bool asset::set_amount(const std::string& s)
205 {
206  int a = atof(s.c_str());
207  bool rc = true;
208 
209  if ((a < 0) || (a > 100000000))
210  {
211  rc = false;
212  error_message = error_message + "<li>the amount seems wrong (please correct or email the " + t_author + ")</li>";
213  }
214  else {amount = a;}
215  return rc;
216 }
217 
218 bool asset::set_currency(const std::string& s)
219 {
220  bool rc = true;
221  if (s.length() != 3) //TODO: do a real check if the currency exists
222  {
223  rc = false;
224  error_message = error_message + "<li>Invalid currency ... how did you do that?</li>";
225  }
226  else {currency = s;}
227  return rc;
228 }
229 
231 {
232  string s;
233  if (!exists(asset_id))
234  {
235  s = "INSERT INTO " + oConfig.tbl_prefix + "_assets (investor, asset_class, description, currency, amount) ";
236  s = s + "value (" + std::to_string(investor) + ", " + std::to_string(asset_class) + ", '" + description + "', ";
237  s = s + "'" + currency + "', " + std::to_string(amount) + ");";
238  }
239  else
240  {
241  s = "UPDATE " + oConfig.tbl_prefix + "_assets SET asset_class=" + std::to_string(asset_class);
242  s = s + ", description='" + description + "', currency='" + currency + "', amount=" + std::to_string(amount);
243  s = s + " WHERE asset_id=" + std::to_string(asset_id) + ";";
244  }
245  if (db.runSQL(s))
246  {return true;}
247  else
248  {
249 #ifdef DEBUG
250 cout << s;
251 #endif
252  error_message = error_message + "Sorry, I failed to add/edit the asset :-/";
253  return false;
254  }
255 }
256 
257 bool asset::exists(int aid)
258 {
259  string s;
260  bool rc = false;
261  s = "SELECT COUNT(*) AS CNT FROM " + oConfig.tbl_prefix + "_assets WHERE asset_id = " + std::to_string(aid) + ";";
262  db.res = db.stmt->executeQuery(s);
263  while (db.res->next()) {rc = (db.res->getString("CNT") == "1") ? true : false;}
264  return rc;
265 }
266 
268 {
269  string s;
270  s = "DELETE FROM " + oConfig.tbl_prefix + "_assets WHERE asset_id=" + std::to_string(asset_id) + ";";
271  if (db.runSQL(s))
272  {return true;}
273  else
274  {
275 #ifdef DEBUG
276 cout << s;
277 #endif
278  error_message = error_message + "Sorry, I failed to delete the asset :-/";
279  return false;
280  }
281 }
string soft_url
const string css_dir
string currency
Definition: asset.class.cpp:18
bool set_currency(const std::string &s)
sql::ResultSet * res
bool set_description(const std::string &s)
const string t_save
Definition: texts_Eng_UK.h:20
cls_currency oCurrency
Definition: efp.cpp:187
asset_class oAssetClass
Definition: efp.cpp:190
sql::Statement * stmt
bool set_asset_id(const std::string &s)
void show_edit_form(int iid)
Definition: asset.class.cpp:63
bool set_amount(const std::string &s)
string error_message
global message variables
Definition: efp.cpp:132
const string t_cancel
Definition: texts_Eng_UK.h:23
string dropDown_currency(string defaultCurr="EUR")
const string tbl_prefix
tables start with "" + oConfig.tbl_prefix + "_" if the prefix is eg. "tbl" then we have "tbl_invesotr...
bool load()
Definition: asset.class.cpp:42
float amount
Definition: asset.class.cpp:19
string dropDown_assetClass(int defaultAC=0)
const string t_author
Definition: texts_Eng_UK.h:6
int asset_id
Definition: asset.class.cpp:14
bool exists(int aid)
const string t_edit
Definition: texts_Eng_UK.h:21
bool runSQL(string s)
int investor
Definition: asset.class.cpp:15
db_helper db
Definition: efp.cpp:183
const string t_delete
Definition: texts_Eng_UK.h:22
bool set_asset_class(const std::string &s)
bool asset_delete()
void list_assets(int iid)
bool save()
int asset_class
Definition: asset.class.cpp:16
string description
Definition: asset.class.cpp:17
static const double a[]
Definition: normdist.h:5
config oConfig
mysql connection mysql connector from dev.mysql.com
Definition: efp.cpp:149
char layout
Presentation.
bool set_investor(const std::string &s)