File indexing completed on 2024-05-12 16:42:38
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 setDecimalSeparator(const QChar &); 0117 static void setNegativeMonetarySignPosition(const eMyMoney::Money::signPosition pos); 0118 static void setPositiveMonetarySignPosition(const eMyMoney::Money::signPosition pos); 0119 static void setNegativePrefixCurrencySymbol(const bool flag); 0120 static void setPositivePrefixCurrencySymbol(const bool flag); 0121 static void setNegativeSpaceSeparatesSymbol(const bool flag); 0122 static void setPositiveSpaceSeparatesSymbol(const bool flag); 0123 0124 static const QChar thousandSeparator(); 0125 static const QChar decimalSeparator(); 0126 static eMyMoney::Money::signPosition negativeMonetarySignPosition(); 0127 static eMyMoney::Money::signPosition positiveMonetarySignPosition(); 0128 0129 const MyMoneyMoney& operator=(const QString& pszAmount); 0130 const MyMoneyMoney& operator=(const AlkValue& val); 0131 const MyMoneyMoney& operator=(const MyMoneyMoney& val); 0132 0133 // comparison 0134 bool operator==(const MyMoneyMoney& Amount) const; 0135 bool operator!=(const MyMoneyMoney& Amount) const; 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 0141 bool operator==(const QString& pszAmount) const; 0142 bool operator!=(const QString& pszAmount) const; 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 0148 // calculation 0149 const MyMoneyMoney operator+(const MyMoneyMoney& Amount) const; 0150 const MyMoneyMoney operator-(const MyMoneyMoney& Amount) const; 0151 const MyMoneyMoney operator*(const MyMoneyMoney& factor) const; 0152 const MyMoneyMoney operator/(const MyMoneyMoney& Amount) const; 0153 const MyMoneyMoney operator-() const; 0154 const MyMoneyMoney operator*(int factor) const; 0155 0156 static MyMoneyMoney maxValue; 0157 static MyMoneyMoney minValue; 0158 static MyMoneyMoney autoCalc; 0159 0160 bool isNegative() const; 0161 bool isPositive() const; 0162 bool isZero() const; 0163 bool isAutoCalc() const; 0164 0165 MyMoneyMoney reduce() const; 0166 0167 static const MyMoneyMoney ONE; 0168 static const MyMoneyMoney MINUS_ONE; 0169 }; 0170 0171 //============================================================================= 0172 // 0173 // Inline functions 0174 // 0175 //============================================================================= 0176 0177 //////////////////////////////////////////////////////////////////////////////// 0178 // Name: MyMoneyMoney 0179 // Purpose: Constructor - constructs object set to 0. 0180 // Returns: None 0181 // Throws: Nothing. 0182 // Arguments: None 0183 // 0184 //////////////////////////////////////////////////////////////////////////////// 0185 inline MyMoneyMoney::MyMoneyMoney() : 0186 AlkValue() 0187 { 0188 } 0189 0190 0191 //////////////////////////////////////////////////////////////////////////////// 0192 // Name: MyMoneyMoney 0193 // Purpose: Constructor - constructs object from an amount in a double value 0194 // Returns: None 0195 // Throws: Nothing. 0196 // Arguments: dAmount - double object containing amount 0197 // denom - denominator of the object 0198 // 0199 //////////////////////////////////////////////////////////////////////////////// 0200 inline MyMoneyMoney::MyMoneyMoney(const double dAmount, const unsigned int denom) : 0201 AlkValue(dAmount, denom) 0202 { 0203 } 0204 0205 //////////////////////////////////////////////////////////////////////////////// 0206 // Name: MyMoneyMoney 0207 // Purpose: Copy Constructor - constructs object from another 0208 // MyMoneyMoney object 0209 // Returns: None 0210 // Throws: Nothing. 0211 // Arguments: Amount - MyMoneyMoney object to be copied 0212 // 0213 //////////////////////////////////////////////////////////////////////////////// 0214 inline MyMoneyMoney::MyMoneyMoney(const MyMoneyMoney& Amount) : 0215 AlkValue(Amount) 0216 { 0217 } 0218 0219 inline MyMoneyMoney::MyMoneyMoney(const AlkValue& Amount) : 0220 AlkValue(Amount) 0221 { 0222 } 0223 0224 inline const MyMoneyMoney& MyMoneyMoney::operator=(const AlkValue & val) 0225 { 0226 AlkValue::operator=(val); 0227 return *this; 0228 } 0229 0230 inline const MyMoneyMoney& MyMoneyMoney::operator=(const MyMoneyMoney & val) 0231 { 0232 AlkValue::operator=(val); 0233 return *this; 0234 } 0235 0236 //////////////////////////////////////////////////////////////////////////////// 0237 // Name: operator= 0238 // Purpose: Assignment operator - modifies object from input NULL terminated 0239 // string 0240 // Returns: Const reference to the object 0241 // Throws: Nothing. 0242 // Arguments: pszAmount - NULL terminated string that contains amount 0243 // 0244 //////////////////////////////////////////////////////////////////////////////// 0245 inline const MyMoneyMoney& MyMoneyMoney::operator=(const QString & pszAmount) 0246 { 0247 AlkValue::operator=(pszAmount); 0248 return *this; 0249 } 0250 0251 //////////////////////////////////////////////////////////////////////////////// 0252 // Name: operator== 0253 // Purpose: Compare equal operator - compares object with input MyMoneyMoney object 0254 // Returns: true if equal, otherwise false 0255 // Throws: Nothing. 0256 // Arguments: Amount - MyMoneyMoney object to be compared with 0257 // 0258 //////////////////////////////////////////////////////////////////////////////// 0259 inline bool MyMoneyMoney::operator==(const MyMoneyMoney& Amount) const 0260 { 0261 return AlkValue::operator==(Amount); 0262 } 0263 0264 //////////////////////////////////////////////////////////////////////////////// 0265 // Name: operator!= 0266 // Purpose: Compare not equal operator - compares object with input MyMoneyMoney object 0267 // Returns: true if not equal, otherwise false 0268 // Throws: Nothing. 0269 // Arguments: Amount - MyMoneyMoney object to be compared with 0270 // 0271 //////////////////////////////////////////////////////////////////////////////// 0272 inline bool MyMoneyMoney::operator!=(const MyMoneyMoney& Amount) const 0273 { 0274 return AlkValue::operator!=(Amount); 0275 } 0276 0277 //////////////////////////////////////////////////////////////////////////////// 0278 // Name: operator< 0279 // Purpose: Compare less than operator - compares object with input MyMoneyMoney object 0280 // Returns: true if object less than input amount 0281 // Throws: Nothing. 0282 // Arguments: Amount - MyMoneyMoney object to be compared with 0283 // 0284 //////////////////////////////////////////////////////////////////////////////// 0285 inline bool MyMoneyMoney::operator<(const MyMoneyMoney& Amount) const 0286 { 0287 return AlkValue::operator<(Amount); 0288 } 0289 0290 //////////////////////////////////////////////////////////////////////////////// 0291 // Name: operator> 0292 // Purpose: Compare greater than operator - compares object with input MyMoneyMoney 0293 // object 0294 // Returns: true if object greater than input amount 0295 // Throws: Nothing. 0296 // Arguments: Amount - MyMoneyMoney object to be compared with 0297 // 0298 //////////////////////////////////////////////////////////////////////////////// 0299 inline bool MyMoneyMoney::operator>(const MyMoneyMoney& Amount) const 0300 { 0301 return AlkValue::operator>(Amount); 0302 } 0303 0304 //////////////////////////////////////////////////////////////////////////////// 0305 // Name: operator<= 0306 // Purpose: Compare less than equal to operator - compares object with input 0307 // MyMoneyMoney object 0308 // Returns: true if object less than or equal to input amount 0309 // Throws: Nothing. 0310 // Arguments: Amount - MyMoneyMoney object to be compared with 0311 // 0312 //////////////////////////////////////////////////////////////////////////////// 0313 inline bool MyMoneyMoney::operator<=(const MyMoneyMoney& Amount) const 0314 { 0315 return AlkValue::operator<=(Amount); 0316 } 0317 0318 //////////////////////////////////////////////////////////////////////////////// 0319 // Name: operator>= 0320 // Purpose: Compare greater than equal to operator - compares object with input 0321 // MyMoneyMoney object 0322 // Returns: true if object greater than or equal to input amount 0323 // Throws: Nothing. 0324 // Arguments: Amount - MyMoneyMoney object to be compared with 0325 // 0326 //////////////////////////////////////////////////////////////////////////////// 0327 inline bool MyMoneyMoney::operator>=(const MyMoneyMoney& Amount) const 0328 { 0329 return AlkValue::operator>=(Amount); 0330 } 0331 0332 //////////////////////////////////////////////////////////////////////////////// 0333 // Name: operator== 0334 // Purpose: Compare equal operator - compares object with input amount in a 0335 // NULL terminated string 0336 // Returns: true if equal, otherwise false 0337 // Throws: Nothing. 0338 // Arguments: pszAmount - NULL terminated string that contains amount 0339 // 0340 //////////////////////////////////////////////////////////////////////////////// 0341 inline bool MyMoneyMoney::operator==(const QString& pszAmount) const 0342 { 0343 return *this == MyMoneyMoney(pszAmount); 0344 } 0345 0346 //////////////////////////////////////////////////////////////////////////////// 0347 // Name: operator!= 0348 // Purpose: Compare not equal operator - compares object with input amount in 0349 // a NULL terminated string 0350 // Returns: true if not equal, otherwise false 0351 // Throws: Nothing. 0352 // Arguments: pszAmount - NULL terminated string that contains amount 0353 // 0354 //////////////////////////////////////////////////////////////////////////////// 0355 inline bool MyMoneyMoney::operator!=(const QString& pszAmount) const 0356 { 0357 return ! operator==(pszAmount) ; 0358 } 0359 0360 //////////////////////////////////////////////////////////////////////////////// 0361 // Name: operator- 0362 // Purpose: Unary operator - returns the negative value from the object 0363 // Returns: The current object 0364 // Throws: Nothing. 0365 // Arguments: None 0366 // 0367 //////////////////////////////////////////////////////////////////////////////// 0368 inline const MyMoneyMoney MyMoneyMoney::operator-() const 0369 { 0370 return static_cast<const MyMoneyMoney>(AlkValue::operator-()); 0371 } 0372 0373 //////////////////////////////////////////////////////////////////////////////// 0374 // Name: operator* 0375 // Purpose: Multiplication operator - multiplies the object with factor 0376 // Returns: The current object 0377 // Throws: Nothing. 0378 // Arguments: AmountInPence - long object to be multiplied 0379 // 0380 //////////////////////////////////////////////////////////////////////////////// 0381 inline const MyMoneyMoney MyMoneyMoney::operator*(int factor) const 0382 { 0383 return static_cast<const MyMoneyMoney>(AlkValue::operator*(factor)); 0384 } 0385 0386 /** 0387 * Make it possible to hold @ref MyMoneyMoney objects 0388 * inside @ref QVariant objects. 0389 */ 0390 Q_DECLARE_METATYPE(MyMoneyMoney) 0391 0392 #endif 0393