% include( 'elements/browse.html',
                 'title'                 => 'Package Definitions',
                 'html_init'             => $html_init,
                 'name'                  => 'package definitions',
                 'disableable'           => 1,
                 'disabled_statuspos'    => 3,
                 'agent_virt'            => 1,
                 'agent_null_right'      => [ $edit, $edit_global ],
                 'agent_null_right_link' => $edit_global,
                 'agent_pos'             => 5,
                 'query'                 => { 'select'    => $select,
                                              'table'     => 'part_pkg',
                                              'hashref'   => \%hash,
                                              'extra_sql' => $extra_sql,
                                              'order_by'  => "ORDER BY $orderby"
                                            },
                 'count_query'           => $count_query,
                 'header'                => \@header,
                 'fields'                => \@fields,
                 'links'                 => \@links,
                 'align'                 => $align,
             )
%>
<%init>
my $curuser = $FS::CurrentUser::CurrentUser;
my $edit        = 'Edit package definitions';
my $edit_global = 'Edit global package definitions';
my $acl_edit        = $curuser->access_right($edit);
my $acl_edit_global = $curuser->access_right($edit_global);
my $acl_config      = $curuser->access_right('Configuration'); #to edit services
                                                               #and agent types
die "access denied"
  unless $acl_edit || $acl_edit_global;
my $conf = new FS::Conf;
my $taxclasses = $conf->exists('enable_taxclasses');
my $money_char = $conf->config('money_char') || '$';
my $select = '*';
my $orderby = 'pkgpart';
my %hash = ();
my $extra_count = '';
if ( $cgi->param('active') ) {
  $orderby = 'num_active DESC';
}
my $extra_sql = '';
#if ( $cgi->param('activeONLY') ) {
#  $extra_sql = ' WHERE num_active > 0 '; #XXX doesn't affect count...
#}
if ( $cgi->param('recurring') ) {
  $hash{'freq'} = { op=>'!=', value=>'0' };
  $extra_count = ' freq != 0 ';
}
if ( $cgi->param('missing_recur_fee') ) {
  my $missing = "0 = ( SELECT COUNT(*) FROM part_pkg_option
                         WHERE optionname = 'recur_fee'
                           AND part_pkg_option.pkgpart = part_pkg.pkgpart
                           AND CAST ( optionvalue AS NUMERIC ) > 0
                     )";
  $extra_sql .= ( ( scalar(keys %hash) || $extra_sql ) ? ' AND ' : ' WHERE ' ).
                $missing;
}
unless ( $acl_edit_global ) {
  $extra_sql .= ( ( scalar(keys %hash) || $extra_sql ) ? ' AND ' : ' WHERE ' ).
                 FS::part_pkg->curuser_pkgs_sql;
}
my $agentnums = join(',', $curuser->agentnums);
my $count_cust_pkg = "
  SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )
    WHERE cust_pkg.pkgpart = part_pkg.pkgpart
      AND cust_main.agentnum IN ($agentnums)
";
$select = "
  *,
  ( $count_cust_pkg
      AND ( cancel IS NULL OR cancel = 0 )
      AND ( susp IS NULL OR susp = 0 )
  ) AS num_active,
  ( $count_cust_pkg
      AND ( cancel IS NULL OR cancel = 0 )
      AND susp IS NOT NULL AND susp != 0
  ) AS num_suspended,
  ( $count_cust_pkg
      AND cancel IS NOT NULL AND cancel != 0
  ) AS num_cancelled
";
my $html_init;
#unless ( $cgi->param('active') ) {
  $html_init = qq!
    One or more service definitions are grouped together into a package 
    definition and given pricing information.  Customers purchase packages
    rather than purchase services directly.