Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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 } else {
88
89   #some false laziness w/cust_bill::re_X
90   my $orderby = 'ORDER BY cust_bill._date';
91
92   if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
93     $search{'agentnum'} = $1;
94   }
95
96   if ( $cgi->param('refnum') =~ /^(\d+)$/ ) {
97     $search{'refnum'} = $1;
98   }
99
100 if ( grep { $_ eq 'cust_classnum' } $cgi->param ) {
101     $search{'cust_classnum'} = [ $cgi->param('cust_classnum') ];
102   }
103
104   if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
105     $search{'custnum'} = $1;
106   }
107
108   # begin/end/beginning/ending
109   my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi, '');
110   $search{'_date'} = [ $beginning, $ending ]
111     unless $beginning == 0 && $ending == 4294967295;
112
113   if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
114     $search{'invnum_min'} = $1;
115   }
116   if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
117     $search{'invnum_max'} = $1;
118   }
119
120   #amounts
121   $search{$_} = [ FS::UI::Web::parse_lt_gt($cgi, $_) ]
122     foreach qw( charged owed );
123
124   $search{'open'} = 1 if $cgi->param('open');
125   $search{'net'}  = 1 if $cgi->param('net' );
126
127   my($query) = $cgi->keywords;
128   if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
129     $search{'open'} = 1 if $1;
130     ($search{'days'}, my $field) = ($2, $3);
131     $field = "_date" if $field eq 'date';
132     $orderby = "ORDER BY cust_bill.$field";
133   }
134
135   if ( $cgi->param('newest_percust') ) {
136     $search{'newest_percust'} = 1;
137     $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
138   }
139
140   # promised date
141   my $start_of_day = timelocal(0, 0, 0, (localtime(time))[3,4,5]);
142   foreach ( $cgi->param('promised_date') ) {
143     # only if at least one box is checked
144     $search{promised_date} ||= [ $start_of_day, $start_of_day, 0 ];
145     if ($_ eq 'past') {
146       # accept everything before today
147       $search{promised_date}[0] = 0;
148     }
149     elsif ( $_ eq 'future' ) {
150       # accept everything after today
151       $search{promised_date}[1] = 4294967295;
152     }
153     elsif ( $_ eq 'null' ) {
154       # accept nulls
155       $search{promised_date}[2] = 1;
156     }
157   }
158
159   my $payby_sql = '';
160   $payby_sql = ' AND (' . 
161     join(' OR ', map { "cust_main.payby = '$_'" } $cgi->param('payby') ) . 
162     ')' 
163     if $cgi->param('payby');
164
165   my $extra_sql = ' WHERE '.
166     FS::cust_bill->search_sql_where( \%search ).
167     $payby_sql;
168
169   unless ( $count_query ) {
170     $count_query = 'SELECT COUNT(*), '. join(', ',
171                      map "SUM($_)",
172                          ( 'charged',
173                            FS::cust_bill->net_sql,
174                            FS::cust_bill->owed_sql,
175                          )
176                    );
177     $count_addl = [ '$%.2f invoiced (gross)',
178                     '$%.2f invoiced (net)',
179                     '$%.2f outstanding balance',
180                   ];
181   }
182   $count_query .=  " FROM cust_bill $join_cust_main $extra_sql";
183
184   $sql_query = {
185     'table'     => 'cust_bill',
186     'addl_from' => $join_cust_main,
187     'hashref'   => {},
188     'select'    => join(', ',
189                      'cust_bill.*',
190                      #( map "cust_main.$_", qw(custnum last first company) ),
191                      'cust_main.custnum as cust_main_custnum',
192                      FS::UI::Web::cust_sql_fields(),
193                      FS::cust_bill->owed_sql. ' AS owed',
194                      FS::cust_bill->net_sql.  ' AS net',
195                    ),
196     'extra_sql' => $extra_sql,
197     'order_by'  => $orderby,
198   };
199
200 }
201 my $link  = [ "${p}view/cust_bill.cgi?", 'invnum', ];
202 my $clink = sub {
203   my $cust_bill = shift;
204   $cust_bill->cust_main_custnum
205     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
206     : '';
207 };
208
209 my $conf = new FS::Conf;
210 my $money_char = $conf->config('money_char') || '$';
211
212 my $html_init = join("\n", map {
213  ( my $action = $_ ) =~ s/_$//;
214  include('/elements/progress-init.html',
215            $_.'form',
216            [ keys %search ],
217            "../misc/${_}invoices.cgi",
218            { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
219            $_, #key
220         ),
221  qq!<FORM NAME="${_}form">!,
222  ( map { my $f = $_;
223          my @values = ref($search{$f}) ? @{ $search{$f} } : $search{$f};
224          map qq!<INPUT TYPE="hidden" NAME="$f" VALUE="$_">!, @values;
225        }
226        keys %search
227  ),
228  qq!</FORM>!
229 } qw( print_ email_ fax_ ftp_ spool_ ) ). 
230
231 '<SCRIPT TYPE="text/javascript">
232
233 function confirm_print_process() {
234   if ( ! confirm('.js_mt("Are you sure you want to reprint these invoices?").') ) {
235     return;
236   }
237   print_process();
238 }
239 function confirm_email_process() {
240   if ( ! confirm('.js_mt("Are you sure you want to re-email these invoices?").') ) {
241     return;
242   }
243   email_process();
244 }
245 function confirm_fax_process() {
246   if ( ! confirm('.js_mt("Are you sure you want to re-fax these invoices?").') ) {
247     return;
248   }
249   fax_process();
250 }
251 function confirm_ftp_process() {
252   if ( ! confirm('.js_mt("Are you sure you want to re-FTP these invoices?").') ) {
253     return;
254   }
255   ftp_process();
256 }
257 function confirm_spool_process() {
258   if ( ! confirm('.js_mt("Are you sure you want to re-spool these invoices?").') ) {
259     return;
260   }
261   spool_process();
262 }
263
264 </SCRIPT>';
265
266 my $menubar = [];
267
268 if ( $FS::CurrentUser::CurrentUser->access_right('Resend invoices') ) {
269
270   push @$menubar, emt('Print these invoices') =>
271                     "javascript:confirm_print_process()",
272                   emt('Email these invoices') =>
273                     "javascript:confirm_email_process()";
274
275   push @$menubar, emt('Fax these invoices') =>
276                     "javascript:confirm_fax_process()"
277     if $conf->exists('hylafax');
278
279   push @$menubar, emt('FTP these invoices') =>
280                     "javascript:confirm_ftp_process()"
281     if $conf->exists('cust_bill-ftpformat');
282
283   push @$menubar, emt('Spool these invoices') =>
284                     "javascript:confirm_spool_process()"
285     if $conf->exists('cust_bill-spoolformat');
286
287 }
288
289 </%init>