FreeDebks  1.0.3
 All Classes Files Functions Variables Friends Pages
FdItemCoa.cpp
Go to the documentation of this file.
1 // --------------------------------------------------------------------
2 // Copyright © 2011-2013 Mathieu Schopfer
3 //
4 // This file is part of FreeDebks.
5 //
6 // FreeDebks is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // FreeDebks is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with FreeDebks. If not, see <http://www.gnu.org/licenses/>.
18 // --------------------------------------------------------------------
19 
20 #include "FdItemCoa.hpp"
21 
22 FdItemCoa::FdItemCoa(FdItemCoa_p parent,
23  QString id,
24  QString label,
25  AccountSignature signature,
26  double initial,
27  double balance,
28  double previous) :
29  mParent(parent),
30  mId(id),
31  mLabel(label),
32  mSignature(signature),
33  mInitialBalance(initial),
34  mBalance(balance),
35  mPreviousBalance(previous)
36 {
37  if(!parent.isNull() && parent->type() != RootType)
38  mOnBalanceSheet = parent->onBalanceSheet();
39  else
40  mOnBalanceSheet = false;
41 }
42 
46 FdItemCoa_p FdItemCoa::pointer(QDomElement item, FdItemCoa_p parent)
47 {
48  FdItemCoa_p myself;
49  if(!parent.isNull())
50  myself = FdItemCoa_p(new FdItemCoa(parent,
51  item.attribute("Id"),
52  item.attribute("Label"),
53  AccountSignature(item.attribute("Signature", "0").toDouble()),
54  item.attribute("Initial", "0").toDouble(),
55  0,
56  item.attribute("Previous", "0").toDouble()));
57  else
58  myself = FdItemCoa_p(new FdItemCoa());
59 
60  if(!parent.isNull() && parent->type() == RootType)
61  myself->setOnBalanceSheet(item.attribute("OnBalanceSheet").toDouble());
62 
63  if(item.tagName() != "Account")
64  {
65  QDomElement child = item.firstChildElement();
66  while(!child.isNull())
67  {
68  myself->appendChild(FdItemCoa::pointer(child, myself));
69  child = child.nextSiblingElement();
70  }
71  }
72 
73  return myself;
74 }
75 
82 FdItemCoa_p FdItemCoa::pointer(FdItemCoa_p model, FdItemCoa_p parent)
83 {
84  FdItemCoa_p myself = FdItemCoa_p(new FdItemCoa(parent,
85  model->id(),
86  model->label(),
87  model->signature(),
88  0,
89  0,
90  model->balance()));
91 
92  if(!parent.isNull() && parent->type() == RootType)
93  myself->setOnBalanceSheet(model->onBalanceSheet());
94 
95  if(myself->onBalanceSheet())
96  myself->setInitialBalance(myself->previousBalance());
97 
98  for(int i = 0; i < model->childrenCount(); ++i)
99  myself->appendChild(FdItemCoa::pointer(model->child(i), myself));
100 
101  return myself;
102 }
103 
107 CoaItemType FdItemCoa::type() const
108 {
109  if(mParent.isNull())
110  return RootType;
111  if(mChildren.size() == 0)
112  return AccountType;
113 
114  return CategoryType;
115 }
116 
120 QString FdItemCoa::id() const
121 {
122  return mId;
123 }
124 
125 QString FdItemCoa::label() const
126 {
127  return mLabel;
128 }
129 
130 bool FdItemCoa::onBalanceSheet() const
131 {
132  return mOnBalanceSheet;
133 }
134 
135 AccountSignature FdItemCoa::signature() const
136 {
137  return mSignature;
138 }
139 
140 double FdItemCoa::initialBalance() const
141 {
142  return mInitialBalance;
143 }
144 
145 double FdItemCoa::balance() const
146 {
147  return mBalance;
148 }
149 
150 double FdItemCoa::previousBalance() const
151 {
152  return mPreviousBalance;
153 }
154 
155 void FdItemCoa::setId(QString id)
156 {
157  mId = id;
158 }
159 
160 void FdItemCoa::setLabel(QString label)
161 {
162  mLabel = label;
163 }
164 
165 void FdItemCoa::setOnBalanceSheet(bool var)
166 {
167  mOnBalanceSheet = var;
168  for(int i = 0; i < childrenCount(); ++i)
169  mChildren[i]->setOnBalanceSheet(var);
170 }
171 
172 void FdItemCoa::setSignature(AccountSignature signature)
173 {
175 }
176 
177 void FdItemCoa::setInitialBalance(double initial)
178 {
179  mInitialBalance = initial;
180 }
181 
182 void FdItemCoa::setPreviousBalance(double previous)
183 {
184  mPreviousBalance = previous;
185 }
186 
191 bool FdItemCoa::wasUsed() const
192 {
194  return true;
195 
196  return false;
197 }
198 
202 QString FdItemCoa::displayLabel() const
203 {
204  return mId+" "+mLabel;
205 }
206 
207 FdItemCoa_p FdItemCoa::parent()
208 {
209  return mParent;
210 }
211 
212 void FdItemCoa::setParent(FdItemCoa_p parent)
213 {
214  mParent = parent;
215 }
216 
220 FdItemCoa_p FdItemCoa::child(int row)
221 {
222  return mChildren.value(row);
223 }
224 
228 QList<FdItemCoa_p> FdItemCoa::children()
229 {
230  return mChildren;
231 }
232 
236 QList<FdItemCoa_p> FdItemCoa::findChildren(QString text)
237 {
238  QList<FdItemCoa_p> results;
239  for(int i = 0; i < mChildren.size(); ++i)
240  {
241  if(mChildren[i]->id().contains(text, Qt::CaseInsensitive) || mChildren[i]->label().contains(text, Qt::CaseInsensitive))
242  results << mChildren[i];
243  if(mChildren[i]->type() == CategoryType)
244  results << mChildren[i]->findChildren(text);
245  }
246  return results;
247 }
248 
252 int FdItemCoa::childRow(const FdItemCoa* child) const
253 {
254  for(int i = 0; i < mChildren.size(); ++i)
255  {
256  if(mChildren[i] == child)
257  return i;
258  }
259 
260  return -1;
261 }
262 
266 int FdItemCoa::childrenCount() const
267 {
268  return mChildren.count();
269 }
270 
271 int FdItemCoa::row() const
272 {
273  if(mParent)
274  return mParent->childRow(this);
275 
276  return 0;
277 }
278 
282 void FdItemCoa::appendChild(FdItemCoa_p item)
283 {
284  mChildren.append(item);
285 }
286 
290 bool FdItemCoa::insertChild(FdItemCoa_p item, int row)
291 {
292  if (row < 0 || row > childrenCount())
293  return false;
294 
295  mChildren.insert(row, item);
296  return true;
297 }
298 
302 bool FdItemCoa::removeChild(int row)
303 {
304  if(row < 0 || row > mChildren.size())
305  return false;
306 
307  mChildren.removeAt(row);
308  return true;
309 }
310 
314 QStringList FdItemCoa::childrenAccountsIds() const
315 {
316  QStringList ids;
317  for(int i = 0; i < mChildren.size(); ++i)
318  {
319  if(mChildren[i]->type() == AccountType)
320  ids << mChildren[i]->id();
321  else
322  ids.append(mChildren[i]->childrenAccountsIds());
323  }
324  return ids;
325 }
326 
330 QStringList FdItemCoa::childrenAccountsLabels() const
331 {
332  QStringList labels;
333  for(int i = 0; i < mChildren.size(); ++i)
334  {
335  if(mChildren[i]->type() == AccountType)
336  labels << mChildren[i]->label();
337  else
338  labels.append(mChildren[i]->childrenAccountsLabels());
339  }
340  return labels;
341 }
342 
343 QList<FdItemCoa_p> FdItemCoa::childrenAccounts() const
344 {
345  QList<FdItemCoa_p> accounts;
346  for(int i = 0; i < mChildren.size(); ++i)
347  {
348  if(mChildren[i]->type() == AccountType)
349  accounts << mChildren[i];
350  else
351  accounts.append(mChildren[i]->childrenAccounts());
352  }
353  return accounts;
354 }
355 
361 FdItemCoa_p FdItemCoa::childById(QString id) const
362 {
363  if(id != QString())
364  {
365  for(int i = 0; i < mChildren.size(); ++i)
366  {
367  if(id == mChildren[i]->id())
368  return mChildren[i];
369  else
370  {
371  FdItemCoa_p item = mChildren[i]->childById(id);
372  if(!item.isNull())
373  return item;
374  }
375  }
376  }
377  return FdItemCoa_p();
378 }
379 
385 FdItemCoa_p FdItemCoa::childByLabel(QString label) const
386 {
387  if(label != QString())
388  {
389  for(int i = 0; i < mChildren.size(); ++i)
390  {
391  if(label == mChildren[i]->label())
392  return mChildren[i];
393  else
394  {
395  FdItemCoa_p item = mChildren[i]->childByLabel(label);
396  if(!item.isNull())
397  return item;
398  }
399  }
400  }
401  return FdItemCoa_p();
402 }
403 
408 {
409  if(type() == AccountType)
411 
412  for (int i = 0; i < mChildren.size(); ++i)
413  mChildren[i]->resetBalance();
414 }
415 
420 {
421  double balance = 0;
422  for(int i = 0; i < mChildren.size(); ++i)
423  {
424  if(mChildren[i]->type() == CategoryType)
426 
427  balance += mChildren[i]->balance();
428  }
429  if(abs(balance) < 1e-2)
430  balance = 0;
431 
432  mBalance = balance;
433 
434  if(type() == CategoryType)
435  {
436  double initial = 0;
437  for(int i = 0; i < mChildren.size(); ++i)
438  {
439  initial += mChildren[i]->initialBalance();
440  }
441  if(abs(initial) < 1e-2)
442  initial = 0;
443 
444  mInitialBalance = initial;
445  }
446 }
447 
451 void FdItemCoa::debited(double amount)
452 {
453  mBalance += mSignature*amount;
454 }
455 
459 void FdItemCoa::credited(double amount)
460 {
461  mBalance -= mSignature*amount;
462 }
463 
464 QDomElement FdItemCoa::toXml(QDomDocument &document) const
465 {
466  QDomElement item = document.createElement("Coa");
467 
468  if(type() != RootType)
469  {
470  item.setAttribute("Id", mId);
471  item.setAttribute("Label", mLabel);
472  if(type() == CategoryType)
473  item.setTagName("Category");
474  else
475  {
476  item.setTagName("Account");
477  item.setAttribute("Signature", mSignature);
478  item.setAttribute("Initial", QString::number(mInitialBalance, 'f', 2));
479  item.setAttribute("Previous", QString::number(mPreviousBalance, 'f', 2));
480  }
481  if(mParent->type() == RootType)
482  item.setAttribute("OnBalanceSheet", mOnBalanceSheet);
483  }
484 
485  if(type() != AccountType)
486  {
487  for(int i = 0; i < mChildren.size(); ++i)
488  item.appendChild(mChildren[i]->toXml(document));
489  }
490 
491  return item;
492 }