summaryrefslogtreecommitdiff
path: root/item.cc
blob: 11203c10d1d00da8e5e46779f3587a5efb2d5634 (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
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
#include "item.h"
#include "expr.h"

namespace ledger {

// jww (2004-07-21): If format.show_empty is set, then include all
// subaccounts, empty, balanced or no

item_t * walk_accounts(const account_t * account,
		       const node_t *    predicate,
		       const bool        show_subtotals)
{
  item_t * item = new item_t;
  item->account = account;

  std::time_t latest = 0;
  for (transactions_list::const_iterator i
	 = std::find_if(account->transactions.begin(),
			account->transactions.end(),
			value_predicate(predicate));
       i != account->transactions.end();
       i = std::find_if(++i, account->transactions.end(),
			value_predicate(predicate))) {
    if (std::difftime(latest, (*i)->entry->date) < 0)
      latest = (*i)->entry->date;

    item->value += *(*i);
    if (show_subtotals)
      item->total += *(*i);
  }
  item->date = latest;

  for (accounts_map::const_iterator i = account->accounts.begin();
       i != account->accounts.end();
       i++) {
    item_t * subitem = walk_accounts((*i).second, predicate, show_subtotals);
    subitem->parent = item;

    if (std::difftime(item->date, subitem->date) < 0)
      item->date = subitem->date;

    if (show_subtotals)
      item->total += subitem->total;
    if (show_subtotals ? subitem->total : subitem->value)
      item->subitems.push_back(subitem);
  }

  return item;
}

static inline void sum_items(const item_t * top,
			     const bool     show_subtotals,
			     item_t *	    item)
{
  if (top->account == item->account) {
    item->value += top->value;
    if (show_subtotals)
      item->total += top->value;
  }

  for (items_deque::const_iterator i = top->subitems.begin();
       i != top->subitems.end();
       i++)
    sum_items(*i, show_subtotals, item);
}

item_t * walk_items(const item_t *    top,
		    const account_t * account,
		    const node_t *    predicate,
		    const bool	      show_subtotals)
{
  item_t * item = new item_t;
  item->account = account;

  sum_items(top, show_subtotals, item);

  for (accounts_map::const_iterator i = account->accounts.begin();
       i != account->accounts.end();
       i++) {
    item_t * subitem = walk_items(top, (*i).second, predicate, show_subtotals);
    subitem->parent = item;

    if (show_subtotals)
      item->total += subitem->total;

    if (show_subtotals ? subitem->total : subitem->value)
      item->subitems.push_back(subitem);
  }

  return item;
}

item_t * walk_entries(entries_list::const_iterator begin,
		      entries_list::const_iterator end,
		      const node_t * predicate,
		      const bool     show_related,
		      const bool     show_inverted)
{
  unsigned int	  count  = 0;
  item_t *	  result = NULL;
  value_predicate pred_obj(predicate);

  for (entries_list::const_iterator i = std::find_if(begin, end, pred_obj);
       i != end;
       i = std::find_if(++i, end, pred_obj)) {
    item_t * item = NULL;

    for (transactions_list::const_iterator j
	   = std::find_if((*i)->transactions.begin(),
			  (*i)->transactions.end(), pred_obj);
	 j != (*i)->transactions.end();
	 j = std::find_if(++j,
		transactions_list::const_iterator((*i)->transactions.end()),
		pred_obj)) {
      assert(*i == (*j)->entry);

      if (! item) {
	item = new item_t;
	item->index = count++;
	item->date  = (*i)->date;
	item->payee = (*i)->payee;
      }

      // If show_inverted is true, it implies show_related.
      if (! show_inverted) {
	item_t * subitem = new item_t;
	subitem->parent  = item;
	subitem->date    = item->date;
	subitem->payee   = item->payee;
	subitem->account = (*j)->account;
	subitem->value   = *(*j);
	item->value += subitem->value;
	item->subitems.push_back(subitem);
      }

      if (show_related)
	for (transactions_list::iterator k = (*i)->transactions.begin();
	     k != (*i)->transactions.end();
	     k++)
	  if (*k != *j && ! ((*k)->flags & TRANSACTION_VIRTUAL)) {
	    item_t * subitem = new item_t;
	    subitem->parent  = item;
	    subitem->date    = item->date;
	    subitem->payee   = item->payee;
	    subitem->account = (*k)->account;
	    subitem->value   = *(*k);
	    if (show_inverted)
	      subitem->value.negate();
	    item->subitems.push_back(subitem);
	  }
    }

    if (item) {
      if (! result)
	result = new item_t;
      item->parent = result;
      result->subitems.push_back(item);
    }
  }

  return result;
}

struct cmp_items {
  const node_t * sort_order;

  cmp_items(const node_t * _sort_order) : sort_order(_sort_order) {
    assert(sort_order);
  }

  bool operator()(const item_t * left, const item_t * right) const {
    assert(left);
    assert(right);
    return sort_order->compute(left) < sort_order->compute(right);
  }
};

void item_t::sort(const node_t * sort_order)
{
  std::stable_sort(subitems.begin(), subitems.end(), cmp_items(sort_order));
}

} // namespace ledger