import legacy invoice numbers to cust_bill.agent_invid, RT#5351
[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                                     'Net Amount',
13                                     'Gross Amount',
14                                     '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                  'align' => 'rrrr'.FS::UI::Web::cust_aligns(),
26                  'links' => [
27                    $link,
28                    $link,
29                    $link,
30                    $link,
31                    $link,
32                    ( map { $_ ne 'Cust. Status' ? $clink : '' }
33                          FS::UI::Web::cust_header()
34                    ),
35                  ],
36                  'color' => [ 
37                               '',
38                               '',
39                               '',
40                               '',
41                               '',
42                               FS::UI::Web::cust_colors(),
43                             ],
44                  'style' => [ 
45                               '',
46                               '',
47                               '',
48                               '',
49                               '',
50                               FS::UI::Web::cust_styles(),
51                             ],
52
53   
54       )
55 %>
56 <%init>
57
58 die "access denied"
59   unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
60
61 my $join_cust_main = 'LEFT JOIN cust_main USING ( custnum )';
62 #here is the agent virtualization
63 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
64
65 my( $count_query, $sql_query );
66 my $count_addl = '';
67 #my $distinct = '';
68 my %search;
69
70 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
71
72   my $invnum_or_invid = "( invnum = $2 OR agent_invid = $2 )";
73   my $where = "WHERE $invnum_or_invid AND $agentnums_sql";
74   
75   $count_query = "SELECT COUNT(*) FROM cust_bill $join_cust_main $where";
76
77   $sql_query = {
78     #'select'    => '*',
79     'table'     => 'cust_bill',
80     'addl_from' => $join_cust_main,
81     'hashref'   => {},
82     'extra_sql' => $where,
83   };
84
85 } else {
86
87   #some false laziness w/cust_bill::re_X
88   my @where;
89   my $orderby = 'ORDER BY cust_bill._date';
90
91   if ( $cgi->param('beginning')
92        && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
93     $search{'begin'} = str2time($1);
94   }
95   if ( $cgi->param('ending')
96         && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
97     $search{'end'} = str2time($1) + 86399;
98   }
99
100   if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
101     $search{'begin'} = $1;
102   }
103   if ( $cgi->param('end') =~ /^(\d+)$/ ) {
104     $search{'end'} = $1;
105   }
106
107   if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
108     $search{'invnum_min'} = $1;
109   }
110   if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
111     $search{'invnum_max'} = $1;
112   }
113
114   if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
115     $search{'agentnum'} = $1;
116   }
117
118   $search{'open'} = 1 if $cgi->param('open');
119   $search{'net'}  = 1 if $cgi->param('net' );
120
121   my($query) = $cgi->keywords;
122   if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
123     $search{'open'} = 1 if $1;
124     ($search{'days'}, my $field) = ($2, $3);
125     $field = "_date" if $field eq 'date';
126     $orderby = "ORDER BY cust_bill.$field";
127   }
128
129   if ( $cgi->param('newest_percust') ) {
130     $search{'newest_percust'} = 1;
131     $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
132   }
133
134   my $extra_sql = ' WHERE '. FS::cust_bill->search_sql( \%search );
135
136   unless ( $count_query ) {
137     $count_query = 'SELECT COUNT(*), '. join(', ',
138                      map "SUM($_)",
139                          ( 'charged',
140                            FS::cust_bill->net_sql,
141                            FS::cust_bill->owed_sql,
142                          )
143                    );
144     $count_addl = [ '$%.2f invoiced (gross)',
145                     '$%.2f invoiced (net)',
146                     '$%.2f outstanding balance',
147                   ];
148   }
149   $count_query .=  " FROM cust_bill $join_cust_main $extra_sql";
150
151   $sql_query = {
152     'table'     => 'cust_bill',
153     'addl_from' => $join_cust_main,
154     'hashref'   => {},
155     #'select'    => "$distinct ". join(', ',
156     'select'    => join(', ',
157                      'cust_bill.*',
158                      #( map "cust_main.$_", qw(custnum last first company) ),
159                      'cust_main.custnum as cust_main_custnum',
160                      FS::UI::Web::cust_sql_fields(),
161                      FS::cust_bill->owed_sql. ' AS owed',
162                      FS::cust_bill->net_sql.  ' AS net',
163                    ),
164     'extra_sql' => $extra_sql,
165     'order_by'  => $orderby,
166   };
167
168 }
169
170 my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
171 my $clink = sub {
172   my $cust_bill = shift;
173   $cust_bill->cust_main_custnum
174     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
175     : '';
176 };
177
178 my $conf = new FS::Conf;
179 my $money_char = $conf->config('money_char') || '$';
180
181 my $html_init = join("\n", map {
182  ( my $action = $_ ) =~ s/_$//;
183  include('/elements/progress-init.html',
184            $_.'form',
185            [ keys %search ],
186            "../misc/${_}invoices.cgi",
187            { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
188            $_, #key
189         ),
190  qq!<FORM NAME="${_}form">!,
191  ( map qq!<INPUT TYPE="hidden" NAME="$_" VALUE="$search{$_}">!, keys %search ),
192  qq!</FORM>!
193 } qw( print_ email_ fax_ ftp_ spool_ ) ). 
194
195 '<SCRIPT TYPE="text/javascript">
196
197 function confirm_print_process() {
198   if ( ! confirm("Are you sure you want to reprint these invoices?") ) {
199     return;
200   }
201   print_process();
202 }
203 function confirm_email_process() {
204   if ( ! confirm("Are you sure you want to re-email these invoices?") ) {
205     return;
206   }
207   email_process();
208 }
209 function confirm_fax_process() {
210   if ( ! confirm("Are you sure you want to re-fax these invoices?") ) {
211     return;
212   }
213   fax_process();
214 }
215 function confirm_ftp_process() {
216   if ( ! confirm("Are you sure you want to re-FTP these invoices?") ) {
217     return;
218   }
219   ftp_process();
220 }
221 function confirm_spool_process() {
222   if ( ! confirm("Are you sure you want to re-spool these invoices?") ) {
223     return;
224   }
225   spool_process();
226 }
227
228 </SCRIPT>';
229
230 my $menubar = [];
231
232 if ( $FS::CurrentUser::CurrentUser->access_right('Resend invoices') ) {
233
234   push @$menubar, 'Print these invoices' =>
235                     "javascript:confirm_print_process()",
236                   'Email these invoices' =>
237                     "javascript:confirm_email_process()";
238
239   push @$menubar, 'Fax these invoices' =>
240                     "javascript:confirm_fax_process()"
241     if $conf->exists('hylafax');
242
243   push @$menubar, 'FTP these invoices' =>
244                     "javascript:confirm_ftp_process()"
245     if $conf->exists('cust_bill-ftpformat');
246
247   push @$menubar, 'Spool these invoices' =>
248                     "javascript:confirm_spool_process()"
249     if $conf->exists('cust_bill-spoolformat');
250
251 }
252
253 </%init>