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

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 SKGTrackerObject.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgtrackerobject.h"
0012 
0013 #include <klocalizedstring.h>
0014 
0015 #include "skgdocumentbank.h"
0016 #include "skgsuboperationobject.h"
0017 #include "skgtraces.h"
0018 
0019 SKGTrackerObject::SKGTrackerObject(): SKGTrackerObject(nullptr)
0020 {}
0021 
0022 SKGTrackerObject::SKGTrackerObject(SKGDocument* iDocument, int iID): SKGNamedObject(iDocument, QStringLiteral("v_refund"), iID)
0023 {}
0024 
0025 SKGTrackerObject::~SKGTrackerObject()
0026     = default;
0027 
0028 SKGTrackerObject::SKGTrackerObject(const SKGTrackerObject& iObject) = default;
0029 
0030 SKGTrackerObject::SKGTrackerObject(const SKGObjectBase& iObject)
0031 
0032 {
0033     if (iObject.getRealTable() == QStringLiteral("refund")) {
0034         copyFrom(iObject);
0035     } else {
0036         *this = SKGNamedObject(iObject.getDocument(), QStringLiteral("v_refund"), iObject.getID());
0037     }
0038 }
0039 
0040 SKGTrackerObject& SKGTrackerObject::operator= (const SKGObjectBase& iObject)
0041 {
0042     copyFrom(iObject);
0043     return *this;
0044 }
0045 
0046 SKGTrackerObject& SKGTrackerObject::operator= (const SKGTrackerObject& iObject)
0047 {
0048     copyFrom(iObject);
0049     return *this;
0050 }
0051 
0052 SKGError SKGTrackerObject::createTracker(SKGDocumentBank* iDocument,
0053         const QString& iName,
0054         SKGTrackerObject& oTracker,
0055         bool iSendPopupMessageOnCreation)
0056 {
0057     SKGError err;
0058     SKGTRACEINFUNCRC(10, err)
0059 
0060     // Check if refund is already existing
0061     if (iName.isEmpty()) {
0062         oTracker = SKGTrackerObject(nullptr, 0);
0063     } else if (iDocument != nullptr) {
0064         iDocument->getObject(QStringLiteral("v_refund"), "t_name='" % SKGServices::stringToSqlString(iName) % '\'', oTracker);
0065         if (oTracker.getID() == 0) {
0066             // No, we have to create it
0067             oTracker = SKGTrackerObject(iDocument);
0068             err = oTracker.setName(iName);
0069             IFOKDO(err, oTracker.save())
0070 
0071             if (!err && iSendPopupMessageOnCreation) {
0072                 err = iDocument->sendMessage(i18nc("Information message", "Tracker '%1' has been created", iName), SKGDocument::Positive);
0073             }
0074         }
0075     }
0076 
0077     return err;
0078 }
0079 
0080 SKGError SKGTrackerObject::getSubOperations(SKGListSKGObjectBase& oSubOperations) const
0081 {
0082     SKGError err = getDocument()->getObjects(QStringLiteral("v_suboperation"),
0083                    "r_refund_id=" % SKGServices::intToString(getID()),
0084                    oSubOperations);
0085     return err;
0086 }
0087 
0088 SKGError SKGTrackerObject::setClosed(bool iClosed)
0089 {
0090     return setAttribute(QStringLiteral("t_close"), iClosed ? QStringLiteral("Y") : QStringLiteral("N"));
0091 }
0092 
0093 bool SKGTrackerObject::isClosed() const
0094 {
0095     return (getAttribute(QStringLiteral("t_close")) == QStringLiteral("Y"));
0096 }
0097 
0098 SKGError SKGTrackerObject::setComment(const QString& iComment)
0099 {
0100     return setAttribute(QStringLiteral("t_comment"), iComment);
0101 }
0102 
0103 QString SKGTrackerObject::getComment() const
0104 {
0105     return getAttribute(QStringLiteral("t_comment"));
0106 }
0107 
0108 double SKGTrackerObject::getCurrentAmount() const
0109 {
0110     return SKGServices::stringToDouble(getAttributeFromView(QStringLiteral("v_refund_amount"), QStringLiteral("f_CURRENTAMOUNT")));
0111 }
0112 
0113 SKGError SKGTrackerObject::merge(const SKGTrackerObject& iTracker)
0114 {
0115     SKGError err;
0116 
0117     SKGObjectBase::SKGListSKGObjectBase ops;
0118     IFOKDO(err, iTracker.getSubOperations(ops))
0119     int nb = ops.count();
0120     for (int i = 0; !err && i < nb; ++i) {
0121         SKGSubOperationObject op(ops.at(i));
0122         err = op.setTracker(*this);
0123         IFOKDO(err, op.save(true, false))
0124     }
0125 
0126     IFOKDO(err, iTracker.remove(false))
0127     return err;
0128 }
0129 
0130