fix multiple-agent virtualization properly for these reports
[freeside.git] / httemplate / search / cust_credit.html
1 <%
2    my $title = 'Credit Search Results';
3    #my( $count_query, $sql_query );
4
5    my @search = ();
6
7    if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
8      push @search, "cust_credit.otaker = '$1'";
9    }
10
11    if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
12      push @search, "agentnum = $1";
13      my $agent = qsearchs('agent', { 'agentnum' => $1 } );
14      die "unknown agentnum $1" unless $agent;
15      $title = $agent->agent. " $title";
16    }
17
18    #false laziness with cust_pkg.cgi and cust_pay.cgi
19    if ( $cgi->param('beginning')
20         && $cgi->param('beginning') =~ /^([ 0-9\-\/]{1,10})$/ ) {
21      my $beginning = str2time($1);
22      push @search, "_date >= $beginning ";
23    }
24    if ( $cgi->param('ending')
25              && $cgi->param('ending') =~ /^([ 0-9\-\/]{1,10})$/ ) {
26      my $ending = str2time($1) + 86399;
27      push @search, " _date <= $ending ";
28    }
29
30    if ( $cgi->param('begin')
31         && $cgi->param('begin') =~ /^(\d+)$/ ) {
32      push @search, "_date >= $1 ";
33    }
34    if ( $cgi->param('end')
35              && $cgi->param('end') =~ /^(\d+)$/ ) {
36      push @search, " _date < $1 ";
37    }
38
39    #here is the agent virtualization
40    push @search, $FS::CurrentUser::CurrentUser->agentnums_sql;
41
42    my $where = 'WHERE '. join(' AND ', @search);
43
44    my $count_query = 'SELECT COUNT(*), SUM(amount) '.
45                      'FROM cust_credit LEFT JOIN cust_main USING ( custnum ) '.
46                      $where;
47
48    my $sql_query   = {
49      'table'     => 'cust_credit',
50      'select'    => join(', ',
51                       'cust_credit.*',
52                       'cust_main.custnum as cust_main_custnum',
53                       FS::UI::Web::cust_sql_fields(),
54                     ),
55      'hashref'   => {},
56      'extra_sql' => $where,
57      'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
58    };
59
60      my $clink = sub {
61        my $cust_bill = shift;
62        $cust_bill->cust_main_custnum
63          ? [ "${p}view/cust_main.cgi?", 'custnum' ]
64          : '';
65      };
66
67 %><%= include( 'elements/search.html',
68                  'title'       => $title,
69                  'name'        => 'credits',
70                  'query'       => $sql_query,
71                  'count_query' => $count_query,
72                  'count_addl'  => [ '$%.2f total credited', ],
73                  #'redirect'    => $link,
74                  'header'      => [ 'Amount',
75                                     'Date',
76                                     FS::UI::Web::cust_header(),
77                                     'By',
78                                     'Reason'
79                                   ],
80                  'fields'      => [
81                    #'crednum',
82                    sub { sprintf('$%.2f', shift->amount ) },
83                    sub { time2str('%b %d %Y', shift->_date ) },
84                    \&FS::UI::Web::cust_fields,
85                    'otaker',
86                    'reason',
87                  ],
88                  #'align' => 'rrrllll',
89                  'align' => 'rr',
90                  'links' => [
91                    '',
92                    '',
93                    ( map { $clink } FS::UI::Web::cust_header() ),
94                    '',
95                    '',
96                  ],
97       )
98 %>