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
|
#include "walk.h"
#include "format.h"
namespace ledger {
void calc_transactions::operator()(transaction_t * xact)
{
if (last_xact)
xact->total += last_xact->total;
if (inverted) {
xact->amount.negate();
xact->cost.negate();
}
xact->total += *xact;
xact->index = last_xact ? last_xact->index + 1 : 0;
(*handler)(xact);
if (inverted) {
xact->amount.negate();
xact->cost.negate();
}
last_xact = xact;
}
void collapse_transactions::report_cumulative_subtotal()
{
if (count == 1) {
(*handler)(last_xact);
} else {
assert(count > 1);
totals_account->total = subtotal;
balance_t result;
format_t::compute_total(result, details_t(totals_account));
for (amounts_map::const_iterator i = result.amounts.begin();
i != result.amounts.end();
i++) {
transaction_t * total_xact = new transaction_t(last_entry,
totals_account);
xact_temps.push_back(total_xact);
total_xact->amount = (*i).second;
total_xact->cost = (*i).second;
(*handler)(total_xact);
}
}
subtotal = 0;
count = 0;
}
void changed_value_transactions::operator()(transaction_t * xact)
{
if (last_xact) {
balance_t prev_bal;
balance_t cur_bal;
std::time_t current = xact ? xact->entry->date : std::time(NULL);
std::time_t prev_date = last_xact->entry->date;
format_t::compute_total(prev_bal, details_t(last_xact));
last_xact->entry->date = current;
format_t::compute_total(cur_bal, details_t(last_xact));
last_xact->entry->date = prev_date;
if (balance_t diff = cur_bal - prev_bal) {
entry_t * entry = new entry_t;
entry->payee = "Commodities revalued";
entry->date = current;
entry_temps.push_back(entry);
for (amounts_map::const_iterator i = diff.amounts.begin();
i != diff.amounts.end();
i++) {
transaction_t * temp_xact = new transaction_t(entry, NULL);
xact_temps.push_back(temp_xact);
temp_xact->amount = (*i).second;
temp_xact->total = (*i).second;
temp_xact->total.negate();
(*handler)(temp_xact);
}
}
}
if (xact)
(*handler)(xact);
last_xact = xact;
}
void subtotal_transactions::flush()
{
entry_t * entry = new entry_t;
char buf[256];
// jww (2004-08-10): allow for a format string here
std::strftime(buf, 255, "- %Y/%m/%d", std::gmtime(&finish));
entry->payee = buf;
entry_temps.push_back(entry);
for (balances_map::iterator i = balances.begin();
i != balances.end();
i++) {
entry->date = finish;
transaction_t temp(entry, (*i).first);
temp.total = (*i).second;
balance_t result;
format_t::compute_total(result, details_t(&temp));
entry->date = start;
for (amounts_map::const_iterator j = result.amounts.begin();
j != result.amounts.end();
j++) {
transaction_t * xact = new transaction_t(entry, (*i).first);
xact_temps.push_back(xact);
xact->amount = (*j).second;
xact->cost = (*j).second;
(*handler)(xact);
}
}
balances.clear();
}
void subtotal_transactions::operator()(transaction_t * xact)
{
if (balances.size() == 0) {
start = finish = xact->entry->date;
} else {
if (std::difftime(xact->entry->date, start) < 0)
start = xact->entry->date;
if (std::difftime(xact->entry->date, finish) > 0)
finish = xact->entry->date;
}
balances_map::iterator i = balances.find(xact->account);
if (i == balances.end())
balances.insert(balances_pair(xact->account, *xact));
else
(*i).second += *xact;
}
} // namespace ledger
|