1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2021 S. MANKOWSKI stephane@mankowski.fr
3  * SPDX-FileCopyrightText: 2021 G. DE BURE support@mankowski.fr
4  * SPDX-License-Identifier: GPL-3.0-or-later
5  ***************************************************************************/
6 /** @file
7  * This file defines classes SKGInterestObject.
8  *
9  * @author Stephane MANKOWSKI / Guillaume DE BURE
10  */
11 #include "skginterestobject.h"
12 #include "skgdocument.h"
13 
SKGInterestObject()14 SKGInterestObject::SKGInterestObject(): SKGInterestObject(nullptr)
15 {}
16 
SKGInterestObject(SKGDocument * iDocument,int iID)17 SKGInterestObject::SKGInterestObject(SKGDocument* iDocument, int iID): SKGObjectBase(iDocument, QStringLiteral("v_interest"), iID)
18 {}
19 
20 SKGInterestObject::SKGInterestObject(const SKGInterestObject& iObject)
21     = default;
22 
SKGInterestObject(const SKGObjectBase & iObject)23 SKGInterestObject::SKGInterestObject(const SKGObjectBase& iObject)
24 {
25     if (iObject.getRealTable() == QStringLiteral("interest")) {
26         copyFrom(iObject);
27     } else {
28         *this = SKGObjectBase(iObject.getDocument(), QStringLiteral("v_interest"), iObject.getID());
29     }
30 }
31 
operator =(const SKGObjectBase & iObject)32 SKGInterestObject& SKGInterestObject::operator= (const SKGObjectBase& iObject)
33 {
34     copyFrom(iObject);
35     return *this;
36 }
37 
operator =(const SKGInterestObject & iObject)38 SKGInterestObject& SKGInterestObject::operator= (const SKGInterestObject& iObject)
39 {
40     copyFrom(iObject);
41     return *this;
42 }
43 
44 SKGInterestObject::~SKGInterestObject()
45     = default;
46 
setRate(double iValue)47 SKGError SKGInterestObject::setRate(double iValue)
48 {
49     return setAttribute(QStringLiteral("f_rate"), SKGServices::doubleToString(iValue));
50 }
51 
getRate() const52 double SKGInterestObject::getRate() const
53 {
54     return SKGServices::stringToDouble(getAttribute(QStringLiteral("f_rate")));
55 }
56 
setDate(QDate iDate)57 SKGError SKGInterestObject::setDate(QDate iDate)
58 {
59     return setAttribute(QStringLiteral("d_date"), SKGServices::dateToSqlString(iDate));
60 }
61 
getDate() const62 QDate SKGInterestObject::getDate() const
63 {
64     return SKGServices::stringToTime(getAttribute(QStringLiteral("d_date"))).date();
65 }
66 
setIncomeValueDateMode(SKGInterestObject::ValueDateMode iMode)67 SKGError SKGInterestObject::setIncomeValueDateMode(SKGInterestObject::ValueDateMode iMode)
68 {
69     return setAttribute(QStringLiteral("t_income_value_date_mode"), (iMode == FIFTEEN ? QStringLiteral("F") : SKGServices::intToString(static_cast<int>(iMode) - 1)));
70 }
71 
getIncomeValueDateMode() const72 SKGInterestObject::ValueDateMode SKGInterestObject::getIncomeValueDateMode() const
73 {
74     QString mode = getAttribute(QStringLiteral("t_income_value_date_mode"));
75     return (mode == QStringLiteral("F") ? FIFTEEN : static_cast<SKGInterestObject::ValueDateMode>(SKGServices::stringToInt(mode) + 1));
76 }
77 
setExpenditueValueDateMode(SKGInterestObject::ValueDateMode iMode)78 SKGError SKGInterestObject::setExpenditueValueDateMode(SKGInterestObject::ValueDateMode iMode)
79 {
80     return setAttribute(QStringLiteral("t_expenditure_value_date_mode"), (iMode == FIFTEEN ? QStringLiteral("F") : SKGServices::intToString(static_cast<int>(iMode) - 1)));
81 }
82 
getExpenditueValueDateMode() const83 SKGInterestObject::ValueDateMode SKGInterestObject::getExpenditueValueDateMode() const
84 {
85     QString mode = getAttribute(QStringLiteral("t_expenditure_value_date_mode"));
86     return (mode == QStringLiteral("F") ? FIFTEEN : static_cast<SKGInterestObject::ValueDateMode>(SKGServices::stringToInt(mode) + 1));
87 }
88 
setInterestComputationMode(SKGInterestObject::InterestMode iMode)89 SKGError SKGInterestObject::setInterestComputationMode(SKGInterestObject::InterestMode iMode)
90 {
91     return setAttribute(QStringLiteral("t_base"),
92                         (iMode == FIFTEEN24 ? QStringLiteral("24") :
93                          (iMode == DAYS360 ? QStringLiteral("360") :
94                           QStringLiteral("365"))));
95 }
96 
getInterestComputationMode() const97 SKGInterestObject::InterestMode SKGInterestObject::getInterestComputationMode() const
98 {
99     QString mode = getAttribute(QStringLiteral("t_base"));
100     return (mode == QStringLiteral("24") ? FIFTEEN24 : (mode == QStringLiteral("360") ? DAYS360 : DAYS365));
101 }
102 
getWhereclauseId() const103 QString SKGInterestObject::getWhereclauseId() const
104 {
105     // Could we use the id
106     QString output = SKGObjectBase::getWhereclauseId();
107     if (output.isEmpty()) {
108         // No, so we use the date and parent
109         if (!(getAttribute(QStringLiteral("d_date")).isEmpty()) && !(getAttribute(QStringLiteral("rd_account_id")).isEmpty())) {
110             output = "d_date='" % getAttribute(QStringLiteral("d_date")) % "' AND rd_account_id=" % getAttribute(QStringLiteral("rd_account_id"));
111         }
112     }
113     return output;
114 }
115 
setAccount(const SKGAccountObject & iAccount)116 SKGError SKGInterestObject::setAccount(const SKGAccountObject& iAccount)
117 {
118     return setAttribute(QStringLiteral("rd_account_id"), SKGServices::intToString(iAccount.getID()));
119 }
120 
getAccount(SKGAccountObject & oAccount) const121 SKGError SKGInterestObject::getAccount(SKGAccountObject& oAccount) const
122 {
123     SKGError err = getDocument()->getObject(QStringLiteral("v_account"), "id=" % getAttribute(QStringLiteral("rd_account_id")), oAccount);
124     return err;
125 }
126 
127 
128