quiet warnings about CGI::param in list context
[freeside.git] / httemplate / search / cust_bill.html
1 <& elements/search.html,
2   'title'       => emt('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'      => [ emt('Invoice #'),
11                      emt($invoiced ? 'Charged' : 'Gross Amount'),
12                      emt('Discount'),
13                      emt('Credits'),
14                      emt('Net Amount'),
15                      emt('Balance'),
16                      emt('Date'),
17                      FS::UI::Web::cust_header(),
18                    ],
19   'fields'      => [
20     'display_invnum',
21     $invoiced ? 'charged' : 'gross',
22     'discounted',
23     'credited',
24     'net',
25     'owed',
26     sub { time2str('%b %d %Y', shift->_date ) },
27     \&FS::UI::Web::cust_fields,
28   ],
29   'sort_fields' => [
30     'COALESCE( agent_invid, invnum )',
31     $invoiced ? 'charged' : 'gross',
32     'discounted',
33     'credited',
34     'net',
35     'owed',
36     '_date',
37   ],
38   'format' => [
39     '',
40     $money_char.'%.2f',
41     $money_char.'%.2f',
42     $money_char.'%.2f',
43     $money_char.'%.2f',
44     $money_char.'%.2f',
45     '',
46   ],
47   'align' => 'rrrrrrl'.FS::UI::Web::cust_aligns(),
48   'links' => [
49     $link,
50     $link,
51     $link,
52     $link,
53     $link,
54     $link,
55     $link,
56     ( map { $_ ne 'Cust. Status' ? $clink : '' }
57           FS::UI::Web::cust_header()
58     ),
59   ],
60   'color' => [ 
61                '',
62                '',
63                '',
64                '',
65                '',
66                '',
67                '',
68                FS::UI::Web::cust_colors(),
69              ],
70   'style' => [ 
71                '',
72                '',
73                '',
74                '',
75                '',
76                '',
77                '',
78                FS::UI::Web::cust_styles(),
79              ],
80 &>
81 <%init>
82
83 die "access denied"
84   unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
85
86 my( $count_query, $sql_query );
87 my $count_addl = '';
88 my %search = ();
89
90 # show invoiced amount (charged) instead of gross sales
91 my $invoiced = $cgi->param('invoiced') ? 1 : 0;
92
93 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
94
95   my $join_cust_main = FS::UI::Web::join_cust_main('cust_bill');
96   #here is the agent virtualization
97   my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
98
99   my $invnum_or_invid = "( invnum = $2 OR agent_invid = $2 )";
100   my $where = "WHERE $invnum_or_invid AND $agentnums_sql";
101   
102   $count_query = "SELECT COUNT(*) FROM cust_bill $join_cust_main $where";
103
104   $sql_query = {
105     'table'     => 'cust_bill',
106     'addl_from' => $join_cust_main,
107     'hashref'   => {},
108     'extra_sql' => $where,
109   };
110
111   if ( FS::Record->scalar_sql($count_query) == 0 ) {
112
113     # check for a voided invoice
114     $count_query =~ s/cust_bill/cust_bill_void/g;
115     if ( FS::Record->scalar_sql($count_query) > 0 ) {
116       # Redirect to the void search.
117       my $url = $cgi->self_url;
118       $url =~ s(search/cust_bill)(search/cust_bill_void);
119       $m->clear_buffer;
120       $m->print($cgi->redirect($url));
121       $m->abort;
122     }
123   }
124
125 } else {
126
127   #deal with some old-style URLs
128   my($query) = $cgi->keywords;
129   if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
130     $search{'open'} = 1 if $1;
131     ($search{'days'}, my $field) = ($2, $3);
132     $field = "_date" if $field eq 'date';
133     $search{'order_by'} = "cust_bill.$field";
134   }
135
136   #scalars
137   for (qw( agentnum custnum cust_status refnum invnum_min invnum_max
138            open net newest_percust invoiced
139
140       )) 
141   {
142     $search{$_} = $cgi->param($_) if length($cgi->param($_));
143   }
144
145   #arrays
146   for my $param (qw( cust_classnum )) {
147   $search{$param} = [ $cgi->param($param) ]
148     if grep { $_ eq $param } $cgi->param;
149   }
150
151   #amounts (range)
152   $search{$_} = [ FS::UI::Web::parse_lt_gt($cgi, $_) ]
153     foreach qw( charged owed );
154
155   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
156   $search{'_date'} = [ $beginning, $ending ]
157     unless $beginning == 0 && $ending == 4294967295;
158
159   # promised date
160   my $start_of_day = timelocal(0, 0, 0, (localtime(time))[3,4,5]);
161   foreach ( $cgi->param('promised_date') ) {
162     # only if at least one box is checked
163     $search{promised_date} ||= [ $start_of_day, $start_of_day, 0 ];
164     if ($_ eq 'past') {
165       # accept everything before today
166       $search{promised_date}[0] = 0;
167     }
168     elsif ( $_ eq 'future' ) {
169       # accept everything after today
170       $search{promised_date}[1] = 4294967295;
171     }
172     elsif ( $_ eq 'null' ) {
173       # accept nulls
174       $search{promised_date}[2] = 1;
175     }
176   }
177
178   $sql_query   = FS::cust_bill->search(\%search);
179   $count_query = delete( $sql_query->{'count_query'} );
180   $count_addl  = delete( $sql_query->{'count_addl'}  );
181
182 }
183
184 my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
185 my $clink = sub {
186   my $cust_bill = shift;
187   $cust_bill->cust_main_custnum
188     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
189     : '';
190 };
191
192 my $conf = new FS::Conf;
193 my $money_char = $conf->config('money_char') || '$';
194
195 my $html_init = join("\n", map {
196  ( my $action = $_ ) =~ s/_$//;
197  include('/elements/progress-init.html',
198            $_.'form',
199            [ sort keys %search ],
200            "../misc/${_}invoices.cgi",
201            { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
202            $_, #key
203         ),
204  qq!<FORM NAME="${_}form">!,
205  ( map { my $f = $_;
206          my @values = ref($search{$f}) ? @{ $search{$f} } : $search{$f};
207          map qq!<INPUT TYPE="hidden" NAME="$f" VALUE="$_">!, @values;
208        }
209        sort keys %search
210  ),
211  qq!</FORM>!
212 } qw( print_ email_ fax_ ftp_ spool_ ) ). 
213
214 '<SCRIPT TYPE="text/javascript">
215
216 function confirm_print_process() {
217   if ( ! confirm('.js_mt("Are you sure you want to reprint these invoices?").') ) {
218     return;
219   }
220   print_process();
221 }
222 function confirm_email_process() {
223   if ( ! confirm('.js_mt("Are you sure you want to re-email these invoices?").') ) {
224     return;
225   }
226   email_process();
227 }
228 function confirm_fax_process() {
229   if ( ! confirm('.js_mt("Are you sure you want to re-fax these invoices?").') ) {
230     return;
231   }
232   fax_process();
233 }
234 function confirm_ftp_process() {
235   if ( ! confirm('.js_mt("Are you sure you want to re-FTP these invoices?").') ) {
236     return;
237   }
238   ftp_process();
239 }
240 function confirm_spool_process() {
241   if ( ! confirm('.js_mt("Are you sure you want to re-spool these invoices?").') ) {
242     return;
243   }
244   spool_process();
245 }
246
247 </SCRIPT>';
248
249 my $menubar = [];
250
251 if ( $FS::CurrentUser::CurrentUser->access_right('Resend invoices') ) {
252
253   push @$menubar, emt('Print these invoices') =>
254                     "javascript:confirm_print_process()",
255                   emt('Email these invoices') =>
256                     "javascript:confirm_email_process()";
257
258   push @$menubar, emt('Fax these invoices') =>
259                     "javascript:confirm_fax_process()"
260     if $conf->exists('hylafax');
261
262   push @$menubar, emt('FTP these invoices') =>
263                     "javascript:confirm_ftp_process()"
264     if $conf->exists('cust_bill-ftpformat');
265
266   push @$menubar, emt('Spool these invoices') =>
267                     "javascript:confirm_spool_process()"
268     if $conf->exists('cust_bill-spoolformat');
269
270 }
271
272 </%init>