summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneybudget.h
blob: 64613701749bb27153005e4416e6097f97815687 (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
/***************************************************************************
                          mymoneybudget.h
                             -------------------
    begin                : Sun Jan 22 2006
    copyright            : (C) 2006 by Darren Gould
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef MYMONEYBUDGET_H
#define MYMONEYBUDGET_H

// ----------------------------------------------------------------------------
// QT Includes

#include <tqmap.h>
#include <tqvaluelist.h>
#include <tqstring.h>
class TQDomElement;
class TQDomDocument;

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

#include <kmymoney/mymoneyobject.h>
#include <kmymoney/mymoneyaccount.h>
#include <kmymoney/mymoneymoney.h>
#include <kmymoney/export.h>

/**
  * This class defines a Budget within the MyMoneyEngine.  The Budget class
  * contains all the configuration parameters needed to run a Budget, plus
  * XML serialization.
  *
  * As noted above, this class only provides a Budget DEFINITION.  The
  * generation and presentation of the Budget itself are left to higher
  * level classes.
  *
  * @author Darren Gould <[email protected]>
  */
class KMYMONEY_EXPORT MyMoneyBudget: public MyMoneyObject
{
public:
  MyMoneyBudget(void);
  ~MyMoneyBudget();
  MyMoneyBudget(const TQString& _name);
  /**
    * This constructor creates an object based on the data found in the
    * TQDomElement referenced by @p node. If problems arise, the @p id of
    * the object is cleared (see MyMoneyObject::clearId()).
    */
  MyMoneyBudget(const TQDomElement& node);

  /**
    * This constructor creates an object based on the data found in the
    * MyMoneyBudget budget object.
    */
  MyMoneyBudget(const TQString& id, const MyMoneyBudget& budget);

  /**
    * Helper class for MyMoneyBudget
    *
    * This is an abstraction of the PERIOD stored in the BUDGET/ACCOUNT tag in XML
    *
    * @author Darren Gould
    */
  class PeriodGroup
  {
  public:
    // get functions
    const TQDate&    startDate ( void ) const { return m_start; }
    const MyMoneyMoney& amount( void ) const { return m_amount; }

    // set functions
    void setStartDate  ( const TQDate& _start )    { m_start  = _start; }
    void setAmount( const MyMoneyMoney& _amount ) { m_amount = _amount;}

    bool operator == (const PeriodGroup &r) const { return (m_start == r.m_start && m_amount == r.m_amount); }

 private:
    TQDate         m_start;
    MyMoneyMoney  m_amount;
  };

  /**
    * Helper class for MyMoneyBudget
    *
    * This is an abstraction of the Account Data stored in the BUDGET tag in XML
    *
    * @author Darren Gould
    */
  class AccountGroup
  {
  public:
    typedef enum
    {
      eNone = 0,
      eMonthly,
      eMonthByMonth,
      eYearly,
      eMax
    } eBudgetLevel;

    static const TQStringList kBudgetLevelText;

  public:
    AccountGroup() : m_budgetlevel(eNone), m_budgetsubaccounts(false) {}

    // get functions
    const TQString& id( void ) const { return m_id; }
    bool budgetSubaccounts( void ) const { return m_budgetsubaccounts; }
    eBudgetLevel budgetLevel( void ) const { return m_budgetlevel; }
    const PeriodGroup& period( const TQDate &_date ) const { return m_periods[_date]; }
    const TQMap<TQDate, PeriodGroup>& getPeriods( void ) const { return m_periods; }
    void clearPeriods(void) { m_periods.clear(); }
    const MyMoneyMoney balance( void ) const
    {
      MyMoneyMoney balance;

      TQMap<TQDate, PeriodGroup>::const_iterator it;
      for(it = m_periods.begin(); it != m_periods.end(); ++it)
      {
        balance += (*it).amount();
      }
      return balance;
    };

    const MyMoneyMoney totalBalance(void) const
    {
      MyMoneyMoney bal = balance();
      switch(m_budgetlevel) {
        default:
          break;
        case eMonthly:
          bal = bal * 12;
          break;
      }
      return bal;
    }

    // set functions
    void setId( TQString _id ) { m_id = _id; }
    void setBudgetLevel( eBudgetLevel _level ) { m_budgetlevel = _level; }
    void setBudgetSubaccounts( bool _b ) { m_budgetsubaccounts = _b; }
    void addPeriod( const TQDate& _date, PeriodGroup &period ) { m_periods[_date] = period; }

    // This member adds the value of another account group
    // m_budgetlevel is adjusted to the larger one of both
    // m_budgetsubaccounts remains unaffected
    AccountGroup operator += (const AccountGroup& r);

    bool operator == (const AccountGroup &r) const;

    bool isZero(void) const;

  protected:
    void convertToMonthly(void);
    void convertToYearly(void);
    void convertToMonthByMonth(void);

  private:
    TQString m_id;

    eBudgetLevel             m_budgetlevel;
    bool                     m_budgetsubaccounts;
    TQMap<TQDate, PeriodGroup> m_periods;
  };

  /**
   * This operator tests for equality of two MyMoneyBudget objects
   */
  bool operator == (const MyMoneyBudget &) const;

  // Simple get operations
  const TQString& name(void) const { return m_name; }
  const TQDate& budgetStart(void) const { return m_start; }
  TQString id(void) const { return m_id; }
  const AccountGroup & account(const TQString _id) const;
  bool contains(const TQString _id) const { return m_accounts.contains(_id); }
  TQValueList<AccountGroup> getaccounts(void) const { return m_accounts.values(); }

  // Simple set operations
  void setName(const TQString& _name) { m_name = _name; }
  void setBudgetStart(const TQDate& _start);
  void setAccount(const AccountGroup &_account, const TQString _id);

  /**
    * This method writes this Budget to the DOM element @p e,
    * within the DOM document @p doc.
    *
    * @param e The element which should be populated with info from this Budget
    * @param doc The document which we can use to create new sub-elements
    *              if needed
    */
  void write(TQDomElement& e, TQDomDocument *doc) const;

  /**
    * This method reads a Budget from the DOM element @p e, and
    * populates this Budget with the results.
    *
    * @param e The element from which the Budget should be read
    *
    * @return bool True if a Budget was successfully loaded from the
    *    element @p e.  If false is returned, the contents of this Budget
    *    object are undefined.
    */
  bool read(const TQDomElement& e);

  /**
    * This method creates a TQDomElement for the @p document
    * under the parent node @p parent.  (This version overwrites the
    * MMObject base class.)
    *
    * @param document reference to TQDomDocument
    * @param parent reference to TQDomElement parent node
    */
  virtual void writeXML(TQDomDocument& document, TQDomElement& parent) const;

  /**
    * This method checks if a reference to the given object exists. It returns,
    * a @p true if the object is referencing the one requested by the
    * parameter @p id and the balance() returned is zero.
    * If it does not, this method returns @p false.
    *
    * @param id id of the object to be checked for references
    * @retval true This object references object with id @p id.
    * @retval false This object does not reference the object with id @p id.
    */
  virtual bool hasReferenceTo(const TQString& id) const;

  /**
    * This member removes all references to object identified by @p id. Used
    * to remove objects which are about to be removed from the engine.
    */
  void removeReference(const TQString& id);

private:
  /**
    * The user-assigned name of the Budget
    */
  TQString m_name;

  /**
    * The user-assigned year of the Budget
    */
  TQDate m_start;

  /**
    * Map the budgeted accounts
    *
    * Each account Id is stored against the AccountGroup information
    */
  TQMap<TQString, AccountGroup> m_accounts;
};

#endif // MYMONEYBudget_H