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

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 /** @file
0007 * This file defines classes SKGBudgetRuleObject.
0008 *
0009 * @author Stephane MANKOWSKI / Guillaume DE BURE
0010 */
0011 #include "skgbudgetruleobject.h"
0012 
0013 #include <klocalizedstring.h>
0014 
0015 #include "skgbudgetobject.h"
0016 #include "skgcategoryobject.h"
0017 #include "skgdocumentbank.h"
0018 #include "skgtraces.h"
0019 #include "skgtransactionmng.h"
0020 
0021 SKGBudgetRuleObject::SKGBudgetRuleObject()
0022     : SKGBudgetRuleObject(nullptr)
0023 {}
0024 
0025 SKGBudgetRuleObject::SKGBudgetRuleObject(SKGDocument* iDocument, int iID)
0026     : SKGObjectBase(iDocument, QStringLiteral("v_budgetrule"), iID)
0027 {}
0028 
0029 SKGBudgetRuleObject::~SKGBudgetRuleObject()
0030     = default;
0031 
0032 SKGBudgetRuleObject::SKGBudgetRuleObject(const SKGBudgetRuleObject& iObject)
0033     = default;
0034 
0035 SKGBudgetRuleObject::SKGBudgetRuleObject(const SKGObjectBase& iObject)
0036 {
0037     if (iObject.getRealTable() == QStringLiteral("budgetrule")) {
0038         copyFrom(iObject);
0039     } else {
0040         *this = SKGObjectBase(iObject.getDocument(), QStringLiteral("v_budgetrule"), iObject.getID());
0041     }
0042 }
0043 
0044 SKGBudgetRuleObject& SKGBudgetRuleObject::operator= (const SKGObjectBase& iObject)
0045 {
0046     copyFrom(iObject);
0047     return *this;
0048 }
0049 
0050 SKGError SKGBudgetRuleObject::enableYearCondition(bool iEnable)
0051 {
0052     return setAttribute(QStringLiteral("t_year_condition"), iEnable ? QStringLiteral("Y") : QStringLiteral("N"));
0053 }
0054 
0055 bool SKGBudgetRuleObject::isYearConditionEnabled() const
0056 {
0057     return (getAttribute(QStringLiteral("t_year_condition")) == QStringLiteral("Y"));
0058 }
0059 
0060 SKGError SKGBudgetRuleObject::enableMonthCondition(bool iEnable)
0061 {
0062     return setAttribute(QStringLiteral("t_month_condition"), iEnable ? QStringLiteral("Y") : QStringLiteral("N"));
0063 }
0064 
0065 bool SKGBudgetRuleObject::isMonthConditionEnabled() const
0066 {
0067     return (getAttribute(QStringLiteral("t_month_condition")) == QStringLiteral("Y"));
0068 }
0069 
0070 SKGError SKGBudgetRuleObject::enableCategoryCondition(bool iEnable)
0071 {
0072     return setAttribute(QStringLiteral("t_category_condition"), iEnable ? QStringLiteral("Y") : QStringLiteral("N"));
0073 }
0074 
0075 bool SKGBudgetRuleObject::isCategoryConditionEnabled() const
0076 {
0077     return (getAttribute(QStringLiteral("t_category_condition")) == QStringLiteral("Y"));
0078 }
0079 
0080 SKGError SKGBudgetRuleObject::setBudgetYear(int iYear)
0081 {
0082     return setAttribute(QStringLiteral("i_year"), SKGServices::intToString(iYear));
0083 }
0084 
0085 int SKGBudgetRuleObject::getBudgetYear() const
0086 {
0087     return SKGServices::stringToInt(getAttribute(QStringLiteral("i_year")));
0088 }
0089 
0090 SKGError SKGBudgetRuleObject::setBudgetMonth(int iMonth)
0091 {
0092     return setAttribute(QStringLiteral("i_month"), SKGServices::intToString(iMonth));
0093 }
0094 
0095 int SKGBudgetRuleObject::getBudgetMonth() const
0096 {
0097     return SKGServices::stringToInt(getAttribute(QStringLiteral("i_month")));
0098 }
0099 
0100 SKGError SKGBudgetRuleObject::setBudgetCategory(const SKGCategoryObject& iCategory)
0101 {
0102     return setAttribute(QStringLiteral("rc_category_id"), SKGServices::intToString(iCategory.getID()));
0103 }
0104 
0105 SKGError SKGBudgetRuleObject::getBudgetCategory(SKGCategoryObject& oCategory) const
0106 {
0107     return getDocument()->getObject(QStringLiteral("v_category"), "id=" % getAttribute(QStringLiteral("rc_category_id")), oCategory);
0108 }
0109 
0110 SKGError SKGBudgetRuleObject::removeBudgetCategory()
0111 {
0112     return setAttribute(QStringLiteral("rc_category_id"), QStringLiteral("0"));
0113 }
0114 
0115 SKGError SKGBudgetRuleObject::setCondition(SKGBudgetRuleObject::Condition iCondition)
0116 {
0117     return setAttribute(QStringLiteral("i_condition"), SKGServices::intToString(static_cast<int>(iCondition)));
0118 }
0119 
0120 SKGBudgetRuleObject::Condition SKGBudgetRuleObject::getCondition() const
0121 {
0122     return static_cast<SKGBudgetRuleObject::Condition>(SKGServices::stringToInt(getAttribute(QStringLiteral("i_condition"))));
0123 }
0124 
0125 SKGError SKGBudgetRuleObject::setQuantity(double iQuantity, bool iAbsolute)
0126 {
0127     SKGError err = setAttribute(QStringLiteral("f_quantity"), SKGServices::doubleToString(iQuantity));
0128     IFOKDO(err, setAttribute(QStringLiteral("t_absolute"), iAbsolute ? QStringLiteral("Y") : QStringLiteral("N")))
0129     return err;
0130 }
0131 
0132 double SKGBudgetRuleObject::getQuantity() const
0133 {
0134     return SKGServices::stringToDouble(getAttribute(QStringLiteral("f_quantity")));
0135 }
0136 
0137 bool SKGBudgetRuleObject::isAbolute() const
0138 {
0139     return getAttribute(QStringLiteral("t_absolute")) != QStringLiteral("N");
0140 }
0141 
0142 SKGError SKGBudgetRuleObject::enableCategoryChange(bool iEnable)
0143 {
0144     return setAttribute(QStringLiteral("t_category_target"), iEnable ? QStringLiteral("Y") : QStringLiteral("N"));
0145 }
0146 
0147 bool SKGBudgetRuleObject::isCategoryChangeEnabled() const
0148 {
0149     return getAttribute(QStringLiteral("t_category_target")) == QStringLiteral("Y");
0150 }
0151 
0152 SKGError SKGBudgetRuleObject::setTransfer(SKGBudgetRuleObject::Mode iMode, const SKGCategoryObject& iCategory)
0153 {
0154     SKGError err = setAttribute(QStringLiteral("t_rule"), iMode == NEXT ? QStringLiteral("N") : iMode == CURRENT ? QStringLiteral("C") : QStringLiteral("Y"));
0155     IFOKDO(err, setAttribute(QStringLiteral("rc_category_id_target"), SKGServices::intToString(iCategory.getID())))
0156     return err;
0157 }
0158 
0159 SKGBudgetRuleObject::Mode SKGBudgetRuleObject::getTransferMode() const
0160 {
0161     return (getAttribute(QStringLiteral("t_rule")) == QStringLiteral("N") ? NEXT : (getAttribute(QStringLiteral("t_rule")) == QStringLiteral("C") ? CURRENT : YEAR));
0162 }
0163 
0164 SKGError SKGBudgetRuleObject::getTransferCategory(SKGCategoryObject& oCategory) const
0165 {
0166     return getDocument()->getObject(QStringLiteral("v_category"), "id=" % getAttribute(QStringLiteral("rc_category_id_target")), oCategory);
0167 }
0168 
0169 SKGError SKGBudgetRuleObject::processAllRules(SKGDocumentBank* iDocument)
0170 {
0171     SKGError err;
0172     SKGTRACEINFUNCRC(10, err)
0173     if (iDocument != nullptr) {
0174         // Initialize
0175         err = iDocument->executeSqliteOrder(QStringLiteral("UPDATE budget SET f_budgeted_modified=f_budgeted  WHERE f_budgeted_modified!=f_budgeted"));
0176         IFOKDO(err, iDocument->executeSqliteOrder(QStringLiteral("UPDATE budget SET f_transferred=0  WHERE f_transferred!=0")))
0177 
0178         // Get budgets ordered by date
0179         SKGObjectBase::SKGListSKGObjectBase budgets;
0180         IFOKDO(err, iDocument->getObjects(QStringLiteral("vm_budget_tmp"), QStringLiteral("length(t_RULES)>0 "
0181                                           "AND (t_PERIOD<=STRFTIME('%Y-%m', date('now', 'localtime')) OR t_PERIOD=STRFTIME('%Y', date('now', 'localtime'))) "
0182                                           "ORDER BY t_PERIOD, id"), budgets));
0183         int nb = budgets.count();
0184         if (!err && nb > 0) {
0185             err = iDocument->beginTransaction("#INTERNAL#" % i18nc("Progression step", "Apply rules"), nb);
0186             for (int i = 0; !err && i < nb; ++i) {
0187                 SKGBudgetObject bud(budgets.at(i));
0188                 err = bud.load();  // Reload to be sure that delta has been updated
0189                 IFOKDO(err, bud.process())
0190 
0191                 IFOKDO(err, iDocument->stepForward(i + 1))
0192             }
0193 
0194             IFOKDO(err, iDocument->setParameter(QStringLiteral("SKG_LAST_BUDGET_PROCESSING"), QDate::currentDate().toString(QStringLiteral("yyyy-MM-dd"))))
0195 
0196             SKGENDTRANSACTION(iDocument,  err)
0197         }
0198     }
0199 
0200     return err;
0201 }
0202 
0203 SKGError SKGBudgetRuleObject::setOrder(double iOrder)
0204 {
0205     SKGError err;
0206     double order = iOrder;
0207     if (order == -1) {
0208         order = 1;
0209         SKGStringListList result;
0210         err = getDocument()->executeSelectSqliteOrder(QStringLiteral("SELECT max(f_sortorder) from budgetrule"), result);
0211         if (!err && result.count() == 2) {
0212             order = SKGServices::stringToDouble(result.at(1).at(0)) + 1;
0213         }
0214     }
0215     IFOKDO(err, setAttribute(QStringLiteral("f_sortorder"), SKGServices::doubleToString(order)))
0216     return err;
0217 }
0218 
0219 double SKGBudgetRuleObject::getOrder() const
0220 {
0221     return SKGServices::stringToDouble(getAttribute(QStringLiteral("f_sortorder")));
0222 }
0223