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

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 SKGUnitValueObject.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgunitvalueobject.h"
0012 
0013 #include <klocalizedstring.h>
0014 
0015 #include "skgdocument.h"
0016 #include "skgunitobject.h"
0017 
0018 SKGUnitValueObject::SKGUnitValueObject() : SKGUnitValueObject(nullptr)
0019 {}
0020 
0021 SKGUnitValueObject::SKGUnitValueObject(SKGDocument* iDocument, int iID) : SKGObjectBase(iDocument, QStringLiteral("v_unitvalue"), iID)
0022 {}
0023 
0024 SKGUnitValueObject::SKGUnitValueObject(const SKGUnitValueObject& iObject) = default;
0025 
0026 SKGUnitValueObject::SKGUnitValueObject(const SKGObjectBase& iObject)
0027 {
0028     if (iObject.getRealTable() == QStringLiteral("unitvalue")) {
0029         copyFrom(iObject);
0030     } else {
0031         *this = SKGObjectBase(iObject.getDocument(), QStringLiteral("v_unitvalue"), iObject.getID());
0032     }
0033 }
0034 
0035 SKGUnitValueObject& SKGUnitValueObject::operator= (const SKGObjectBase& iObject)
0036 {
0037     copyFrom(iObject);
0038     return *this;
0039 }
0040 
0041 SKGUnitValueObject& SKGUnitValueObject::operator= (const SKGUnitValueObject& iObject)
0042 {
0043     copyFrom(iObject);
0044     return *this;
0045 }
0046 
0047 SKGUnitValueObject::~SKGUnitValueObject()
0048     = default;
0049 
0050 SKGError SKGUnitValueObject::setQuantity(double iValue)
0051 {
0052     return setAttribute(QStringLiteral("f_quantity"), SKGServices::doubleToString(iValue));
0053 }
0054 
0055 double SKGUnitValueObject::getQuantity() const
0056 {
0057     return SKGServices::stringToDouble(getAttribute(QStringLiteral("f_quantity")));
0058 }
0059 
0060 SKGError SKGUnitValueObject::setDate(QDate iDate)
0061 {
0062     return setAttribute(QStringLiteral("d_date"), SKGServices::dateToSqlString(iDate));
0063 }
0064 
0065 QDate SKGUnitValueObject::getDate() const
0066 {
0067     return SKGServices::stringToTime(getAttribute(QStringLiteral("d_date"))).date();
0068 }
0069 
0070 QString SKGUnitValueObject::getWhereclauseId() const
0071 {
0072     // Could we use the id
0073     QString output = SKGObjectBase::getWhereclauseId();
0074     if (output.isEmpty()) {
0075         // No, so we use the date and parent
0076         if (!(getAttribute(QStringLiteral("d_date")).isEmpty()) && !(getAttribute(QStringLiteral("rd_unit_id")).isEmpty())) {
0077             output = "d_date='" % getAttribute(QStringLiteral("d_date")) % "' AND rd_unit_id=" % getAttribute(QStringLiteral("rd_unit_id"));
0078         }
0079     }
0080     return output;
0081 }
0082 
0083 SKGError SKGUnitValueObject::getUnit(SKGUnitObject& oUnit) const
0084 {
0085     SKGError err;
0086     if (getDocument() != nullptr) {
0087         err = getDocument()->getObject(QStringLiteral("v_unit"), "id=" % getAttribute(QStringLiteral("rd_unit_id")), oUnit);
0088     }
0089     return err;
0090 }
0091