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

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 SKGRULEOBJECT_H
0007 #define SKGRULEOBJECT_H
0008 /** @file
0009  * This file defines classes SKGRuleObject.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 
0014 #include "skgbankmodeler_export.h"
0015 #include "skgdocument.h"
0016 #include "skgerror.h"
0017 #include "skgobjectbase.h"
0018 
0019 /**
0020  * This class allows to define rules
0021  */
0022 class SKGBANKMODELER_EXPORT SKGRuleObject final : public SKGObjectBase
0023 {
0024 public:
0025     /**
0026      * Alarm
0027      */
0028     struct SKGAlarmInfo {
0029         /** To know if the alarm is raised */
0030         bool Raised{};
0031         /** The message to display */
0032         QString Message;
0033         /** The amount in absolute value */
0034         double Amount{};
0035         /** The limit */
0036         double Limit{};
0037     };
0038 
0039     /**
0040      * Process mode
0041      */
0042     enum ProcessMode {ALL,
0043                       NOTCHECKED,
0044                       IMPORTED,
0045                       IMPORTEDNOTVALIDATE,
0046                       IMPORTING
0047                      };
0048     /**
0049      * Process mode
0050      */
0051     Q_ENUM(ProcessMode)
0052 
0053     /**
0054      * Action type
0055      */
0056     enum ActionType {SEARCH,
0057                      UPDATE,
0058                      ALARM,
0059                      APPLYTEMPLATE
0060                     };
0061     /**
0062      * Action type
0063      */
0064     Q_ENUM(ActionType)
0065 
0066     /**
0067      * Default constructor
0068      */
0069     explicit SKGRuleObject();
0070 
0071     /**
0072      * Constructor
0073      * @param iDocument the document containing the object
0074      * @param iID the identifier of the object
0075      */
0076     explicit SKGRuleObject(SKGDocument* iDocument, int iID = 0);
0077 
0078     /**
0079      * Copy constructor
0080      * @param iObject the object to copy
0081      */
0082     SKGRuleObject(const SKGRuleObject& iObject);
0083 
0084     /**
0085      * Copy constructor
0086      * @param iObject the object to copy
0087      */
0088     explicit SKGRuleObject(const SKGObjectBase& iObject);
0089 
0090     /**
0091      * Operator affectation
0092      * @param iObject the object to copy
0093      */
0094     SKGRuleObject& operator= (const SKGObjectBase& iObject);
0095 
0096     /**
0097      * Operator affectation
0098      * @param iObject the object to copy
0099      */
0100     SKGRuleObject& operator= (const SKGRuleObject& iObject);
0101 
0102     /**
0103      * Destructor
0104      */
0105     virtual ~SKGRuleObject();
0106 
0107     /**
0108      * Return the name of the object for the display
0109      * @return name of the object
0110      */
0111     QString getDisplayName() const override;
0112 
0113     /**
0114      * Set the XML for the search definition
0115      * @param iXml the XML search definition
0116      * @code example:
0117      * <!DOCTYPE SKGML>
0118      *   <element> <!--OR-->
0119      *     <element>  <!--AND-->
0120      *       <element attribute="d_date" operator="STRFTIME('%Y',#ATT#)=STRFTIME('%Y',date('now', 'localtime'))" />
0121      *     </element>
0122      *   </element>
0123      * @endcode
0124      * @return an object managing the error
0125      *   @see SKGError
0126      */
0127     SKGError setXMLSearchDefinition(const QString& iXml);
0128 
0129     /**
0130      * Get the XML for the search definition
0131      * @return the XML
0132      */
0133     QString getXMLSearchDefinition() const;
0134 
0135     /**
0136      * Set the XML for the action definition
0137      * @param iXml the XML action definition
0138      * @code example for an update action:
0139      * <!DOCTYPE SKGML>
0140      *   <element> <!--OR-->
0141      *     <element>  <!--AND-->
0142      *       <element attribute="t_number" att2="t_PAYEE" value2="10000000" operator="#ATT#=substr(#ATT2#,'#V1#','#V2#')" att2s="Payee" value="11" />
0143      *     </element>
0144      *   </element>
0145      * @endcode
0146      * @code example for an alarm action:
0147      * <!DOCTYPE SKGML>
0148      *   <element> <!--OR-->
0149      *     <element>  <!--AND-->
0150      *       <element attribute="f_REALCURRENTAMOUNT" operator="ABS(TOTAL(#ATT#))#OP##V1#,ABS(TOTAL(#ATT#)), #V1#, '#V2S#'" value="1000" value2="Take care!"  operator2=">="/>
0151      *     </element>
0152      *   </element>
0153      * @endcode
0154      * @code example to apply a template:
0155      * <!DOCTYPE SKGML>
0156      *   <element> <!--OR-->
0157      *     <element>  <!--AND-->
0158      *       <element attribute="id" operator="APPLYTEMPLATE(#V1#)" value="123" value2="The template name"/>
0159      *     </element>
0160      *   </element>
0161      * @endcode*
0162      * @return an object managing the error
0163      *   @see SKGError
0164      */
0165     SKGError setXMLActionDefinition(const QString& iXml);
0166 
0167     /**
0168      * Get the XML for the action definition
0169      * @return the XML
0170      */
0171     QString getXMLActionDefinition() const;
0172 
0173     /**
0174      * Set the search description
0175      * @param iDescription the search description
0176      * @return an object managing the error
0177      *   @see SKGError
0178      */
0179     SKGError setSearchDescription(const QString& iDescription);
0180 
0181     /**
0182      * Get the search description
0183      * @return the description
0184      */
0185     QString getSearchDescription() const;
0186 
0187     /**
0188      * Set the action description
0189      * @param iDescription the action description
0190      * @return an object managing the error
0191      *   @see SKGError
0192      */
0193     SKGError setActionDescription(const QString& iDescription);
0194 
0195     /**
0196      * Get the action description
0197      * @return the description
0198      */
0199     QString getActionDescription() const;
0200 
0201 
0202     /**
0203     * Set the action type
0204     * @param iType the action type
0205     * @return an object managing the error
0206     *   @see SKGError
0207     */
0208     SKGError setActionType(SKGRuleObject::ActionType iType);
0209 
0210     /**
0211      * Get the action type
0212      * @return the type
0213      */
0214     SKGRuleObject::ActionType getActionType() const;
0215 
0216     /**
0217      * Set the order for the rule
0218      * @param iOrder the order. (-1 means "at the end")
0219      * @return an object managing the error
0220      *   @see SKGError
0221      */
0222     SKGError setOrder(double iOrder);
0223 
0224     /**
0225      * Get the order for the rule
0226      * @return the order
0227      */
0228     double getOrder() const;
0229 
0230     /**
0231      * To bookmark or not an account
0232      * @param iBookmark the bookmark: true or false
0233      * @return an object managing the error
0234      *   @see SKGError
0235      */
0236     SKGError bookmark(bool iBookmark);
0237 
0238     /**
0239      * To know if the account is bookmarked
0240      * @return an object managing the error
0241      *   @see SKGError
0242      */
0243     bool isBookmarked() const;
0244 
0245     /**
0246      * save the object into the database
0247      * @param iInsertOrUpdate the save mode.
0248      *    true: try an insert, if the insert failed then try an update.
0249      *    false: try an insert, if the insert failed then return an error.
0250      * @param iReloadAfterSave to reload the object after save. Set false if you are sure that you will not use this object after save
0251      * @return an object managing the error
0252      *   @see SKGError
0253      */
0254     SKGError save(bool iInsertOrUpdate = true, bool iReloadAfterSave = true) override;
0255 
0256     /**
0257      * Execute actions
0258      * @param iMode mode
0259      * @return an object managing the error
0260      *   @see SKGError
0261      */
0262     SKGError execute(ProcessMode iMode = SKGRuleObject::ALL);
0263 
0264     /**
0265      * Get where clause corresponding to search condition
0266      * @param iAdditionalCondition additional select condition
0267      * @return the where clause
0268      */
0269     QString getSelectSqlOrder(const QString& iAdditionalCondition = QString()) const;
0270 
0271     /**
0272      * Get alarm info
0273      * @return alarm info
0274      */
0275     SKGRuleObject::SKGAlarmInfo getAlarmInfo() const;
0276 
0277     /**
0278      * Create a rule to update a category on transactions having a specific payee
0279      * @param iDocument the document containing the object
0280      * @param iPayee the payee
0281      * @param iCategory the category
0282      * @param oRule the created rule
0283      * @return an object managing the error
0284      *   @see SKGError
0285      */
0286     static SKGError createPayeeCategoryRule(SKGDocument* iDocument, const QString& iPayee, const QString& iCategory, SKGRuleObject& oRule);
0287 
0288     /**
0289      * Get the list of supported operators
0290      * @param iAttributeType type of attribute
0291      * @param  iType mode
0292      * @return list of supported operators
0293      */
0294     static QStringList getListOfOperators(SKGServices::AttributeType iAttributeType, SKGRuleObject::ActionType iType = SEARCH);
0295 
0296     /**
0297      * Get the NLS display of an operator
0298      * @param iOperator the operator (see @see getListOfOperators)
0299      * @param iParam1 parameter
0300      * @param iParam2 parameter
0301      * @param iAtt2 attribute number 2
0302      * @return the NLS display
0303      */
0304     static QString getDisplayForOperator(const QString& iOperator, const QString& iParam1, const QString& iParam2, const QString& iAtt2);
0305 
0306     /**
0307      * Get the NLS tooltip of an operator
0308      * @param iOperator the operator (see @see getListOfOperators)
0309      * @return the NLS display
0310      */
0311     static QString getToolTipForOperator(const QString& iOperator);
0312 
0313 protected:
0314     /**
0315      * Get the description of an XML definition
0316      * @param iDocument the document
0317      * @param iXML the XML
0318      * @param iSQL to define if you want the TXT or SQL description
0319      * @param iType mode
0320      * @return the description
0321      */
0322     static QString getDescriptionFromXML(SKGDocument* iDocument, const QString& iXML, bool iSQL = false, SKGRuleObject::ActionType iType = SEARCH);
0323 
0324 private:
0325     static QStringList getFromXML(SKGDocument* iDocument, const QString& iXML, bool iSQL = false, SKGRuleObject::ActionType iType = SEARCH, bool iFullUpdate = false);
0326 };
0327 /**
0328  * Declare the class
0329  */
0330 Q_DECLARE_TYPEINFO(SKGRuleObject, Q_MOVABLE_TYPE);
0331 #endif