show a better message when no results are found
[freeside.git] / httemplate / search / cust_credit.html
1 <%
2    #my( $count_query, $sql_query );
3
4    my @search = ();
5
6    if ( $cgi->param('otaker') && $cgi->param('otaker') =~ /^([\w\.\-]+)$/ ) {
7      push @search, "otaker = '$1'";
8    }
9
10    #false laziness with cust_pkg.cgi and cust_pay.cgi
11    if ( $cgi->param('beginning')
12         && $cgi->param('beginning') =~ /^([ 0-9\-\/]{1,10})$/ ) {
13      my $beginning = str2time($1);
14      push @search, "_date >= $beginning ";
15    }
16    if ( $cgi->param('ending')
17              && $cgi->param('ending') =~ /^([ 0-9\-\/]{1,10})$/ ) {
18      my $ending = str2time($1) + 86399;
19      push @search, " _date <= $ending ";
20    }
21
22    if ( $cgi->param('begin')
23         && $cgi->param('begin') =~ /^(\d+)$/ ) {
24      push @search, "_date >= $1 ";
25    }
26    if ( $cgi->param('end')
27              && $cgi->param('end') =~ /^(\d+)$/ ) {
28      push @search, " _date < $1 ";
29    }
30
31    my $where = scalar(@search)
32                  ? 'WHERE '. join(' AND ', @search)
33                  : '';
34
35    my $count_query = "SELECT COUNT(*), SUM(amount) FROM cust_credit $where";
36    my $sql_query   = {
37      'table'     => 'cust_credit',
38      'hashref'   => {},
39      'extra_sql' => $where,
40    };
41
42    my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
43
44 %>
45 <%= include( 'elements/search.html',
46                'title'       => 'Credit Search Results',
47                'name'        => 'credits',
48                'query'       => $sql_query,
49                'count_query' => $count_query,
50                'count_addl'  => [ '$%.2f total credited', ],
51                #'redirect'    => $link,
52                'header'      =>
53                  [ qw(Amount Date), 'Contact name', qw(Company By Reason) ],
54                'fields'      => [
55                  #'crednum',
56                  sub { sprintf('$%.2f', shift->amount ) },
57                  sub { time2str('%b %d %Y', shift->_date ) },
58                  sub { my $cust_main = shift->cust_main;
59                        $cust_main->get('last'). ', '. $cust_main->first;
60                      },
61                  sub { my $cust_main = shift->cust_main;
62                        $cust_main->company;
63                      },
64                  'otaker',
65                  'reason',
66                ],
67                'links' => [
68                  '',
69                  '',
70                  $clink,
71                  $clink,
72                  '',
73                  '',
74                ],
75     )
76 %>