8f6258e4e79bba6cd319e1aa178f6d15e49599f0
[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, "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    my $where = scalar(@search)
40                  ? 'WHERE '. join(' AND ', @search)
41                  : '';
42
43    my $count_query = 'SELECT COUNT(*), SUM(amount) '.
44                      'FROM cust_credit LEFT JOIN cust_main USING ( custnum ) '.
45                      $where;
46
47    my $sql_query   = {
48      'table'     => 'cust_credit',
49      'select'    => 'cust_credit.*, cust_main.last, cust_main.first, cust_main.company',
50      'hashref'   => {},
51      'extra_sql' => $where,
52      'addl_from' => 'LEFT JOIN cust_main USING ( custnum )',
53    };
54
55    my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
56
57 %>
58 <%= include( 'elements/search.html',
59                'title'       => $title,
60                'name'        => 'credits',
61                'query'       => $sql_query,
62                'count_query' => $count_query,
63                'count_addl'  => [ '$%.2f total credited', ],
64                #'redirect'    => $link,
65                'header'      =>
66                  [ qw(Amount Date), 'Cust #', 'Contact name',
67                    qw(Company By Reason) ],
68                'fields'      => [
69                  #'crednum',
70                  sub { sprintf('$%.2f', shift->amount ) },
71                  sub { time2str('%b %d %Y', shift->_date ) },
72                  'custnum',
73                  sub { $_[0]->get('last'). ', '. $_[0]->first; },
74                  'company',
75                  'otaker',
76                  'reason',
77                ],
78                'align' => 'rrrllll',
79                'links' => [
80                  '',
81                  '',
82                  $clink,
83                  $clink,
84                  $clink,
85                  '',
86                  '',
87                ],
88     )
89 %>