File indexing completed on 2024-05-19 05:07:20

0001 /*
0002     SPDX-FileCopyrightText: 2005-2019 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 /**
0008   * @author Thomas Baumgart
0009   */
0010 
0011 #include "mymoneyprice.h"
0012 
0013 // ----------------------------------------------------------------------------
0014 // QT Includes
0015 
0016 #include <QDate>
0017 #include <QString>
0018 #include <QDomElement>
0019 #include <QSet>
0020 
0021 // ----------------------------------------------------------------------------
0022 // KDE Includes
0023 
0024 // ----------------------------------------------------------------------------
0025 // Project Includes
0026 
0027 #include "mymoneymoney.h"
0028 #include "mymoneyexception.h"
0029 
0030 class MyMoneyPricePrivate
0031 {
0032 public:
0033     QString       m_fromSecurity;
0034     QString       m_toSecurity;
0035     QDate         m_date;
0036     MyMoneyMoney  m_rate;
0037     MyMoneyMoney  m_invRate;
0038     QString       m_source;
0039 };
0040 
0041 MyMoneyPrice::MyMoneyPrice() :
0042     d_ptr(new MyMoneyPricePrivate)
0043 {
0044 }
0045 
0046 MyMoneyPrice::MyMoneyPrice(const QString& from,
0047                            const QString& to,
0048                            const QDomElement& node) :
0049     d_ptr(new MyMoneyPricePrivate)
0050 {
0051     if ("PRICE" != node.tagName())
0052         throw MYMONEYEXCEPTION_CSTRING("Node was not PRICE");
0053 
0054     Q_D(MyMoneyPrice);
0055     d->m_fromSecurity = from;
0056     d->m_toSecurity = to;
0057 
0058     d->m_date = QDate::fromString(node.attribute("date"), Qt::ISODate);
0059     d->m_rate = MyMoneyMoney(node.attribute("price"));
0060     d->m_source = node.attribute("source");
0061 
0062     if (!d->m_rate.isZero())
0063         d->m_invRate = MyMoneyMoney::ONE / d->m_rate;
0064     else
0065         qDebug("Price with zero value loaded");
0066 }
0067 
0068 MyMoneyPrice::MyMoneyPrice(const QString& from, const QString& to, const QDate& date, const MyMoneyMoney& rate, const QString& source) :
0069     d_ptr(new MyMoneyPricePrivate)
0070 {
0071     Q_D(MyMoneyPrice);
0072     d->m_fromSecurity =from;
0073     d->m_toSecurity = to;
0074     d->m_date = date;
0075     d->m_rate = rate;
0076     d->m_source = source;
0077 
0078     if (!d->m_rate.isZero())
0079         d->m_invRate = MyMoneyMoney::ONE / d->m_rate;
0080     else
0081         qDebug("Price with zero value created for '%s' to '%s'",
0082                qPrintable(from), qPrintable(to));
0083 }
0084 
0085 MyMoneyPrice::MyMoneyPrice(const MyMoneyPrice& other) :
0086     d_ptr(new MyMoneyPricePrivate(*other.d_func()))
0087 {
0088 }
0089 
0090 MyMoneyPrice::~MyMoneyPrice()
0091 {
0092     Q_D(MyMoneyPrice);
0093     delete d;
0094 }
0095 
0096 MyMoneyMoney MyMoneyPrice::rate(const QString& id) const
0097 {
0098     Q_D(const MyMoneyPrice);
0099     static MyMoneyMoney dummyPrice(1, 1);
0100 
0101     if (!isValid())
0102         return dummyPrice;
0103 
0104     if (id.isEmpty() || id == d->m_toSecurity)
0105         return d->m_rate;
0106     if (id == d->m_fromSecurity)
0107         return d->m_invRate;
0108 
0109     throw MYMONEYEXCEPTION(QString::fromLatin1("Unknown security id %1 for price info %2/%3.").arg(id, d->m_fromSecurity, d->m_toSecurity));
0110 }
0111 
0112 QDate MyMoneyPrice::date() const
0113 {
0114     Q_D(const MyMoneyPrice);
0115     return d->m_date;
0116 }
0117 
0118 QString MyMoneyPrice::source() const
0119 {
0120     Q_D(const MyMoneyPrice);
0121     return d->m_source;
0122 }
0123 
0124 QString MyMoneyPrice::from() const
0125 {
0126     Q_D(const MyMoneyPrice);
0127     return d->m_fromSecurity;
0128 }
0129 
0130 QString MyMoneyPrice::to() const
0131 {
0132     Q_D(const MyMoneyPrice);
0133     return d->m_toSecurity;
0134 }
0135 
0136 bool MyMoneyPrice::isValid() const
0137 {
0138     Q_D(const MyMoneyPrice);
0139     return (d->m_date.isValid() && !d->m_fromSecurity.isEmpty() && !d->m_toSecurity.isEmpty());
0140 }
0141 
0142 // Equality operator
0143 bool MyMoneyPrice::operator == (const MyMoneyPrice &right) const
0144 {
0145     Q_D(const MyMoneyPrice);
0146     auto d2 = static_cast<const MyMoneyPricePrivate *>(right.d_func());
0147     return ((d->m_date == d2->m_date) &&
0148             (d->m_rate == d2->m_rate) &&
0149             ((d->m_fromSecurity.length() == 0 && d2->m_fromSecurity.length() == 0) || (d->m_fromSecurity == d2->m_fromSecurity)) &&
0150             ((d->m_toSecurity.length() == 0 && d2->m_toSecurity.length() == 0) || (d->m_toSecurity == d2->m_toSecurity)) &&
0151             ((d->m_source.length() == 0 && d2->m_source.length() == 0) || (d->m_source == d2->m_source)));
0152 }
0153 
0154 bool MyMoneyPrice::operator != (const MyMoneyPrice &right) const
0155 {
0156     return !(operator == (right));
0157 }
0158 
0159 bool MyMoneyPrice::hasReferenceTo(const QString& id) const
0160 {
0161     Q_D(const MyMoneyPrice);
0162     return (id == d->m_fromSecurity) || (id == d->m_toSecurity);
0163 }
0164 
0165 QSet<QString> MyMoneyPrice::referencedObjects() const
0166 {
0167     Q_D(const MyMoneyPrice);
0168     return { d->m_fromSecurity, d->m_toSecurity };
0169 }