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

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 SKGIMPORTEXPORTMANAGER_H
0007 #define SKGIMPORTEXPORTMANAGER_H
0008 /** @file
0009  * This file defines classes SKGImportExportManager.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 #include <qmap.h>
0014 #include <qobject.h>
0015 #include <qstringlist.h>
0016 #include <qurl.h>
0017 #include <qdom.h>
0018 
0019 #include "skgaccountobject.h"
0020 #include "skgbankmodeler_export.h"
0021 #include "skgerror.h"
0022 
0023 class SKGDocumentBank;
0024 class SKGAccountObject;
0025 class SKGUnitObject;
0026 class QDate;
0027 class SKGImportPlugin;
0028 
0029 /**
0030  *Manage import and export
0031  */
0032 class SKGBANKMODELER_EXPORT SKGImportExportManager : public QObject
0033 {
0034     Q_OBJECT
0035 public:
0036     /**
0037      * Constructor.
0038      * @param iDocument the document
0039      * @param iFileName the file name
0040      */
0041     explicit SKGImportExportManager(SKGDocumentBank* iDocument = nullptr,
0042                                     QUrl  iFileName = QUrl(QLatin1String("")));
0043 
0044     /**
0045      * Destructor
0046      */
0047     virtual ~SKGImportExportManager() override;
0048 
0049     /**
0050      * Set the codec used for imports.
0051      * @param iCodec the codec name.
0052      */
0053     inline void setCodec(const QString& iCodec)
0054     {
0055         m_codec = iCodec;
0056     }
0057 
0058     /**
0059      * Get the codec used for imports.
0060      * @return code.
0061      */
0062     inline QString getCodec() const
0063     {
0064         return m_codec;
0065     }
0066 
0067     /**
0068      * Set the default account for import in case of account is not detected in imported file.
0069      * @param iAccount the account where to import. nullptr if you want to create a specific account for that.
0070      * @return an object managing the error.
0071      *   @see SKGError
0072      */
0073     SKGError setDefaultAccount(SKGAccountObject* iAccount);
0074 
0075     /**
0076      * Set the default unit for import in case of unit is not detected in imported file.
0077      * @param iUnit the unit where to import. nullptr if you want to create a specific unit for that.
0078      * @return an object managing the error.
0079      *   @see SKGError
0080      */
0081     SKGError setDefaultUnit(SKGUnitObject* iUnit);
0082 
0083     /**
0084      * Enable/disable the automatically validation of imported transactions.
0085      * @param iValidation the mode.
0086      */
0087     void setAutomaticValidation(bool iValidation);
0088 
0089     /**
0090      * get the automatic validation state
0091      * @return The automatic validation state.
0092      */
0093     bool automaticValidation() const;
0094 
0095     /**
0096      * Enable/disable the automatically rules application on imported transactions.
0097      * @param iApply the mode.
0098      */
0099     void setAutomaticApplyRules(bool iApply);
0100 
0101     /**
0102     * get the automatic apply rules state
0103     * @return The automatic apply rules state.
0104     */
0105     bool automaticApplyRules() const;
0106 
0107     /**
0108      * To authorize the import of transactions after the last imported transaction
0109      * @param iSinceLast the mode.
0110      */
0111     void setSinceLastImportDate(bool iSinceLast);
0112 
0113     /**
0114     * get the since last import state
0115     * @return The since last import state.
0116     */
0117     bool sinceLastImportDate() const;
0118 
0119     /**
0120      * Get parameters for Export
0121      * @return the parameters
0122      */
0123     QMap<QString, QString> getExportParameters();
0124 
0125     /**
0126      * Set parameters for Export
0127      * @param iParameters the parameters
0128      */
0129     void setExportParameters(const QMap<QString, QString>& iParameters);
0130 
0131     /**
0132      * Get parameters for Import
0133      * @return the parameters
0134      */
0135     QMap<QString, QString> getImportParameters();
0136 
0137     /**
0138      * Set parameters for Import
0139      * @param iParameters the parameters
0140      */
0141     void setImportParameters(const QMap<QString, QString>& iParameters);
0142 
0143     /**
0144      * Get the default value of a parameter
0145      * @param iParameter the parameter
0146      */
0147     QString getParameterDefaultValue(const QString& iParameter);
0148 
0149     /**
0150      * Get the mime type filter for import
0151      * @param iIncludingAll to include the "All supported format"
0152      * @return the mime type filter
0153      */
0154     QString getImportMimeTypeFilter(bool iIncludingAll = true);
0155 
0156     /**
0157      * Import the file in the document
0158      * @return an object managing the error.
0159      *   @see SKGError
0160      */
0161     SKGError importFile();
0162 
0163     /**
0164      * Get the mime type filter for export
0165      * @param iIncludingAll to include the "All supported format"
0166      * @return the mime type filter
0167      */
0168     QString getExportMimeTypeFilter(bool iIncludingAll = true);
0169 
0170     /**
0171      * Export the file in the document
0172      * @return an object managing the error.
0173      *   @see SKGError
0174      */
0175     SKGError exportFile();
0176 
0177     /**
0178      * Anonymize the document.
0179      * This function must not be launched into a transaction
0180      * @param iKey the key for anonymisation. If "" then the anonymisation is not reversible, else the anonymisation can be undone with the same key.
0181      * @return an object managing the error.
0182      *   @see SKGError
0183      */
0184     SKGError anonymize(const QString& iKey);
0185 
0186     /**
0187      * Find and group transactions
0188      * @param oNbOperationsMerged returns the number of transactions merged.
0189      * @param iAdditionnalCondition a condition on transactions to check (eg. A.t_imported='T' AND B.t_imported='T'")
0190      * @return an object managing the error.
0191      *   @see SKGError
0192      */
0193     SKGError findAndGroupTransfers(int& oNbOperationsMerged, const QString& iAdditionnalCondition);
0194 
0195     /**
0196      * Find and group transactions
0197      * @param oNbOperationsMerged returns the number of transactions merged.
0198      * @param iOnCurrentlyImport to apply the grouping only on currently imported transactions.
0199      * @return an object managing the error.
0200      *   @see SKGError
0201      */
0202     SKGError findAndGroupTransfers(int& oNbOperationsMerged, bool iOnCurrentlyImport = false);
0203 
0204     /**
0205      * Clean transactions after an import coming from bank's web sites
0206      * @return an object managing the error.
0207      *   @see SKGError
0208      */
0209     SKGError cleanBankImport();
0210 
0211     /**
0212      * Get the document
0213      * @return the document.
0214      */
0215     inline SKGDocumentBank* getDocument()
0216     {
0217         return m_document;
0218     }
0219 
0220     /**
0221      * Get the file name extension
0222      * @return the file name.
0223      */
0224     QString getFileNameExtension() const;
0225 
0226     /**
0227      * Get the file name
0228      * @return the file name.
0229      */
0230     QUrl getFileName() const;
0231 
0232     /**
0233      * Get the local file name
0234      * @param iDownload create the local file by downloading the file.
0235      * @return the local file name.
0236      */
0237     QString getLocalFileName(bool iDownload = true);
0238 
0239     /**
0240      * Return the default account for import
0241      * @param oAccount the default account for import.
0242      * @return an object managing the error.
0243      *   @see SKGError
0244      */
0245     SKGError getDefaultAccount(SKGAccountObject& oAccount);
0246 
0247     /**
0248      * Return the preferred unit for a date for import
0249      * @param oUnit the default unit for import.
0250      * @param iDate the date.
0251      * @brief
0252      * If @see setDefaultUnit is used then getDefaultUnit will return this unit.
0253      * else return the unit compatible with entry date and with a value nearest than 1
0254      * else a new unit is created and returned
0255      * @return an object managing the error.
0256      *   @see SKGError
0257      */
0258     SKGError getDefaultUnit(SKGUnitObject& oUnit, const QDate* iDate = nullptr);
0259 
0260     /**
0261      * Finalize an import by changing state and applying rules
0262      *   @see SKGError
0263      */
0264     SKGError finalizeImportation();
0265 
0266     /**
0267      * Add an account to check during finalization
0268      * @param iAccount the account.
0269      * @param iBalance the balance.
0270      */
0271     void addAccountToCheck(const SKGAccountObject& iAccount, double iBalance);
0272 
0273     /**
0274      * get accounts to check
0275      * @return the accounts and balances to check.
0276      */
0277     QList<QPair<SKGAccountObject, double>> getAccountsToCheck();
0278 
0279     /**
0280      * Get the document as an XML object
0281      * @param oDocument The output XML document.
0282      * @return an object managing the error.
0283      *   @see SKGError
0284      */
0285     SKGError getXMLDocument(QDomElement& oDocument);
0286 
0287     /**
0288      * Import a XML document
0289      * @param docElem The document to import.
0290      * @return an object managing the error.
0291      *   @see SKGError
0292      */
0293     SKGError importItems(QDomElement& docElem);
0294 
0295 private:
0296     Q_DISABLE_COPY(SKGImportExportManager)
0297 
0298 
0299     SKGImportPlugin* getImportPlugin();
0300     SKGImportPlugin* getExportPlugin();
0301 
0302     void loadPlugins();
0303     QMap<QString, SKGObjectBase> m_mapIdObject;
0304     SKGDocumentBank* m_document;
0305     QUrl m_fileName;
0306     QString m_localFileName;
0307     SKGAccountObject* m_defaultAccount;
0308     SKGUnitObject* m_defaultUnit;
0309     QString m_codec;
0310     bool m_automaticValidationOfImportedOperation{};
0311     bool m_automaticApplyRulesOfImportedOperation{};
0312     bool m_since_last_import;
0313     SKGImportPlugin* m_importPlugin;
0314     SKGImportPlugin* m_exportPlugin;
0315     QList<QPair<SKGAccountObject, double>> m_AccountToCheck;
0316     QList<SKGImportPlugin*> m_plugins;
0317 };
0318 
0319 #endif