summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneybudget.cpp
blob: 44773a64cf1192871d5cce97a5bef4dce2acfc3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/***************************************************************************
                          mymoneybudget.cpp
                             -------------------
    begin                : Sun July 4 2004
    copyright            : (C) 2004-2005 by Ace Jones
    email                : acejones@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// ----------------------------------------------------------------------------
// TQt Includes

#include <tqstring.h>
#include <tqdom.h>

// ----------------------------------------------------------------------------
// TDE Includes

// ----------------------------------------------------------------------------
// Project Includes

#include "mymoneybudget.h"

const TQStringList MyMoneyBudget::AccountGroup::kBudgetLevelText = TQStringList::split(",","none,monthly,monthbymonth,yearly,invalid",true);
const int BUDGET_VERSION = 2;

bool MyMoneyBudget::AccountGroup::isZero(void) const
{
  return (!m_budgetsubaccounts && m_budgetlevel == eMonthly && balance().isZero());
}

void MyMoneyBudget::AccountGroup::convertToMonthly(void)
{
  MyMoneyBudget::PeriodGroup period;

  switch(m_budgetlevel) {
    case eYearly:
    case eMonthByMonth:
      period = *(m_periods.begin());         // make him monthly
      period.setAmount(balance() / MyMoneyMoney(12,1));
      clearPeriods();
      addPeriod(period.startDate(), period);
      break;
    default:
      break;
  }
  m_budgetlevel = eMonthly;
}

void MyMoneyBudget::AccountGroup::convertToYearly(void)
{
  MyMoneyBudget::PeriodGroup period;

  switch(m_budgetlevel) {
    case eMonthByMonth:
    case eMonthly:
      period = *(m_periods.begin());         // make him monthly
      period.setAmount(totalBalance());
      clearPeriods();
      addPeriod(period.startDate(), period);
      break;
    default:
      break;
  }
  m_budgetlevel = eYearly;
}

void MyMoneyBudget::AccountGroup::convertToMonthByMonth(void)
{
  MyMoneyBudget::PeriodGroup period;
  TQDate date;

  switch(m_budgetlevel) {
    case eMonthByMonth:
    case eMonthly:
      period = *(m_periods.begin());
      period.setAmount(totalBalance() / MyMoneyMoney(12,1));
      clearPeriods();
      date = period.startDate();
      for(int i = 0; i < 12; ++i) {
        addPeriod(date, period);
        date = date.addMonths(1);
        period.setStartDate(date);
      }
      break;
    default:
      break;
  }
  m_budgetlevel = eYearly;
}

MyMoneyBudget::AccountGroup MyMoneyBudget::AccountGroup::operator += (const MyMoneyBudget::AccountGroup& _r)
{
  MyMoneyBudget::AccountGroup r(_r);

  // make both operands based on the same budget level
  if(m_budgetlevel != r.m_budgetlevel) {
    if(m_budgetlevel == eMonthly) {         // my budget is monthly
      if(r.m_budgetlevel == eYearly) {      // his his yearly
        r.convertToMonthly();
      } else if(r.m_budgetlevel == eMonthByMonth) { // his is month by month
        convertToMonthByMonth();
      }
    } else if(m_budgetlevel == eYearly) {   // my budget is yearly
      if(r.m_budgetlevel == eMonthly) {     // his is monthly
        r.convertToYearly();
      } else if(r.m_budgetlevel == eMonthByMonth) { // his is month by month
        convertToMonthByMonth();
      }
    } else if(m_budgetlevel == eMonthByMonth) {   // my budget is month by month
      r.convertToMonthByMonth();
    }
  }

  // now both budgets should be of the same type and we simply need
  // to iterate over the period list and add the values
  TQMap<TQDate, MyMoneyBudget::PeriodGroup> periods = m_periods;
  TQMap<TQDate, MyMoneyBudget::PeriodGroup> rPeriods = r.m_periods;
  TQMap<TQDate, MyMoneyBudget::PeriodGroup>::const_iterator it_p;
  TQMap<TQDate, MyMoneyBudget::PeriodGroup>::const_iterator it_pr;
  m_periods.clear();
  it_p = periods.begin();
  it_pr = rPeriods.begin();
  TQDate date = (*it_p).startDate();
  while(it_p != periods.end()) {
    MyMoneyBudget::PeriodGroup period = *it_p;
    if(it_pr != rPeriods.end()) {
      period.setAmount(period.amount() + (*it_pr).amount());
      ++it_pr;
    }
    addPeriod(date, period);
    date = date.addMonths(1);
    ++it_p;
  }
  return *this;
}

bool MyMoneyBudget::AccountGroup::operator == (const AccountGroup &r) const
{
  return (m_id == r.m_id
       && m_budgetlevel == r.m_budgetlevel
       && m_budgetsubaccounts == r.m_budgetsubaccounts
       && m_periods.keys() == r.m_periods.keys()
       && m_periods.values() == r.m_periods.values());
}

MyMoneyBudget::MyMoneyBudget(void) :
  m_name("Unconfigured Budget")
{
}

MyMoneyBudget::MyMoneyBudget(const TQString& _name) :
  m_name(_name)
{
}

MyMoneyBudget::MyMoneyBudget(const TQDomElement& node) :
  MyMoneyObject(node)
{
  if(!read(node))
    clearId();
}

MyMoneyBudget::MyMoneyBudget(const TQString& id, const MyMoneyBudget& budget)
{
  *this = budget;
  m_id = id;
}

MyMoneyBudget::~MyMoneyBudget()
{
}

bool MyMoneyBudget::operator == (const MyMoneyBudget& right) const
{
  return (MyMoneyObject::operator==(right) &&
      (m_accounts.count() == right.m_accounts.count()) &&
      (m_accounts.keys() == right.m_accounts.keys()) &&
      (m_accounts.values() == right.m_accounts.values()) &&
      (m_name == right.m_name) &&
      (m_start == right.m_start) );
}

void MyMoneyBudget::write(TQDomElement& e, TQDomDocument *doc) const
{
  writeBaseXML(*doc, e);

  e.setAttribute("name",  m_name);
  e.setAttribute("start", m_start.toString(TQt::ISODate) );
  e.setAttribute("version", BUDGET_VERSION);

  TQMap<TQString, AccountGroup>::const_iterator it;
  for(it = m_accounts.begin(); it != m_accounts.end(); ++it) {
    // only add the account if there is a budget entered
    if(!(*it).balance().isZero()) {
      TQDomElement domAccount = doc->createElement("ACCOUNT");
      domAccount.setAttribute("id", it.key());
      domAccount.setAttribute("budgetlevel", AccountGroup::kBudgetLevelText[it.data().budgetLevel()]);
      domAccount.setAttribute("budgetsubaccounts", it.data().budgetSubaccounts());

      const TQMap<TQDate, PeriodGroup> periods = it.data().getPeriods();
      TQMap<TQDate, PeriodGroup>::const_iterator it_per;
      for(it_per = periods.begin(); it_per != periods.end(); ++it_per) {
        if(!(*it_per).amount().isZero()) {
          TQDomElement domPeriod = doc->createElement("PERIOD");

          domPeriod.setAttribute("amount", (*it_per).amount().toString());
          domPeriod.setAttribute("start", (*it_per).startDate().toString(TQt::ISODate));
          domAccount.appendChild(domPeriod);
        }
      }

      e.appendChild(domAccount);
    }
  }
}

bool MyMoneyBudget::read(const TQDomElement& e)
{
  // The goal of this reading method is 100% backward AND 100% forward
  // compatability.  Any Budget ever created with any version of KMyMoney
  // should be able to be loaded by this method (as long as it's one of the
  // Budget types supported in this version, of course)

  bool result = false;

  if ("BUDGET" == e.tagName())
  {
    result = true;
    m_name  = e.attribute("name");
    m_start = TQDate::fromString(e.attribute("start"), TQt::ISODate);
    m_id    = e.attribute("id");

    TQDomNode child = e.firstChild();
    while(!child.isNull() && child.isElement())
    {
      TQDomElement c = child.toElement();

      AccountGroup account;

      if("ACCOUNT" == c.tagName()) {
        if(c.hasAttribute("id"))
          account.setId(c.attribute("id"));

        if(c.hasAttribute("budgetlevel")) {
          int i = AccountGroup::kBudgetLevelText.findIndex(c.attribute("budgetlevel"));
          if ( i != -1 )
            account.setBudgetLevel(static_cast<AccountGroup::eBudgetLevel>(i));
        }

        if(c.hasAttribute("budgetsubaccounts"))
          account.setBudgetSubaccounts(c.attribute("budgetsubaccounts").toUInt());
      }

      TQDomNode period = c.firstChild();
      while(!period.isNull() && period.isElement())
      {
        TQDomElement per = period.toElement();
        PeriodGroup pGroup;

        if("PERIOD" == per.tagName() && per.hasAttribute("amount") && per.hasAttribute("start"))
        {
          pGroup.setAmount( MyMoneyMoney(per.attribute("amount")) );
          pGroup.setStartDate( TQDate::fromString(per.attribute("start"), TQt::ISODate) );
          account.addPeriod(pGroup.startDate(), pGroup);
        }

        period = period.nextSibling();
      }

      m_accounts[account.id()] = account;

      child = child.nextSibling();
    }
  }

  return result;
}

void MyMoneyBudget::writeXML(TQDomDocument& document, TQDomElement& parent) const
{
  TQDomElement el = document.createElement("BUDGET");
  write(el,&document);
  parent.appendChild(el);
}

bool MyMoneyBudget::hasReferenceTo(const TQString& id) const
{
  // return true if we have an assignment for this id
  return (m_accounts.contains(id));
}

void MyMoneyBudget::removeReference(const TQString& id)
{
  if(m_accounts.contains(id)) {
    m_accounts.remove(id);
  }
}

void MyMoneyBudget::setAccount(const AccountGroup &_account, const TQString _id)
{
  if(_account.isZero()) {
    m_accounts.remove(_id);
  } else {
    // make sure we store a correct id
    AccountGroup account(_account);
    if(account.id() != _id)
      account.setId(_id);
    m_accounts[_id] = account;
  }
}

const MyMoneyBudget::AccountGroup& MyMoneyBudget::account(const TQString _id) const
{
  static AccountGroup empty;

  if ( m_accounts.contains(_id) )
    return m_accounts[_id];
  return empty;
}

void MyMoneyBudget::setBudgetStart(const TQDate& _start)
{
  TQDate oldDate = TQDate(m_start.year(), m_start.month(), 1);
  m_start = TQDate(_start.year(), _start.month(), 1);
  if(oldDate.isValid()) {
    int adjust = ((m_start.year() - oldDate.year())*12) + (m_start.month() - oldDate.month());
    TQMap<TQString, AccountGroup>::iterator it;
    for(it = m_accounts.begin(); it != m_accounts.end(); ++it) {
      const TQMap<TQDate, PeriodGroup> periods = (*it).getPeriods();
      TQMap<TQDate, PeriodGroup>::const_iterator it_per;
      (*it).clearPeriods();
      for(it_per = periods.begin(); it_per != periods.end(); ++it_per) {
        PeriodGroup pgroup = (*it_per);
        pgroup.setStartDate(pgroup.startDate().addMonths(adjust));
        (*it).addPeriod(pgroup.startDate(), pgroup);
      }
    }
  }
}