repeatability cleanup, #37340
[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     $search{'invoiced'} = 1; # preserve old behavior under 3.x
135   }
136
137   #scalars
138   for (qw( agentnum custnum cust_status refnum invnum_min invnum_max
139            open net newest_percust invoiced
140
141       )) 
142   {
143     $search{$_} = $cgi->param($_) if length($cgi->param($_));
144   }
145
146   #arrays
147   for my $param (qw( cust_classnum payby )) {
148   $search{$param} = [ $cgi->param($param) ]
149     if grep { $_ eq $param } $cgi->param;
150   }
151
152   #amounts (range)
153   $search{$_} = [ FS::UI::Web::parse_lt_gt($cgi, $_) ]
154     foreach qw( charged owed );
155
156   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
157   $search{'_date'} = [ $beginning, $ending ]
158     unless $beginning == 0 && $ending == 4294967295;
159
160   # promised date
161   my $start_of_day = timelocal(0, 0, 0, (localtime(time))[3,4,5]);
162   foreach ( $cgi->param('promised_date') ) {
163     # only if at least one box is checked
164     $search{promised_date} ||= [ $start_of_day, $start_of_day, 0 ];
165     if ($_ eq 'past') {
166       # accept everything before today
167       $search{promised_date}[0] = 0;
168     }
169     elsif ( $_ eq 'future' ) {
170       # accept everything after today
171       $search{promised_date}[1] = 4294967295;
172     }
173     elsif ( $_ eq 'null' ) {
174       # accept nulls
175       $search{promised_date}[2] = 1;
176     }
177   }
178
179   $sql_query   = FS::cust_bill->search(\%search);
180   $count_query = delete( $sql_query->{'count_query'} );
181   $count_addl  = delete( $sql_query->{'count_addl'}  );
182
183 }
184
185 my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
186 my $clink = sub {
187   my $cust_bill = shift;
188   $cust_bill->cust_main_custnum
189     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
190     : '';
191 };
192
193 my $conf = new FS::Conf;
194 my $money_char = $conf->config('money_char') || '$';
195
196 my $html_init = join("\n", map {
197  ( my $action = $_ ) =~ s/_$//;
198  include('/elements/progress-init.html',
199            $_.'form',
200            [ sort keys %search ],
201            "../misc/${_}invoices.cgi",
202            { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
203            $_, #key
204         ),
205  qq!<FORM NAME="${_}form">!,
206  ( map { my $f = $_;
207          my @values = ref($search{$f}) ? @{ $search{$f} } : $search{$f};
208          map qq!<INPUT TYPE="hidden" NAME="$f" VALUE="$_">!, @values;
209        }
210        sort keys %search
211  ),
212  qq!</FORM>!
213 } qw( print_ email_ fax_ ftp_ spool_ ) ). 
214
215 '<SCRIPT TYPE="text/javascript">
216
217 function confirm_print_process() {
218   if ( ! confirm('.js_mt("Are you sure you want to reprint these invoices?").') ) {
219     return;
220   }
221   print_process();
222 }
223 function confirm_email_process() {
224   if ( ! confirm('.js_mt("Are you sure you want to re-email these invoices?").') ) {
225     return;
226   }
227   email_process();
228 }
229 function confirm_fax_process() {
230   if ( ! confirm('.js_mt("Are you sure you want to re-fax these invoices?").') ) {
231     return;
232   }
233   fax_process();
234 }
235 function confirm_ftp_process() {
236   if ( ! confirm('.js_mt("Are you sure you want to re-FTP these invoices?").') ) {
237     return;
238   }
239   ftp_process();
240 }
241 function confirm_spool_process() {
242   if ( ! confirm('.js_mt("Are you sure you want to re-spool these invoices?").') ) {
243     return;
244   }
245   spool_process();
246 }
247
248 </SCRIPT>';
249
250 my $menubar = [];
251
252 if ( $FS::CurrentUser::CurrentUser->access_right('Resend invoices') ) {
253
254   push @$menubar, emt('Print these invoices') =>
255                     "javascript:confirm_print_process()",
256                   emt('Email these invoices') =>
257                     "javascript:confirm_email_process()";
258
259   push @$menubar, emt('Fax these invoices') =>
260                     "javascript:confirm_fax_process()"
261     if $conf->exists('hylafax');
262
263   push @$menubar, emt('FTP these invoices') =>
264                     "javascript:confirm_ftp_process()"
265     if $conf->exists('cust_bill-ftpformat');
266
267   push @$menubar, emt('Spool these invoices') =>
268                     "javascript:confirm_spool_process()"
269     if $conf->exists('cust_bill-spoolformat');
270
271 }
272
273 </%init>