+ $opt->{'title'} = delete $opt->{'name'}
+ if !exists($opt->{'title'}) && exists($opt->{'name'});
+
+ my $custnum = $opt->{'custnum'};
+
+ my $curuser = $FS::CurrentUser::CurrentUser;
+
+ my $cust_main = qsearchs({
+ 'table' => 'cust_main',
+ 'hashref' => { 'custnum' => $custnum },
+ 'extra_sql' => ' AND '. $curuser->agentnums_sql,
+ })
+ or return { 'error' => "custnum $custnum not found" };
+
+ my $status = $cust_main->status;
+ #false laziness w/ClientAPI/Signup.pm
+
+ my $cust_pkg = new FS::cust_pkg ( {
+ 'custnum' => $custnum,
+ 'pkgpart' => $opt->{'pkgpart'},
+ } );
+ my $error = $cust_pkg->check;
+ return { 'error' => $error } if $error;
+
+ my @svc = ();
+ unless ( $opt->{'svcpart'} eq 'none' ) {
+
+ my $svcpart = '';
+ if ( $opt->{'svcpart'} =~ /^(\d+)$/ ) {
+ $svcpart = $1;
+ } else {
+ $svcpart = $cust_pkg->part_pkg->svcpart; #($svcdb);
+ }
+
+ my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
+ return { 'error' => "Unknown svcpart $svcpart" } unless $part_svc;
+
+ my $svcdb = $part_svc->svcdb;
+
+ my %fields = (
+ 'svc_acct' => [ qw( username domsvc _password sec_phrase popnum ) ],
+ 'svc_domain' => [ qw( domain ) ],
+ 'svc_phone' => [ qw( phonenum pin sip_password phone_name ) ],
+ 'svc_external' => [ qw( id title ) ],
+ 'svc_pbx' => [ qw( id title ) ],
+ );
+
+ my $svc_x = "FS::$svcdb"->new( {
+ 'svcpart' => $svcpart,
+ map { $_ => $opt->{$_} } @{$fields{$svcdb}}
+ } );
+
+ #snarf processing not necessary here (or probably at all, anymore)
+
+ my $y = $svc_x->setdefault; # arguably should be in new method
+ return { 'error' => $y } if $y && !ref($y);
+
+ $error = $svc_x->check;
+ return { 'error' => $error } if $error;
+
+ push @svc, $svc_x;