%
my %search;
if ( $cgi->param('showdisabled') ) {
  %search = ();
} else {
  %search = ( 'disabled' => '' );
}
my @part_pkg = qsearch('part_pkg', \%search );
my $total = scalar(@part_pkg);
my $sortby;
my %num_active_cust_pkg = ();
my( $suspended_sth, $canceled_sth ) = ( '', '' );
if ( $cgi->param('active') ) {
  my $active_sth = dbh->prepare(
    'SELECT COUNT(*) FROM cust_pkg WHERE pkgpart = ?'.
    ' AND ( cancel IS NULL OR cancel = 0 )'.
    ' AND ( susp IS NULL OR susp = 0 )'
  ) or die dbh->errstr;
  foreach my $part_pkg ( @part_pkg ) {
    $active_sth->execute($part_pkg->pkgpart) or die $active_sth->errstr;
    $num_active_cust_pkg{$part_pkg->pkgpart} =
      $active_sth->fetchrow_arrayref->[0];
  }
  $sortby = sub {
    $num_active_cust_pkg{$b->pkgpart} <=> $num_active_cust_pkg{$a->pkgpart};
  };
  $suspended_sth = dbh->prepare(
    'SELECT COUNT(*) FROM cust_pkg WHERE pkgpart = ?'.
    ' AND ( cancel IS NULL OR cancel = 0 )'.
    ' AND susp IS NOT NULL AND susp != 0'
  ) or die dbh->errstr;
  $canceled_sth = dbh->prepare(
    'SELECT COUNT(*) FROM cust_pkg WHERE pkgpart = ?'.
    ' AND cancel IS NOT NULL AND cancel != 0'
  ) or die dbh->errstr;
} else {
  $sortby = sub { $a->pkgpart <=> $b->pkgpart; };
}
my $conf = new FS::Conf;
my $taxclasses = $conf->exists('enable_taxclasses');
%>
<%= include("/elements/header.html","Package Definition Listing",menubar( 'Main Menu' => $p )) %>
<% unless ( $cgi->param('active') ) { %>
  One or more service definitions are grouped together into a package 
  definition and given pricing information.  Customers purchase packages
  rather than purchase services directly.
  Add a new package definition
  
<% } %>
<%= $total %> package definitions
<% if ( $cgi->param('showdisabled') ) { $cgi->param('showdisabled', 0); %>
  ( hide disabled packages )
<% } else { $cgi->param('showdisabled', 1); %>
  ( show disabled packages )
<% } %>
<% my $colspan = $cgi->param('showdisabled') ? 2 : 3; %>
<%= &table() %>