move account search (httemplate/search/svc_acct.cgi) to new template, cust-fields...
[freeside.git] / httemplate / search / cust_bill.html
1 <%
2    my( $count_query, $sql_query );
3    my( $count_addl ) = ( '' );
4    my( $distinct ) = ( '' );
5    my($begin, $end) = ( '', '' );
6    my($agentnum) = ( '' );
7    my($open, $days) = ( '', '' );
8    if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
9      $count_query = "SELECT COUNT(*) FROM cust_bill WHERE invnum = $2";
10      $sql_query = {
11        'table'     => 'cust_bill',
12        'hashref'   => { 'invnum' => $2 },
13        #'select'    => '*',
14      };
15    } else {
16    #if ( $cgi->param('begin') || $cgi->param('end')
17    #     || $cgi->param('beginning') || $cgi->param('ending')
18    #     || $cgi->keywords
19    #   )
20    #{
21
22      #some false laziness w/cust_bill::re_X
23      my @where;
24      my $orderby = 'ORDER BY cust_bill._date';
25
26      if ( $cgi->param('beginning')
27           && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
28        $begin = str2time($1);
29        push @where, "cust_bill._date >= $begin";
30      }
31      if (  $cgi->param('ending')
32            && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
33        $end = str2time($1) + 86399;
34        push @where, "cust_bill._date < $end";
35      }
36
37      if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
38        $begin = $1;
39        push @where, "cust_bill._date >= $begin";
40      }
41      if ( $cgi->param('end') =~ /^(\d+)$/ ) {
42        $end = $1;
43        push @where, "cust_bill._date < $end";
44      }
45
46      if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
47        $agentnum = $1;
48        push @where, "cust_main.agentnum = $agentnum";
49      }
50
51      my $owed =
52        "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
53                     WHERE cust_bill_pay.invnum = cust_bill.invnum )
54                 - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
55                     WHERE cust_credit_bill.invnum = cust_bill.invnum )";
56
57      if ( $cgi->param('open') ) {
58        push @where, "0 != $owed";
59        $open = 1;
60      }
61
62      my($query) = $cgi->keywords;
63      if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
64        ($open, $days, my $field) = ($1, $2, $3);
65        $field = "_date" if $field eq 'date';
66        $orderby = "ORDER BY cust_bill.$field";
67        push @where, "0 != $owed" if $open;
68        push @where, "cust_bill._date < ". (time-86400*$days) if $days;
69      }
70
71      my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
72
73      my $addl_from = 'left join cust_main using ( custnum )';
74
75      if ( $cgi->param('newest_percust') ) {
76        $distinct = 'DISTINCT ON ( cust_bill.custnum )';
77        $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
78        #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
79        $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
80      }
81    
82      unless ( $count_query ) {
83        $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
84        $count_addl = [ '$%.2f total invoiced',
85                        '$%.2f total outstanding balance',
86                      ];
87      }
88      $count_query .=  " FROM cust_bill $addl_from $extra_sql";
89    
90      $sql_query = {
91        'table'     => 'cust_bill',
92        'addl_from' => $addl_from,
93        'hashref'   => {},
94        'select'    => "$distinct ". join(', ',
95                         'cust_bill.*',
96                         #( map "cust_main.$_", qw(custnum last first company) ),
97                         'cust_main.custnum as cust_main_custnum',
98                         FS::UI::Web::cust_sql_fields(),
99                         "$owed as owed",
100                       ),
101        'extra_sql' => "$extra_sql $orderby"
102      };
103    
104    }
105
106    my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
107    my $clink = sub {
108      my $cust_bill = shift;
109      $cust_bill->cust_main_custnum
110        ? [ "${p}view/cust_main.cgi?", 'custnum' ]
111        : '';
112    };
113
114   my $conf = new FS::Conf;
115   my $money_char = $conf->config('money_char') || '$';
116
117   my $html_init = join("\n", map {
118     ( my $action = $_ ) =~ s/_$//;
119     include('/elements/progress-init.html',
120               $_.'form',
121               [ 'begin', 'end', 'agentnum', 'open', 'days', 'newest_percust' ],
122               "../misc/${_}invoices.cgi",
123               { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
124               $_, #key
125            ),
126     qq!<FORM NAME="${_}form">!,
127     qq!<INPUT TYPE="hidden" NAME="begin"     VALUE="$begin">!,
128     qq!<INPUT TYPE="hidden" NAME="end"       VALUE="$end">!,
129     qq!<INPUT TYPE="hidden" NAME="agentnum"  VALUE="$agentnum">!,
130     qq!<INPUT TYPE="hidden" NAME="open"      VALUE="$open">!,
131     qq!<INPUT TYPE="hidden" NAME="days"      VALUE="$days">!,
132     qq!</FORM>!
133   } qw( print_ email_ fax_ ) );
134
135   my $menubar =  [
136                    'Main menu' => $p,
137                    'Print these invoices' =>
138                      "javascript:print_process()",
139                    'Email these invoices' =>
140                      "javascript:email_process()",
141                  ];
142
143   push @$menubar, 'Fax these invoices' =>
144                     "javascript:fax_process()"
145     if $conf->exists('hylafax');
146
147 %><%= include( 'elements/search.html',
148                  'title'       => 'Invoice Search Results',
149                  'html_init'   => $html_init,
150                  'menubar'     => $menubar,
151                  'name'        => 'invoices',
152                  'query'       => $sql_query,
153                  'count_query' => $count_query,
154                  'count_addl'  => $count_addl,
155                  'redirect'    => $link,
156                  'header'      => [ 'Invoice #',
157                                     'Balance',
158                                     'Amount',
159                                     'Date',
160                                     FS::UI::Web::cust_header(),
161                                   ],
162                  'fields'      => [
163                    'invnum',
164                    sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
165                    sub { sprintf($money_char.'%.2f', shift->charged     ) },
166                    sub { time2str('%b %d %Y', shift->_date ) },
167                    \&FS::UI::Web::cust_fields,
168                  ],
169                  'align' => 'rrrrll',
170                  'links' => [
171                    $link,
172                    $link,
173                    $link,
174                    $link,
175                    ( map { $clink } FS::UI::Web::cust_header() ),
176                  ],
177   
178       )
179 %>