1 <% include( 'elements/search.html',
2 'title' => 'Invoice Search Results',
3 'html_init' => $html_init,
7 'count_query' => $count_query,
8 'count_addl' => $count_addl,
10 'header' => [ 'Invoice #',
15 FS::UI::Web::cust_header(),
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,
25 'align' => 'rrrr'.FS::UI::Web::cust_aligns(),
32 ( map { $_ ne 'Cust. Status' ? $clink : '' }
33 FS::UI::Web::cust_header()
42 FS::UI::Web::cust_colors(),
50 FS::UI::Web::cust_styles(),
59 unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
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;
65 my( $count_query, $sql_query );
70 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
73 "SELECT COUNT(*) FROM cust_bill $join_cust_main".
74 " WHERE invnum = $2 AND $agentnums_sql"; #agent virtualization
76 'table' => 'cust_bill',
77 'addl_from' => $join_cust_main,
78 'hashref' => { 'invnum' => $2 },
80 'extra_sql' => " AND $agentnums_sql", #agent virtualization
85 #some false laziness w/cust_bill::re_X
87 my $orderby = 'ORDER BY cust_bill._date';
89 if ( $cgi->param('beginning')
90 && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
91 $search{'begin'} = str2time($1);
92 push @where, 'cust_bill._date >= '. $search{'begin'};
94 if ( $cgi->param('ending')
95 && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
96 $search{'end'} = str2time($1) + 86399;
97 push @where, 'cust_bill._date < '. $search{'end'};
100 if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
101 $search{'begin'} = $1;
102 push @where, 'cust_bill._date >= '. $search{'begin'};
104 if ( $cgi->param('end') =~ /^(\d+)$/ ) {
106 push @where, 'cust_bill._date < '. $search{'end'};
109 if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
110 $search{'invnum_min'} = $1;
111 push @where, 'cust_bill.invnum >= '. $search{'invnum_min'};
113 if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
114 $search{'invnum_max'} = $1;
115 push @where, 'cust_bill.invnum <= '. $search{'invnum_max'};
118 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
119 $search{'agentnum'} = $1;
120 push @where, 'cust_main.agentnum = '. $search{'agentnum'};
123 if ( $cgi->param('open') ) {
124 push @where, '0 != '. FS::cust_bill->owed_sql;
128 if ( $cgi->param('net') ) {
129 push @where, '0 != '. FS::cust_bill->net_sql;
133 my($query) = $cgi->keywords;
134 if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
135 ($search{'open'}, $search{'days'}, my $field) = ($1, $2, $3);
136 $field = "_date" if $field eq 'date';
137 $orderby = "ORDER BY cust_bill.$field";
138 push @where, '0 != '. FS::cust_bill->owed_sql if $search{'open'};
139 push @where, "cust_bill._date < ". (time-86400*$search{'days'})
143 if ( $cgi->param('newest_percust') ) {
144 $search{'newest_percust'} = 1;
146 #$distinct = 'DISTINCT ON ( cust_bill.custnum )';
147 #$orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
148 #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
150 my @newest_where = map { my $x = $_;
151 $x =~ s/\bcust_bill\./newest_cust_bill./g;
154 grep ! /^cust_main./, @where;
155 my $newest_where = scalar(@newest_where)
156 ? ' AND '. join(' AND ', @newest_where)
159 push @where, "cust_bill._date = (
160 SELECT(MAX(newest_cust_bill._date)) FROM cust_bill AS newest_cust_bill
161 WHERE newest_cust_bill.custnum = cust_bill.custnum
166 $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
169 #here is the agent virtualization
170 push @where, $agentnums_sql;
172 my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
174 unless ( $count_query ) {
175 $count_query = 'SELECT COUNT(*), '. join(', ',
178 FS::cust_bill->net_sql,
179 FS::cust_bill->owed_sql,
182 $count_addl = [ '$%.2f invoiced (gross)',
183 '$%.2f invoiced (net)',
184 '$%.2f outstanding balance',
187 $count_query .= " FROM cust_bill $join_cust_main $extra_sql";
190 'table' => 'cust_bill',
191 'addl_from' => $join_cust_main,
193 #'select' => "$distinct ". join(', ',
194 'select' => join(', ',
196 #( map "cust_main.$_", qw(custnum last first company) ),
197 'cust_main.custnum as cust_main_custnum',
198 FS::UI::Web::cust_sql_fields(),
199 FS::cust_bill->owed_sql. ' AS owed',
200 FS::cust_bill->net_sql. ' AS net',
202 'extra_sql' => "$extra_sql $orderby"
207 my $link = [ "${p}view/cust_bill.cgi?", 'invnum', ];
209 my $cust_bill = shift;
210 $cust_bill->cust_main_custnum
211 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
215 my $conf = new FS::Conf;
216 my $money_char = $conf->config('money_char') || '$';
218 my $html_init = join("\n", map {
219 ( my $action = $_ ) =~ s/_$//;
220 include('/elements/progress-init.html',
223 "../misc/${_}invoices.cgi",
224 { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
227 qq!<FORM NAME="${_}form">!,
228 ( map qq!<INPUT TYPE="hidden" NAME="$_" VALUE="$search{$_}">!, keys %search ),
230 } qw( print_ email_ fax_ ) );
234 'Print these invoices' =>
235 "javascript:print_process()",
236 'Email these invoices' =>
237 "javascript:email_process()",
240 push @$menubar, 'Fax these invoices' =>
241 "javascript:fax_process()"
242 if $conf->exists('hylafax');