summaryrefslogtreecommitdiff
path: root/src/filters.h
blob: 18bf912d5d89cae603e27ffd0bdb5066e175c627 (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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
/*
 * Copyright (c) 2003-2023, John Wiegley.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of New Artisans LLC nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @addtogroup report
 */

/**
 * @file   filters.h
 * @author John Wiegley
 *
 * @ingroup report
 */
#pragma once

#include "chain.h"
#include "xact.h"
#include "post.h"
#include "account.h"
#include "temps.h"

namespace ledger {

using namespace boost::placeholders;

//////////////////////////////////////////////////////////////////////
//
// Posting collector
//

class post_splitter : public item_handler<post_t>
{
public:
  typedef std::map<value_t, posts_list> value_to_posts_map;
  typedef function<void (const value_t&)> custom_flusher_t;

protected:
  value_to_posts_map         posts_map;
  post_handler_ptr           post_chain;
  report_t&                  report;
  expr_t&                    group_by_expr;
  custom_flusher_t           preflush_func;
  optional<custom_flusher_t> postflush_func;

public:
  post_splitter(post_handler_ptr _post_chain,
                report_t&        _report,
                expr_t&          _group_by_expr)
    : post_chain(_post_chain), report(_report),
      group_by_expr(_group_by_expr) {
    preflush_func = bind(&post_splitter::print_title, this, _1);
    TRACE_CTOR(post_splitter, "scope_t&, post_handler_ptr, expr_t");
  }
  virtual ~post_splitter() {
    TRACE_DTOR(post_splitter);
  }

  void set_preflush_func(custom_flusher_t functor) {
    preflush_func = functor;
  }
  void set_postflush_func(custom_flusher_t functor) {
    postflush_func = functor;
  }

  virtual void print_title(const value_t& val);

  virtual void flush();
  virtual void operator()(post_t& post);

  virtual void clear() {
    posts_map.clear();
    post_chain->clear();
    item_handler<post_t>::clear();
  }
};

//////////////////////////////////////////////////////////////////////
//
// Posting filters
//

class ignore_posts : public item_handler<post_t>
{
public:
  virtual void operator()(post_t&) {}
};

class collect_posts : public item_handler<post_t>
{
public:
  std::vector<post_t *> posts;

  collect_posts() : item_handler<post_t>() {
    TRACE_CTOR(collect_posts, "");
  }
  virtual ~collect_posts() {
    TRACE_DTOR(collect_posts);
  }

  std::size_t length() const {
    return posts.size();
  }

  std::vector<post_t *>::iterator begin() {
    return posts.begin();
  }
  std::vector<post_t *>::iterator end() {
    return posts.end();
  }

  virtual void flush() {}
  virtual void operator()(post_t& post) {
    posts.push_back(&post);
  }

  virtual void clear() {
    posts.clear();
    item_handler<post_t>::clear();
  }
};

template <typename Iterator>
class pass_down_posts : public item_handler<post_t>
{
  pass_down_posts();

public:
  pass_down_posts(post_handler_ptr handler, Iterator& iter)
    : item_handler<post_t>(handler) {
    while (post_t * post = *iter) {
      try {
        item_handler<post_t>::operator()(*post);
      }
      catch (const std::exception&) {
        add_error_context(item_context(*post, _("While handling posting")));
        throw;
      }
      iter.increment();
    }

    item_handler<post_t>::flush();

    TRACE_CTOR(pass_down_posts, "post_handler_ptr, posts_iterator");
  }

  virtual ~pass_down_posts() {
    TRACE_DTOR(pass_down_posts);
  }
};

class push_to_posts_list : public item_handler<post_t>
{
  push_to_posts_list();

public:
  posts_list& posts;

  push_to_posts_list(posts_list& _posts) : posts(_posts) {
    TRACE_CTOR(push_to_posts_list, "posts_list&");
  }
  virtual ~push_to_posts_list() {
    TRACE_DTOR(push_to_posts_list);
  }

  virtual void operator()(post_t& post) {
    posts.push_back(&post);
  }
};

class truncate_xacts : public item_handler<post_t>
{
  int  head_count;
  int  tail_count;
  bool completed;

  posts_list  posts;
  std::size_t xacts_seen;
  xact_t *    last_xact;

  truncate_xacts();

public:
  truncate_xacts(post_handler_ptr handler,
                 int _head_count, int _tail_count)
    : item_handler<post_t>(handler),
      head_count(_head_count), tail_count(_tail_count),
      completed(false), xacts_seen(0), last_xact(NULL) {
    TRACE_CTOR(truncate_xacts, "post_handler_ptr, int, int");
  }
  virtual ~truncate_xacts() {
    TRACE_DTOR(truncate_xacts);
  }

  virtual void flush();
  virtual void operator()(post_t& post);

  virtual void clear() {
    completed = false;
    posts.clear();
    xacts_seen = 0;
    last_xact = NULL;

    item_handler<post_t>::clear();
  }
};

class sort_posts : public item_handler<post_t>
{
  typedef std::deque<post_t *> posts_deque;

  posts_deque posts;
  expr_t      sort_order;
  report_t&   report;

  sort_posts();

public:
  sort_posts(post_handler_ptr handler, const expr_t& _sort_order,
             report_t& _report)
    : item_handler<post_t>(handler), sort_order(_sort_order), report(_report) {
    TRACE_CTOR(sort_posts, "post_handler_ptr, const value_expr&, report_t&");
  }
  sort_posts(post_handler_ptr handler, const string& _sort_order,
             report_t& _report)
    : item_handler<post_t>(handler), sort_order(_sort_order), report(_report) {
    TRACE_CTOR(sort_posts, "post_handler_ptr, const string&, report_t&");
  }
  virtual ~sort_posts() {
    TRACE_DTOR(sort_posts);
  }

  virtual void post_accumulated_posts();

  virtual void flush() {
    post_accumulated_posts();
    item_handler<post_t>::flush();
  }

  virtual void operator()(post_t& post) {
    posts.push_back(&post);
  }

  virtual void clear() {
    posts.clear();
    sort_order.mark_uncompiled();

    item_handler<post_t>::clear();
  }
};

class sort_xacts : public item_handler<post_t>
{
  sort_posts sorter;
  xact_t *  last_xact;

  sort_xacts();

public:
  sort_xacts(post_handler_ptr handler, const expr_t& _sort_order,
             report_t& _report)
    : sorter(handler, _sort_order, _report) {
    TRACE_CTOR(sort_xacts,
               "post_handler_ptr, const value_expr&, report_t&");
  }
  sort_xacts(post_handler_ptr handler, const string& _sort_order,
             report_t& _report)
    : sorter(handler, _sort_order, _report) {
    TRACE_CTOR(sort_xacts,
               "post_handler_ptr, const string&, report_t&");
  }
  virtual ~sort_xacts() {
    TRACE_DTOR(sort_xacts);
  }

  virtual void flush() {
    sorter.flush();
    item_handler<post_t>::flush();
  }

  virtual void operator()(post_t& post) {
    if (last_xact && post.xact != last_xact)
      sorter.post_accumulated_posts();

    sorter(post);

    last_xact = post.xact;
  }

  virtual void clear() {
    sorter.clear();
    last_xact = NULL;

    item_handler<post_t>::clear();
  }
};

class filter_posts : public item_handler<post_t>
{
  predicate_t pred;
  scope_t&    context;

  filter_posts();

public:
  filter_posts(post_handler_ptr   handler,
               const predicate_t& predicate,
               scope_t&           _context)
    : item_handler<post_t>(handler), pred(predicate), context(_context) {
    TRACE_CTOR(filter_posts, "post_handler_ptr, predicate_t, scope_t&");
  }
  virtual ~filter_posts() {
    TRACE_DTOR(filter_posts);
  }

  virtual void operator()(post_t& post) {
    bind_scope_t bound_scope(context, post);
    if (pred(bound_scope)) {
      post.xdata().add_flags(POST_EXT_MATCHES);
      (*handler)(post);
    }
  }

  virtual void clear() {
    pred.mark_uncompiled();
    item_handler<post_t>::clear();
  }
};

class anonymize_posts : public item_handler<post_t>
{
  typedef std::map<commodity_t *, std::size_t>        commodity_index_map;
  typedef variate_generator<mt19937&, uniform_int<> > int_generator_t;

  temporaries_t       temps;
  commodity_index_map comms;
  std::size_t         next_comm_id;
  xact_t *            last_xact;
  mt19937             rnd_gen;
  uniform_int<>       integer_range;
  int_generator_t     integer_gen;

  anonymize_posts();

public:
  anonymize_posts(post_handler_ptr handler)
    : item_handler<post_t>(handler), next_comm_id(0), last_xact(NULL),
      rnd_gen(static_cast<unsigned int>(static_cast<boost::uintmax_t>(std::time(0)))),
      integer_range(1, 2000000000L),
      integer_gen(rnd_gen, integer_range) {
    TRACE_CTOR(anonymize_posts, "post_handler_ptr");
  }
  virtual ~anonymize_posts() {
    TRACE_DTOR(anonymize_posts);
    handler.reset();
  }

  void render_commodity(amount_t& amt);

  virtual void operator()(post_t& post);

  virtual void clear() {
    temps.clear();
    comms.clear();
    last_xact = NULL;

    item_handler<post_t>::clear();
  }
};

class calc_posts : public item_handler<post_t>
{
  post_t * last_post;
  expr_t&  amount_expr;
  bool     calc_running_total;

  calc_posts();

public:
  calc_posts(post_handler_ptr handler,
             expr_t&          _amount_expr,
             bool             _calc_running_total = false)
    : item_handler<post_t>(handler), last_post(NULL),
      amount_expr(_amount_expr), calc_running_total(_calc_running_total) {
    TRACE_CTOR(calc_posts, "post_handler_ptr, expr_t&, bool");
  }
  virtual ~calc_posts() {
    TRACE_DTOR(calc_posts);
  }

  virtual void operator()(post_t& post);

  virtual void clear() {
    last_post = NULL;
    amount_expr.mark_uncompiled();

    item_handler<post_t>::clear();
  }
};

class collapse_posts : public item_handler<post_t>
{

  typedef std::map<account_t *,value_t> totals_map;

  expr_t&             amount_expr;
  predicate_t         display_predicate;
  predicate_t         only_predicate;
  value_t             subtotal;
  std::size_t         count;
  xact_t *            last_xact;
  post_t *            last_post;
  temporaries_t       temps;
  account_t *         global_totals_account;
  totals_map          totals;
  bool                only_collapse_if_zero;
  unsigned short      collapse_depth;
  std::list<post_t *> component_posts;
  report_t&           report;

  collapse_posts();

public:
  collapse_posts(post_handler_ptr handler,
                 report_t&        _report,
                 expr_t&          _amount_expr,
                 predicate_t      _display_predicate,
                 predicate_t      _only_predicate,
                 bool             _only_collapse_if_zero = false,
                 unsigned short   _collapse_depth = 0)
    : item_handler<post_t>(handler), amount_expr(_amount_expr),
      display_predicate(_display_predicate),
      only_predicate(_only_predicate), count(0),
      last_xact(NULL), last_post(NULL),
      only_collapse_if_zero(_only_collapse_if_zero),
      collapse_depth(_collapse_depth), report(_report) {
    create_accounts();
    TRACE_CTOR(collapse_posts, "post_handler_ptr, ...");
  }
  virtual ~collapse_posts() {
    TRACE_DTOR(collapse_posts);
    handler.reset();
  }

  void create_accounts() {
    global_totals_account = &temps.create_account(_("<Total>"));
  }

  value_t& find_totals(account_t* account);

  virtual void flush() {
    report_subtotal();
    item_handler<post_t>::flush();
  }

  void report_subtotal();

  virtual void operator()(post_t& post);

  virtual void clear() {
    amount_expr.mark_uncompiled();
    display_predicate.mark_uncompiled();
    only_predicate.mark_uncompiled();

    subtotal  = value_t();
    count     = 0;
    last_xact = NULL;
    last_post = NULL;

    temps.clear();
    create_accounts();
    totals.clear();
    component_posts.clear();

    item_handler<post_t>::clear();
  }
};

class related_posts : public item_handler<post_t>
{
  posts_list posts;
  bool       also_matching;

  related_posts();

public:
  related_posts(post_handler_ptr handler,
                       const bool _also_matching = false)
    : item_handler<post_t>(handler),
      also_matching(_also_matching) {
    TRACE_CTOR(related_posts, "post_handler_ptr, const bool");
  }
  virtual ~related_posts() throw() {
    TRACE_DTOR(related_posts);
  }

  virtual void flush();
  virtual void operator()(post_t& post) {
    post.xdata().add_flags(POST_EXT_RECEIVED);
    posts.push_back(&post);
  }

  virtual void clear() {
    posts.clear();
    item_handler<post_t>::clear();
  }
};

class display_filter_posts : public item_handler<post_t>
{
  // This filter requires that calc_posts be used at some point
  // later in the chain.

  report_t&     report;
  expr_t&       display_amount_expr;
  expr_t&       display_total_expr;
  bool          show_rounding;
  value_t       last_display_total;
  temporaries_t temps;
  account_t *   rounding_account;

  display_filter_posts();

public:
  account_t *   revalued_account;

  display_filter_posts(post_handler_ptr handler,
                       report_t&        _report,
                       bool             _show_rounding);

  virtual ~display_filter_posts() {
    TRACE_DTOR(display_filter_posts);
    handler.reset();
  }

  void create_accounts() {
    rounding_account = &temps.create_account(_("<Adjustment>"));
    revalued_account = &temps.create_account(_("<Revalued>"));
  }

  bool output_rounding(post_t& post);

  virtual void operator()(post_t& post);

  virtual void clear() {
    display_amount_expr.mark_uncompiled();
    display_total_expr.mark_uncompiled();

    last_display_total = value_t();

    temps.clear();
    item_handler<post_t>::clear();

    create_accounts();
  }
};

class changed_value_posts : public item_handler<post_t>
{
  // This filter requires that calc_posts be used at some point
  // later in the chain.

  report_t&     report;
  expr_t&       total_expr;
  expr_t&       display_total_expr;
  bool          changed_values_only;
  bool          historical_prices_only;
  bool          for_accounts_report;
  bool          show_unrealized;
  post_t *      last_post;
  value_t       last_total;
  value_t       repriced_total;
  temporaries_t temps;
  account_t *   revalued_account;
  account_t *   gains_equity_account;
  account_t *   losses_equity_account;

  display_filter_posts * display_filter;

  changed_value_posts();

public:
  changed_value_posts(post_handler_ptr       handler,
                      report_t&              _report,
                      bool                   _for_accounts_report,
                      bool                   _show_unrealized,
                      display_filter_posts * _display_filter);

  virtual ~changed_value_posts() {
    TRACE_DTOR(changed_value_posts);
    temps.clear();
    handler.reset();
  }

  void create_accounts() {
    revalued_account = (display_filter ? display_filter->revalued_account :
                        &temps.create_account(_("<Revalued>")));
  }

  virtual void flush();

  void output_revaluation(post_t& post, const date_t& current);
  void output_intermediate_prices(post_t& post, const date_t& current);

  virtual void operator()(post_t& post);

  virtual void clear() {
    total_expr.mark_uncompiled();
    display_total_expr.mark_uncompiled();

    last_post = NULL;
    last_total = value_t();

    temps.clear();
    item_handler<post_t>::clear();

    create_accounts();
  }
};

class subtotal_posts : public item_handler<post_t>
{
  subtotal_posts();

protected:
  class acct_value_t
  {
    acct_value_t();

  public:
    account_t * account;
    value_t     value;
    bool        is_virtual;
    bool        must_balance;

    acct_value_t(account_t * a, bool _is_virtual = false,
                 bool _must_balance = false)
      : account(a), is_virtual(_is_virtual), must_balance(_must_balance) {
      TRACE_CTOR(acct_value_t, "account_t *, bool, bool");
    }
    acct_value_t(account_t * a, value_t& v, bool _is_virtual = false,
                 bool _must_balance = false)
      : account(a), value(v), is_virtual(_is_virtual),
        must_balance(_must_balance) {
      TRACE_CTOR(acct_value_t, "account_t *, value_t&, bool, bool");
    }
    acct_value_t(const acct_value_t& av)
      : account(av.account), value(av.value), is_virtual(av.is_virtual),
        must_balance(av.must_balance) {
      TRACE_CTOR(acct_value_t, "copy");
    }
    ~acct_value_t() throw() {
      TRACE_DTOR(acct_value_t);
    }
  };

  typedef std::map<string, acct_value_t>  values_map;
  typedef std::pair<string, acct_value_t> values_pair;

protected:
  expr_t&              amount_expr;
  values_map           values;
  optional<string>     date_format;
  temporaries_t        temps;
  std::deque<post_t *> component_posts;

public:
  subtotal_posts(post_handler_ptr handler, expr_t& _amount_expr,
                 const optional<string>& _date_format = none)
    : item_handler<post_t>(handler), amount_expr(_amount_expr),
      date_format(_date_format) {
    TRACE_CTOR(subtotal_posts,
               "post_handler_ptr, expr_t&, const optional<string>&");
  }
  virtual ~subtotal_posts() {
    TRACE_DTOR(subtotal_posts);
    handler.reset();
  }

  void report_subtotal(const char * spec_fmt = NULL,
                       const optional<date_interval_t>& interval = none);

  virtual void flush() {
    if (values.size() > 0)
      report_subtotal();
    item_handler<post_t>::flush();
  }
  virtual void operator()(post_t& post);

  virtual void clear() {
    amount_expr.mark_uncompiled();
    values.clear();
    temps.clear();
    component_posts.clear();

    item_handler<post_t>::clear();
  }
};

class interval_posts : public subtotal_posts
{
  date_interval_t start_interval;
  date_interval_t interval;
  account_t *     empty_account;
  bool            exact_periods;
  bool            generate_empty_posts;

  std::deque<post_t *> all_posts;

  interval_posts();

public:

  interval_posts(post_handler_ptr       _handler,
                 expr_t&                amount_expr,
                 const date_interval_t& _interval,
                 bool                   _exact_periods        = false,
                 bool                   _generate_empty_posts = false)
    : subtotal_posts(_handler, amount_expr), start_interval(_interval),
      interval(start_interval), exact_periods(_exact_periods),
      generate_empty_posts(_generate_empty_posts) {
    create_accounts();
    TRACE_CTOR(interval_posts,
               "post_handler_ptr, expr_t&, date_interval_t, bool, bool");
  }
  virtual ~interval_posts() throw() {
    TRACE_DTOR(interval_posts);
  }

  void create_accounts() {
    empty_account = &temps.create_account(_("<None>"));
  }

  void report_subtotal(const date_interval_t& ival);

#if DEBUG_ON
  void debug_interval(const date_interval_t& ival) {
    if (ival.start)
      DEBUG("filters.interval", "start  = " << *ival.start);
    else
      DEBUG("filters.interval", "no start");

    if (ival.finish)
      DEBUG("filters.interval", "finish = " << *ival.finish);
    else
      DEBUG("filters.interval", "no finish");
  }
#endif

  virtual void operator()(post_t& post);
  virtual void flush();

  virtual void clear() {
    interval  = start_interval;

    subtotal_posts::clear();
    create_accounts();
  }
};

class posts_as_equity : public subtotal_posts
{
  report_t&   report;
  post_t *    last_post;
  account_t * equity_account;
  account_t * balance_account;
  bool        unround;

  posts_as_equity();

public:
  posts_as_equity(post_handler_ptr _handler, report_t& _report,
                  expr_t& amount_expr, bool _unround)
    : subtotal_posts(_handler, amount_expr), report(_report), unround(_unround) {
    create_accounts();
    TRACE_CTOR(posts_as_equity, "post_handler_ptr, expr_t&");
  }
  virtual ~posts_as_equity() throw() {
    TRACE_DTOR(posts_as_equity);
  }

  void create_accounts() {
    equity_account  = &temps.create_account(_("Equity"));
    balance_account = equity_account->find_account(_("Opening Balances"));
  }

  void report_subtotal();

  virtual void flush() {
    report_subtotal();
    subtotal_posts::flush();
  }

  virtual void clear() {
    last_post = NULL;
    subtotal_posts::clear();
    create_accounts();
  }
};

class by_payee_posts : public item_handler<post_t>
{
  typedef std::map<string, shared_ptr<subtotal_posts> >  payee_subtotals_map;
  typedef std::pair<string, shared_ptr<subtotal_posts> > payee_subtotals_pair;

  expr_t&             amount_expr;
  payee_subtotals_map payee_subtotals;

  by_payee_posts();

 public:
  by_payee_posts(post_handler_ptr handler, expr_t& _amount_expr)
    : item_handler<post_t>(handler), amount_expr(_amount_expr) {
    TRACE_CTOR(by_payee_posts, "post_handler_ptr, expr_t&");
  }
  virtual ~by_payee_posts() {
    TRACE_DTOR(by_payee_posts);
  }

  virtual void flush();
  virtual void operator()(post_t& post);

  virtual void clear() {
    amount_expr.mark_uncompiled();
    payee_subtotals.clear();

    item_handler<post_t>::clear();
  }
};

class transfer_details : public item_handler<post_t>
{
  account_t *   master;
  expr_t        expr;
  scope_t&      scope;
  temporaries_t temps;

  transfer_details();

public:
  enum element_t {
    SET_DATE,
    SET_ACCOUNT,
    SET_PAYEE
  } which_element;

  transfer_details(post_handler_ptr handler,
                   element_t        _which_element,
                   account_t *      _master,
                   const expr_t&    _expr,
                   scope_t&         _scope)
    : item_handler<post_t>(handler), master(_master),
      expr(_expr), scope(_scope), which_element(_which_element) {
    TRACE_CTOR(transfer_details,
               "post_handler_ptr, element_t, account_t *, expr_t, scope_t&");
  }
  virtual ~transfer_details() {
    TRACE_DTOR(transfer_details);
    handler.reset();
  }

  virtual void operator()(post_t& post);

  virtual void clear() {
    expr.mark_uncompiled();
    temps.clear();

    item_handler<post_t>::clear();
  }
};

class day_of_week_posts : public subtotal_posts
{
  posts_list days_of_the_week[7];

  day_of_week_posts();

public:
  day_of_week_posts(post_handler_ptr handler, expr_t& amount_expr)
    : subtotal_posts(handler, amount_expr) {
    TRACE_CTOR(day_of_week_posts, "post_handler_ptr, bool");
  }
  virtual ~day_of_week_posts() throw() {
    TRACE_DTOR(day_of_week_posts);
  }

  virtual void flush();
  virtual void operator()(post_t& post) {
    days_of_the_week[post.date().day_of_week()].push_back(&post);
  }

  virtual void clear() {
    for (int i = 0; i < 7; i++)
      days_of_the_week[i].clear();

    subtotal_posts::clear();
  }
};

class generate_posts : public item_handler<post_t>
{
  generate_posts();

protected:
  typedef std::pair<date_interval_t, post_t *> pending_posts_pair;
  typedef std::list<pending_posts_pair>        pending_posts_list;

  pending_posts_list pending_posts;
  temporaries_t      temps;

public:
  generate_posts(post_handler_ptr handler)
    : item_handler<post_t>(handler) {
    TRACE_CTOR(generate_posts, "post_handler_ptr");
  }

  virtual ~generate_posts() {
    TRACE_DTOR(generate_posts);
    handler.reset();
  }

  void add_period_xacts(period_xacts_list& period_xacts);

  virtual void add_post(const date_interval_t& period, post_t& post);

  virtual void clear() {
    pending_posts.clear();
    temps.clear();

    item_handler<post_t>::clear();
  }
};

class budget_posts : public generate_posts
{
#define BUDGET_NO_BUDGET   0x00
#define BUDGET_BUDGETED    0x01
#define BUDGET_UNBUDGETED  0x02
#define BUDGET_WRAP_VALUES 0x04

  uint_least8_t flags;
  date_t        terminus;

  budget_posts();

public:
  budget_posts(post_handler_ptr handler,
               date_t           _terminus,
               uint_least8_t    _flags = BUDGET_BUDGETED)
    : generate_posts(handler), flags(_flags), terminus(_terminus) {
    TRACE_CTOR(budget_posts, "post_handler_ptr, date_t, uint_least8_t");
  }
  virtual ~budget_posts() throw() {
    TRACE_DTOR(budget_posts);
  }

  void report_budget_items(const date_t& date);

  virtual void flush();
  virtual void operator()(post_t& post);
};

class forecast_posts : public generate_posts
{
  predicate_t       pred;
  scope_t&          context;
  const std::size_t forecast_years;

 public:
  forecast_posts(post_handler_ptr   handler,
                 const predicate_t& predicate,
                 scope_t&           _context,
                 const std::size_t  _forecast_years)
    : generate_posts(handler), pred(predicate), context(_context),
      forecast_years(_forecast_years) {
    TRACE_CTOR(forecast_posts,
               "post_handler_ptr, predicate_t, scope_t&, std::size_t");
  }
  virtual ~forecast_posts() throw() {
    TRACE_DTOR(forecast_posts);
  }

  virtual void add_post(const date_interval_t& period, post_t& post);
  virtual void flush();

  virtual void clear() {
    pred.mark_uncompiled();
    generate_posts::clear();
  }
};

class inject_posts : public item_handler<post_t>
{
  typedef std::set<xact_t *>                       tag_injected_set;
  typedef std::pair<account_t *, tag_injected_set> tag_mapping_pair;
  typedef std::pair<string, tag_mapping_pair>      tags_list_pair;

  std::list<tags_list_pair> tags_list;
  temporaries_t             temps;

 public:
  inject_posts(post_handler_ptr handler, const string& tag_list,
               account_t * master);

  virtual ~inject_posts() throw() {
    TRACE_DTOR(inject_posts);
    handler.reset();
  }

  virtual void operator()(post_t& post);
};

//////////////////////////////////////////////////////////////////////
//
// Account filters
//

template <typename Iterator>
class pass_down_accounts : public item_handler<account_t>
{
  pass_down_accounts();

  optional<predicate_t> pred;
  optional<scope_t&>    context;

public:
  pass_down_accounts(acct_handler_ptr             handler,
                     Iterator&                    iter,
                     const optional<predicate_t>& _pred    = none,
                     const optional<scope_t&>&    _context = none)
    : item_handler<account_t>(handler), pred(_pred), context(_context) {
    TRACE_CTOR(pass_down_accounts, "acct_handler_ptr, accounts_iterator, ...");

    while (account_t * account = *iter++) {
      if (! pred) {
        item_handler<account_t>::operator()(*account);
      } else {
        bind_scope_t bound_scope(*context, *account);
        if ((*pred)(bound_scope))
          item_handler<account_t>::operator()(*account);
      }
    }

    item_handler<account_t>::flush();
  }

  virtual ~pass_down_accounts() {
    TRACE_DTOR(pass_down_accounts);
  }

  virtual void clear() {
    if (pred)
      pred->mark_uncompiled();

    item_handler<account_t>::clear();
  }
};

} // namespace ledger