summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneyprice.cpp
blob: 152fbeb62ed87632a2f9dcca5546d1a9aa45e774 (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
/***************************************************************************
                          mymoneyprice  -  description
                             -------------------
    begin                : Sun Nov 21 2004
    copyright            : (C) 2000-2004 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.                                   *
 *                                                                         *
 ***************************************************************************/

/**
  * @author Thomas Baumgart
  */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// ----------------------------------------------------------------------------
// TQt Includes

// ----------------------------------------------------------------------------
// TDE Includes

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

#include "mymoneyprice.h"
#include "mymoneyexception.h"

MyMoneyPrice::MyMoneyPrice() :
  m_date(TQDate())
{
}

MyMoneyPrice::MyMoneyPrice(const TQString& from, const TQString& to, const TQDomElement& node)
{
  if("PRICE" != node.tagName())
    throw new MYMONEYEXCEPTION("Node was not PRICE");

  m_fromSecurity = from;
  m_toSecurity = to;

  m_date = TQDate::fromString(node.attribute("date"), TQt::ISODate);
  m_rate = MyMoneyMoney(node.attribute("price"));
  m_source = node.attribute("source");

  if(!m_rate.isZero())
    m_invRate = MyMoneyMoney(1,1) / m_rate;
  else
    tqDebug("Price with zero value loaded");
}

MyMoneyPrice::MyMoneyPrice(const TQString& from, const TQString& to, const TQDate& date, const MyMoneyMoney& rate, const TQString& source) :
  m_fromSecurity(from),
  m_toSecurity(to),
  m_date(date),
  m_rate(rate),
  m_source(source)
{
  if(!m_rate.isZero())
    m_invRate = MyMoneyMoney(1,1) / m_rate;
  else
    tqDebug("Price with zero value created");
}

MyMoneyPrice::~MyMoneyPrice()
{
}

const MyMoneyMoney MyMoneyPrice::rate(const TQString& id) const
{
  static MyMoneyMoney dummyPrice(1,1);

  if(!isValid())
    return dummyPrice;

  if(id.isEmpty() || id == m_toSecurity)
    return m_rate;
  if(id == m_fromSecurity)
    return m_invRate;

  TQString msg = TQString("Unknown security id %1 for price info %2/%3.").arg(id).arg(m_fromSecurity).arg(m_toSecurity);
  throw new MYMONEYEXCEPTION(msg);
}

bool MyMoneyPrice::isValid(void) const
{
  return (m_date.isValid() && !m_fromSecurity.isEmpty() && !m_toSecurity.isEmpty());
}

// Equality operator
bool MyMoneyPrice::operator == (const MyMoneyPrice &right) const
{
  return ((m_date == right.m_date) &&
      (m_rate == right.m_rate) &&
      ((m_fromSecurity.length() == 0 && right.m_fromSecurity.length() == 0) || (m_fromSecurity == right.m_fromSecurity)) &&
      ((m_toSecurity.length() == 0 && right.m_toSecurity.length() == 0) || (m_toSecurity == right.m_toSecurity)) &&
      ((m_source.length() == 0 && right.m_source.length() == 0) || (m_source == right.m_source)));
}

bool MyMoneyPrice::hasReferenceTo(const TQString& id) const
{
  return (id == m_fromSecurity) || (id == m_toSecurity);
}