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 #',
14 FS::UI::Web::cust_header(),
18 sub { sprintf($money_char.'%.2f', shift->get('owed') ) },
19 sub { sprintf($money_char.'%.2f', shift->charged ) },
20 sub { time2str('%b %d %Y', shift->_date ) },
21 \&FS::UI::Web::cust_fields,
23 'align' => 'rrrr'.FS::UI::Web::cust_aligns(),
29 ( map { $_ ne 'Cust. Status' ? $clink : '' }
30 FS::UI::Web::cust_header()
38 FS::UI::Web::cust_colors(),
45 FS::UI::Web::cust_styles(),
54 unless $FS::CurrentUser::CurrentUser->access_right('List invoices');
56 my $join_cust_main = 'LEFT JOIN cust_main USING ( custnum )';
57 #here is the agent virtualization
58 my $agentnums_sql = $FS::CurrentUser::CurrentUser->agentnums_sql;
60 my( $count_query, $sql_query );
61 my( $count_addl ) = ( '' );
62 my( $distinct ) = ( '' );
63 my($begin, $end) = ( '', '' );
64 my($agentnum) = ( '' );
65 my($open, $days) = ( '', '' );
66 if ( $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/ ) {
68 "SELECT COUNT(*) FROM cust_bill $join_cust_main".
69 " WHERE invnum = $2 AND $agentnums_sql"; #agent virtualization
71 'table' => 'cust_bill',
72 'addl_from' => $join_cust_main,
73 'hashref' => { 'invnum' => $2 },
75 'extra_sql' => " AND $agentnums_sql", #agent virtualization
78 #if ( $cgi->param('begin') || $cgi->param('end')
79 # || $cgi->param('beginning') || $cgi->param('ending')
84 #some false laziness w/cust_bill::re_X
86 my $orderby = 'ORDER BY cust_bill._date';
88 if ( $cgi->param('beginning')
89 && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) {
90 $begin = str2time($1);
91 push @where, "cust_bill._date >= $begin";
93 if ( $cgi->param('ending')
94 && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) {
95 $end = str2time($1) + 86399;
96 push @where, "cust_bill._date < $end";
99 if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
101 push @where, "cust_bill._date >= $begin";
103 if ( $cgi->param('end') =~ /^(\d+)$/ ) {
105 push @where, "cust_bill._date < $end";
108 if ( $cgi->param('invnum_min') =~ /^\s*(\d+)\s*$/ ) {
109 push @where, "cust_bill.invnum >= $1";
111 if ( $cgi->param('invnum_max') =~ /^\s*(\d+)\s*$/ ) {
112 push @where, "cust_bill.invnum <= $1";
115 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
117 push @where, "cust_main.agentnum = $agentnum";
121 "charged - ( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
122 WHERE cust_bill_pay.invnum = cust_bill.invnum )
123 - ( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
124 WHERE cust_credit_bill.invnum = cust_bill.invnum )";
126 if ( $cgi->param('open') ) {
127 push @where, "0 != $owed";
131 my($query) = $cgi->keywords;
132 if ( $query =~ /^(OPEN(\d*)_)?(invnum|date|custnum)$/ ) {
133 ($open, $days, my $field) = ($1, $2, $3);
134 $field = "_date" if $field eq 'date';
135 $orderby = "ORDER BY cust_bill.$field";
136 push @where, "0 != $owed" if $open;
137 push @where, "cust_bill._date < ". (time-86400*$days) if $days;
140 #here is the agent virtualization
141 push @where, $agentnums_sql;
142 my $extra_sql = scalar(@where) ? 'WHERE '. join(' AND ', @where) : '';
144 if ( $cgi->param('newest_percust') ) {
145 $distinct = 'DISTINCT ON ( cust_bill.custnum )';
146 $orderby = 'ORDER BY cust_bill.custnum ASC, cust_bill._date DESC';
147 #$count_query = "SELECT 'N/A', 'N/A', 'N/A'"; #XXXXXXX fix
148 $count_query = "SELECT COUNT(DISTINCT cust_bill.custnum), 'N/A', 'N/A'";
151 unless ( $count_query ) {
152 $count_query = "SELECT COUNT(*), sum(charged), sum($owed)";
153 $count_addl = [ '$%.2f total invoiced',
154 '$%.2f total outstanding balance',
157 $count_query .= " FROM cust_bill $join_cust_main $extra_sql";
160 'table' => 'cust_bill',
161 'addl_from' => $join_cust_main,
163 'select' => "$distinct ". join(', ',
165 #( map "cust_main.$_", qw(custnum last first company) ),
166 'cust_main.custnum as cust_main_custnum',
167 FS::UI::Web::cust_sql_fields(),
170 'extra_sql' => "$extra_sql $orderby"
175 my $link = [ "${p}view/cust_bill.cgi?", 'invnum', ];
177 my $cust_bill = shift;
178 $cust_bill->cust_main_custnum
179 ? [ "${p}view/cust_main.cgi?", 'custnum' ]
183 my $conf = new FS::Conf;
184 my $money_char = $conf->config('money_char') || '$';
186 my $html_init = join("\n", map {
187 ( my $action = $_ ) =~ s/_$//;
188 include('/elements/progress-init.html',
190 [ 'begin', 'end', 'agentnum', 'open', 'days', 'newest_percust' ],
191 "../misc/${_}invoices.cgi",
192 { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
195 qq!<FORM NAME="${_}form">!,
196 qq!<INPUT TYPE="hidden" NAME="begin" VALUE="$begin">!,
197 qq!<INPUT TYPE="hidden" NAME="end" VALUE="$end">!,
198 qq!<INPUT TYPE="hidden" NAME="agentnum" VALUE="$agentnum">!,
199 qq!<INPUT TYPE="hidden" NAME="open" VALUE="$open">!,
200 qq!<INPUT TYPE="hidden" NAME="days" VALUE="$days">!,
202 } qw( print_ email_ fax_ ) ).
204 '<SCRIPT TYPE="text/javascript">
206 function confirm_print_process() {
207 if ( ! confirm("Are you sure you want to reprint these invoices?") ) {
212 function confirm_email_process() {
213 if ( ! confirm("Are you sure you want to re-email these invoices?") ) {
218 function confirm_fax_process() {
219 if ( ! confirm("Are you sure you want to re-fax these invoices?") ) {
229 'Print these invoices' =>
230 "javascript:confirm_print_process()",
231 'Email these invoices' =>
232 "javascript:confirm_email_process()",
235 push @$menubar, 'Fax these invoices' =>
236 "javascript:confirm_fax_process()"
237 if $conf->exists('hylafax');