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