X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fsearch%2Fcust_bill.cgi;h=d6492fe49e3cb2923931e3c03567267fe44bbd80;hb=d83a273f3ac71431f5b007bb83fb55d7c83eae5c;hp=02dbf558f89bf5050d2715e7e38bd51f91cb4ab0;hpb=91387f8f489e561deaf1de052d80ef800a4970a3;p=freeside.git diff --git a/httemplate/search/cust_bill.cgi b/httemplate/search/cust_bill.cgi index 02dbf558f..d6492fe49 100755 --- a/httemplate/search/cust_bill.cgi +++ b/httemplate/search/cust_bill.cgi @@ -1,76 +1,146 @@ <% -# - -use strict; -use vars qw ( $cgi $invnum $query $sortby @cust_bill ); -use CGI; -use CGI::Carp qw(fatalsToBrowser); -use Date::Format; -use FS::UID qw(cgisuidsetup); -use FS::CGI qw(popurl header menubar eidiot table ); -use FS::Record qw(qsearch qsearchs); -use FS::cust_bill; -use FS::cust_main; - -$cgi = new CGI; -cgisuidsetup($cgi); +my $conf = new FS::Conf; +my $maxrecords = $conf->config('maxsearchrecordsperpage'); + +my $orderby = ''; #removeme + +my $limit = ''; +$limit .= "LIMIT $maxrecords" if $maxrecords; + +my $offset = $cgi->param('offset') || 0; +$limit .= " OFFSET $offset" if $offset; + +my $total; + +my(@cust_bill, $sortby); if ( $cgi->keywords ) { my($query) = $cgi->keywords; + my $open_sql = + "having 0 != charged - coalesce(sum(cust_bill_pay.amount),0) + - coalesce(sum(cust_credit_bill.amount),0)"; + my $having = ''; + my $where = ''; if ( $query eq 'invnum' ) { $sortby = \*invnum_sort; - @cust_bill = qsearch('cust_bill', {} ); + #@cust_bill = qsearch('cust_bill', {} ); } elsif ( $query eq 'date' ) { $sortby = \*date_sort; - @cust_bill = qsearch('cust_bill', {} ); + #@cust_bill = qsearch('cust_bill', {} ); } elsif ( $query eq 'custnum' ) { $sortby = \*custnum_sort; - @cust_bill = qsearch('cust_bill', {} ); + #@cust_bill = qsearch('cust_bill', {} ); } elsif ( $query eq 'OPEN_invnum' ) { $sortby = \*invnum_sort; - @cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} ); + #@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} ); + $having = $open_sql; } elsif ( $query eq 'OPEN_date' ) { $sortby = \*date_sort; - @cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} ); + #@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} ); + $having = $open_sql; } elsif ( $query eq 'OPEN_custnum' ) { $sortby = \*custnum_sort; - @cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} ); + #@cust_bill = grep $_->owed != 0, qsearch('cust_bill', {} ); + $having = $open_sql; } elsif ( $query =~ /^OPEN(\d+)_invnum$/ ) { my $open = $1 * 86400; $sortby = \*invnum_sort; - @cust_bill = - grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} ); + #@cust_bill = + # grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} ); + $having = $open_sql; + $where = "where cust_bill._date < ". (time-$open); } elsif ( $query =~ /^OPEN(\d+)_date$/ ) { my $open = $1 * 86400; $sortby = \*date_sort; - @cust_bill = - grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} ); + #@cust_bill = + # grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} ); + $having = $open_sql; + $where = "where cust_bill._date < ". (time-$open); } elsif ( $query =~ /^OPEN(\d+)_custnum$/ ) { my $open = $1 * 86400; $sortby = \*custnum_sort; - @cust_bill = - grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} ); + #@cust_bill = + # grep $_->owed != 0 && $_->_date < time - $open, qsearch('cust_bill', {} ); + $having = $open_sql; + $where = "where cust_bill._date < ". (time-$open); } else { die "unknown query string $query"; } + + my $extra_sql = " + left outer join cust_bill_pay using ( invnum ) + left outer join cust_credit_bill using ( invnum ) + $where + group by ". join(', ', map "cust_bill.$_", fields('cust_bill') ). ' '. + $having; + + my $statement = "SELECT COUNT(*) FROM cust_bill $extra_sql"; + my $sth = dbh->prepare($statement) or die dbh->errstr. " doing $statement"; + $sth->execute or die "Error executing \"$statement\": ". $sth->errstr; + + $total = $sth->fetchrow_arrayref->[0]; + + @cust_bill = qsearch( + 'cust_bill', + {}, + 'cust_bill.*, + charged - coalesce(sum(cust_bill_pay.amount),0) + - coalesce(sum(cust_credit_bill.amount),0) as owed', + "$extra_sql $orderby $limit" + ); } else { $cgi->param('invnum') =~ /^\s*(FS-)?(\d+)\s*$/; - $invnum = $2; + my $invnum = $2; @cust_bill = qsearchs('cust_bill', { 'invnum' => $invnum } ); $sortby = \*invnum_sort; + $total = scalar(@cust_bill); } -if ( scalar(@cust_bill) == 1 ) { +#if ( scalar(@cust_bill) == 1 ) { +if ( $total == 1 ) { my $invnum = $cust_bill[0]->invnum; print $cgi->redirect(popurl(2). "view/cust_bill.cgi?$invnum"); #redirect } elsif ( scalar(@cust_bill) == 0 ) { +%> + +<% eidiot("Invoice not found."); } else { - my $total = scalar(@cust_bill); - print $cgi->header( @FS::CGI::header ), - &header("Invoice Search Results", menubar( +%> + +<% + #$total = scalar(@cust_bill); + + #begin pager + my $pager = ''; + if ( $total != scalar(@cust_bill) && $maxrecords ) { + unless ( $offset == 0 ) { + $cgi->param('offset', $offset - $maxrecords); + $pager .= 'Previous '; + } + my $poff; + my $page; + for ( $poff = 0; $poff < $total; $poff += $maxrecords ) { + $page++; + if ( $offset == $poff ) { + $pager .= qq!$page !; + } else { + $cgi->param('offset', $poff); + $pager .= qq!$page !; + } + } + unless ( $offset + $maxrecords > $total ) { + $cgi->param('offset', $offset + $maxrecords); + $pager .= 'Next '; + } + } + #end pager + + print header("Invoice Search Results", menubar( 'Main Menu', popurl(2) - )), "$total matching invoices found
", &table(), <
$pager", &table(), < Balance @@ -82,7 +152,7 @@ if ( scalar(@cust_bill) == 1 ) { END my(%saw, $cust_bill); - my($tot_balance, $tot_amount) = (0, 0); + my($tot_balance, $tot_amount) = (0, 0); #BOGUS foreach $cust_bill ( sort $sortby grep(!$saw{$_->invnum}++, @cust_bill) ) { @@ -132,7 +202,7 @@ END print <TotalTotal \$$tot_balance\$$tot_amount - + $pager END