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

0001 /*
0002     SPDX-FileCopyrightText: 2000-2004 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2001-2017 Thomas Baumgart <tbaumgart@kde.org>
0004     SPDX-FileCopyrightText: 2001-2002 Felix Rodriguez <frodriguez@users.sourceforge.net>
0005     SPDX-FileCopyrightText: 2002-2004 Kevin Tambascio <ktambascio@users.sourceforge.net>
0006     SPDX-FileCopyrightText: 2005 Ace Jones <acejones@users.sourceforge.net>
0007     SPDX-FileCopyrightText: 2007-2010 Alvaro Soliverez <asoliverez@gmail.com>
0008     SPDX-FileCopyrightText: 2011 Carlos Eduardo da Silva <kaduardo@gmail.com>
0009     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0010     SPDX-License-Identifier: GPL-2.0-or-later
0011 */
0012 
0013 // krazy:excludeall=dpointer
0014 
0015 #ifndef MYMONEYMONEY_H
0016 #define MYMONEYMONEY_H
0017 
0018 // So we can save this object
0019 #include <QMetaType>
0020 
0021 #include "kmm_mymoney_export.h"
0022 #include "mymoneyunittestable.h"
0023 
0024 #include <alkimia/alkvalue.h>
0025 
0026 typedef qint64 signed64;
0027 typedef quint64 unsigned64;
0028 
0029 namespace eMyMoney {
0030 namespace Money {
0031 enum signPosition : int;
0032 }
0033 }
0034 
0035 /**
0036   * This class represents a value within the MyMoney Engine
0037   *
0038   * @author Michael Edwardes
0039   * @author Thomas Baumgart
0040   */
0041 class KMM_MYMONEY_EXPORT MyMoneyMoney : public AlkValue
0042 {
0043     KMM_MYMONEY_UNIT_TESTABLE
0044 
0045 public:
0046     // construction
0047     MyMoneyMoney();
0048     explicit MyMoneyMoney(const int iAmount, const unsigned int denom);
0049     explicit MyMoneyMoney(const long int iAmount, const unsigned int denom);
0050     explicit MyMoneyMoney(const QString& pszAmount);
0051     explicit MyMoneyMoney(const qint64 Amount, const unsigned int denom);
0052     explicit MyMoneyMoney(const double dAmount, const unsigned int denom = 100);
0053 
0054     // copy constructor
0055     MyMoneyMoney(const MyMoneyMoney& Amount);
0056     explicit MyMoneyMoney(const AlkValue& Amount);
0057 
0058     virtual ~MyMoneyMoney();
0059 
0060     MyMoneyMoney abs() const;
0061 
0062     /**
0063       * This method returns a formatted string according to the settings
0064       * of _thousandSeparator, _decimalSeparator, _negativeMonetarySignPosition,
0065       * _positiveMonetaryPosition, _negativePrefixCurrencySymbol,
0066       * _positivePrefixCurrencySymbol, _negativeSpaceSeparatesSymbol and
0067       * _positiveSpaceSeparatesSymbol. Those values can be modified using
0068       * the appropriate set-methods.
0069       *
0070       * @param currency The currency symbol
0071       * @param prec The number of fractional digits
0072       * @param showThousandSeparator should the thousandSeparator symbol
0073       *                               be inserted (@a true)
0074       *                               or not (@a false) (default true)
0075       */
0076     QString formatMoney(const QString& currency, const int prec, bool showThousandSeparator = true) const;
0077 
0078     /**
0079      * This is a convenience method. It behaves exactly as the above one,
0080      * but takes the information about precision out of the denomination
0081      * @a denom. No currency symbol is shown. If you want to add a currency
0082      * symbol, please use MyMoneyUtils::formatMoney(const MyMoneyAccount& acc, const MyMoneySecurity& sec, bool showThousandSeparator)
0083      * instead.
0084      *
0085      * @note denom is often set to account.fraction(security).
0086      */
0087     QString formatMoney(int denom, bool showThousandSeparator = true) const;
0088 
0089     /**
0090       * This method is used to convert the smallest fraction information into
0091       * the corresponding number of digits used for precision.
0092       *
0093       * @param fract smallest fractional part (e.g. 100 for cents)
0094       * @return number of precision digits (e.g. 2 for cents)
0095       */
0096     static int denomToPrec(signed64 fract);
0097 
0098     MyMoneyMoney convert(const signed64 denom = 100, const AlkValue::RoundingMethod how = AlkValue::RoundRound) const;
0099 
0100     /**
0101      * Returns the current value converted to the given @a denom.
0102      * The rounding method used is controlled by the @a how argument
0103      * and defaults to @p RoundRound.
0104      *
0105      * @param denom The wanted denominator (defaults to 100 or 2 digits)
0106      * @param how The rounding method. See AlkValue::RoundingMethod for details
0107      *
0108      * @note This should eventually be moved to AlkValue
0109      */
0110     MyMoneyMoney convertDenominator(mpz_class denom = 100, const AlkValue::RoundingMethod how = AlkValue::RoundRound) const;
0111 
0112     static signed64 precToDenom(int prec);
0113     double toDouble() const;
0114 
0115     static void setThousandSeparator(const QChar &);
0116     static void setThousandSeparator(const QString&);
0117     static void setDecimalSeparator(const QChar &);
0118     static void setDecimalSeparator(const QString&);
0119     static void setNegativeMonetarySignPosition(const eMyMoney::Money::signPosition pos);
0120     static void setPositiveMonetarySignPosition(const eMyMoney::Money::signPosition pos);
0121     static void setNegativePrefixCurrencySymbol(const bool flag);
0122     static void setPositivePrefixCurrencySymbol(const bool flag);
0123     static void setNegativeSpaceSeparatesSymbol(const bool flag);
0124     static void setPositiveSpaceSeparatesSymbol(const bool flag);
0125 
0126     static const QChar thousandSeparator();
0127     static const QChar decimalSeparator();
0128     static eMyMoney::Money::signPosition negativeMonetarySignPosition();
0129     static eMyMoney::Money::signPosition positiveMonetarySignPosition();
0130 
0131     const MyMoneyMoney& operator=(const QString& pszAmount);
0132     const MyMoneyMoney& operator=(const AlkValue& val);
0133     const MyMoneyMoney& operator=(const MyMoneyMoney& val);
0134 
0135     // comparison
0136     bool operator==(const MyMoneyMoney& Amount) const;
0137     bool operator!=(const MyMoneyMoney& Amount) const;
0138     bool operator<(const MyMoneyMoney& Amount) const;
0139     bool operator>(const MyMoneyMoney& Amount) const;
0140     bool operator<=(const MyMoneyMoney& Amount) const;
0141     bool operator>=(const MyMoneyMoney& Amount) const;
0142 
0143     bool operator==(const QString& pszAmount) const;
0144     bool operator!=(const QString& pszAmount) const;
0145     bool operator<(const QString& pszAmount) const;
0146     bool operator>(const QString& pszAmount) const;
0147     bool operator<=(const QString& pszAmount) const;
0148     bool operator>=(const QString& pszAmount) const;
0149 
0150     // calculation
0151     const MyMoneyMoney operator+(const MyMoneyMoney& Amount) const;
0152     const MyMoneyMoney operator-(const MyMoneyMoney& Amount) const;
0153     const MyMoneyMoney operator*(const MyMoneyMoney& factor) const;
0154     const MyMoneyMoney operator/(const MyMoneyMoney& Amount) const;
0155     const MyMoneyMoney operator-() const;
0156     const MyMoneyMoney operator*(int factor) const;
0157 
0158     static MyMoneyMoney maxValue;
0159     static MyMoneyMoney minValue;
0160     static MyMoneyMoney autoCalc;
0161 
0162     bool isNegative() const;
0163     bool isPositive() const;
0164     bool isZero() const;
0165     bool isAutoCalc() const;
0166 
0167     MyMoneyMoney reduce() const;
0168 
0169     static const MyMoneyMoney ONE;
0170     static const MyMoneyMoney MINUS_ONE;
0171 };
0172 
0173 //=============================================================================
0174 //
0175 //  Inline functions
0176 //
0177 //=============================================================================
0178 
0179 ////////////////////////////////////////////////////////////////////////////////
0180 //      Name: MyMoneyMoney
0181 //   Purpose: Constructor - constructs object set to 0.
0182 //   Returns: None
0183 //    Throws: Nothing.
0184 // Arguments: None
0185 //
0186 ////////////////////////////////////////////////////////////////////////////////
0187 inline MyMoneyMoney::MyMoneyMoney() :
0188     AlkValue()
0189 {
0190 }
0191 
0192 
0193 ////////////////////////////////////////////////////////////////////////////////
0194 //      Name: MyMoneyMoney
0195 //   Purpose: Constructor - constructs object from an amount in a double value
0196 //   Returns: None
0197 //    Throws: Nothing.
0198 // Arguments: dAmount - double object containing amount
0199 //            denom   - denominator of the object
0200 //
0201 ////////////////////////////////////////////////////////////////////////////////
0202 inline MyMoneyMoney::MyMoneyMoney(const double dAmount, const unsigned int denom) :
0203     AlkValue(dAmount, denom)
0204 {
0205 }
0206 
0207 ////////////////////////////////////////////////////////////////////////////////
0208 //      Name: MyMoneyMoney
0209 //   Purpose: Copy Constructor - constructs object from another
0210 //            MyMoneyMoney object
0211 //   Returns: None
0212 //    Throws: Nothing.
0213 // Arguments: Amount - MyMoneyMoney object to be copied
0214 //
0215 ////////////////////////////////////////////////////////////////////////////////
0216 inline MyMoneyMoney::MyMoneyMoney(const MyMoneyMoney& Amount) :
0217     AlkValue(Amount)
0218 {
0219 }
0220 
0221 inline MyMoneyMoney::MyMoneyMoney(const AlkValue& Amount) :
0222     AlkValue(Amount)
0223 {
0224 }
0225 
0226 inline const MyMoneyMoney& MyMoneyMoney::operator=(const AlkValue & val)
0227 {
0228     AlkValue::operator=(val);
0229     return *this;
0230 }
0231 
0232 inline const MyMoneyMoney& MyMoneyMoney::operator=(const MyMoneyMoney & val)
0233 {
0234     AlkValue::operator=(val);
0235     return *this;
0236 }
0237 
0238 ////////////////////////////////////////////////////////////////////////////////
0239 //      Name: operator=
0240 //   Purpose: Assignment operator - modifies object from input NULL terminated
0241 //            string
0242 //   Returns: Const reference to the object
0243 //    Throws: Nothing.
0244 // Arguments: pszAmount - NULL terminated string that contains amount
0245 //
0246 ////////////////////////////////////////////////////////////////////////////////
0247 inline const MyMoneyMoney& MyMoneyMoney::operator=(const QString & pszAmount)
0248 {
0249     AlkValue::operator=(pszAmount);
0250     return *this;
0251 }
0252 
0253 ////////////////////////////////////////////////////////////////////////////////
0254 //      Name: operator==
0255 //   Purpose: Compare equal operator - compares object with input MyMoneyMoney object
0256 //   Returns: true if equal, otherwise false
0257 //    Throws: Nothing.
0258 // Arguments: Amount - MyMoneyMoney object to be compared with
0259 //
0260 ////////////////////////////////////////////////////////////////////////////////
0261 inline bool MyMoneyMoney::operator==(const MyMoneyMoney& Amount) const
0262 {
0263     return AlkValue::operator==(Amount);
0264 }
0265 
0266 ////////////////////////////////////////////////////////////////////////////////
0267 //      Name: operator!=
0268 //   Purpose: Compare not equal operator - compares object with input MyMoneyMoney object
0269 //   Returns: true if not equal, otherwise false
0270 //    Throws: Nothing.
0271 // Arguments: Amount - MyMoneyMoney object to be compared with
0272 //
0273 ////////////////////////////////////////////////////////////////////////////////
0274 inline bool MyMoneyMoney::operator!=(const MyMoneyMoney& Amount) const
0275 {
0276     return AlkValue::operator!=(Amount);
0277 }
0278 
0279 ////////////////////////////////////////////////////////////////////////////////
0280 //      Name: operator<
0281 //   Purpose: Compare less than operator - compares object with input MyMoneyMoney object
0282 //   Returns: true if object less than input amount
0283 //    Throws: Nothing.
0284 // Arguments: Amount - MyMoneyMoney object to be compared with
0285 //
0286 ////////////////////////////////////////////////////////////////////////////////
0287 inline bool MyMoneyMoney::operator<(const MyMoneyMoney& Amount) const
0288 {
0289     return AlkValue::operator<(Amount);
0290 }
0291 
0292 ////////////////////////////////////////////////////////////////////////////////
0293 //      Name: operator>
0294 //   Purpose: Compare greater than operator - compares object with input MyMoneyMoney
0295 //            object
0296 //   Returns: true if object greater than input amount
0297 //    Throws: Nothing.
0298 // Arguments: Amount - MyMoneyMoney object to be compared with
0299 //
0300 ////////////////////////////////////////////////////////////////////////////////
0301 inline bool MyMoneyMoney::operator>(const MyMoneyMoney& Amount) const
0302 {
0303     return AlkValue::operator>(Amount);
0304 }
0305 
0306 ////////////////////////////////////////////////////////////////////////////////
0307 //      Name: operator<=
0308 //   Purpose: Compare less than equal to operator - compares object with input
0309 //            MyMoneyMoney object
0310 //   Returns: true if object less than or equal to input amount
0311 //    Throws: Nothing.
0312 // Arguments: Amount - MyMoneyMoney object to be compared with
0313 //
0314 ////////////////////////////////////////////////////////////////////////////////
0315 inline bool MyMoneyMoney::operator<=(const MyMoneyMoney& Amount) const
0316 {
0317     return AlkValue::operator<=(Amount);
0318 }
0319 
0320 ////////////////////////////////////////////////////////////////////////////////
0321 //      Name: operator>=
0322 //   Purpose: Compare greater than equal to operator - compares object with input
0323 //            MyMoneyMoney object
0324 //   Returns: true if object greater than or equal to input amount
0325 //    Throws: Nothing.
0326 // Arguments: Amount - MyMoneyMoney object to be compared with
0327 //
0328 ////////////////////////////////////////////////////////////////////////////////
0329 inline bool MyMoneyMoney::operator>=(const MyMoneyMoney& Amount) const
0330 {
0331     return AlkValue::operator>=(Amount);
0332 }
0333 
0334 ////////////////////////////////////////////////////////////////////////////////
0335 //      Name: operator==
0336 //   Purpose: Compare equal operator - compares object with input amount in a
0337 //            NULL terminated string
0338 //   Returns: true if equal, otherwise false
0339 //    Throws: Nothing.
0340 // Arguments: pszAmount - NULL terminated string that contains amount
0341 //
0342 ////////////////////////////////////////////////////////////////////////////////
0343 inline bool MyMoneyMoney::operator==(const QString& pszAmount) const
0344 {
0345     return *this == MyMoneyMoney(pszAmount);
0346 }
0347 
0348 ////////////////////////////////////////////////////////////////////////////////
0349 //      Name: operator!=
0350 //   Purpose: Compare not equal operator - compares object with input amount in
0351 //            a NULL terminated string
0352 //   Returns: true if not equal, otherwise false
0353 //    Throws: Nothing.
0354 // Arguments: pszAmount - NULL terminated string that contains amount
0355 //
0356 ////////////////////////////////////////////////////////////////////////////////
0357 inline bool MyMoneyMoney::operator!=(const QString& pszAmount) const
0358 {
0359     return ! operator==(pszAmount) ;
0360 }
0361 
0362 ////////////////////////////////////////////////////////////////////////////////
0363 //      Name: operator-
0364 //   Purpose: Unary operator - returns the negative value from the object
0365 //   Returns: The current object
0366 //    Throws: Nothing.
0367 // Arguments: None
0368 //
0369 ////////////////////////////////////////////////////////////////////////////////
0370 inline const MyMoneyMoney MyMoneyMoney::operator-() const
0371 {
0372     return static_cast<const MyMoneyMoney>(AlkValue::operator-());
0373 }
0374 
0375 ////////////////////////////////////////////////////////////////////////////////
0376 //      Name: operator*
0377 //   Purpose: Multiplication operator - multiplies the object with factor
0378 //   Returns: The current object
0379 //    Throws: Nothing.
0380 // Arguments: AmountInPence - long object to be multiplied
0381 //
0382 ////////////////////////////////////////////////////////////////////////////////
0383 inline const MyMoneyMoney MyMoneyMoney::operator*(int factor) const
0384 {
0385     return static_cast<const MyMoneyMoney>(AlkValue::operator*(factor));
0386 }
0387 
0388 /**
0389   * Make it possible to hold @ref MyMoneyMoney objects
0390   * inside @ref QVariant objects.
0391   */
0392 Q_DECLARE_METATYPE(MyMoneyMoney)
0393 
0394 #endif
0395