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