From 63ec39a8c2a32b5ac325c4973945a26398678347 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 3 Dec 2001 10:59:25 +0000 Subject: [PATCH] paged package browse --- httemplate/search/cust_main.cgi | 8 ++- httemplate/search/cust_pkg.cgi | 127 ++++++++++++++++++++++++++++++---------- 2 files changed, 101 insertions(+), 34 deletions(-) diff --git a/httemplate/search/cust_main.cgi b/httemplate/search/cust_main.cgi index b26889d0b..d5bc17aec 100755 --- a/httemplate/search/cust_main.cgi +++ b/httemplate/search/cust_main.cgi @@ -1,15 +1,17 @@ <% -# +# use strict; #use vars qw( $conf %ncancelled_pkgs %all_pkgs $cgi @cust_main $sortby ); #use vars qw( $conf %all_pkgs $cgi @cust_main $sortby ); -use vars qw( $conf %all_pkgs $cgi @cust_main $sortby $orderby $maxrecords $limit $offset ); +use vars qw( $conf %all_pkgs $cgi @cust_main $sortby + $orderby $maxrecords $limit $offset ); use CGI; use CGI::Carp qw(fatalsToBrowser); use IO::Handle; use String::Approx qw(amatch); use FS::UID qw(dbh cgisuidsetup); +use FS::Conf; use FS::Record qw(qsearch qsearchs dbdef jsearch); use FS::CGI qw(header menubar eidiot popurl table); use FS::cust_main; @@ -163,6 +165,7 @@ if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) { 'Main Menu', popurl(2) )), "$total matching customers found "; + #begin pager my $pager = ''; if ( $total != scalar(@cust_main) && $maxrecords ) { unless ( $offset == 0 ) { @@ -187,6 +190,7 @@ if ( scalar(@cust_main) == 1 && ! $cgi->param('referral_custnum') ) { '">Next '; } } + #end pager if ( $cgi->param('showcancelledcustomers') eq '0' #see if it was set by me || ( $conf->exists('hidecancelledcustomers') diff --git a/httemplate/search/cust_pkg.cgi b/httemplate/search/cust_pkg.cgi index 93ce7ae29..e862cefab 100755 --- a/httemplate/search/cust_pkg.cgi +++ b/httemplate/search/cust_pkg.cgi @@ -1,11 +1,13 @@ <% -# +# use strict; -use vars qw ( $cgi @cust_pkg $sortby $query %part_pkg ); +use vars qw ( $cgi @cust_pkg $sortby $query %part_pkg + $conf $maxrecords $limit $offset ); use CGI; use CGI::Carp qw(fatalsToBrowser); -use FS::UID qw(cgisuidsetup); +use FS::UID qw(dbh cgisuidsetup); +use FS::Conf; use FS::Record qw(qsearch qsearchs dbdef); use FS::CGI qw(header eidiot popurl table); use FS::cust_pkg; @@ -16,40 +18,79 @@ use FS::cust_main; $cgi = new CGI; &cgisuidsetup($cgi); +$conf = new FS::Conf; +$maxrecords = $conf->config('maxsearchrecordsperpage'); + my %part_pkg = map { $_->pkgpart => $_ } qsearch('part_pkg', {}); +$limit = ''; +$limit .= "LIMIT $maxrecords" if $maxrecords; + +$offset = $cgi->param('offset') || 0; +$limit .= " OFFSET $offset" if $offset; + +my $total; + ($query) = $cgi->keywords; +my $unconf = ''; #this tree is a little bit redundant if ( $query eq 'pkgnum' ) { $sortby=\*pkgnum_sort; - @cust_pkg=qsearch('cust_pkg',{}); + + } elsif ( $query eq 'APKG_pkgnum' ) { + $sortby=\*pkgnum_sort; - @cust_pkg=(); - #perhaps this should go in cust_pkg as a qsearch-like constructor? - my($cust_pkg); - foreach $cust_pkg (qsearch('cust_pkg',{})) { - my($flag)=0; - my($pkg_svc); - PKG_SVC: - foreach $pkg_svc (qsearch('pkg_svc',{ 'pkgpart' => $cust_pkg->pkgpart })) { - if ( $pkg_svc->quantity - > scalar(qsearch('cust_svc',{ - 'pkgnum' => $cust_pkg->pkgnum, - 'svcpart' => $pkg_svc->svcpart, - })) - ) - { - $flag=1; - last PKG_SVC; - } - } - push @cust_pkg, $cust_pkg if $flag; - } + + $unconf = " + WHERE 0 < + ( SELECT count(*) FROM pkg_svc + WHERE pkg_svc.pkgpart = cust_pkg.pkgpart + AND pkg_svc.quantity > ( SELECT count(*) FROM cust_svc + WHERE cust_svc.pkgnum = cust_pkg.pkgnum + AND cust_svc.svcpart = pkg_svc.svcpart + ) + ) + "; + + #@cust_pkg=(); + ##perhaps this should go in cust_pkg as a qsearch-like constructor? + #my($cust_pkg); + #foreach $cust_pkg ( + # qsearch('cust_pkg',{}, '', "ORDER BY pkgnum $limit" ) + #) { + # my($flag)=0; + # my($pkg_svc); + # PKG_SVC: + # foreach $pkg_svc (qsearch('pkg_svc',{ 'pkgpart' => $cust_pkg->pkgpart })) { + # if ( $pkg_svc->quantity + # > scalar(qsearch('cust_svc',{ + # 'pkgnum' => $cust_pkg->pkgnum, + # 'svcpart' => $pkg_svc->svcpart, + # })) + # ) + # { + # $flag=1; + # last PKG_SVC; + # } + # } + # push @cust_pkg, $cust_pkg if $flag; + #} + } else { die "Empty QUERY_STRING!"; } +my $statement = "SELECT COUNT(*) FROM cust_pkg $unconf"; +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_pkg = qsearch('cust_pkg',{}, '', "$unconf ORDER BY pkgnum $limit" ); + + if ( scalar(@cust_pkg) == 1 ) { my($pkgnum)=$cust_pkg[0]->pkgnum; print $cgi->redirect(popurl(2). "view/cust_pkg.cgi?$pkgnum"); @@ -57,9 +98,35 @@ if ( scalar(@cust_pkg) == 1 ) { } elsif ( scalar(@cust_pkg) == 0 ) { #error eidiot("No packages found"); } else { - my($total)=scalar(@cust_pkg); + $total ||= scalar(@cust_pkg); + + my $pager = ''; + if ( $total != scalar(@cust_pkg) && $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 '; + } + } + print header('Package Search Results',''), - "$total matching packages found
", &table(), <
$pager", &table(), < Package Cust# @@ -153,11 +220,7 @@ END } print ''; - print < - - -END + print "$pager"; } -- 2.11.0