File indexing completed on 2024-04-28 16:29:36

0001 /*
0002     SPDX-FileCopyrightText: 2000-2003 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000-2003 Javier Campos Morales <javi_c@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2000-2003 Felix Rodriguez <frodriguez@users.sourceforge.net>
0005     SPDX-FileCopyrightText: 2000-2003 John C <thetacoturtle@users.sourceforge.net>
0006     SPDX-FileCopyrightText: 2000-2003 Thomas Baumgart <ipwizard@users.sourceforge.net>
0007     SPDX-FileCopyrightText: 2000-2003 Kevin Tambascio <ktambascio@users.sourceforge.net>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef KMYMONEYUTILS_H
0012 #define KMYMONEYUTILS_H
0013 
0014 // ----------------------------------------------------------------------------
0015 // QT Includes
0016 
0017 #include <QStandardPaths>
0018 #include <QMap>
0019 #include <QUrl>
0020 #include <QDebug>
0021 
0022 // ----------------------------------------------------------------------------
0023 // KDE Headers
0024 
0025 // ----------------------------------------------------------------------------
0026 // Project Includes
0027 
0028 #ifndef KMYMONEY_DEPRECATED
0029 #define KMYMONEY_DEPRECATED Q_DECL_DEPRECATED
0030 #endif
0031 
0032 class QIcon;
0033 
0034 /**
0035   * @author Thomas Baumgart
0036   */
0037 
0038 class QPixmap;
0039 class QWizard;
0040 class QWidget;
0041 
0042 class KGuiItem;
0043 class KXmlGuiWindow;
0044 
0045 class MyMoneyMoney;
0046 class MyMoneyAccount;
0047 class MyMoneySecurity;
0048 class MyMoneySchedule;
0049 class MyMoneySplit;
0050 class MyMoneyTransaction;
0051 class MyMoneyStatement;
0052 class MyMoneyInstitution;
0053 class MyMoneyForecast;
0054 
0055 namespace eMyMoney {
0056 namespace Schedule {
0057 enum class Occurrence;
0058 enum class PaymentType;
0059 enum class WeekendOption;
0060 enum class Type;
0061 }
0062 namespace Split {
0063 enum class State;
0064 enum class InvestmentTransactionType;
0065 }
0066 }
0067 
0068 class KMyMoneyUtils
0069 {
0070 public:
0071     enum transactionTypeE {
0072         /**
0073           * Unknown transaction type (e.g. used for a transaction with only
0074           * a single split)
0075           */
0076         Unknown,
0077 
0078         /**
0079           * A 'normal' transaction is one that consists out two splits: one
0080           * referencing an income/expense account, the other referencing
0081           * an asset/liability account.
0082           */
0083         Normal,
0084 
0085         /**
0086           * A transfer denotes a transaction consisting of two splits.
0087           * Both of the splits reference an asset/liability
0088           * account.
0089           */
0090         Transfer,
0091 
0092         /**
0093           * Whenever a transaction consists of more than 2 splits,
0094           * it is treated as 'split transaction'.
0095           */
0096         SplitTransaction,
0097 
0098         /**
0099           * This transaction denotes a specific transaction where
0100           * a loan account is involved. Usually, a special dialog
0101           * is used to modify this transaction.
0102           */
0103         LoanPayment,
0104 
0105         /**
0106           * This transaction denotes a specific transaction where
0107           * an investment is involved. Usually, a special dialog
0108           * is used to modify this transaction.
0109           */
0110         InvestmentTransaction,
0111     };
0112 
0113     static const int maxHomePageItems = 5;
0114 
0115     KMyMoneyUtils();
0116     ~KMyMoneyUtils();
0117 
0118     /**
0119       * This method is used to convert the occurrence type from its
0120       * internal representation into a human readable format.
0121       *
0122       * @param occurrence numerical representation of the MyMoneySchedule
0123       *                  occurrence type
0124       *
0125       * @return QString representing the human readable format translated according to the language catalog
0126       *
0127       * @sa MyMoneySchedule::occurrenceToString()
0128       *
0129       * @deprecated Use i18n(MyMoneySchedule::occurrenceToString(occurrence)) instead
0130       */
0131     static const QString occurrenceToString(const eMyMoney::Schedule::Occurrence occurrence);
0132 
0133     /**
0134       * This method is used to convert the payment type from its
0135       * internal representation into a human readable format.
0136       *
0137       * @param paymentType numerical representation of the MyMoneySchedule
0138       *                  payment type
0139       *
0140       * @return QString representing the human readable format translated according to the language catalog
0141       *
0142       * @sa MyMoneySchedule::paymentMethodToString()
0143       */
0144     static const QString paymentMethodToString(eMyMoney::Schedule::PaymentType paymentType);
0145 
0146     /**
0147       * This method is used to convert the schedule weekend option from its
0148       * internal representation into a human readable format.
0149       *
0150       * @param weekendOption numerical representation of the MyMoneySchedule
0151       *                  weekend option
0152       *
0153       * @return QString representing the human readable format translated according to the language catalog
0154       *
0155       * @sa MyMoneySchedule::weekendOptionToString()
0156       */
0157     static const QString weekendOptionToString(eMyMoney::Schedule::WeekendOption weekendOption);
0158 
0159     /**
0160       * This method is used to convert the schedule type from its
0161       * internal representation into a human readable format.
0162       *
0163       * @param type numerical representation of the MyMoneySchedule
0164       *                  schedule type
0165       *
0166       * @return QString representing the human readable format translated according to the language catalog
0167       *
0168       * @sa MyMoneySchedule::scheduleTypeToString()
0169       */
0170     static const QString scheduleTypeToString(eMyMoney::Schedule::Type type);
0171 
0172     /**
0173       * This method is used to convert a numeric index of an item
0174       * represented on the home page into its string form.
0175       *
0176       * @param idx numeric index of item
0177       *
0178       * @return QString with text of this item
0179       */
0180     static const QString homePageItemToString(const int idx);
0181 
0182     /**
0183       * This method is used to convert the name of a home page item
0184       * to its internal numerical representation
0185       *
0186       * @param txt QString reference of the items name
0187       *
0188       * @retval 0 @p txt is unknown
0189       * @retval >0 numeric value for @p txt
0190       */
0191     static int stringToHomePageItem(const QString& txt);
0192 
0193     /**
0194       * Retrieve a KDE KGuiItem for the new schedule button.
0195       *
0196       * @return The KGuiItem that can be used to display the icon and text
0197       */
0198     static KGuiItem scheduleNewGuiItem();
0199 
0200     /**
0201       * Retrieve a KDE KGuiItem for the account filter button
0202       *
0203       * @return The KGuiItem that can be used to display the icon and text
0204       */
0205     static KGuiItem accountsFilterGuiItem();
0206 
0207     /**
0208       * This method adds the file extension passed as argument @p extension
0209       * to the end of the file name passed as argument @p name if it is not present.
0210       * If @p name contains an extension it will be removed.
0211       *
0212       * @param name filename to be checked
0213       * @param extension extension to be added (w/o the dot)
0214       *
0215       * @retval true if @p name was changed
0216       * @retval false if @p name remained unchanged
0217       */
0218     static bool appendCorrectFileExt(QString& name, const QString& extension);
0219 
0220     /**
0221       * Check that internal MyMoney engine constants use the same
0222       * values as the KDE constants.
0223       */
0224     static void checkConstants();
0225 
0226     static QString variableCSS();
0227 
0228     /**
0229       * This method searches a KDE specific resource and applies country and
0230       * language settings during the search. Therefore, the parameter @p filename must contain
0231       * the characters '%1' which gets replaced with the language/country values.
0232       *
0233       * The search is performed in the following order (stopped immediately if a file was found):
0234       * - @c \%1 is replaced with <tt>_\<country\>.\<language\></tt>
0235       * - @c \%1 is replaced with <tt>_\<language\></tt>
0236       * - @c \%1 is replaced with <tt>_\<country\></tt>
0237       * - @c \%1 is replaced with the empty string
0238       *
0239       * @c \<country\> and @c \<language\> denote the respective KDE settings.
0240       *
0241       * Example: The KDE settings for country is Spain (es) and language is set
0242       * to Galician (gl). The code for looking up a file looks like this:
0243       *
0244       * @code
0245       *
0246       *  :
0247       *  QString fname = KMyMoneyUtils::findResource("appdata", "html/home%1.html")
0248       *  :
0249       *
0250       * @endcode
0251       *
0252       * The method calls KStandardDirs::findResource() with the following values for the
0253       * parameter @p filename:
0254       *
0255       * - <tt>html/home_es.gl.html</tt>
0256       * - <tt>html/home_gl.html</tt>
0257       * - <tt>html/home_es.html</tt>
0258       * - <tt>html/home.html</tt>
0259       *
0260       * @note See KStandardDirs::findResource() for details on the parameters
0261       */
0262     static QString findResource(QStandardPaths::StandardLocation type, const QString& filename);
0263 
0264     /**
0265       * This method returns the split referencing a stock account if
0266       * one exists in the transaction passed as @p t. If none is present
0267       * in @p t, an empty MyMoneySplit() object will be returned.
0268       *
0269       * @param t transaction to be checked for a stock account
0270       * @return MyMoneySplit object referencing a stock account or an
0271       *         empty MyMoneySplit object.
0272       */
0273     static const MyMoneySplit stockSplit(const MyMoneyTransaction& t);
0274 
0275     /**
0276       * This method analyses the splits of a transaction and returns
0277       * the type of transaction. Possible values are defined by the
0278       * KMyMoneyUtils::transactionTypeE enum.
0279       *
0280       * @param t const reference to the transaction
0281       *
0282       * @return KMyMoneyUtils::transactionTypeE value of the action
0283       */
0284     static transactionTypeE transactionType(const MyMoneyTransaction& t);
0285 
0286     /**
0287       * This method modifies a scheduled loan transaction such that all
0288       * references to automatic calculated values are resolved to actual values.
0289       *
0290       * @param schedule const reference to the schedule the transaction is based on
0291       * @param transaction reference to the transaction to be checked and modified
0292       * @param balances QMap of (account-id,balance) pairs to be used as current balance
0293       *                 for the calculation of interest. If map is empty, the engine
0294       *                 will be interrogated for current balances.
0295       */
0296     static void calculateAutoLoan(const MyMoneySchedule& schedule, MyMoneyTransaction& transaction, const QMap<QString, MyMoneyMoney>& balances);
0297 
0298     /**
0299       * Returns the next check number for account @a acc. No check is performed, if the
0300       * number is already in use.
0301       */
0302     static QString nextCheckNumber(const MyMoneyAccount& acc);
0303 
0304     /**
0305       * Returns the next check free number for account @a acc.
0306       */
0307     static QString nextFreeCheckNumber(const MyMoneyAccount& acc);
0308 
0309 
0310     // static void setLastNumberUsed(const QString& num);
0311 
0312     // static QString lastNumberUsed();
0313 
0314     /**
0315       * Returns previous number if offset is -1 or
0316       * the following number if offset is 1.
0317       */
0318     static QString getAdjacentNumber(const QString& number, int offset = 1);
0319 
0320 
0321     /**
0322     * remove any non-numeric characters from check number
0323     * to allow validity check
0324     */
0325     static quint64 numericPart(const QString & num);
0326 
0327     /**
0328       * Returns the text representing the reconcile flag. If @a text is @p true
0329       * then the full text will be returned otherwise a short form (usually one character).
0330       */
0331     static QString reconcileStateToString(eMyMoney::Split::State flag, bool text = false);
0332 
0333     /**
0334      * Returns the transaction for @a schedule. In case of a loan payment the
0335      * transaction will be modified by calculateAutoLoan().
0336      * The ID of the transaction as well as the entryDate will be reset.
0337      *
0338      * @returns adjusted transaction
0339      */
0340     static MyMoneyTransaction scheduledTransaction(const MyMoneySchedule& schedule);
0341 
0342     /**
0343      * This method replaces the deprecated QApplication::mainWidget() from Qt 3.x.
0344      * It assumes that there is only one KXmlGuiWindow in the application, and
0345      * returns it.
0346      *
0347      * @return the first KXmlGuiWindow found in QApplication::topLevelWidgets()
0348      */
0349     static KXmlGuiWindow* mainWindow();
0350 
0351     /**
0352       * This method sets the button text and icons to the KDE standard ones
0353       * for the QWizard passed as argument.
0354       */
0355     static void updateWizardButtons(QWizard *);
0356 
0357     static void dissectTransaction(const MyMoneyTransaction& transaction, const MyMoneySplit& split, MyMoneySplit& assetAccountSplit, QList<MyMoneySplit>& feeSplits, QList<MyMoneySplit>& interestSplits, MyMoneySecurity& security, MyMoneySecurity& currency, eMyMoney::Split::InvestmentTransactionType& transactionType);
0358 
0359     static void processPriceList(const MyMoneyStatement& st);
0360 
0361     /**
0362       * This method deletes security and associated price list but asks beforehand.
0363       */
0364     static void deleteSecurity(const MyMoneySecurity &security, QWidget *parent = nullptr);
0365 
0366     /**
0367      * Check whether the url links to an existing file or not
0368      * @returns whether the file exists or not
0369      */
0370     static bool fileExists(const QUrl &url);
0371 
0372     static QString downloadFile(const QUrl &url);
0373 
0374     static bool newPayee(const QString& newnameBase, QString& id);
0375 
0376     static void newTag(const QString& newnameBase, QString& id);
0377 
0378     /**
0379       * Creates a new institution entry in the MyMoneyFile engine
0380       *
0381       * @param institution MyMoneyInstitution object containing the data of
0382       *                    the institution to be created.
0383       */
0384     static void newInstitution(MyMoneyInstitution& institution);
0385 
0386     static QDebug debug();
0387 
0388     static MyMoneyForecast forecast();
0389 
0390     static bool canUpdateAllAccounts();
0391 
0392     static void showStatementImportResult(const QStringList& resultMessages, uint statementCount);
0393 
0394     /**
0395       * This method returns a double converted into a QString
0396       * after removing any group separators, trailing zeros,
0397       * and decimal separators.
0398       *
0399       * @param val reference to a qreal value to be converted
0400       * @param loc reference to a Qlocale converter
0401       * @param f format specifier:
0402       *          e - format as [-]9.9e[+|-]999
0403       *          E - format as [-]9.9E[+|-]999
0404       *          f - format as [-]9.9
0405       *          g - use e or f format, whichever is the most concise
0406       *          G - use E or f format, whichever is the most concise
0407       * @param prec precision representing the number of digits
0408       *             after the decimal point ('e', 'E' and 'f' formats)
0409       *             or the maximum number of significant digits
0410       *             (trailing zeroes are omitted) ('g' and 'G' formats)
0411       * @return QString object containing the converted value
0412       */
0413     static QString normalizeNumericString(const qreal& val, const QLocale& loc, const char f = 'g', const int prec = 6);
0414 };
0415 
0416 #endif