1 /*
2  * Copyright 2016-2018  Thomas Baumgart <tbaumgart@kde.org>
3  * Copyright 2017-2018  Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "ledgertransaction.h"
20 #include "ledgertransaction_p.h"
21 
22 // ----------------------------------------------------------------------------
23 // QT Includes
24 
25 #include <QString>
26 #include <QDate>
27 
28 // ----------------------------------------------------------------------------
29 // KDE Includes
30 
31 #include <KLocalizedString>
32 
33 // ----------------------------------------------------------------------------
34 // Project Includes
35 
36 #include "mymoneysplit.h"
37 #include "mymoneytransaction.h"
38 #include "mymoneymoney.h"
39 #include "mymoneyenums.h"
40 
41 using namespace eMyMoney;
42 
LedgerTransaction()43 LedgerTransaction::LedgerTransaction() :
44   LedgerItem(),
45   d_ptr(new LedgerTransactionPrivate)
46 {
47 }
48 
LedgerTransaction(const MyMoneyTransaction & t,const MyMoneySplit & s)49 LedgerTransaction::LedgerTransaction(const MyMoneyTransaction& t, const MyMoneySplit& s) :
50   LedgerItem(),
51   d_ptr(new LedgerTransactionPrivate)
52 {
53   Q_D(LedgerTransaction);
54   d->init(t,s);
55 }
56 
LedgerTransaction(LedgerTransactionPrivate & dd)57 LedgerTransaction::LedgerTransaction(LedgerTransactionPrivate &dd) :
58   d_ptr(&dd)
59 {
60 }
61 
LedgerTransaction(LedgerTransactionPrivate & dd,const MyMoneyTransaction & t,const MyMoneySplit & s)62 LedgerTransaction::LedgerTransaction(LedgerTransactionPrivate &dd, const MyMoneyTransaction& t, const MyMoneySplit& s) :
63   d_ptr(&dd)
64 {
65   Q_D(LedgerTransaction);
66   d->init(t,s);
67 }
68 
LedgerTransaction(const LedgerTransaction & other)69 LedgerTransaction::LedgerTransaction(const LedgerTransaction& other) :
70   d_ptr(new LedgerTransactionPrivate(*other.d_func()))
71 {
72 }
73 
~LedgerTransaction()74 LedgerTransaction::~LedgerTransaction()
75 {
76   delete d_ptr;
77 }
78 
postDate() const79 QDate LedgerTransaction::postDate() const
80 {
81   Q_D(const LedgerTransaction);
82   return d->m_transaction.postDate();
83 }
84 
transaction() const85 MyMoneyTransaction LedgerTransaction::transaction() const
86 {
87   Q_D(const LedgerTransaction);
88   return d->m_transaction;
89 }
90 
split() const91 const MyMoneySplit& LedgerTransaction::split() const
92 {
93   Q_D(const LedgerTransaction);
94   return d->m_split;
95 }
96 
accountId() const97 QString LedgerTransaction::accountId() const
98 {
99   Q_D(const LedgerTransaction);
100   return d->m_split.accountId();
101 }
102 
account() const103 QString LedgerTransaction::account() const
104 {
105   Q_D(const LedgerTransaction);
106   return d->m_account;
107 }
108 
counterAccountId() const109 QString LedgerTransaction::counterAccountId() const
110 {
111   Q_D(const LedgerTransaction);
112   return d->m_counterAccountId;
113 }
114 
counterAccount() const115 QString LedgerTransaction::counterAccount() const
116 {
117   Q_D(const LedgerTransaction);
118   return d->m_counterAccount;
119 }
120 
costCenterId() const121 QString LedgerTransaction::costCenterId() const
122 {
123   Q_D(const LedgerTransaction);
124   return d->m_costCenterId;
125 }
126 
payeeName() const127 QString LedgerTransaction::payeeName() const
128 {
129   Q_D(const LedgerTransaction);
130   return d->m_payeeName;
131 }
132 
payeeId() const133 QString LedgerTransaction::payeeId() const
134 {
135   Q_D(const LedgerTransaction);
136   return d->m_payeeId;
137 }
138 
transactionNumber() const139 QString LedgerTransaction::transactionNumber() const
140 {
141   Q_D(const LedgerTransaction);
142   return d->m_split.number();
143 }
144 
flags() const145 Qt::ItemFlags LedgerTransaction::flags() const
146 {
147   return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
148 }
149 
transactionSplitId() const150 QString LedgerTransaction::transactionSplitId() const
151 {
152   Q_D(const LedgerTransaction);
153   QString rc;
154   if(!d->m_transaction.id().isEmpty()) {
155     rc = QString("%1-%2").arg(d->m_transaction.id(), d->m_split.id());
156   }
157   return rc;
158 }
159 
splitCount() const160 int LedgerTransaction::splitCount() const
161 {
162   Q_D(const LedgerTransaction);
163   return d->m_transaction.splitCount();
164 }
165 
transactionId() const166 QString LedgerTransaction::transactionId() const
167 {
168   Q_D(const LedgerTransaction);
169   return d->m_transaction.id();
170 }
171 
reconciliationState() const172 eMyMoney::Split::State LedgerTransaction::reconciliationState() const
173 {
174   Q_D(const LedgerTransaction);
175   return d->m_split.reconcileFlag();
176 }
177 
reconciliationStateShort() const178 QString LedgerTransaction::reconciliationStateShort() const
179 {
180   Q_D(const LedgerTransaction);
181   QString rc;
182   switch(d->m_split.reconcileFlag()) {
183     case eMyMoney::Split::State::NotReconciled:
184     default:
185       break;
186     case eMyMoney::Split::State::Cleared:
187       rc = i18nc("Reconciliation flag C", "C");
188       break;
189     case eMyMoney::Split::State::Reconciled:
190       rc = i18nc("Reconciliation flag R", "R");
191       break;
192     case eMyMoney::Split::State::Frozen:
193       rc = i18nc("Reconciliation flag F", "F");
194       break;
195   }
196   return rc;
197 }
198 
reconciliationStateLong() const199 QString LedgerTransaction::reconciliationStateLong() const
200 {
201   Q_D(const LedgerTransaction);
202   QString rc;
203   switch(d->m_split.reconcileFlag()) {
204     case eMyMoney::Split::State::NotReconciled:
205     default:
206       rc = i18nc("Reconciliation flag empty", "Not reconciled");
207       break;
208     case eMyMoney::Split::State::Cleared:
209       rc = i18nc("Reconciliation flag C", "Cleared");
210       break;
211     case eMyMoney::Split::State::Reconciled:
212       rc = i18nc("Reconciliation flag R", "Reconciled");
213       break;
214     case eMyMoney::Split::State::Frozen:
215       rc = i18nc("Reconciliation flag F", "Frozen");
216       break;
217   }
218   return rc;
219 }
220 
payment() const221 QString LedgerTransaction::payment() const
222 {
223   Q_D(const LedgerTransaction);
224   return d->m_payment;
225 }
226 
deposit() const227 QString LedgerTransaction::deposit() const
228 {
229   Q_D(const LedgerTransaction);
230   return d->m_deposit;
231 }
232 
setBalance(QString txt)233 void LedgerTransaction::setBalance(QString txt)
234 {
235   Q_D(LedgerTransaction);
236   d->m_balance = txt;
237 }
238 
balance() const239 QString LedgerTransaction::balance() const
240 {
241   Q_D(const LedgerTransaction);
242   return d->m_balance;
243 }
244 
shares() const245 MyMoneyMoney LedgerTransaction::shares() const
246 {
247   Q_D(const LedgerTransaction);
248   return d->m_split.shares();
249 }
250 
sharesAmount() const251 QString LedgerTransaction::sharesAmount() const
252 {
253   Q_D(const LedgerTransaction);
254   return d->m_shares;
255 }
256 
signedSharesAmount() const257 QString LedgerTransaction::signedSharesAmount() const
258 {
259   Q_D(const LedgerTransaction);
260   return d->m_signedShares;
261 }
262 
sharesSuffix() const263 QString LedgerTransaction::sharesSuffix() const
264 {
265   Q_D(const LedgerTransaction);
266   return d->m_sharesSuffix;
267 }
268 
value() const269 MyMoneyMoney LedgerTransaction::value() const
270 {
271   Q_D(const LedgerTransaction);
272   return d->m_split.value();
273 }
274 
memo() const275 QString LedgerTransaction::memo() const
276 {
277   Q_D(const LedgerTransaction);
278   auto memo = d->m_split.memo();
279   if(memo.isEmpty()) {
280     memo = d->m_transaction.memo();
281   }
282   return memo;
283 }
284 
isErroneous() const285 bool LedgerTransaction::isErroneous() const
286 {
287   Q_D(const LedgerTransaction);
288   return d->m_erroneous;
289 }
290 
isImported() const291 bool LedgerTransaction::isImported() const
292 {
293   Q_D(const LedgerTransaction);
294   return d->m_transaction.isImported();
295 }
296 
newTransactionEntry()297 LedgerTransaction LedgerTransaction::newTransactionEntry()
298 {
299   // create a dummy entry for new transactions
300   MyMoneyTransaction t;
301   t.setPostDate(QDate(2900,12,31));
302   return LedgerTransaction(t, MyMoneySplit());
303 }
304 
isNewTransactionEntry() const305 bool LedgerTransaction::isNewTransactionEntry() const
306 {
307   Q_D(const LedgerTransaction);
308   return d->m_transaction.id().isEmpty() && d->m_split.id().isEmpty();
309 }
310 
transactionCommodity() const311 QString LedgerTransaction::transactionCommodity() const
312 {
313   Q_D(const LedgerTransaction);
314   return d->m_transaction.commodity();
315 }
316