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

0001 /*
0002     SPDX-FileCopyrightText: 2006 Ace Jones <acejones@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2006 Darren Gould <darren_gould@gmx.de>
0004     SPDX-FileCopyrightText: 2010-2019 Thomas Baumgart <tbaumgart@kde.org>
0005     SPDX-FileCopyrightText: 2017 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef MYMONEYBUDGET_H
0010 #define MYMONEYBUDGET_H
0011 
0012 // ----------------------------------------------------------------------------
0013 // QT Includes
0014 
0015 #include <QMetaType>
0016 
0017 // ----------------------------------------------------------------------------
0018 // Project Includes
0019 
0020 #include "kmm_mymoney_export.h"
0021 #include "mymoneyenums.h"
0022 #include "mymoneyobject.h"
0023 #include "mymoneyunittestable.h"
0024 
0025 class QString;
0026 class QDate;
0027 class MyMoneyMoney;
0028 
0029 template <typename T> class QList;
0030 template <class Key, class T> class QMap;
0031 
0032 namespace eMyMoney {
0033 namespace Budget {
0034 enum class Level;
0035 }
0036 }
0037 
0038 /**
0039   * This class defines a Budget within the MyMoneyEngine.  The Budget class
0040   * contains all the configuration parameters needed to run a Budget, plus
0041   * XML serialization.
0042   *
0043   * As noted above, this class only provides a Budget DEFINITION.  The
0044   * generation and presentation of the Budget itself are left to higher
0045   * level classes.
0046   *
0047   * @author Darren Gould <darren_gould@gmx.de>
0048   */
0049 class MyMoneyBudgetPrivate;
0050 class KMM_MYMONEY_EXPORT MyMoneyBudget: public MyMoneyObject
0051 {
0052     Q_DECLARE_PRIVATE(MyMoneyBudget)
0053 
0054     KMM_MYMONEY_UNIT_TESTABLE
0055 
0056 public:
0057     MyMoneyBudget();
0058     explicit MyMoneyBudget(const QString &id);
0059 
0060     /**
0061       * This constructor creates an object based on the data found in the
0062       * MyMoneyBudget budget object.
0063       */
0064     MyMoneyBudget(const QString& id,
0065                   const MyMoneyBudget& other);
0066 
0067     MyMoneyBudget(const MyMoneyBudget& other);
0068     MyMoneyBudget(MyMoneyBudget && other);
0069     MyMoneyBudget & operator=(MyMoneyBudget other);
0070     friend void swap(MyMoneyBudget& first, MyMoneyBudget& second);
0071 
0072     ~MyMoneyBudget();
0073 
0074     /**
0075       * Helper class for MyMoneyBudget
0076       *
0077       * This is an abstraction of the PERIOD stored in the BUDGET/ACCOUNT tag in XML
0078       *
0079       * @author Darren Gould
0080       */
0081     class PeriodGroupPrivate;
0082     class KMM_MYMONEY_EXPORT PeriodGroup
0083     {
0084         Q_DECLARE_PRIVATE(PeriodGroup)
0085         PeriodGroupPrivate* d_ptr;
0086 
0087     public:
0088         PeriodGroup();
0089         PeriodGroup(const PeriodGroup & other);
0090         PeriodGroup(PeriodGroup && other);
0091         PeriodGroup & operator=(PeriodGroup other);
0092         friend void swap(PeriodGroup& first, PeriodGroup& second);
0093 
0094         ~PeriodGroup();
0095 
0096         QDate startDate() const;
0097         void setStartDate(const QDate& start);
0098 
0099         MyMoneyMoney amount() const;
0100         void setAmount(const MyMoneyMoney& amount);
0101 
0102         bool operator == (const PeriodGroup &right) const;
0103     };
0104 
0105     /**
0106       * Helper class for MyMoneyBudget
0107       *
0108       * This is an abstraction of the Account Data stored in the BUDGET tag in XML
0109       *
0110       * @author Darren Gould
0111       */
0112     class AccountGroupPrivate;
0113     class KMM_MYMONEY_EXPORT AccountGroup
0114     {
0115         Q_DECLARE_PRIVATE(AccountGroup)
0116         AccountGroupPrivate* d_ptr;
0117 
0118     public:
0119         AccountGroup();
0120         AccountGroup(const AccountGroup & other);
0121         AccountGroup(AccountGroup && other);
0122         AccountGroup & operator=(AccountGroup other);
0123         friend void swap(AccountGroup& first, AccountGroup& second);
0124 
0125         ~AccountGroup();
0126 
0127         QString id() const;
0128         void setId(const QString& id);
0129 
0130         bool budgetSubaccounts() const;
0131         void setBudgetSubaccounts(bool budgetsubaccounts);
0132 
0133         eMyMoney::Budget::Level budgetLevel() const;
0134         void setBudgetLevel(eMyMoney::Budget::Level level);
0135 
0136         eMyMoney::Account::Type budgetType() const;
0137         void setBudgetType(eMyMoney::Account::Type type);
0138 
0139         PeriodGroup period(const QDate& date) const;
0140         void addPeriod(const QDate& date, PeriodGroup& period);
0141         const QMap<QDate, PeriodGroup> getPeriods() const;
0142         void clearPeriods();
0143 
0144         MyMoneyMoney balance() const;
0145         MyMoneyMoney totalBalance() const;
0146 
0147         // This member adds the value of another account group
0148         // m_budgetlevel is adjusted to the larger one of both
0149         // m_budgetsubaccounts remains unaffected
0150         AccountGroup operator += (const AccountGroup& right);
0151 
0152         bool operator == (const AccountGroup &right) const;
0153 
0154         bool isZero() const;
0155 
0156     protected:
0157         void convertToMonthly();
0158         void convertToYearly();
0159         void convertToMonthByMonth();
0160     };
0161 
0162     /**
0163      * This operator tests for equality of two MyMoneyBudget objects
0164      */
0165     bool operator == (const MyMoneyBudget &) const;
0166 
0167     QString name() const;
0168     void setName(const QString& name);
0169 
0170     QDate budgetStart() const;
0171     void setBudgetStart(const QDate& start);
0172 
0173     const AccountGroup & account(const QString &id) const;
0174     void setAccount(const AccountGroup& account, const QString &id);
0175 
0176     bool contains(const QString &id) const;
0177     QList<AccountGroup> getaccounts() const;
0178 
0179     QMap<QString, MyMoneyBudget::AccountGroup> accountsMap() const;
0180 
0181     /**
0182       * This member removes all references to object identified by @p id. Used
0183       * to remove objects which are about to be removed from the engine.
0184       */
0185     void removeReference(const QString& id);
0186 };
0187 
0188 inline void swap(MyMoneyBudget::PeriodGroup& first, MyMoneyBudget::PeriodGroup& second) // krazy:exclude=inline
0189 {
0190     using std::swap;
0191     swap(first.d_ptr, second.d_ptr);
0192 }
0193 
0194 inline MyMoneyBudget::PeriodGroup::PeriodGroup(MyMoneyBudget::PeriodGroup && other) : PeriodGroup() // krazy:exclude=inline
0195 {
0196     swap(*this, other);
0197 }
0198 
0199 inline MyMoneyBudget::PeriodGroup & MyMoneyBudget::PeriodGroup::operator=(MyMoneyBudget::PeriodGroup other) // krazy:exclude=inline
0200 {
0201     swap(*this, other);
0202     return *this;
0203 }
0204 
0205 inline void swap(MyMoneyBudget::AccountGroup& first, MyMoneyBudget::AccountGroup& second) // krazy:exclude=inline
0206 {
0207     using std::swap;
0208     swap(first.d_ptr, second.d_ptr);
0209 }
0210 
0211 inline MyMoneyBudget::AccountGroup::AccountGroup(MyMoneyBudget::AccountGroup && other) : AccountGroup() // krazy:exclude=inline
0212 {
0213     swap(*this, other);
0214 }
0215 
0216 inline MyMoneyBudget::AccountGroup & MyMoneyBudget::AccountGroup::operator=(MyMoneyBudget::AccountGroup other) // krazy:exclude=inline
0217 {
0218     swap(*this, other);
0219     return *this;
0220 }
0221 
0222 inline void swap(MyMoneyBudget& first, MyMoneyBudget& second) // krazy:exclude=inline
0223 {
0224     using std::swap;
0225     swap(first.d_ptr, second.d_ptr);
0226 }
0227 
0228 inline MyMoneyBudget::MyMoneyBudget(MyMoneyBudget && other) : MyMoneyBudget() // krazy:exclude=inline
0229 {
0230     swap(*this, other);
0231 }
0232 
0233 inline MyMoneyBudget & MyMoneyBudget::operator=(MyMoneyBudget other) // krazy:exclude=inline
0234 {
0235     swap(*this, other);
0236     return *this;
0237 }
0238 
0239 /**
0240   * Make it possible to hold @ref MyMoneyBudget objects inside @ref QVariant objects.
0241   */
0242 Q_DECLARE_METATYPE(MyMoneyBudget)
0243 
0244 #endif // MYMONEYBudget_H