+print "</TR></TABLE>$r required fields for each billing type";
+
+unless ( $custnum ) {
+ # pry the wrong place for this logic. also pretty expensive
+ #use FS::pkg_svc;
+ #use FS::part_svc;
+ #use FS::part_pkg;
+
+ #false laziness, copied from FS::cust_pkg::order
+ my %part_pkg;
+ if ( scalar(@agents) == 1 ) {
+ # generate %part_pkg
+ # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
+ #my($cust_main)=qsearchs('cust_main',{'custnum'=>$custnum});
+ #my($agent)=qsearchs('agent',{'agentnum'=> $cust_main->agentnum });
+ my($agent)=qsearchs('agent',{'agentnum'=> $agentnum });
+
+ my($type_pkgs);
+ foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
+ my($pkgpart)=$type_pkgs->pkgpart;
+ $part_pkg{$pkgpart}++;
+ }
+ } else {
+ #can't know (agent not chosen), so, allow all
+ my %typenum;
+ foreach my $agent ( @agents ) {
+ next if $typenum{$agent->typenum}++;
+ foreach my $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
+ my($pkgpart)=$type_pkgs->pkgpart;
+ $part_pkg{$pkgpart}++;
+ }
+ }
+
+ }
+ #eslaf
+
+ my %pkgpart;
+ #foreach ( @pkg_svc ) {
+ foreach ( qsearch( 'pkg_svc', {} ) ) {
+ my $part_svc = qsearchs ( 'part_svc', { 'svcpart' => $_->svcpart } );
+ $pkgpart{ $_->pkgpart } = 9999 # never will == 1 below
+ if ( $part_svc->svcdb ne 'svc_acct' );
+ $pkgpart{ $_->pkgpart }++;
+ }
+
+ my @part_pkg =
+ #grep { $pkgpart{ $_->pkgpart } == 1 } qsearch( 'part_pkg', {} );
+ grep {
+ ( $pkgpart{ $_->pkgpart } || 0 ) == 1
+ && $part_pkg{ $_->pkgpart }
+ } qsearch( 'part_pkg', {} );
+
+ if ( @part_pkg ) {
+
+ print "<BR><BR>First package", itable("#c0c0c0"),
+ qq!<TR><TD COLSPAN=2><SELECT NAME="pkgpart">!;
+
+ print qq!<OPTION VALUE="">(none)!;