credit report, add some links to sales/credits/receipts summary, move payment search...
[freeside.git] / httemplate / search / cust_bill.html
1 <%
2    my( $count_query, $sql_query );
3    if ( $cgi->param('begin') || $cgi->param('end') || $cgi->keywords ) {
4
5      my $owed =
6        "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
7                     WHERE cust_bill_pay.invnum = cust_bill.invnum )
8                 - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
9                     WHERE cust_credit_bill.invnum = cust_bill.invnum )";
10
11      my @where;
12      my $orderby = 'ORDER BY cust_bill._date';
13
14      if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
15        push @where, "cust_bill._date >= $1",
16      }
17      if ( $cgi->param('end') =~ /^(\d+)$/ ) {
18        push @where, "cust_bill._date < $1",
19      }
20
21      my($query) = $cgi->keywords;
22      if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
23        my($open, $days, $field) = ($1, $2, $3);
24        $field = "_date" if $field eq 'date';
25        $orderby = "ORDER BY cust_bill.$field";
26        push @where, "0 != $owed" if $open;
27        push @where, "cust_bill._date < ". (time-86400*$days) if $days;
28      }
29    
30      my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
31    
32      $count_query = "SELECT COUNT(*), sum(charged), sum($owed)
33                      FROM cust_bill $extra_sql";
34    
35      $sql_query = {
36        'table'     => 'cust_bill',
37        'hashref'   => {},
38        'select'    => "cust_bill.*, $owed as owed",
39        'extra_sql' => "$extra_sql $orderby"
40      };
41    
42    } else {
43      $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/;
44      $count_query = 'SELECT 1';
45      $sql_query = {
46        'table'     => 'cust_bill',
47        'hashref'   => { 'invnum' => $2 },
48        #'select'    => '*',
49      };
50    }
51
52    my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
53    my $clink = [ "${p}view/cust_main.cgi?", 'custnum' ];
54
55 %>
56 <%= include( 'elements/search.html',
57                'title'       => 'Invoice Search Results',
58                'name'        => 'invoices',
59                'query'       => $sql_query,
60                'count_query' => $count_query,
61                'count_addl'  => [ '$%.2f total invoiced',
62                                   '$%.2f total outstanding balance',
63                                 ],
64                'redirect'    => $link,
65                'header'      =>
66                  [ 'Invoice #', qw(Balance Amount Date), 'Contact name',
67                    'Company' ],
68                'fields'      => [
69                  'invnum',
70                  sub { sprintf('$%.2f', shift->get('owed') ) },
71                  sub { sprintf('$%.2f', shift->charged     ) },
72                  sub { time2str('%b %d %Y', shift->_date ) },
73                  sub { my $cust_main = shift->cust_main;
74                        $cust_main->get('last'). ', '. $cust_main->first;
75                      },
76                  sub { my $cust_main = shift->cust_main;
77                        $cust_main->company;
78                      },
79                ],
80                'links' => [
81                  $link,
82                  $link,
83                  $link,
84                  $link,
85                  $clink,
86                  $clink,
87                ],
88
89     )
90 %>