fix advanced package report, RT#40292, fallout from RT#20173
[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               FS::UI::Web::cust_header(),
51               ;
52 push @fields, sub { time2str('%b %d %Y', shift->void_date ) },
53               'void_access_user_name',
54               'void_reason',
55               sub { time2str('%b %d %Y', shift->_date ) },
56               'otaker',
57               'reason',
58               \&FS::UI::Web::cust_fields,
59               ;
60 push @sort_fields, 'void_date',
61                    'void_usernum',                #not ideal, but at least groups them together
62                    'void_reasonnum, void_reason', #ditto
63                    '_date',
64                    'usernum',                     #ditto
65                    'reasonnum, reason',           #ditto 
66                    FS::UI::Web::cust_sort_fields();
67 $align .= 'rllrll'.FS::UI::Web::cust_aligns();
68 push @links,  '',
69               '',
70               '',
71               '',
72               '',
73               '',
74               ( map { $_ ne 'Cust. Status' ? $clink : '' }
75                          FS::UI::Web::cust_header()
76                    ),
77               ;
78 push @color,  '',
79               '',
80               '',
81               '',
82               '',
83               '',
84               FS::UI::Web::cust_colors(),
85               ;
86 push @style,  '',
87               '',
88               '',
89               '',
90               '',
91               '',
92               FS::UI::Web::cust_styles(),
93               ;
94
95 my @search = ();
96 my $addl_from = '';
97
98 # note that cgi field is usernum, but we're actually searching void_usernum
99 # because true laziness with tr-select-user in report_cust_credit_void.html
100 if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
101   push @search, "cust_credit_void.void_usernum = $1";
102 }
103
104 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
105   push @search, "agentnum = $1";
106   my $agent = qsearchs('agent', { 'agentnum' => $1 } );
107   die "unknown agentnum $1" unless $agent;
108   $title = $agent->agent. " $title";
109 }
110
111 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
112 push @search, "void_date >= $beginning ",
113               "void_date <= $ending";
114
115 push @search, FS::UI::Web::parse_lt_gt($cgi, 'amount' );
116
117 #here is the agent virtualization
118 push @search, $FS::CurrentUser::CurrentUser->agentnums_sql(table=>'cust_main');
119
120 my @select = (
121     'cust_credit_void.*',
122     'cust_main.custnum as cust_main_custnum',
123     FS::UI::Web::cust_sql_fields(),
124 );
125
126 my $where = 'WHERE '. join(' AND ', @search);
127
128 my $count_query = 'SELECT COUNT(*), SUM(amount) ';
129 $count_query .= 'FROM cust_credit_void'. 
130                   $addl_from. FS::UI::Web::join_cust_main('cust_credit_void').
131                   $where;
132
133 my $sql_query   = {
134   'table'     => 'cust_credit_void',
135   'select'    => join(', ',@select),
136   'hashref'   => {},
137   'extra_sql' => $where,
138   'addl_from' => $addl_from. FS::UI::Web::join_cust_main('cust_credit_void')
139 };
140
141 </%init>