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 /** @file
0007  * This file implements classes SKGSubOperationObject.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgsuboperationobject.h"
0012 
0013 #include <klocalizedstring.h>
0014 
0015 #include "skgcategoryobject.h"
0016 #include "skgdocument.h"
0017 #include "skgoperationobject.h"
0018 #include "skgservices.h"
0019 #include "skgtrackerobject.h"
0020 
0021 SKGSubOperationObject::SKGSubOperationObject(): SKGSubOperationObject(nullptr)
0022 {}
0023 
0024 SKGSubOperationObject::SKGSubOperationObject(SKGDocument* iDocument, int iID): SKGObjectBase(iDocument, QStringLiteral("v_suboperation"), iID)
0025 {}
0026 
0027 SKGSubOperationObject::~SKGSubOperationObject()
0028     = default;
0029 
0030 SKGSubOperationObject::SKGSubOperationObject(const SKGSubOperationObject& iObject)
0031     = default;
0032 
0033 SKGSubOperationObject::SKGSubOperationObject(const SKGObjectBase& iObject)
0034 {
0035     if (iObject.getRealTable() == QStringLiteral("suboperation")) {
0036         copyFrom(iObject);
0037     } else {
0038         *this = SKGObjectBase(iObject.getDocument(), QStringLiteral("v_suboperation"), iObject.getID());
0039     }
0040 }
0041 
0042 SKGSubOperationObject& SKGSubOperationObject::operator= (const SKGObjectBase& iObject)
0043 {
0044     copyFrom(iObject);
0045     return *this;
0046 }
0047 
0048 SKGSubOperationObject& SKGSubOperationObject::operator= (const SKGSubOperationObject& iObject)
0049 {
0050     copyFrom(iObject);
0051     return *this;
0052 }
0053 
0054 SKGError SKGSubOperationObject::setDate(QDate iDate)
0055 {
0056     return setAttribute(QStringLiteral("d_date"), iDate.isValid() ? SKGServices::dateToSqlString(iDate) : QLatin1String(""));
0057 }
0058 
0059 QDate SKGSubOperationObject::getDate() const
0060 {
0061     return SKGServices::stringToTime(getAttribute(QStringLiteral("d_date"))).date();
0062 }
0063 
0064 SKGError SKGSubOperationObject::setOrder(int iOrder)
0065 {
0066     return setAttribute(QStringLiteral("i_order"), SKGServices::intToString(iOrder));
0067 }
0068 
0069 int SKGSubOperationObject::getOrder() const
0070 {
0071     return SKGServices::stringToInt(getAttribute(QStringLiteral("i_order")));
0072 }
0073 
0074 SKGError SKGSubOperationObject::setComment(const QString& iComment)
0075 {
0076     return setAttribute(QStringLiteral("t_comment"), iComment);
0077 }
0078 
0079 QString SKGSubOperationObject::getComment() const
0080 {
0081     return getAttribute(QStringLiteral("t_comment"));
0082 }
0083 
0084 QString SKGSubOperationObject::getFormula() const
0085 {
0086     return getAttribute(QStringLiteral("t_formula"));
0087 }
0088 
0089 SKGError SKGSubOperationObject::setFormula(const QString& iFormula)
0090 {
0091     return setAttribute(QStringLiteral("t_formula"), iFormula);
0092 }
0093 
0094 SKGError SKGSubOperationObject::getParentOperation(SKGOperationObject& oOperation) const
0095 {
0096     SKGError err = getDocument()->getObject(QStringLiteral("v_operation"), "id=" % getAttribute(QStringLiteral("rd_operation_id")), oOperation);
0097     return err;
0098 }
0099 
0100 SKGError SKGSubOperationObject::setParentOperation(const SKGOperationObject& iOperation)
0101 {
0102     SKGError err;
0103     if (!getDate().isValid()) {
0104         err = setDate(iOperation.getDate());
0105     }
0106     IFOKDO(err, setAttribute(QStringLiteral("rd_operation_id"), SKGServices::intToString(iOperation.getID())))
0107     return err;
0108 }
0109 
0110 SKGError SKGSubOperationObject::getCategory(SKGCategoryObject& oCategory) const
0111 {
0112     SKGError err = getDocument()->getObject(QStringLiteral("v_category"), "id=" % getAttribute(QStringLiteral("r_category_id")), oCategory);
0113     return err;
0114 }
0115 
0116 SKGError SKGSubOperationObject::setCategory(const SKGCategoryObject& iCategory)
0117 {
0118     return setAttribute(QStringLiteral("r_category_id"), SKGServices::intToString(iCategory.getID()));
0119 }
0120 
0121 SKGError SKGSubOperationObject::setQuantity(double iValue)
0122 {
0123     return setAttribute(QStringLiteral("f_value"), SKGServices::doubleToString(iValue));
0124 }
0125 
0126 double SKGSubOperationObject::getQuantity() const
0127 {
0128     return SKGServices::stringToDouble(getAttribute(QStringLiteral("f_value")));
0129 }
0130 
0131 
0132 SKGError SKGSubOperationObject::setTracker(const SKGTrackerObject& iTracker, bool iForce)
0133 {
0134     SKGError err;
0135     SKGTrackerObject previous;
0136     getTracker(previous);
0137     if (iTracker != previous) {
0138         if (!iForce && previous.isClosed()) {
0139             err = SKGError(ERR_FAIL, i18nc("Error message", "Impossible to remove a transaction from a closed tracker"));
0140         } else if (!iForce && iTracker.isClosed()) {
0141             err = SKGError(ERR_FAIL, i18nc("Error message", "Impossible to add a transaction in a closed tracker"));
0142         } else {
0143             err = setAttribute(QStringLiteral("r_refund_id"), SKGServices::intToString(iTracker.getID()));
0144         }
0145     }
0146     return err;
0147 }
0148 
0149 SKGError SKGSubOperationObject::getTracker(SKGTrackerObject& oTracker) const
0150 {
0151     QString idS = getAttribute(QStringLiteral("r_refund_id"));
0152     if (idS.isEmpty()) {
0153         idS = '0';
0154     }
0155     SKGError err;
0156     if ((getDocument() != nullptr) && idS != QStringLiteral("0")) {
0157         err = getDocument()->getObject(QStringLiteral("v_refund"), "id=" % idS, oTracker);
0158     }
0159     return err;
0160 }
0161 
0162