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