so Search.tsf and Search.rdf work
[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 COUNT(*) FROM cust_bill WHERE invnum = $2";
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 = sub {
54      my $cust_bill = shift;
55      my $cust_main = $cust_bill->cust_main;
56      $cust_main
57        ? [ "${p}view/cust_main.cgi?", 'custnum' ]
58        : '';
59    };
60
61   my $conf = new FS::Conf;
62   my $money_char = $conf->config('money_char') || '$';
63
64 %><%= include( 'elements/search.html',
65                  'title'       => 'Invoice Search Results',
66                  'name'        => 'invoices',
67                  'query'       => $sql_query,
68                  'count_query' => $count_query,
69                  'count_addl'  => [ '$%.2f total invoiced',
70                                     '$%.2f total outstanding balance',
71                                   ],
72                  'redirect'    => $link,
73                  'header'      =>
74                    [ 'Invoice #', qw(Balance Amount Date), 'Contact name',
75                      'Company' ],
76                  'fields'      => [
77                    'invnum',
78                    sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
79                    sub { sprintf($money_char.'%.2f', shift->charged     ) },
80                    sub { time2str('%b %d %Y', shift->_date ) },
81                    sub { my $cust_bill = shift;
82                          my $cust_main = $cust_bill->cust_main;
83                          $cust_main
84                            ? $cust_main->get('last'). ', '. $cust_main->first
85                            : "WARNING: can't find cust_main.custnum ".
86                              $cust_bill->custnum. ' (cust_bill.invnum '.
87                              $cust_bill->invnum. ')';
88                        },
89                    sub { my $cust_main = shift->cust_main;
90                          $cust_main ? $cust_main->company : '';
91                        },
92                  ],
93                  'align' => 'rrrrll',
94                  'links' => [
95                    $link,
96                    $link,
97                    $link,
98                    $link,
99                    $clink,
100                    $clink,
101                  ],
102   
103       )
104 %>