summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneysecurity.cpp
blob: 20c5fd420bc8b627746adb1d26554ec7e75afa37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/***************************************************************************
                          mymoneysecurity.cpp  -  description
                             -------------------
    begin                : Tue Jan 29 2002
    copyright            : (C) 2000-2002 by Michael Edwardes
    email                : mte@users.sourceforge.net
                           Javier Campos Morales <javi_c@users.sourceforge.net>
                           Felix Rodriguez <frodriguez@users.sourceforge.net>
                           John C <thetacoturtle@users.sourceforge.net>
                           Thomas Baumgart <ipwizard@users.sourceforge.net>
                           Kevin Tambascio <ktambascio@users.sourceforge.net>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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

// ----------------------------------------------------------------------------
// QT Includes

// ----------------------------------------------------------------------------
// KDE Includes

#include <tdelocale.h>

// ----------------------------------------------------------------------------
// Project Includes

#include "mymoneysecurity.h"
#include "mymoneyexception.h"

MyMoneySecurity::MyMoneySecurity() :
  m_securityType(SECURITY_NONE),
  m_smallestAccountFraction(100),
  m_smallestCashFraction(100),
  m_partsPerUnit(100)
{
}

MyMoneySecurity::MyMoneySecurity(const TQString& id, const TQString& name, const TQString& symbol, const int partsPerUnit, const int smallestCashFraction, const int smallestAccountFraction) :
  MyMoneyObject(id),
  m_name(name),
  m_securityType(SECURITY_CURRENCY)
{
  if(symbol.isEmpty())
    m_tradingSymbol = id;
  else
    m_tradingSymbol = symbol;

  m_partsPerUnit = partsPerUnit;
  m_smallestCashFraction = smallestCashFraction;
  if(smallestAccountFraction)
    m_smallestAccountFraction = smallestAccountFraction;
  else
    m_smallestAccountFraction = smallestCashFraction;
}

MyMoneySecurity::MyMoneySecurity(const TQString& id, const MyMoneySecurity& equity) :
  MyMoneyObject(id)
{
  *this = equity;
  m_id = id;
}

MyMoneySecurity::MyMoneySecurity(const TQDomElement& node) :
  MyMoneyObject(node),
  MyMoneyKeyValueContainer(node.elementsByTagName("KEYVALUEPAIRS").item(0).toElement())
{
  if(("SECURITY" != node.tagName())
      && ("EQUITY" != node.tagName())
      && ("CURRENCY" != node.tagName()))
    throw new MYMONEYEXCEPTION("Node was not SECURITY or CURRENCY");

  setName(TQStringEmpty(node.attribute("name")));
  setTradingSymbol(TQStringEmpty(node.attribute("symbol")));
  setSecurityType(static_cast<eSECURITYTYPE>(node.attribute("type").toInt()));
  setSmallestAccountFraction(node.attribute("saf").toInt());

  if(isCurrency()) {
    setPartsPerUnit(node.attribute("ppu").toInt());
    setSmallestCashFraction(node.attribute("scf").toInt());
  } else {
    setTradingCurrency(TQStringEmpty(node.attribute("trading-currency")));
    setTradingMarket(TQStringEmpty(node.attribute("trading-market")));
  }
}

MyMoneySecurity::~MyMoneySecurity()
{
}

bool MyMoneySecurity::operator == (const MyMoneySecurity& r) const
{
  return (m_id == r.m_id)
      && (m_name == r.m_name)
      && (m_tradingSymbol == r.m_tradingSymbol)
      && (m_tradingMarket == r.m_tradingMarket)
      && (m_tradingSymbol == r.m_tradingSymbol)
      && (m_tradingCurrency == r.m_tradingCurrency)
      && (m_securityType == r.m_securityType)
      && (m_smallestAccountFraction == r.m_smallestAccountFraction)
      && (m_smallestCashFraction == r.m_smallestCashFraction)
      && (m_partsPerUnit == r.m_partsPerUnit)
      && this->MyMoneyKeyValueContainer::operator == (r);

}

bool MyMoneySecurity::operator < (const MyMoneySecurity& right) const
{
  if(m_securityType == right.m_securityType)
    return m_name < right.m_name;
  return m_securityType < right.m_securityType;
}


bool MyMoneySecurity::hasReferenceTo(const TQString& id) const
{
  return (id == m_tradingCurrency);
}

void MyMoneySecurity::writeXML(TQDomDocument& document, TQDomElement& parent) const
{
  TQDomElement el;
  if(isCurrency())
    el = document.createElement("CURRENCY");
  else
    el = document.createElement("SECURITY");

  writeBaseXML(document, el);

  el.setAttribute("name", m_name);
  el.setAttribute("symbol", m_tradingSymbol);
  el.setAttribute("type", static_cast<int>(m_securityType));
  el.setAttribute("saf", m_smallestAccountFraction);
  if(isCurrency()) {
    el.setAttribute("ppu", m_partsPerUnit);
    el.setAttribute("scf", m_smallestCashFraction);
  } else {
    el.setAttribute("trading-currency", m_tradingCurrency);
    el.setAttribute("trading-market", m_tradingMarket);
  }

  //Add in Key-Value Pairs for securities.
  MyMoneyKeyValueContainer::writeXML(document, el);

  parent.appendChild(el);
}

TQString MyMoneySecurity::securityTypeToString(const eSECURITYTYPE securityType)
{
  TQString returnString;

  switch (securityType) {
    case MyMoneySecurity::SECURITY_STOCK:
      returnString = I18N_NOOP("Stock");
      break;
    case MyMoneySecurity::SECURITY_MUTUALFUND:
      returnString = I18N_NOOP("Mutual Fund");
      break;
    case MyMoneySecurity::SECURITY_BOND:
      returnString = I18N_NOOP("Bond");
      break;
    case MyMoneySecurity::SECURITY_CURRENCY:
      returnString = I18N_NOOP("Currency");
      break;
    case MyMoneySecurity::SECURITY_NONE:
      returnString = I18N_NOOP("None");
      break;
    default:
      returnString = I18N_NOOP("Unknown");
  }

  return returnString;
}