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

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 SKGPayeeObject.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgpayeeobject.h"
0012 
0013 #include <klocalizedstring.h>
0014 
0015 #include "skgcategoryobject.h"
0016 #include "skgdocumentbank.h"
0017 #include "skgoperationobject.h"
0018 #include "skgtraces.h"
0019 
0020 SKGPayeeObject::SKGPayeeObject(): SKGPayeeObject(nullptr)
0021 {}
0022 
0023 SKGPayeeObject::SKGPayeeObject(SKGDocument* iDocument, int iID): SKGNamedObject(iDocument, QStringLiteral("v_payee"), iID)
0024 {}
0025 
0026 SKGPayeeObject::~SKGPayeeObject()
0027     = default;
0028 
0029 SKGPayeeObject::SKGPayeeObject(const SKGPayeeObject& iObject) = default;
0030 
0031 SKGPayeeObject::SKGPayeeObject(const SKGObjectBase& iObject)
0032 
0033 {
0034     if (iObject.getRealTable() == QStringLiteral("payee")) {
0035         copyFrom(iObject);
0036     } else {
0037         *this = SKGNamedObject(iObject.getDocument(), QStringLiteral("v_payee"), iObject.getID());
0038     }
0039 }
0040 
0041 SKGPayeeObject& SKGPayeeObject::operator= (const SKGObjectBase& iObject)
0042 {
0043     copyFrom(iObject);
0044     return *this;
0045 }
0046 
0047 SKGPayeeObject& SKGPayeeObject::operator= (const SKGPayeeObject& iObject)
0048 {
0049     copyFrom(iObject);
0050     return *this;
0051 }
0052 
0053 SKGError SKGPayeeObject::createPayee(SKGDocumentBank* iDocument,
0054                                      const QString& iName,
0055                                      SKGPayeeObject& oPayee,
0056                                      bool iSendPopupMessageOnCreation)
0057 {
0058     SKGError err;
0059     SKGTRACEINFUNCRC(10, err)
0060 
0061     // Check if refund is already existing
0062     if (iName.isEmpty()) {
0063         oPayee = SKGPayeeObject(nullptr, 0);
0064     } else if (iDocument != nullptr) {
0065         iDocument->getObject(QStringLiteral("v_payee"), "t_name='" % SKGServices::stringToSqlString(iName) % '\'', oPayee);
0066         if (oPayee.getID() == 0) {
0067             // No, we have to create it
0068             oPayee = SKGPayeeObject(iDocument);
0069             err = oPayee.setName(iName);
0070             IFOKDO(err, oPayee.save())
0071 
0072             if (!err && iSendPopupMessageOnCreation) {
0073                 err = iDocument->sendMessage(i18nc("Information message", "Payee '%1' has been created", iName), SKGDocument::Positive);
0074             }
0075         }
0076     }
0077 
0078     return err;
0079 }
0080 
0081 SKGError SKGPayeeObject::getOperations(SKGListSKGObjectBase& oOperations) const
0082 {
0083     SKGError err = getDocument()->getObjects(QStringLiteral("v_operation"),
0084                    "r_payee_id=" % SKGServices::intToString(getID()),
0085                    oOperations);
0086     return err;
0087 }
0088 
0089 SKGError SKGPayeeObject::setAddress(const QString& iAddress)
0090 {
0091     return setAttribute(QStringLiteral("t_address"), iAddress);
0092 }
0093 
0094 QString SKGPayeeObject::getAddress() const
0095 {
0096     return getAttribute(QStringLiteral("t_address"));
0097 }
0098 
0099 SKGError SKGPayeeObject::setClosed(bool iClosed)
0100 {
0101     return setAttribute(QStringLiteral("t_close"), iClosed ? QStringLiteral("Y") : QStringLiteral("N"));
0102 }
0103 
0104 bool SKGPayeeObject::isClosed() const
0105 {
0106     return (getAttribute(QStringLiteral("t_close")) == QStringLiteral("Y"));
0107 }
0108 
0109 SKGError SKGPayeeObject::bookmark(bool iBookmark)
0110 {
0111     return setAttribute(QStringLiteral("t_bookmarked"), iBookmark ? QStringLiteral("Y") : QStringLiteral("N"));
0112 }
0113 
0114 bool SKGPayeeObject::isBookmarked() const
0115 {
0116     return (getAttribute(QStringLiteral("t_bookmarked")) == QStringLiteral("Y"));
0117 }
0118 
0119 SKGError SKGPayeeObject::getCategory(SKGCategoryObject& oCategory) const
0120 {
0121     SKGError err = getDocument()->getObject(QStringLiteral("v_category"), "id=" % getAttribute(QStringLiteral("r_category_id")), oCategory);
0122     return err;
0123 }
0124 
0125 SKGError SKGPayeeObject::setCategory(const SKGCategoryObject& iCategory)
0126 {
0127     return setAttribute(QStringLiteral("r_category_id"), SKGServices::intToString(iCategory.getID()));
0128 }
0129 
0130 SKGError SKGPayeeObject::merge(const SKGPayeeObject& iPayee)
0131 {
0132     SKGError err;
0133 
0134     SKGObjectBase::SKGListSKGObjectBase ops;
0135     IFOKDO(err, iPayee.getOperations(ops))
0136     int nb = ops.count();
0137     for (int i = 0; !err && i < nb; ++i) {
0138         SKGOperationObject op(ops.at(i));
0139         err = op.setPayee(*this);
0140         IFOKDO(err, op.save(true, false))
0141     }
0142 
0143     IFOKDO(err, iPayee.remove(false))
0144     return err;
0145 }
0146 
0147