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

0001 /*
0002     SPDX-FileCopyrightText: 2007-2010 Alvaro Soliverez <asoliverez@gmail.com>
0003     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef MYMONEYFORECAST_H
0008 #define MYMONEYFORECAST_H
0009 
0010 #include "kmm_mymoney_export.h"
0011 
0012 // ----------------------------------------------------------------------------
0013 // QT Includes
0014 
0015 #include <QMetaType>
0016 
0017 // ----------------------------------------------------------------------------
0018 // Project Includes
0019 
0020 #include "mymoneyunittestable.h"
0021 
0022 class QDate;
0023 
0024 class MyMoneyMoney;
0025 class MyMoneyAccount;
0026 class MyMoneyBudget;
0027 class MyMoneySchedule;
0028 class MyMoneyTransaction;
0029 
0030 template <class T> class QList;
0031 template <class T1, class T2> class QMap;
0032 
0033 /**
0034   *
0035   *
0036   * @author Alvaro Soliverez <asoliverez@gmail.com>
0037   */
0038 class MyMoneyForecastPrivate;
0039 class KMM_MYMONEY_EXPORT MyMoneyForecast
0040 {
0041     Q_DECLARE_PRIVATE(MyMoneyForecast)
0042     MyMoneyForecastPrivate * d_ptr;
0043 
0044     KMM_MYMONEY_UNIT_TESTABLE
0045 
0046 public:
0047     /**
0048       * The default forecast ctor sets the following defaults:
0049       *
0050       * - forecastCycles = 3
0051       * - accountsCycle = 30
0052       * - forecastDays = 90
0053       * - beginForecastDay = 0 (today)
0054       * - forecastMethod = 0 (scheduled)
0055       * - historyMethod = 1
0056       * - includeFutureTransactions = true
0057       * - includeScheduledTransactions = true
0058       */
0059     MyMoneyForecast();
0060     MyMoneyForecast(const MyMoneyForecast & other);
0061     MyMoneyForecast(MyMoneyForecast && other);
0062     MyMoneyForecast & operator=(MyMoneyForecast other);
0063     friend void swap(MyMoneyForecast& first, MyMoneyForecast& second);
0064     virtual ~MyMoneyForecast();
0065 
0066     /**
0067      * calculate forecast based on historic transactions
0068      */
0069     void doForecast();
0070 
0071     /**
0072      * Returns the list of accounts to be forecast.
0073      */
0074     QList<MyMoneyAccount> accountList();
0075 
0076     /**
0077      * Returns the balance trend for account @a acc based on a number of days @p forecastDays
0078      * Collects and processes all transactions in the past for the
0079      * same period of forecast and calculates the balance trend
0080      */
0081     static MyMoneyMoney calculateAccountTrend(const MyMoneyAccount& acc, qint64 forecastDays);
0082 
0083     /**
0084      * Returns the forecast balance trend for account @a acc for day @p QDate
0085      */
0086     MyMoneyMoney forecastBalance(const MyMoneyAccount& acc, const QDate &forecastDate);
0087 
0088     /**
0089      * Returns the forecast balance trend for account @a acc for offset @p int
0090      * offset is days from current date, inside forecast days.
0091      * Returns 0 if offset not in range of forecast days.
0092      */
0093     MyMoneyMoney forecastBalance(const MyMoneyAccount& acc, qint64 offset);
0094 
0095     /**
0096      * Returns true if an account @a acc is an account to be forecast
0097      */
0098     bool isForecastAccount(const MyMoneyAccount& acc);
0099 
0100     /**
0101      * returns the number of days when a given account is forecast to be below minimum balance
0102      * returns -1 if it will not be below minimum balance in the forecast period
0103      */
0104     qint64 daysToMinimumBalance(const MyMoneyAccount& acc);
0105 
0106     /**
0107      * returns the number of days when a given account is forecast to be below zero if it is an asset accounts
0108      * or above zero if it is a liability account
0109      * returns -1 if it will not happen in the forecast period
0110      */
0111     qint64 daysToZeroBalance(const MyMoneyAccount& acc);
0112 
0113     /**
0114      * amount of variation of a given account in one cycle
0115      */
0116     MyMoneyMoney accountCycleVariation(const MyMoneyAccount& acc);
0117 
0118     /**
0119      * amount of variation of a given account for the whole forecast period
0120      */
0121     MyMoneyMoney accountTotalVariation(const MyMoneyAccount& acc);
0122 
0123     /**
0124      * returns a list of the dates where the account was on its lowest balance in each cycle
0125      */
0126     QList<QDate> accountMinimumBalanceDateList(const MyMoneyAccount& acc);
0127 
0128     /**
0129      * returns a list of the dates where the account was on its highest balance in each cycle
0130      */
0131     QList<QDate> accountMaximumBalanceDateList(const MyMoneyAccount& acc);
0132 
0133     /**
0134      * returns the average balance of the account within the forecast period
0135      */
0136     MyMoneyMoney accountAverageBalance(const MyMoneyAccount& acc);
0137 
0138     /**
0139      * creates a budget based on the history of a given timeframe
0140      */
0141     void createBudget(MyMoneyBudget& budget, QDate historyStart, QDate historyEnd, QDate budgetStart, QDate budgetEnd, const bool returnBudget);
0142 
0143     /**
0144      * number of days to go back in history to calculate forecast
0145      */
0146     qint64 historyDays() const;
0147 
0148     void setAccountsCycle(qint64 accountsCycle);
0149     void setForecastCycles(qint64 forecastCycles);
0150     void setForecastDays(qint64 forecastDays);
0151     void setBeginForecastDate(const QDate &beginForecastDate);
0152     void setBeginForecastDay(qint64 beginDay);
0153     void setForecastMethod(qint64 forecastMethod);
0154     void setHistoryStartDate(const QDate &historyStartDate);
0155     void setHistoryEndDate(const QDate &historyEndDate);
0156     void setHistoryStartDate(qint64 daysToStartDate);
0157     void setHistoryEndDate(qint64 daysToEndDate);
0158     void setForecastStartDate(const QDate &_startDate);
0159     void setForecastEndDate(const QDate &_endDate);
0160     void setSkipOpeningDate(bool _skip);
0161     void setHistoryMethod(int historyMethod);
0162     void setIncludeUnusedAccounts(bool _bool);
0163     void setForecastDone(bool _bool);
0164     void setIncludeFutureTransactions(bool _bool);
0165     void setIncludeScheduledTransactions(bool _bool);
0166     qint64 accountsCycle() const;
0167     qint64 forecastCycles() const;
0168     qint64 forecastDays() const;
0169     QDate beginForecastDate() const;
0170     qint64 beginForecastDay() const;
0171     QDate historyStartDate() const;
0172     QDate historyEndDate() const;
0173     QDate forecastStartDate() const;
0174     QDate forecastEndDate() const;
0175     bool skipOpeningDate() const;
0176     int historyMethod() const;
0177     bool isIncludingUnusedAccounts() const;
0178     bool isForecastDone() const;
0179     bool isIncludingFutureTransactions() const;
0180     bool isIncludingScheduledTransactions() const;
0181 
0182     /**
0183       * This method modifies a scheduled loan transaction such that all
0184       * references to automatic calculated values are resolved to actual values.
0185       *
0186       * @param schedule const reference to the schedule the transaction is based on
0187       * @param transaction reference to the transaction to be checked and modified
0188       * @param balances QMap of (account-id,balance) pairs to be used as current balance
0189       *                 for the calculation of interest. If map is empty, the engine
0190       *                 will be interrogated for current balances.
0191       */
0192     static void calculateAutoLoan(const MyMoneySchedule& schedule, MyMoneyTransaction& transaction, const QMap<QString, MyMoneyMoney>& balances);
0193 
0194     /**
0195      * Returns the list of accounts to be forecast. Only Asset and Liability are returned.
0196      */
0197     static QList<MyMoneyAccount> forecastAccountList();
0198 };
0199 
0200 /**
0201   * Make it possible to hold @ref MyMoneyForecast objects inside @ref QVariant objects.
0202   */
0203 Q_DECLARE_METATYPE(MyMoneyForecast)
0204 
0205 #endif // MYMONEYFORECAST_H
0206