File indexing completed on 2024-05-12 05:06:34

0001 /*
0002     SPDX-FileCopyrightText: 2000-2002 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2001 Felix Rodriguez <frodriguez@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2002-2003 Kevin Tambascio <ktambascio@users.sourceforge.net>
0005     SPDX-FileCopyrightText: 2006-2019 Thomas Baumgart <tbaumgart@kde.org>
0006     SPDX-FileCopyrightText: 2006 Ace Jones <acejones@users.sourceforge.net>
0007     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef MYMONEYACCOUNT_H
0012 #define MYMONEYACCOUNT_H
0013 
0014 // ----------------------------------------------------------------------------
0015 // QT Includes
0016 
0017 #include <QMetaType>
0018 
0019 // ----------------------------------------------------------------------------
0020 // Project Includes
0021 
0022 #include "mymoneykeyvaluecontainer.h"
0023 #include "mymoneyobject.h"
0024 #include "kmm_mymoney_export.h"
0025 #include "mymoneyunittestable.h"
0026 
0027 class QString;
0028 class QDate;
0029 class MyMoneySecurity;
0030 class MyMoneyMoney;
0031 class MyMoneySplit;
0032 class payeeIdentifier;
0033 namespace eMyMoney {
0034 namespace Account {
0035 enum class Type;
0036 enum class Standard;
0037 }
0038 }
0039 namespace Icons {
0040 enum class Icon;
0041 }
0042 template <class T> class payeeIdentifierTyped;
0043 
0044 /**
0045   * A representation of an account.
0046   * This object represents any type of account, those held at an
0047   * institution as well as the accounts used for double entry
0048   * accounting.
0049   *
0050   * Currently, the following account types are known:
0051   *
0052   * @li  Unknown
0053   * @li  Checkings
0054   * @li  Savings
0055   * @li  Cash
0056   * @li  CreditCard
0057   * @li  Loan (collected)
0058   * @li  CertificateDep
0059   * @li  Investment
0060   * @li  MoneyMarket
0061   * @li  Currency
0062   * @li  Asset
0063   * @li  Liability
0064   * @li  Income
0065   * @li  Expense
0066   * @li  Loan (given)
0067   * @li  Stock
0068   * @li  Equity
0069   *
0070   * @see MyMoneyInstitution
0071   * @see MyMoneyFile
0072   *
0073   * @author Michael Edwardes 2000-2001
0074   * @author Thomas Baumgart 2002
0075   * @author Łukasz Wojniłowicz 2017
0076   *
0077 **/
0078 
0079 class MyMoneyAccountPrivate;
0080 class MyMoneyAccountLoan;
0081 class KMM_MYMONEY_EXPORT MyMoneyAccount : public MyMoneyObject, public MyMoneyKeyValueContainer /*, public MyMoneyPayeeIdentifierContainer */
0082 {
0083     Q_DECLARE_PRIVATE_D(MyMoneyObject::d_ptr, MyMoneyAccount)
0084     friend MyMoneyAccountLoan;
0085 
0086     KMM_MYMONEY_UNIT_TESTABLE
0087 
0088 protected:
0089     explicit MyMoneyAccount(MyMoneyAccountPrivate* dd, const MyMoneyAccount& other);
0090 
0091 public:
0092     /**
0093       * This is the constructor for a new empty account
0094       */
0095     MyMoneyAccount();
0096     explicit MyMoneyAccount(const QString &id);
0097 
0098     /**
0099       * This is the constructor for a new account known to the current file
0100       * This is the only constructor that will set the attribute m_openingDate
0101       * to a correct value.
0102       *
0103       * @param id id assigned to the account
0104       * @param right account definition
0105       */
0106     MyMoneyAccount(const QString& id,
0107                    const MyMoneyAccount& other);
0108 
0109     MyMoneyAccount(const MyMoneyAccount & other);
0110     MyMoneyAccount(MyMoneyAccount && other);
0111     MyMoneyAccount & operator=(MyMoneyAccount other);
0112     friend void swap(MyMoneyAccount& first, MyMoneyAccount& second);
0113 
0114     /**
0115       * This is the destructor for any MyMoneyAccount object
0116       */
0117     ~MyMoneyAccount();
0118 
0119     /**
0120       * This operator tests for equality of two MyMoneyAccount objects
0121       */
0122     bool operator == (const MyMoneyAccount &) const;
0123 
0124     /**
0125       * This converts the account type into one of the five
0126       * major account types liability, asset, expense, income or equity.
0127       *
0128       * The current assignment is as follows:
0129       *
0130       * - Asset
0131       *   - Asset
0132       *   - Checkings
0133       *   - Savings
0134       *   - Cash
0135       *   - Currency
0136       *   - Investment
0137       *   - MoneyMarket
0138       *   - CertificateDep
0139       *   - AssetLoan
0140       *   - Stock
0141       *
0142       * - Liability
0143       *   - Liability
0144       *   - CreditCard
0145       *   - Loan
0146       *
0147       * - Income
0148       *   - Income
0149       *
0150       * - Expense
0151       *   - Expense
0152       *
0153       * - Equity
0154       *   - Equity
0155       *
0156       * @return accountTypeE of major account type
0157       */
0158     eMyMoney::Account::Type accountGroup() const;
0159 
0160     /**
0161      * Return the account group for a given account @a type.
0162      */
0163     static eMyMoney::Account::Type accountGroup(eMyMoney::Account::Type type);
0164 
0165     /**
0166       * This method returns the id of the MyMoneyInstitution object this account
0167       * belongs to.
0168       * @return id of MyMoneyInstitution object. QString() if it is
0169       *         an internal account
0170       * @see setInstitution
0171       */
0172     QString institutionId() const;
0173 
0174     /**
0175       * This method is used to set the id of the institution this account
0176       * belongs to.
0177       *
0178       * @param id id of the institution this account belongs to
0179       * @see institution
0180       */
0181     void setInstitutionId(const QString& id);
0182 
0183     /**
0184       * This method returns the name of the account
0185       * @return name of account
0186       * @see setName()
0187       */
0188     QString name() const;
0189 
0190     /**
0191       * This method is used to set the name of the account
0192       * @param name name of the account
0193       * @see name
0194       */
0195     void setName(const QString& name);
0196 
0197     /**
0198       * This method returns the number of the account at the institution
0199       * @return number of account at the institution
0200       * @see setNumber
0201       */
0202     QString number() const;
0203 
0204     /**
0205       * This method is used to set the number of the account at the institution
0206       * @param number number of the account
0207       * @see number
0208       */
0209     void setNumber(const QString& number);
0210 
0211     /**
0212       * This method returns the descriptive text of the account.
0213       * @return description of account
0214       * @see setDescription
0215       */
0216     QString description() const;
0217 
0218     /**
0219       * This method is used to set the descriptive text of the account
0220       *
0221       * @param desc text that serves as description
0222       * @see setDescription
0223       */
0224     void setDescription(const QString& desc);
0225 
0226     /**
0227       * This method returns the opening date of this account
0228       * @return date of opening of this account as const QDate value
0229       * @see setOpeningDate()
0230       */
0231     QDate openingDate() const;
0232 
0233     /**
0234       * This method is used to set the opening date information of an
0235       * account.
0236       *
0237       * @param date QDate of opening date
0238       * @see openingDate
0239       */
0240     void setOpeningDate(const QDate& date);
0241 
0242     /**
0243       * This method returns the date of the last reconciliation of this account
0244       * @return date of last reconciliation as const QDate value
0245       * @see setLastReconciliationDate
0246       */
0247     QDate lastReconciliationDate() const;
0248 
0249     /**
0250       * This method is used to set the date of the last reconciliation
0251       * of an account.
0252       * @param date QDate of last reconciliation
0253       * @see lastReconciliationDate
0254       */
0255     void setLastReconciliationDate(const QDate& date);
0256 
0257     /**
0258       * This method returns the date the account was last modified
0259       * @return date of last modification as const QDate value
0260       * @see setLastModified
0261       */
0262     QDate lastModified() const;
0263 
0264     /**
0265       * This method is used to modify the date of the last
0266       * modification access.
0267       * @param date date of last modification
0268       * @see lastModified
0269       */
0270     void setLastModified(const QDate& date);
0271 
0272     /**
0273       * This method is used to return the ID of the parent account
0274       * @return QString with the ID of the parent of this account
0275       */
0276     QString parentAccountId() const;
0277 
0278     /**
0279       * This method is used to set a new parent account id
0280       * @param parent QString reference to new parent account
0281       */
0282     void setParentAccountId(const QString& parent);
0283 
0284     /**
0285       * This method returns the list of the account id's of
0286       * subordinate accounts
0287       * @return QStringList account ids
0288       */
0289     QStringList accountList() const;
0290 
0291     /**
0292       * This method returns the number of entries in the m_accountList
0293       * @return number of entries in the accountList
0294       */
0295     int accountCount() const;
0296 
0297     /**
0298       * This method is used to add an account id as sub-ordinate account
0299       * @param account const QString reference to account ID
0300       */
0301     void addAccountId(const QString& account);
0302 
0303     /**
0304       * This method is used to remove an account from the list of
0305       * sub-ordinate accounts.
0306       * @param account const QString reference to account ID to be removed
0307       */
0308     void removeAccountId(const QString& account);
0309 
0310     /**
0311       * This method is used to remove all accounts from the list of
0312       * sub-ordinate accounts.
0313       */
0314     void removeAccountIds();
0315 
0316     /**
0317      * Return the stored account identifiers
0318      *
0319      * @internal This method is temporary until MyMoneyAccount is a MyMoneyPayeeIdentifierContainer. But before this
0320      * can happen the account numbers and iban kvp data must be moved there.
0321      */
0322     QList< payeeIdentifier > payeeIdentifiers() const;
0323 
0324     /**
0325       * This method is used to update m_lastModified to the current date
0326       */
0327     void touch();
0328 
0329     /**
0330       * This method returns the type of the account.
0331       */
0332     eMyMoney::Account::Type accountType() const;
0333 
0334     /**
0335       * This method is used to change the account type
0336       *
0337       * @param type account type
0338       */
0339     void setAccountType(const eMyMoney::Account::Type type);
0340 
0341     /**
0342       * This method retrieves the id of the currency used with this account.
0343       * If the return value is empty, the base currency should be used.
0344       *
0345       * @return id of currency
0346       */
0347     QString currencyId() const;
0348 
0349     /** There are three different currencies in play with a single Account:
0350     *   - The underlying currency: What currency the account itself is denominated in
0351     *   - The deep currency: The underlying currency's own underlying currency.  This
0352     *      is only a factor if the underlying currency of this account IS NOT a
0353     *      currency itself, but is some other kind of security.  In that case, the
0354     *      underlying security has its own currency.  The deep currency is the
0355     *      currency of the underlying security.  On the other hand, if the account
0356     *      has a currency itself, then the deep currency == the underlying currency,
0357     *      and this function will return 1.0.
0358     *   - The base currency: The base currency of the user's overall file
0359     *
0360     * @return id of deep currency
0361     */
0362     QString tradingCurrencyId() const;
0363 
0364     /**
0365       * Determine if this account's deep currency is different from the file's
0366       * base currency
0367       *
0368       * @return bool True if this account is in a foreign currency
0369       */
0370     bool isForeignCurrency() const;
0371 
0372     /**
0373       * This method sets the id of the currency used with this account.
0374       *
0375       * @param id ID of currency to be associated with this account.
0376       */
0377     void setCurrencyId(const QString& id);
0378 
0379     /**
0380       * This member returns the balance of this account based on
0381       * all transactions stored in the journal.
0382       */
0383     MyMoneyMoney balance() const;
0384 
0385     /**
0386      * This member returns the value of this account based on
0387      * all transactions stored in the journal and currently available
0388      * price information.
0389      */
0390     MyMoneyMoney postedValue() const;
0391 
0392     /**
0393      * This member returns the value of this account and all its
0394      * subaccounts based on all transactions stored in the journal
0395      * and currently available price information.
0396      */
0397     MyMoneyMoney totalPostedValue() const;
0398 
0399     /**
0400       * This method adjusts the balance of this account
0401       * according to the difference contained in the split @p s.
0402       * If the s.action() is MyMoneySplit::actionName(eMyMoney::Split::Action::SplitShares) then
0403       * the balance will be adjusted accordingly.
0404       *
0405       * @param s const reference to MyMoneySplit object containing the
0406       *             value to be added/subtracted to/from the balance
0407       * @param reverse add (false) or subtract (true) the shares contained in the split.
0408       *          It also affects the balance for share splits in the opposite direction.
0409       */
0410     void adjustBalance(const MyMoneySplit& s, bool reverse = false);
0411 
0412     /**
0413       * This method sets the balance of this account
0414       * according to the value provided by @p val.
0415       *
0416       * @param val const reference to MyMoneyMoney object containing the
0417       *             value to be assigned to the balance
0418       */
0419     void setBalance(const MyMoneyMoney& val);
0420 
0421     /**
0422      * This method sets the total value of this account to @p val. This
0423      * does not include the subaccounts
0424      *
0425      * @param val const reference to MyMoneyMoney object containing the
0426      *             value to be assigned to the total posted value
0427      */
0428     void setPostedValue(const MyMoneyMoney& val);
0429 
0430     /**
0431      * This method sets the total value of this account and all its
0432      * subaccounts to @p val.
0433      *
0434      * @param val const reference to MyMoneyMoney object containing the
0435      *             value to be assigned to the total posted value
0436      */
0437     void setTotalPostedValue(const MyMoneyMoney& val);
0438 
0439     /**
0440       * This method sets the kvp's for online banking with this account
0441       *
0442       * @param values The container of kvp's needed when connecting to this account
0443       */
0444     void setOnlineBankingSettings(const MyMoneyKeyValueContainer& values);
0445 
0446     /**
0447       * This method retrieves the kvp's for online banking with this account
0448       *
0449       * @return The container of kvp's needed when connecting to this account
0450       */
0451     MyMoneyKeyValueContainer onlineBankingSettings() const;
0452 
0453     /**
0454       * This method sets the closed flag for the account. This is just
0455       * an informational flag for the application. It has no other influence
0456       * on the behaviour of the account object. The default for
0457       * new objects @p open.
0458       *
0459       * @param isClosed mark the account closed (@p true) or open (@p false).
0460       */
0461     void setClosed(bool isClosed);
0462 
0463     /**
0464       * Return the closed flag for the account.
0465       *
0466       * @retval false account is marked open (the default for new accounts)
0467       * @retval true account is marked closed
0468       */
0469     bool isClosed() const;
0470 
0471     /**
0472       * returns the applicable smallest fraction for this account
0473       * for the given security based on the account type. At the same
0474       * time, m_fraction is updated to the value returned.
0475       *
0476       * @param sec const reference to currency (security)
0477       *
0478       * @retval sec.smallestCashFraction() for account type Cash
0479       * @retval sec.smallestAccountFraction() for all other account types
0480       */
0481     int fraction(const MyMoneySecurity& sec);
0482 
0483     /**
0484      * Same as the above method, but does not modify m_fraction.
0485      */
0486     int fraction(const MyMoneySecurity& sec) const;
0487 
0488     /**
0489       * This method returns the stored value for the fraction of this
0490       * account or -1 if not initialized. It can be initialized by
0491       * calling fraction(const MyMoneySecurity& sec) once.
0492       *
0493       * @note Don't use this method outside of KMyMoney application context (eg. testcases).
0494       * Use the above method instead.
0495       */
0496     int fraction() const;
0497 
0498     /**
0499       * This method returns @a true if the account type is
0500       * either Income or Expense
0501       *
0502       * @retval true account is of type income or expense
0503       * @retval false for all other account types
0504       *
0505       * @deprecated use isIncomeExpense() instead
0506       */
0507     KMM_MYMONEY_DEPRECATED bool isCategory() const;
0508 
0509     /**
0510       * This method returns @a true if the account type is
0511       * either Income or Expense
0512       *
0513       * @retval true account is of type income or expense
0514       * @retval false for all other account types
0515       */
0516     bool isIncomeExpense() const;
0517 
0518     /**
0519      * Returns @c true if the @a type is either Income or Expense.
0520      * In all other cases it returns @c false.
0521      */
0522     static bool isIncomeExpense(eMyMoney::Account::Type type);
0523 
0524     /**
0525       * This method returns @a true if the account type is
0526       * either Asset or Liability
0527       *
0528       * @retval true account is of type asset or liability
0529       * @retval false for all other account types
0530       */
0531     bool isAssetLiability() const;
0532 
0533     /**
0534       * This method returns @a true if the account type is
0535       * either AssetLoan or Loan
0536       *
0537       * @retval true account is of type Loan or AssetLoan
0538       * @retval false for all other account types
0539       */
0540     bool isLoan() const;
0541 
0542     /**
0543       * This method returns @a true if the account type is
0544       * Stock
0545       *
0546       * @retval true account is of type Stock
0547       * @retval false for all other account types
0548       */
0549     bool isInvest() const;
0550 
0551     /**
0552       * This method returns @a true if the account type is
0553       * Checkings, Savings or Cash
0554       *
0555       * @retval true account is of type Checkings, Savings or Cash
0556       * @retval false for all other account types
0557       */
0558     bool isLiquidAsset() const;
0559 
0560     /**
0561      * Returns whether this account is a liquid liability
0562      *
0563      */
0564     bool isLiquidLiability() const;
0565 
0566     /**
0567      * This method returns true if a costcenter assignment is required for this account
0568      */
0569     bool isCostCenterRequired() const;
0570 
0571     /**
0572      * This method allows to control if a cost center assignment is required
0573      * for this account. It is if @a required is @c true (the default).
0574      */
0575     void setCostCenterRequired(bool required = true);
0576 
0577     /**
0578      * This method returns true if a category is to be included in tax reports
0579      */
0580     bool isInTaxReports() const;
0581 
0582     /**
0583      * This method allows to control if this account (category) is to be included
0584      * in tax reports. It is if @a include is @c true (the default).
0585      */
0586     void setIsInTaxReports(bool include = true);
0587 
0588     /**
0589      * This method returns a name that has a brokerage suffix of
0590      * the current name. It only works on investment accounts and
0591      * returns the name for all other cases.
0592      */
0593     QString brokerageName() const;
0594 
0595     /**
0596      * @return an QIcon for the account.
0597      */
0598     QIcon accountIcon() const;
0599 
0600     /**
0601      * This method is used to convert the internal representation of
0602      * an account type into a human readable format
0603      *
0604      * @param accountType numerical representation of the account type.
0605      *                    For possible values, see eMyMoney::Account::Type
0606      * @return QString representing the human readable form
0607      */
0608     static QString accountTypeToString(const eMyMoney::Account::Type accountType);
0609 
0610     /**
0611       * keeps a history record of a reconciliation for this account on @a date
0612       * with @a amount.
0613       *
0614       * @return @p true in case entry was added, @p false otherwise
0615       *
0616       * @sa reconciliationHistory()
0617       */
0618     bool addReconciliation(const QDate& date, const MyMoneyMoney& amount);
0619 
0620     /**
0621      * Return the information about stored reconciliations of the
0622      * account.
0623      *
0624      * @return QMap with the reconciliation history for the account
0625      *
0626      * @sa addReconciliation()
0627      *
0628      * @note If the internal map is empty, the values will be
0629      * loaded from the KVP with key "reconciliationHistory".
0630      *
0631      * @deprecated Use the const version instead.
0632      */
0633     QMap<QDate, MyMoneyMoney> reconciliationHistory();
0634 
0635     /**
0636      * Return the information about stored reconciliations of the
0637      * account.
0638      *
0639      * @return QMap with the reconciliation history for the account
0640      *
0641      * @sa addReconciliation()
0642      */
0643     QMap<QDate, MyMoneyMoney> reconciliationHistory() const;
0644 
0645     /**
0646       * @return @c true if account has an online mapping, @c false otherwise
0647       */
0648     bool hasOnlineMapping() const;
0649 
0650     eMyMoney::Account::Type budgetAccountType() const;
0651     void setBudgetAccountType(eMyMoney::Account::Type type);
0652 
0653     static QString stdAccName(eMyMoney::Account::Standard stdAccID);
0654     static QString accountSeparator();
0655 
0656     QDataStream &operator<<(const MyMoneyAccount &);
0657     QDataStream &operator>>(MyMoneyAccount &);
0658 };
0659 
0660 inline void swap(MyMoneyAccount& first, MyMoneyAccount& second) // krazy:exclude=inline
0661 {
0662     using std::swap;
0663     swap(first.MyMoneyObject::d_ptr, second.MyMoneyObject::d_ptr);
0664     swap(first.MyMoneyKeyValueContainer::d_ptr, second.MyMoneyKeyValueContainer::d_ptr);
0665 }
0666 
0667 inline MyMoneyAccount::MyMoneyAccount(MyMoneyAccount && other) : MyMoneyAccount() // krazy:exclude=inline
0668 {
0669     swap(*this, other);
0670 }
0671 
0672 inline MyMoneyAccount & MyMoneyAccount::operator=(MyMoneyAccount other) // krazy:exclude=inline
0673 {
0674     swap(*this, other);
0675     return *this;
0676 }
0677 
0678 /**
0679  * Make it possible to hold @ref MyMoneyAccount objects,
0680  * @ref accountTypeE and @ref amountTypeE inside @ref QVariant objects.
0681  */
0682 Q_DECLARE_METATYPE(MyMoneyAccount)
0683 Q_DECLARE_METATYPE(eMyMoney::Account::Type)
0684 
0685 #endif