added comment to the account.h
[account.git] / account.h
1
2 #include <AXE/String.h>
3 #include <AXE/date.h>
4
5 #include "postscript.h"
6
7 // An account number is a number in two parts, separated by a dot (.).
8 // The first part is a 3-digit group number and the second part is a
9 // 2-digit account number within the group.
10
11 // Operations include input and output, conversion to or from a String
12 // and test for equality.
13
14 // added comment for git test
15
16 class acc_nr
17 {
18    unsigned short group, nr;
19
20 public:
21
22    acc_nr()
23    {
24       group = 0; nr = 0;
25    }
26
27    acc_nr(unsigned short g, unsigned short n)
28    {
29       group = g;
30       nr    = n;
31    }
32
33    acc_nr(String &s);
34    operator String();
35
36    int operator==(acc_nr a)
37    {
38       return group == a.group && nr == a.nr;
39    }
40
41    friend std::istream & operator>>(std::istream &, acc_nr&);
42    friend std::ostream & operator<<(std::ostream &, acc_nr);
43 };
44
45 //typedef enum { DEBIT, CREDIT } direction;
46
47 class amount
48 {
49    unsigned long  value;
50
51 public:
52
53    amount()
54    {
55       value = 0;
56    }
57
58    amount(unsigned long l)
59    {
60       value = l;
61    }
62
63    operator long()
64    {
65       return value;
66    }
67
68    amount operator+(amount a)
69    {
70       amount sum;
71
72       sum.value = value + a.value;
73       return sum;
74    }
75
76    amount &operator+=(amount a)
77    {
78       value += a.value;
79
80       return *this;
81    }
82
83    String to_string();
84
85    friend std::istream & operator>>(std::istream &, amount &);
86    friend std::ostream & operator<<(std::ostream &, amount);
87 };
88
89 class balance
90 {
91    amount Debit, Credit;
92
93 public:
94
95    balance()
96    {
97       Debit = 0; Credit = 0;
98    }
99
100    balance(amount D, amount C)
101    {
102       Debit = D;
103       Credit = C;
104    }
105
106    amount debit()
107    {
108       return Debit;
109    }
110
111    amount credit()
112    {
113       return Credit;
114    }
115
116    balance operator~()          //  Calculate balance, leaving either
117    {                            //   Debit or Credit 0.
118       amount  saldo;
119       balance b;
120
121       saldo = Debit - Credit;
122       if (long(Debit) > long(Credit))
123          b.Debit = Debit - Credit;
124       else
125          b.Credit = Credit - Debit;
126
127       return b;
128    }
129
130    balance operator-()          //   Reverse the balance
131    {
132       balance b;
133
134       b.Debit = Credit;
135       b.Credit = Debit;
136
137       return b;
138    }
139
140    balance operator+(balance b)
141    {
142       balance sum;
143
144       sum.Debit = Debit + b.Debit;
145       sum.Credit = Credit + b.Credit;
146
147       return sum;
148    }
149    balance &operator+=(balance b)
150    {
151       Debit += b.Debit;
152       Credit += b.Credit;
153
154       return *this;
155    }
156
157    operator int()      //   Check equality
158    {
159       return Debit == Credit;
160    }
161
162    friend std::ostream & operator<<(std::ostream &, balance );
163 };
164
165 class Booking
166 {
167    int   Bnumber;
168    date  booked;
169    String comment;
170
171 public:
172
173    int number()
174    {
175       return Bnumber;
176    }
177
178    operator date()
179    {
180       return booked;
181    }
182
183    Booking &operator=(Booking &b)
184    {
185       Bnumber = b.Bnumber;
186       booked  = b.booked;
187       comment = b.comment;
188
189       return *this;
190    }
191
192    friend std::istream & operator>>(std::istream &, Booking &);
193    friend std::ostream & operator<<(std::ostream &, Booking &);
194 };
195
196 class Mutation
197 {
198    int booking_nr;
199    acc_nr account;
200    balance bal;
201
202 public:
203
204    Mutation()
205    {
206    }
207    Mutation(int BK_nr)
208    {
209       booking_nr = BK_nr;
210    }
211    int number()
212    {
213       return booking_nr;
214    }
215    operator acc_nr()
216    {
217       return account;
218    }
219    operator balance()
220    {
221       return bal;
222    }
223    friend std::istream & operator>>(std::istream &, Mutation &);
224    friend std::ostream & operator<<(std::ostream &, Mutation &);
225 };
226
227 typedef enum {BALANCE, COST} acc_kind;
228
229 class Account
230 {
231    acc_nr   Anumber;
232    String   name;
233    acc_kind kind;
234    balance  Bal;
235
236 public:
237
238    Account()
239    {
240       kind = BALANCE;
241    }
242
243    Account(acc_nr A)
244    {
245       Anumber = A;
246       kind    = BALANCE;
247    }
248
249    Account& operator=(Account &a)
250    {
251       Anumber  = a.Anumber;
252       name     = a.name;
253       kind     = a.kind;
254       Bal      = a.Bal;
255
256       return *this;
257    }
258
259    acc_nr Number()
260    {
261       return Anumber;
262    }
263
264    String &Name()
265    {
266       return name;
267    }
268
269    int operator==(acc_nr Anr)
270    {
271       return Anumber == Anr;
272    }
273
274    int operator!=(acc_nr Anr)
275    {
276       return !(Anumber == Anr);
277    }
278    Account &operator+=(Mutation &mut)
279    {
280       Bal += mut;
281
282       return *this;
283    }
284
285    int BAL()
286    {
287       return kind == BALANCE;
288    }
289
290    operator balance()
291    {
292       return Bal;
293    }
294
295    void sheet(std::ostream &);
296    void Postscript_sheet(PostScript &);
297
298    friend std::istream & operator>>(std::istream&, Account&);
299    friend std::ostream & operator<<(std::ostream&, Account&);
300 };
301
302 class Ledger
303 {
304    Account accs[200];
305    int     nr_accs;
306    int     idx;
307
308 public:
309
310    Ledger()
311    {
312       nr_accs = 0;
313    }
314
315    Account &operator[](acc_nr Anr);
316
317 /*
318    void operator+=(Account &a)
319    {
320       accs[nr_accs++] = a;
321    }
322 */
323
324    void read(char *filename);
325
326    void start()
327    {
328       idx = 0;
329    }
330
331    Account &next()
332    {
333       static Account bogus = Account(acc_nr(1000, 1000));
334
335       if (idx == nr_accs)
336          return bogus;
337       else
338       {
339          idx++;
340          return accs[idx-1];
341       }
342    }
343
344    friend std::ostream & operator<<(std::ostream &, Ledger&);
345
346    void accounts_report(const char *);
347    void saldi_report(const char *, date period_begin, date period_end);
348    void XML_saldi(std::ostream &, date period_begin, date period_end);
349 };
350