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