1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2021 S. MANKOWSKI stephane@mankowski.fr
3  * SPDX-FileCopyrightText: 2021 G. DE BURE support@mankowski.fr
4  * SPDX-License-Identifier: GPL-3.0-or-later
5  ***************************************************************************/
6 #ifndef SKGDOCUMENTBANK_H
7 #define SKGDOCUMENTBANK_H
8 /** @file
9  * This file defines classes SKGDocumentBank.
10  *
11  * @author Stephane MANKOWSKI / Guillaume DE BURE
12  */
13 
14 #include "skgbankmodeler_export.h"
15 #include "skgdefinebank.h"
16 #include "skgdocument.h"
17 
18 class SKGUnitValueObject;
19 
20 /**
21  * This class manages skg bank documents
22  */
23 class SKGBANKMODELER_EXPORT SKGDocumentBank : public SKGDocument
24 {
25     Q_OBJECT
26     Q_CLASSINFO("D-Bus Interface", "org.kde.skrooge.SKGDocumentBank")
27 
28 public:
29     /**
30     * Constructor
31     */
32     explicit SKGDocumentBank();
33 
34     /**
35     * Destructor
36     */
37     ~SKGDocumentBank() override;
38 
39     /**
40      * dump the document in the std output.
41      * It is useful for debug.
42      * @param iMode to select what you want to dump.
43      * @code
44      * document->dump (DUMPUNIT|DUMPPARAMETERS);
45      * @endcode
46      * @return an object managing the error.
47      *   @see SKGError
48      */
49     SKGError dump(int iMode = DUMPBANKOBJECT) const override;
50 
51     /**
52      * Create or modify an account
53      * @param iName account name
54      * @param iNumber account number
55      * @param iBankName name of the bank
56      * @return an object managing the error.
57      *   @see SKGError
58      */
59     virtual SKGError addOrModifyAccount(const QString& iName, const QString& iNumber, const QString& iBankName) const;
60 
61     /**
62      * Create or modify the value of an unit
63      * @param iUnitName unit name
64      * @param iDate date
65      * @param iValue unit value for the date @p iDate
66      * @param oValue this output unit value
67      * @return an object managing the error.
68      *   @see SKGError
69      */
70     // cppcheck-suppress passedByValue
71     virtual SKGError addOrModifyUnitValue(const QString& iUnitName, QDate iDate, double iValue, SKGUnitValueObject* oValue = nullptr) const;
72 
73     /**
74      * Get Primary unit. WARNING: This value can be not uptodated in a transaction.
75      * @return Primary unit.
76      */
77     virtual SKGServices::SKGUnitInfo getPrimaryUnit() const;
78 
79     /**
80      * Get Secondary unit. WARNING: This value can be not uptodated in a transaction.
81      * @return Secondary unit.
82      */
83     virtual SKGServices::SKGUnitInfo getSecondaryUnit() const;
84 
85     /**
86     * Get the preferred category for a payee. WARNING: This value can be not uptodated in a transaction.
87     * @param iPayee the payee
88     * @param iComputeAllPayees compute all categories for all payees and put them in cache. This is better when you know that you will need all.
89     * @return The preferred category.
90     */
91     virtual QString getCategoryForPayee(const QString& iPayee, bool iComputeAllPayees = true) const;
92 
93     /**
94      * Return the number version of views, indexes and triggers
95      * @return
96      */
97     virtual QString getViewsIndexesAndTriggersVersion() const;
98 
99     /**
100      * Get display schemas
101      * @param iRealTable the real table name
102      * @return list of schemas
103      */
104     SKGDocument::SKGModelTemplateList getDisplaySchemas(const QString& iRealTable) const override;
105 
106     /**
107      * Get the display string for any modeler object (table, attribute)
108      * @param iString the name of the object (example: v_operation, v_unit.t_name)
109      * @return the display string
110      */
111     QString getDisplay(const QString& iString) const override;
112 
113     /**
114      * Get the real attribute
115      * @param iString the name of the attribute (something like t_BANK)
116      * @return the real attribute (something like bank.rd_bank_id.t_name)
117      */
118     QString getRealAttribute(const QString& iString) const override;
119     /**
120      * Get the icon for attribute
121      * @param iString the name of the attribute
122      * @return the icon name
123      */
124     QString getIconName(const QString& iString) const override;
125 
126     /**
127      * Get the attribute type
128      * @param iAttributeName the name of an attribute
129      * @return the type
130      */
131     SKGServices::AttributeType getAttributeType(const QString& iAttributeName) const override;
132 
133     /**
134      * Get the file extension for this kind of document (must be overwritten)
135      * @return file extension (like skg)
136      */
137     QString getFileExtension() const override;
138 
139     /**
140      * Get the header of the file (useful for magic mime type).
141      * @return document header
142      */
143     QString getDocumentHeader() const override;
144 
145     /**
146      * Get budget report
147      * @param iMonth the month
148      * @return the report
149      */
150     virtual QVariantList getBudget(const QString& iMonth) const;
151 
152     /**
153      * Get main categories report
154      * @param iPeriod the period
155      * @param iNb number of categories
156      * @return the report
157      */
158     virtual QVariantList getMainCategories(const QString& iPeriod, int iNb = 5);
159 
160     /**
161      * Get 5 main variation of categories report
162      * @param iPeriod the period
163      * @param iPreviousPeriod the previous period
164      * @param iOnlyIssues only "Expenses increased" and "Incomes decreased"
165      * @param oCategoryList to get the category for each variation
166      * @return the list of variation string
167      */
168     virtual QStringList get5MainCategoriesVariationList(const QString& iPeriod, const QString& iPreviousPeriod, bool iOnlyIssues, QStringList* oCategoryList = nullptr);
169 
170     /**
171      * Get the report
172      * Do not forget to delete the pointer
173      * @return the report
174      */
175     SKGReport* getReport() const override;
176 
177     /**
178      * Refresh all views and indexes in the database
179      * @param iForce force the refresh
180      * @return an object managing the error.
181      *   @see SKGError
182      */
183     SKGError refreshViewsIndexesAndTriggers(bool iForce = false) const override;
184 
185     /**
186      * Get formated money in primary unit
187      * @param iValue value
188      * @return formated value in red or black
189      */
190     Q_INVOKABLE QString formatPrimaryMoney(double iValue) const override;
191 
192     /**
193      * Get formated money in primary unit
194      * @param iValue value
195      * @return formated value in red or black
196      */
197     Q_INVOKABLE QString formatSecondaryMoney(double iValue) const override;
198 
199 public Q_SLOTS:
200     /**
201      * Close the current transaction.
202      * A transaction is needed to modify the SKGDocument.
203      * This transaction is also used to manage the undo/redo.
204      * @see beginTransaction
205      * @param succeedded : true to indicate that current transaction has been successfully executed
206      *                   : false to indicate that current transaction has failed
207      * @return an object managing the error
208      *   @see SKGError
209      */
210     SKGError endTransaction(bool succeedded) override;
211 
212     /**
213      * Enable/disable balances computation
214      */
215     virtual void setComputeBalances(bool iEnabled);
216 
217     /**
218      * Refresh the case.
219      * @param iTable the modified table triggering the cache refresh.
220      */
221     void refreshCache(const QString& iTable) const override;
222 
223 protected:
224     /**
225      * Migrate the current SKGDocument to the latest version of the data model.
226      * WARNING: This method must be used in a transaction.
227      * @see beginTransaction
228      * @param oMigrationDone to know if a migration has been done or not.
229      * @return an object managing the error.
230      *   @see SKGError
231      */
232     SKGError migrate(bool& oMigrationDone) override;
233 
234     /**
235      * Compute balance of each operation.
236      * @return an object managing the error.
237      *   @see SKGError
238      */
239     virtual SKGError computeBalances() const;
240 
241     /**
242      * Compute the budget suboperation links.
243      * @return an object managing the error.
244      *   @see SKGError
245      */
246     virtual SKGError computeBudgetSuboperationLinks() const;
247 
248 private:
249     Q_DISABLE_COPY(SKGDocumentBank)
250 
251     QString m_5mainVariations_inputs;
252     QStringList m_5mainVariations_cache;
253     QStringList m_5mainVariationsCat_cache;
254     bool m_computeBalances{true};
255     QStringList getMigationSteps();
256 };
257 /**
258  * Declare the class
259  */
260 Q_DECLARE_TYPEINFO(SKGDocumentBank, Q_MOVABLE_TYPE);
261 #endif
262