fix top subtotals on refund reports
[freeside.git] / httemplate / search / cust_credit_void.html
1 <& elements/search.html,
2                  'title'       => $title,
3                  'name'        => emt('credits'),
4                  'query'       => $sql_query,
5                  'count_query' => $count_query,
6                  'header'      => \@header,
7                  'fields'      => \@fields,
8                  'sort_fields' => \@sort_fields,
9                  'align' => $align,
10                  'links' => \@links,
11                  'color' => \@color,
12                  'style' => \@style,
13 &>
14 <%init>
15
16 die "access denied"
17   unless $FS::CurrentUser::CurrentUser->access_right('Financial reports');
18
19 my $money_char = FS::Conf->new->config('money_char') || '$';
20
21 my $title = emt('Voided Credit Search Results');
22
23 my $clink = sub {
24   my $cust_bill = shift;
25   $cust_bill->cust_main_custnum
26     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
27     : '';
28 };
29
30 my %void_access_users;
31
32 my (@header, @fields, @sort_fields, $align, @links, @color, @style);
33 $align = '';
34
35 #amount
36 push @header, emt('Amount');
37 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
38 push @sort_fields, 'amount';
39 $align .= 'r';
40 push @links, '';
41 push @color, '';
42 push @style, '';
43
44 push @header, emt('Void Date'),
45               emt('Void By'),
46               emt('Void Reason'),
47               emt('Date'), 
48               emt('By'),
49               emt('Reason'),
50               emt('Info'),
51               FS::UI::Web::cust_header(),
52               ;
53 push @fields, sub { time2str('%b %d %Y', shift->void_date ) },
54               'void_access_user_name',
55               'void_reason',
56               sub { time2str('%b %d %Y', shift->_date ) },
57               'otaker',
58               'reason_only',
59               'addlinfo',
60               \&FS::UI::Web::cust_fields,
61               ;
62 push @sort_fields, 'void_date',
63                    'void_usernum',                #not ideal, but at least groups them together
64                    'void_reasonnum, void_reason', #ditto
65                    '_date',
66                    'usernum',                     #ditto
67                    'reasonnum, reason',           #ditto 
68                    'addlinfo',
69                    FS::UI::Web::cust_sort_fields();
70 $align .= 'rllrlll'.FS::UI::Web::cust_aligns();
71 push @links,  '',
72               '',
73               '',
74               '',
75               '',
76               '',
77               '',
78               ( map { $_ ne 'Cust. Status' ? $clink : '' }
79                          FS::UI::Web::cust_header()
80                    ),
81               ;
82 push @color,  '',
83               '',
84               '',
85               '',
86               '',
87               '',
88               '',
89               FS::UI::Web::cust_colors(),
90               ;
91 push @style,  '',
92               '',
93               '',
94               '',
95               '',
96               '',
97               '',
98               FS::UI::Web::cust_styles(),
99               ;
100
101 my @search = ();
102 my $addl_from = '';
103
104 # note that cgi field is usernum, but we're actually searching void_usernum
105 # because true laziness with tr-select-user in report_cust_credit_void.html
106 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
107   push @search, "cust_credit_void.void_usernum = $1";
108 }
109
110 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
111   push @search, "agentnum = $1";
112   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
113   die "unknown agentnum $1" unless $agent;
114   $title = $agent->agent. " $title";
115 }
116
117 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
118 push @search, "void_date >= $beginning ",
119               "void_date <= $ending";
120
121 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
122
123 #here is the agent virtualization
124 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql(table=>'cust_main');
125
126 my @select = (
127     'cust_credit_void.*',
128     'cust_main.custnum as cust_main_custnum',
129     FS::UI::Web::cust_sql_fields(),
130 );
131
132 my $where = 'WHERE '. join(' AND ', @search);
133
134 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
135 $count_query .= 'FROM cust_credit_void'. 
136                   $addl_from. FS::UI::Web::join_cust_main('cust_credit_void').
137                   $where;
138
139 my $sql_query   = {
140   'table'     => 'cust_credit_void',
141   'select'    => join(', ',@select),
142   'hashref'   => {},
143   'extra_sql' => $where,
144   'addl_from' => $addl_from. FS::UI::Web::join_cust_main('cust_credit_void')
145 };
146
147 </%init>