multiple payment options (remove cust_main.payby reporting options), RT#23741
[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( $count_query, $sql_query );
66 my $count_addl = '';
67 my %search = ();
68
69 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
70
71   my $join_cust_main = FS::UI::Web::join_cust_main('cust_bill');
72   #here is the agent virtualization
73   my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
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   #deal with some old-style URLs
104   my($query) = $cgi->keywords;
105   if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
106     $search{'open'} = 1 if $1;
107     ($search{'days'}, my $field) = ($2, $3);
108     $field = "_date" if $field eq 'date';
109     $search{'order_by'} = "cust_bill.$field";
110   }
111
112   #scalars
113   for (qw( agentnum custnum cust_status refnum invnum_min invnum_max
114            open net newest_percust 
115
116       )) 
117   {
118     $search{$_} = $cgi->param($_) if length($cgi->param($_));
119   }
120
121   #arrays
122   for my $param (qw( cust_classnum )) {
123   $search{$param} = [ $cgi->param($param) ]
124     if grep { $_ eq $param } $cgi->param;
125   }
126
127   #amounts (range)
128   $search{$_} = [ FS::UI::Web::parse_lt_gt($cgi, $_) ]
129     foreach qw( charged owed );
130
131   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
132   $search{'_date'} = [ $beginning, $ending ]
133     unless $beginning == 0 && $ending == 4294967295;
134
135   # promised date
136   my $start_of_day = timelocal(0, 0, 0, (localtime(time))[3,4,5]);
137   foreach ( $cgi->param('promised_date') ) {
138     # only if at least one box is checked
139     $search{promised_date} ||= [ $start_of_day, $start_of_day, 0 ];
140     if ($_ eq 'past') {
141       # accept everything before today
142       $search{promised_date}[0] = 0;
143     }
144     elsif ( $_ eq 'future' ) {
145       # accept everything after today
146       $search{promised_date}[1] = 4294967295;
147     }
148     elsif ( $_ eq 'null' ) {
149       # accept nulls
150       $search{promised_date}[2] = 1;
151     }
152   }
153
154   $sql_query   = FS::cust_bill->search(\%search);
155   $count_query = delete( $sql_query->{'count_query'} );
156   $count_addl  = delete( $sql_query->{'count_addl'}  );
157
158 }
159
160 my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
161 my $clink = sub {
162   my $cust_bill = shift;
163   $cust_bill->cust_main_custnum
164     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
165     : '';
166 };
167
168 my $conf = new FS::Conf;
169 my $money_char = $conf->config('money_char') || '$';
170
171 my $html_init = join("\n", map {
172  ( my $action = $_ ) =~ s/_$//;
173  include('/elements/progress-init.html',
174            $_.'form',
175            [ keys %search ],
176            "../misc/${_}invoices.cgi",
177            { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
178            $_, #key
179         ),
180  qq!<FORM NAME="${_}form">!,
181  ( map { my $f = $_;
182          my @values = ref($search{$f}) ? @{ $search{$f} } : $search{$f};
183          map qq!<INPUT TYPE="hidden" NAME="$f" VALUE="$_">!, @values;
184        }
185        keys %search
186  ),
187  qq!</FORM>!
188 } qw( print_ email_ fax_ ftp_ spool_ ) ). 
189
190 '<SCRIPT TYPE="text/javascript">
191
192 function confirm_print_process() {
193   if ( ! confirm('.js_mt("Are you sure you want to reprint these invoices?").') ) {
194     return;
195   }
196   print_process();
197 }
198 function confirm_email_process() {
199   if ( ! confirm('.js_mt("Are you sure you want to re-email these invoices?").') ) {
200     return;
201   }
202   email_process();
203 }
204 function confirm_fax_process() {
205   if ( ! confirm('.js_mt("Are you sure you want to re-fax these invoices?").') ) {
206     return;
207   }
208   fax_process();
209 }
210 function confirm_ftp_process() {
211   if ( ! confirm('.js_mt("Are you sure you want to re-FTP these invoices?").') ) {
212     return;
213   }
214   ftp_process();
215 }
216 function confirm_spool_process() {
217   if ( ! confirm('.js_mt("Are you sure you want to re-spool these invoices?").') ) {
218     return;
219   }
220   spool_process();
221 }
222
223 </SCRIPT>';
224
225 my $menubar = [];
226
227 if ( $FS::CurrentUser::CurrentUser->access_right('Resend invoices') ) {
228
229   push @$menubar, emt('Print these invoices') =>
230                     "javascript:confirm_print_process()",
231                   emt('Email these invoices') =>
232                     "javascript:confirm_email_process()";
233
234   push @$menubar, emt('Fax these invoices') =>
235                     "javascript:confirm_fax_process()"
236     if $conf->exists('hylafax');
237
238   push @$menubar, emt('FTP these invoices') =>
239                     "javascript:confirm_ftp_process()"
240     if $conf->exists('cust_bill-ftpformat');
241
242   push @$menubar, emt('Spool these invoices') =>
243                     "javascript:confirm_spool_process()"
244     if $conf->exists('cust_bill-spoolformat');
245
246 }
247
248 </%init>