File indexing completed on 2024-04-28 16:30:15

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 #ifndef SKGUNITOBJECT_H
0007 #define SKGUNITOBJECT_H
0008 /** @file
0009  * This file defines classes SKGUnitObject.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 #include <qurl.h>
0014 
0015 #include "skgbankmodeler_export.h"
0016 #include "skgerror.h"
0017 #include "skgnamedobject.h"
0018 #include "skgservices.h"
0019 
0020 class SKGUnitValueObject;
0021 class SKGDocumentBank;
0022 
0023 /**
0024  * This class is a unit
0025  */
0026 class SKGBANKMODELER_EXPORT SKGUnitObject final : public SKGNamedObject
0027 {
0028 public:
0029     /**
0030      * This enumerate defines type of units
0031      */
0032     enum UnitType {PRIMARY,     /**< to define the main currency (only one)*/
0033                    SECONDARY,   /**< to define the secondary currency (only one)*/
0034                    CURRENCY,    /**< for currency: dollar, euro, yen, … */
0035                    SHARE,       /**< for stock: Google, EDF, … */
0036                    INDEX,       /**< for index: CAC 40, … */
0037                    OBJECT       /**< for objects: cars, houses, … */
0038                   };
0039     /**
0040      * This enumerate defines type of units
0041      */
0042     Q_ENUM(UnitType)
0043 
0044     /**
0045      * This enumerate defines the mode of download
0046      */
0047     enum UnitDownloadMode {LAST,        /**< last value only*/
0048                            LAST_MONTHLY,    /**< one value par month since last download*/
0049                            LAST_WEEKLY,     /**< one value par week since last download*/
0050                            LAST_DAILY,      /**< one value par day since last download*/
0051                            ALL_MONTHLY,     /**< one value par month*/
0052                            ALL_WEEKLY,      /**< one value par week*/
0053                            ALL_DAILY        /**< one value par day*/
0054                           };
0055     /**
0056      * This enumerate defines the mode of download
0057      */
0058     Q_ENUM(UnitDownloadMode)
0059 
0060     /**
0061     * Default constructor
0062     */
0063     explicit SKGUnitObject();
0064 
0065     /**
0066     * Constructor
0067     * @param iDocument the document containing the object
0068     * @param iID the identifier in @p iTable of the object
0069     */
0070     explicit SKGUnitObject(SKGDocument* iDocument, int iID = 0);
0071 
0072     /**
0073      * Destructor
0074      */
0075     virtual ~SKGUnitObject();
0076 
0077     /**
0078      * Copy constructor
0079      * @param iObject the object to copy
0080      */
0081     SKGUnitObject(const SKGUnitObject& iObject);
0082 
0083     /**
0084      * Copy constructor
0085      * @param iObject the object to copy
0086      */
0087     explicit SKGUnitObject(const SKGNamedObject& iObject);
0088 
0089     /**
0090      * Copy constructor
0091      * @param iObject the object to copy
0092      */
0093     explicit SKGUnitObject(const SKGObjectBase& iObject);
0094 
0095     /**
0096      * Operator affectation
0097      * @param iObject the object to copy
0098      */
0099     SKGUnitObject& operator= (const SKGObjectBase& iObject);
0100 
0101     /**
0102      * Operator affectation
0103      * @param iObject the object to copy
0104      */
0105     SKGUnitObject& operator= (const SKGUnitObject& iObject);
0106 
0107     /**
0108      * Get unit information
0109      * @return the unit information
0110      *   @see SKGUnitInfo
0111      */
0112     SKGServices::SKGUnitInfo getUnitInfo();
0113 
0114     /**
0115      * Get unit information
0116      * @param iUnitName the currency unit name (nls), or the international code, or the symbol (@see getListofKnownCurrencies)
0117      * @return the unit information
0118      *   @see SKGUnitInfo
0119      */
0120     static SKGServices::SKGUnitInfo getUnitInfo(const QString& iUnitName);
0121 
0122     /**
0123      * Create an existing currency unit
0124      * @param iDocument the document containing the object
0125      * @param iUnitName the currency unit name (nls), or the international code, or the symbol (@see getListofKnownCurrencies)
0126      * @param oUnit the created unit object
0127      * @return an object managing the error
0128      *   @see SKGError
0129      */
0130     static SKGError createCurrencyUnit(SKGDocumentBank* iDocument, const QString& iUnitName, SKGUnitObject& oUnit);
0131 
0132     /**
0133      * Return the list of known currencies.
0134      * @param iIncludingObsolete including obsolete currencies
0135      * @return the list of known currencies
0136      */
0137     static QStringList getListofKnownCurrencies(bool iIncludingObsolete = false);
0138 
0139     /**
0140      * Return the International code of currencies.
0141      * @param iUnitName the currency unit name (nls)
0142      * @return the International code
0143      */
0144     static QString getInternationalCode(const QString& iUnitName);
0145 
0146     /**
0147      * Convert a value from one unit to another one
0148      * @param iValue initial value
0149      * @param iUnitFrom source unit
0150      * @param iUnitTo target unit
0151      * @param iDate the date to use to compute the units values
0152      * @return Value in target unit
0153      */
0154     // cppcheck-suppress passedByValue
0155     static double convert(double iValue, const SKGUnitObject& iUnitFrom, const SKGUnitObject& iUnitTo, QDate iDate = QDate::currentDate());
0156 
0157     /**
0158      * Set the symbol of this unit
0159      * @param iSymbol the symbol
0160      * @return an object managing the error
0161      *   @see SKGError
0162      */
0163     SKGError setSymbol(const QString& iSymbol);
0164 
0165     /**
0166      * Get the symbol of this unit
0167      * @return the symbol
0168      */
0169     QString getSymbol() const;
0170 
0171     /**
0172      * Set the download source of this unit
0173      * @param iSource the download source ("" means native source)
0174      * @return an object managing the error
0175      *   @see SKGError
0176      */
0177     SKGError setDownloadSource(const QString& iSource);
0178 
0179     /**
0180      * Get the download source of this unit
0181      * @return the download source ("" means native source)
0182      */
0183     QString getDownloadSource() const;
0184 
0185     /**
0186      * Set the number of decimal of this unit
0187      * @param iNb number of decimal
0188      * @return an object managing the error
0189      *   @see SKGError
0190      */
0191     SKGError setNumberDecimal(int iNb);
0192 
0193     /**
0194      * Get the number of decimal of this unit
0195      * @return the number of decimal
0196      */
0197     int getNumberDecimal() const;
0198 
0199     /**
0200      * Set the country of this unit
0201      * @param iCountry the country
0202      * @return an object managing the error
0203      *   @see SKGError
0204      */
0205     SKGError setCountry(const QString& iCountry);
0206 
0207     /**
0208      * Get the country of this unit
0209      * @return the country
0210      */
0211     QString getCountry() const;
0212 
0213     /**
0214      * Set the Internet code of this unit
0215      * @param iCode the Internet code
0216      * @return an object managing the error
0217      *   @see SKGError
0218      */
0219     SKGError setInternetCode(const QString& iCode);
0220 
0221     /**
0222      * Get the Internet code of this unit
0223      * @return the Internet code
0224      */
0225     QString getInternetCode() const;
0226 
0227     /**
0228      * Set the type of this unit
0229      * @param iType the type
0230      * @return an object managing the error
0231      *   @see SKGError
0232      */
0233     SKGError setType(SKGUnitObject::UnitType iType);
0234 
0235     /**
0236      * Get the type of this unit
0237      * @return the type
0238      */
0239     SKGUnitObject::UnitType getType() const;
0240 
0241     /**
0242      * Add a unit value
0243      * @param oUnitValue the created unit value
0244      * @return an object managing the error.
0245      *   @see SKGError
0246      */
0247     SKGError addUnitValue(SKGUnitValueObject& oUnitValue);
0248 
0249     /**
0250      * Get unit values
0251      * @param oUnitValueList the list of unit values of this unit
0252      * @return an object managing the error
0253      *   @see SKGError
0254      */
0255     SKGError getUnitValues(SKGListSKGObjectBase& oUnitValueList) const;
0256 
0257     /**
0258      * Get last unit value
0259      * @param oUnitValue the last unit value
0260      * @return an object managing the error.
0261      *   @see SKGError
0262      */
0263     SKGError getLastUnitValue(SKGUnitValueObject& oUnitValue) const;
0264 
0265     /**
0266      * Get unit value at a date
0267      * @param iDate the date
0268      * @param oUnitValue the last unit value
0269      * @return an object managing the error.
0270      *   @see SKGError
0271      */
0272     // cppcheck-suppress passedByValue
0273     SKGError getUnitValue(QDate iDate, SKGUnitValueObject& oUnitValue) const;
0274 
0275     /**
0276      * Download values
0277      * @param iMode download mode (used only for native source)
0278      * @param iNbMaxValues nb max of values to download (used only for native source)
0279      * @return an object managing the error
0280      *   @see SKGError
0281      */
0282     SKGError downloadUnitValue(UnitDownloadMode iMode = LAST, int iNbMaxValues = 50);
0283 
0284 
0285     /**
0286      * Open the URL where the values are downloaded
0287      * @return an object managing the error
0288      *   @see SKGError
0289      */
0290     SKGError openURL() const;
0291 
0292     /**
0293      * Get the URL where the values are downloaded
0294      * @param oUrl the url
0295      * @return an object managing the error
0296      *   @see SKGError
0297      */
0298     SKGError getUrl(QUrl& oUrl) const;
0299 
0300     /**
0301      * Get the list of sources for the download
0302      * @return the list of sources
0303      */
0304     static QStringList downloadSources();
0305 
0306     /**
0307      * Get the source comment/help
0308      * @param iSource the source name (@see  downloadSources)
0309      * @return the comment/help
0310      */
0311     static QString getCommentFromSource(const QString& iSource);
0312 
0313     /**
0314      * Create a new source
0315      * @param iNewSource the name of the new source
0316      * @param iOpenSource to open the created source with the appropriate editor
0317      * @return an object managing the error
0318      *   @see SKGError
0319      */
0320     static SKGError addSource(const QString& iNewSource, bool iOpenSource = true);
0321 
0322     /**
0323      * To know if a source can be modified or deleted
0324      * @param iSource the name of the source to modified or deleted
0325      * @return true or false
0326      */
0327     static bool isWritable(const QString& iSource);
0328 
0329     /**
0330      * Delete a new source
0331      * @param iSource the name of the source to delete
0332      * @return an object managing the error
0333      *   @see SKGError
0334      */
0335     static SKGError deleteSource(const QString& iSource);
0336 
0337     /**
0338      * Get amount of the unit at a date
0339      * @param iDate date
0340      * @return amount of the unit
0341      */
0342     // cppcheck-suppress passedByValue
0343     double getAmount(QDate iDate = QDate::currentDate()) const;
0344 
0345     /**
0346      * Get daily change in percentage at a date.
0347      * @param iDate date
0348      * @return daily change
0349      */
0350     // cppcheck-suppress passedByValue
0351     double getDailyChange(QDate iDate = QDate::currentDate()) const;
0352 
0353     /**
0354      * Set the unit
0355      * @param iUnit the unit
0356      * @return an object managing the error
0357      *   @see SKGError
0358      */
0359     SKGError setUnit(const SKGUnitObject& iUnit);
0360 
0361     /**
0362      * Remove the unit
0363      * @return an object managing the error
0364      *   @see SKGError
0365      */
0366     SKGError removeUnit();
0367 
0368     /**
0369      * Get the unit
0370      * @param oUnit the unit
0371      * @return an object managing the error
0372      *   @see SKGError
0373      */
0374     SKGError getUnit(SKGUnitObject& oUnit) const;
0375 
0376     /**
0377      * Split unit.
0378      * All quantity in transactions will be multiply by iRatio.
0379      * All unit values will be divise by iRatio.
0380      * @param iRatio the split ratio
0381      * @return an object managing the error
0382      *   @see SKGError
0383      */
0384     SKGError split(double iRatio) const;
0385 
0386     /**
0387      * Get all transactions of this unit
0388      * @param oOperations all transactions of this unit
0389      * @return an object managing the error
0390      *   @see SKGError
0391      */
0392     SKGError getOperations(SKGListSKGObjectBase& oOperations) const;
0393 
0394     /**
0395      * Merge iUnit in current unit
0396      * @param iUnit the unit. All transactions will be transferred into this unit. The unit will be removed
0397      * @return an object managing the error
0398      *   @see SKGError
0399      */
0400     SKGError merge(const SKGUnitObject& iUnit);
0401 
0402     /**
0403      * Remove all unit values not relevant.
0404      * The curve is preserved but useless values are removed.
0405      * @return an object managing the error.
0406      *   @see SKGError
0407      */
0408     SKGError simplify();
0409 
0410 protected:
0411     /**
0412      * Get where clause needed to identify objects.
0413      * For this class, the whereclause is based on name
0414      * @return the where clause
0415      */
0416     QString getWhereclauseId() const override;
0417 
0418 private:
0419     static QList<SKGServices::SKGUnitInfo> currencies;
0420 };
0421 /**
0422  * Declare the class
0423  */
0424 Q_DECLARE_TYPEINFO(SKGUnitObject, Q_MOVABLE_TYPE);
0425 #endif