blob: 301e109beb26545c95c0427cf39abba354f3b8b8 (
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
|
#include "ledger.h"
namespace ledger {
commodities_t commodities;
accounts_t accounts;
ledger_t ledger;
bool use_warnings = false;
void entry::print(std::ostream& out) const
{
char buf[32];
std::strftime(buf, 31, "%Y.%m.%d ", std::localtime(&date));
out << buf;
if (cleared)
out << "* ";
if (! code.empty())
out << '(' << code << ") ";
if (! desc.empty())
out << " " << desc;
out << std::endl;
for (std::list<transaction *>::const_iterator i = xacts.begin();
i != xacts.end();
i++) {
out << " ";
std::string acct_name;
for (account * acct = (*i)->acct;
acct;
acct = acct->parent) {
if (acct_name.empty())
acct_name = acct->name;
else
acct_name = acct->name + ":" + acct_name;
}
out.width(30);
out << std::left << acct_name << " ";
out.width(10);
out << std::right << (*i)->cost->as_str(true);
if (! (*i)->note.empty())
out << " ; " << (*i)->note;
out << std::endl;
}
}
bool entry::validate() const
{
totals balance;
for (std::list<transaction *>::const_iterator i = xacts.begin();
i != xacts.end();
i++) {
balance.credit((*i)->cost->value());
}
if (balance) {
std::cerr << "Totals are:" << std::endl;
balance.print(std::cerr);
std::cerr << std::endl;
}
return ! balance; // must balance to 0.0
}
void totals::credit(const totals& other)
{
for (const_iterator_t i = other.amounts.begin();
i != other.amounts.end();
i++) {
credit((*i).second);
}
}
totals::operator bool() const
{
for (const_iterator_t i = amounts.begin(); i != amounts.end(); i++)
if (*((*i).second))
return true;
return false;
}
void totals::print(std::ostream& out) const
{
bool first = true;
for (const_iterator_t i = amounts.begin(); i != amounts.end(); i++)
if (*((*i).second)) {
if (first)
first = false;
else
out << std::endl;
out.width(20);
out << std::right << *((*i).second);
}
}
amount * totals::value(const std::string& commodity)
{
// Render all of the amounts into the given commodity. This
// requires known prices for each commodity.
amount * total = create_amount((commodity + " 0.00").c_str());
for (iterator_t i = amounts.begin(); i != amounts.end(); i++)
*total += *((*i).second);
return total;
}
// Print out the entire ledger that was read in, sorted by date.
// This can be used to "wash" ugly ledger files.
void print_ledger(int argc, char *argv[], std::ostream& out)
{
// Sort the list of entries by date, then print them in order.
std::sort(ledger.begin(), ledger.end(), cmp_entry_date());
for (std::vector<entry *>::const_iterator i = ledger.begin();
i != ledger.end();
i++) {
(*i)->print(out);
}
}
} // namespace ledger
|