File indexing completed on 2024-05-12 16:42:33

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