summaryrefslogtreecommitdiff
path: root/register.cc
blob: 5b5ea1dd7e1091a2e0824fe5c39a074021343d86 (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
#include "ledger.h"

#include <unistd.h>

namespace ledger {

extern bool show_cleared;

extern std::time_t begin_date;
extern bool have_beginning;
extern std::time_t end_date;
extern bool have_ending;

//////////////////////////////////////////////////////////////////////
//
// Register printing code
//

void print_register(int argc, char **argv, std::ostream& out)
{
  optind = 1;
#if 0
  int c;
  while (-1 != (c = getopt(argc, argv, ""))) {
    switch (char(c)) {
    }
  }
#endif

  // Find out which account this register is to be printed for

  account * acct = main_ledger.find_account(argv[optind++], false);
  if (! acct) {
    std::cerr << "Error: Unknown account name: " << argv[optind - 1]
	      << std::endl;
    return;
  }

  // Compile the list of specified regular expressions, which can be
  // specified on the command line, or using an include/exclude file

  for (; optind < argc; optind++)
    record_regexp(argv[optind], regexps);

  // Walk through all of the ledger entries, printing their register
  // formatted equivalent

  totals balance;

  for (entries_iterator i = main_ledger.entries.begin();
       i != main_ledger.entries.end();
       i++) {
    bool applies = false;
    for (std::list<transaction *>::iterator x = (*i)->xacts.begin();
	 x != (*i)->xacts.end();
	 x++) {
      if ((*x)->acct == acct) {
	applies = true;
	break;
      }
    }

    if (! applies || ! (*i)->matches(regexps))
      continue;

    for (std::list<transaction *>::iterator x = (*i)->xacts.begin();
	 x != (*i)->xacts.end();
	 x++) {
      if ((*x)->acct != acct || ! show_cleared && (*i)->cleared)
	continue;

      char buf[32];
      std::strftime(buf, 31, "%m.%d ", std::localtime(&(*i)->date));
      out << buf;

#if 0
      if ((*i)->cleared)
	out << "* ";
      else
	out << "  ";

      out.width(4);
      if ((*i)->code.empty())
	out << " ";
      else
	out << std::left << (*i)->code;
#endif
      out << " ";

      out.width(30);
      if ((*i)->desc.empty())
	out << " ";
      else
	out << std::left << (*i)->desc;
      out << " ";

      transaction * xact = (*i)->xacts.front();

      out.width(22);
      out << std::left << xact->acct->as_str() << " ";

      out.width(12);
      out << std::right << (*x)->cost->as_str(true);

      balance.credit((*x)->cost);
      balance.print(out, 12);

      out << std::endl;
    }
  }
}

} // namespace ledger