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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
#include "journal.h"
#include "utils.h"
#include "valexpr.h"
#include "mask.h"
#include "format.h"
#include "acconf.h"
namespace ledger {
const string version = PACKAGE_VERSION;
bool transaction_t::use_effective_date = false;
transaction_t::~transaction_t()
{
DEBUG("ledger.memory.dtors", "dtor transaction_t");
if (cost) delete cost;
}
datetime_t transaction_t::actual_date() const
{
if (! is_valid(_date) && entry)
return entry->actual_date();
return _date;
}
datetime_t transaction_t::effective_date() const
{
if (! is_valid(_date_eff) && entry)
return entry->effective_date();
return _date_eff;
}
bool transaction_t::valid() const
{
if (! entry) {
DEBUG("ledger.validate", "transaction_t: ! entry");
return false;
}
if (state != UNCLEARED && state != CLEARED && state != PENDING) {
DEBUG("ledger.validate", "transaction_t: state is bad");
return false;
}
bool found = false;
for (transactions_list::const_iterator i = entry->transactions.begin();
i != entry->transactions.end();
i++)
if (*i == this) {
found = true;
break;
}
if (! found) {
DEBUG("ledger.validate", "transaction_t: ! found");
return false;
}
if (! account) {
DEBUG("ledger.validate", "transaction_t: ! account");
return false;
}
if (! amount.valid()) {
DEBUG("ledger.validate", "transaction_t: ! amount.valid()");
return false;
}
if (cost && ! cost->valid()) {
DEBUG("ledger.validate", "transaction_t: cost && ! cost->valid()");
return false;
}
if (flags & ~0x003f) {
DEBUG("ledger.validate", "transaction_t: flags are bad");
return false;
}
return true;
}
void entry_base_t::add_transaction(transaction_t * xact)
{
transactions.push_back(xact);
}
bool entry_base_t::remove_transaction(transaction_t * xact)
{
transactions.remove(xact);
return true;
}
// jww (2008-04-20): Migrate the Common Lisp version here!
bool entry_base_t::finalize()
{
// Scan through and compute the total balance for the entry. This
// is used for auto-calculating the value of entries with no cost,
// and the per-unit price of unpriced commodities.
value_t balance;
bool no_amounts = true;
bool saw_null = false;
for (transactions_list::const_iterator x = transactions.begin();
x != transactions.end();
x++)
if (! ((*x)->flags & TRANSACTION_VIRTUAL) ||
((*x)->flags & TRANSACTION_BALANCE)) {
amount_t * p = (*x)->cost ? (*x)->cost : &(*x)->amount;
if (*p) {
if (no_amounts) {
balance = *p;
no_amounts = false;
} else {
balance += *p;
}
if ((*x)->cost && (*x)->amount.commodity().annotated) {
annotated_commodity_t&
ann_comm(static_cast<annotated_commodity_t&>
((*x)->amount.commodity()));
if (ann_comm.details.price)
balance += ((*ann_comm.details.price) * (*x)->amount -
*((*x)->cost));
}
} else {
saw_null = true;
}
}
// If it's a null entry, then let the user have their fun
if (no_amounts)
return true;
// If there is only one transaction, balance against the basket
// account if one has been set.
if (journal && journal->basket && transactions.size() == 1) {
assert(balance.type() < value_t::BALANCE);
transaction_t * nxact = new transaction_t(journal->basket);
// The amount doesn't need to be set because the code below will
// balance this transaction against the other.
add_transaction(nxact);
nxact->flags |= TRANSACTION_CALCULATED;
}
// If the first transaction of a two-transaction entry is of a
// different commodity than the other, and it has no per-unit price,
// determine its price by dividing the unit count into the value of
// the balance. This is done for the last eligible commodity.
if (! saw_null && balance && balance.is_type(value_t::BALANCE) &&
balance.as_balance_lval().amounts.size() == 2) {
transactions_list::const_iterator x = transactions.begin();
commodity_t& this_comm = (*x)->amount.commodity();
balance_t& bal(balance.as_balance_lval());
balance_t::amounts_map::const_iterator this_amt =
bal.amounts.find(&this_comm);
balance_t::amounts_map::const_iterator other_amt =
bal.amounts.begin();
if (this_amt == other_amt)
other_amt++;
amount_t per_unit_cost =
amount_t((*other_amt).second / (*this_amt).second).unround();
for (; x != transactions.end(); x++) {
if ((*x)->cost || ((*x)->flags & TRANSACTION_VIRTUAL) ||
! (*x)->amount || (*x)->amount.commodity() != this_comm)
continue;
assert((*x)->amount);
balance -= (*x)->amount;
entry_t * entry = dynamic_cast<entry_t *>(this);
if ((*x)->amount.commodity() &&
! (*x)->amount.commodity().annotated)
(*x)->amount.annotate_commodity
(annotation_t(per_unit_cost.abs(),
entry ? optional<datetime_t>(entry->actual_date()) : none,
entry ? optional<string>(entry->code) : none));
(*x)->cost = new amount_t(- (per_unit_cost * (*x)->amount));
balance += *(*x)->cost;
}
}
// Walk through each of the transactions, fixing up any that we
// can, and performing any on-the-fly calculations.
bool empty_allowed = true;
for (transactions_list::const_iterator x = transactions.begin();
x != transactions.end();
x++) {
if (! (*x)->amount.is_null() ||
(((*x)->flags & TRANSACTION_VIRTUAL) &&
! ((*x)->flags & TRANSACTION_BALANCE)))
continue;
if (! empty_allowed)
throw new error("Only one transaction with null amount allowed per entry");
empty_allowed = false;
// If one transaction gives no value at all, its value will become
// the inverse of the value of the others. If multiple
// commodities are involved, multiple transactions will be
// generated to balance them all.
balance_t * bal = NULL;
switch (balance.type()) {
case value_t::BALANCE_PAIR:
bal = &(balance.as_balance_pair_lval().quantity());
// fall through...
case value_t::BALANCE:
if (! bal)
bal = &(balance.as_balance_lval());
if (bal->amounts.size() < 2) {
balance.cast(value_t::AMOUNT);
} else {
bool first = true;
for (balance_t::amounts_map::const_iterator i = bal->amounts.begin();
i != bal->amounts.end();
i++) {
amount_t amt = (*i).second;
amt.negate();
if (first) {
(*x)->amount = amt;
first = false;
} else {
transaction_t * nxact = new transaction_t((*x)->account);
add_transaction(nxact);
nxact->flags |= TRANSACTION_CALCULATED;
nxact->amount = amt;
}
balance += amt;
}
break;
}
// fall through...
case value_t::AMOUNT:
(*x)->amount = balance.as_amount_lval();
(*x)->amount.negate();
(*x)->flags |= TRANSACTION_CALCULATED;
balance += (*x)->amount;
break;
default:
break;
}
}
if (balance) {
error * err =
new balance_error("Entry does not balance",
new entry_context(*this, "While balancing entry:"));
err->context.push_front
(new value_context(balance, "Unbalanced remainder is:"));
throw err;
}
return true;
}
entry_t::entry_t(const entry_t& e)
: entry_base_t(e), _date(e._date), _date_eff(e._date_eff),
code(e.code), payee(e.payee)
{
DEBUG("ledger.memory.ctors", "ctor entry_t");
for (transactions_list::const_iterator i = transactions.begin();
i != transactions.end();
i++)
(*i)->entry = this;
}
bool entry_t::get_state(transaction_t::state_t * state) const
{
bool first = true;
bool hetero = false;
for (transactions_list::const_iterator i = transactions.begin();
i != transactions.end();
i++) {
if (first) {
*state = (*i)->state;
first = false;
}
else if (*state != (*i)->state) {
hetero = true;
break;
}
}
return ! hetero;
}
void entry_t::add_transaction(transaction_t * xact)
{
xact->entry = this;
entry_base_t::add_transaction(xact);
}
bool entry_t::valid() const
{
if (! is_valid(_date) || ! journal) {
DEBUG("ledger.validate", "entry_t: ! _date || ! journal");
return false;
}
for (transactions_list::const_iterator i = transactions.begin();
i != transactions.end();
i++)
if ((*i)->entry != this || ! (*i)->valid()) {
DEBUG("ledger.validate", "entry_t: transaction not valid");
return false;
}
return true;
}
auto_entry_t::auto_entry_t(const string& _predicate)
: predicate_string(_predicate)
{
DEBUG("ledger.memory.ctors", "ctor auto_entry_t");
predicate = new item_predicate<transaction_t>(predicate_string);
}
auto_entry_t::~auto_entry_t()
{
DEBUG("ledger.memory.dtors", "dtor auto_entry_t");
if (predicate)
delete predicate;
}
void auto_entry_t::extend_entry(entry_base_t& entry, bool post)
{
transactions_list initial_xacts(entry.transactions.begin(),
entry.transactions.end());
for (transactions_list::iterator i = initial_xacts.begin();
i != initial_xacts.end();
i++) {
if ((*predicate)(**i)) {
for (transactions_list::iterator t = transactions.begin();
t != transactions.end();
t++) {
amount_t amt;
if (! (*t)->amount.commodity()) {
if (! post)
continue;
amt = (*i)->amount * (*t)->amount;
} else {
if (post)
continue;
amt = (*t)->amount;
}
account_t * account = (*t)->account;
string fullname = account->fullname();
assert(! fullname.empty());
if (fullname == "$account" || fullname == "@account")
account = (*i)->account;
transaction_t * xact
= new transaction_t(account, amt, (*t)->flags | TRANSACTION_AUTO);
// Copy over details so that the resulting transaction is a mirror of
// the automated entry's one.
xact->state = (*t)->state;
xact->_date = (*t)->_date;
xact->_date_eff = (*t)->_date_eff;
xact->note = (*t)->note;
xact->beg_pos = (*t)->beg_pos;
xact->beg_line = (*t)->beg_line;
xact->end_pos = (*t)->end_pos;
xact->end_line = (*t)->end_line;
entry.add_transaction(xact);
}
}
}
}
account_t::~account_t()
{
DEBUG("ledger.memory.dtors", "dtor account_t " << this);
//assert(! data);
for (accounts_map::iterator i = accounts.begin();
i != accounts.end();
i++)
delete (*i).second;
}
account_t * account_t::find_account(const string& name,
const bool auto_create)
{
accounts_map::const_iterator i = accounts.find(name);
if (i != accounts.end())
return (*i).second;
char buf[256];
string::size_type sep = name.find(':');
assert(sep < 256|| sep == string::npos);
const char * first, * rest;
if (sep == string::npos) {
first = name.c_str();
rest = NULL;
} else {
std::strncpy(buf, name.c_str(), sep);
buf[sep] = '\0';
first = buf;
rest = name.c_str() + sep + 1;
}
account_t * account;
i = accounts.find(first);
if (i == accounts.end()) {
if (! auto_create)
return NULL;
account = new account_t(this, first);
account->journal = journal;
std::pair<accounts_map::iterator, bool> result
= accounts.insert(accounts_pair(first, account));
assert(result.second);
} else {
account = (*i).second;
}
if (rest)
account = account->find_account(rest, auto_create);
return account;
}
static inline
account_t * find_account_re_(account_t * account, const mask_t& regexp)
{
if (regexp.match(account->fullname()))
return account;
for (accounts_map::iterator i = account->accounts.begin();
i != account->accounts.end();
i++)
if (account_t * a = find_account_re_((*i).second, regexp))
return a;
return NULL;
}
account_t * journal_t::find_account_re(const string& regexp)
{
return find_account_re_(master, mask_t(regexp));
}
string account_t::fullname() const
{
if (! _fullname.empty()) {
return _fullname;
} else {
const account_t * first = this;
string fullname = name;
while (first->parent) {
first = first->parent;
if (! first->name.empty())
fullname = first->name + ":" + fullname;
}
_fullname = fullname;
return fullname;
}
}
std::ostream& operator<<(std::ostream& out, const account_t& account)
{
out << account.fullname();
return out;
}
bool account_t::valid() const
{
if (depth > 256 || ! journal) {
DEBUG("ledger.validate", "account_t: depth > 256 || ! journal");
return false;
}
for (accounts_map::const_iterator i = accounts.begin();
i != accounts.end();
i++) {
if (this == (*i).second) {
DEBUG("ledger.validate", "account_t: parent refers to itself!");
return false;
}
if (! (*i).second->valid()) {
DEBUG("ledger.validate", "account_t: child not valid");
return false;
}
}
return true;
}
journal_t::~journal_t()
{
DEBUG("ledger.memory.dtors", "dtor journal_t");
assert(master);
delete master;
// Don't bother unhooking each entry's transactions from the
// accounts they refer to, because all accounts are about to
// be deleted.
for (entries_list::iterator i = entries.begin();
i != entries.end();
i++)
if (! item_pool ||
((char *) *i) < item_pool || ((char *) *i) >= item_pool_end)
delete *i;
else
(*i)->~entry_t();
for (auto_entries_list::iterator i = auto_entries.begin();
i != auto_entries.end();
i++)
if (! item_pool ||
((char *) *i) < item_pool || ((char *) *i) >= item_pool_end)
delete *i;
else
(*i)->~auto_entry_t();
for (period_entries_list::iterator i = period_entries.begin();
i != period_entries.end();
i++)
if (! item_pool ||
((char *) *i) < item_pool || ((char *) *i) >= item_pool_end)
delete *i;
else
(*i)->~period_entry_t();
if (item_pool)
delete[] item_pool;
}
bool journal_t::add_entry(entry_t * entry)
{
entry->journal = this;
if (! run_hooks(entry_finalize_hooks, *entry, false) ||
! entry->finalize() ||
! run_hooks(entry_finalize_hooks, *entry, true)) {
entry->journal = NULL;
return false;
}
entries.push_back(entry);
for (transactions_list::const_iterator i = entry->transactions.begin();
i != entry->transactions.end();
i++)
if ((*i)->cost && (*i)->amount)
(*i)->amount.commodity().add_price(entry->date(),
*(*i)->cost / (*i)->amount);
return true;
}
bool journal_t::remove_entry(entry_t * entry)
{
bool found = false;
entries_list::iterator i;
for (i = entries.begin(); i != entries.end(); i++)
if (*i == entry) {
found = true;
break;
}
if (! found)
return false;
entries.erase(i);
entry->journal = NULL;
return true;
}
bool journal_t::valid() const
{
if (! master->valid()) {
DEBUG("ledger.validate", "journal_t: master not valid");
return false;
}
for (entries_list::const_iterator i = entries.begin();
i != entries.end();
i++)
if (! (*i)->valid()) {
DEBUG("ledger.validate", "journal_t: entry not valid");
return false;
}
for (commodity_pool_t::commodities_by_ident::const_iterator
i = amount_t::current_pool->commodities.begin();
i != amount_t::current_pool->commodities.end();
i++)
if (! (*i)->valid()) {
DEBUG("ledger.validate", "journal_t: commodity not valid");
return false;
}
return true;
}
void entry_context::describe(std::ostream& out) const throw()
{
if (! desc.empty())
out << desc << std::endl;
print_entry(out, entry, " ");
}
xact_context::xact_context(const ledger::transaction_t& _xact,
const string& desc) throw()
: file_context("", 0, desc), xact(_xact)
{
const ledger::paths_list& sources(xact.entry->journal->sources);
unsigned int x = 0;
for (ledger::paths_list::const_iterator i = sources.begin();
i != sources.end();
i++, x++)
if (x == xact.entry->src_idx) {
file = *i;
break;
}
line = xact.beg_line;
}
} // namespace ledger
|