+ my $signup_info = { %$signup_info_cache };
+ warn "$me signup info loaded\n" if $DEBUG > 1;
+ warn Dumper($signup_info). "\n" if $DEBUG > 2;
+
+ my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
+
+ if ( grep { $conf->exists($_) } @addl ) {
+
+ $signup_info->{optional_packages} = [];
+
+ foreach my $addl ( @addl ) {
+
+ warn "$me adding optional package info\n" if $DEBUG > 1;
+
+ my $classnum = $conf->config($addl) or next;
+
+ my @pkgs = map { {
+ 'freq_pretty' => $_->freq_pretty,
+ 'options' => { $_->options },
+ %{ $_->hashref }
+ };
+ }
+ qsearch( 'part_pkg', { classnum => $classnum } );
+
+ push @{$signup_info->{optional_packages}}, \@pkgs;
+
+ warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
+
+ }
+
+ }
+
+ my $agentnum = $packet->{'agentnum'}
+ || $conf->config('signup_server-default_agentnum');
+ $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
+ $agentnum = $1;
+
+ my $session = '';
+ if ( exists $packet->{'session_id'} ) {
+
+ warn "$me loading agent session\n" if $DEBUG > 1;
+ my $cache = new FS::ClientAPI_SessionCache( {
+ 'namespace' => 'FS::ClientAPI::Agent',
+ } );
+ $session = $cache->get($packet->{'session_id'});
+ if ( $session ) {
+ $agentnum = $session->{'agentnum'};
+ } else {
+ return { 'error' => "Can't resume session" }; #better error message
+ }
+ warn "$me done loading agent session\n" if $DEBUG > 1;
+
+ } elsif ( exists $packet->{'customer_session_id'} ) {
+
+ warn "$me loading customer session\n" if $DEBUG > 1;
+ my $cache = new FS::ClientAPI_SessionCache( {
+ 'namespace' => 'FS::ClientAPI::MyAccount',
+ } );
+ $session = $cache->get($packet->{'customer_session_id'});
+ if ( $session ) {
+ my $custnum = $session->{'custnum'};
+ my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
+ return { 'error' => "Can't find your customer record" } unless $cust_main;
+ $agentnum = $cust_main->agentnum;
+ } else {
+ return { 'error' => "Can't resume session" }; #better error message
+ }
+ warn "$me done loading customer session\n" if $DEBUG > 1;
+
+ }
+
+ $signup_info->{'part_pkg'} = [];
+
+ if ( $packet->{'reg_code'} ) {
+
+ warn "$me setting package list via reg_code\n" if $DEBUG > 1;
+
+ $signup_info->{'part_pkg'} =
+ [ map { { 'payby' => [ $_->payby ],
+ 'freq_pretty' => $_->freq_pretty,
+ 'options' => { $_->options },
+ %{$_->hashref}
+ };
+ }
+ grep { $_->svcpart($svc_x) }
+ map { $_->part_pkg }
+ qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
+ 'agentnum' => $agentnum, } )
+
+ ];
+
+ $signup_info->{'error'} = 'Unknown registration code'
+ unless @{ $signup_info->{'part_pkg'} };
+
+ warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
+
+ } elsif ( $packet->{'promo_code'} ) {
+
+ warn "$me setting package list via promo_code\n" if $DEBUG > 1;
+
+ $signup_info->{'part_pkg'} =
+ [ map { { 'payby' => [ $_->payby ],
+ 'freq_pretty' => $_->freq_pretty,
+ 'options' => { $_->options },
+ %{$_->hashref}
+ } }
+ grep { $_->svcpart($svc_x) }
+ qsearch( 'part_pkg', { 'promo_code' => {
+ op=>'ILIKE',
+ value=>$packet->{'promo_code'}
+ },
+ 'disabled' => '', } )
+ ];
+
+ $signup_info->{'error'} = 'Unknown promotional code'
+ unless @{ $signup_info->{'part_pkg'} };
+
+ warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
+ }
+
+ if ( $agentnum ) {
+
+ warn "$me setting agent-specific payment flag\n" if $DEBUG > 1;
+ my $agent = qsearchs('agent', { 'agentnum' => $agentnum } )
+ or return { 'error' => "Self-service agent #$agentnum does not exist" };
+ warn "$me has agent $agent\n" if $DEBUG > 1;
+ my @paybys = @{ $signup_info->{'payby'} };
+ $signup_info->{'hide_payment_fields'} = [];
+
+ my $gatewaynum = $conf->config('selfservice-payment_gateway');
+ my $force_gateway;
+ if ( $gatewaynum ) {
+ $force_gateway = qsearchs('payment_gateway', { gatewaynum => $gatewaynum });
+ warn "using forced gateway #$gatewaynum - " .
+ $force_gateway->gateway_username . '@' . $force_gateway->gateway_module
+ if $DEBUG > 1;
+ die "configured gatewaynum $gatewaynum not found!" if !$force_gateway;
+ }
+ foreach my $payby (@paybys) {
+ warn "$me checking $payby payment fields\n" if $DEBUG > 1;
+ my $hide = 0;
+ if ( FS::payby->realtime($payby) ) {
+ my $gateway = $force_gateway ||
+ $agent->payment_gateway( 'method' => FS::payby->payby2bop($payby),
+ 'nofatal' => 1,
+ );
+ if ( $gateway && $gateway->gateway_namespace
+ eq 'Business::OnlineThirdPartyPayment'
+ ) {
+ warn "$me hiding $payby payment fields\n" if $DEBUG > 1;
+ $hide = 1;
+ }
+ }
+ push @{$signup_info->{'hide_payment_fields'}}, $hide;
+ } # foreach $payby
+ warn "$me done setting agent-specific payment flag\n" if $DEBUG > 1;
+
+ warn "$me setting agent-specific package list\n" if $DEBUG > 1;
+ $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
+ unless @{ $signup_info->{'part_pkg'} };
+ warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
+
+ warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
+ $signup_info->{'part_referral'} =
+ [
+ map { $_->hashref }
+ qsearch( {
+ 'table' => 'part_referral',
+ 'hashref' => { 'disabled' => '' },
+ 'extra_sql' => "AND ( agentnum = $agentnum ".
+ " OR agentnum IS NULL ) ",
+ },
+ )
+ ];
+ warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
+
+ $signup_info->{'agent_name'} = $agent->agent;
+
+ $signup_info->{'company_name'} = $conf->config('company_name', $agentnum);
+
+ if ( $signup_info->{'agent_ship_address'} && $agent->agent_custnum ) {
+ my $cust_main = $agent->agent_cust_main;
+ my $prefix = length($cust_main->ship_last) ? 'ship_' : '';
+ $signup_info->{"ship_$_"} = $cust_main->get("$prefix$_")
+ foreach qw( address1 city county state zip country );
+ }
+
+ #some of the above could probably be cached, too
+
+ my $signup_info_cache_agent = $cache->get("signup_info_cache_agent$agentnum");
+
+ if ( $signup_info_cache_agent ) {
+
+ warn "$me loading cached signup info for agentnum $agentnum\n"
+ if $DEBUG > 1;
+
+ } else {
+
+ warn "$me populating signup info cache for agentnum $agentnum\n"
+ if $DEBUG > 1;
+
+ $signup_info_cache_agent = {
+ #( map { $_ => scalar( $conf->config($_, $agentnum) ) }
+ # qw( company_name ) ),
+ ( map { $_ => scalar( $conf->config("selfservice-$_", $agentnum ) ) }
+ qw( body_bgcolor box_bgcolor menu_bgcolor ) ),
+ ( map { $_ => join("\n", $conf->config("selfservice-$_", $agentnum ) ) }
+ qw( head body_header body_footer ) ),
+ };
+
+ $cache->set("signup_info_cache_agent$agentnum", $signup_info_cache_agent);
+
+ }
+
+ $signup_info->{$_} = $signup_info_cache_agent->{$_}
+ foreach keys %$signup_info_cache_agent;
+
+ }
+ # else {
+ # delete $signup_info->{'part_pkg'};
+ #}
+
+ warn "$me sorting package list\n" if $DEBUG > 1;
+ $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
+ @{ $signup_info->{'part_pkg'} }
+ ];
+ warn "$me done sorting package list\n" if $DEBUG > 1;
+
+ if ( exists $packet->{'session_id'} ) {
+ my $agent_signup_info = { %$signup_info };
+ delete $agent_signup_info->{agentnum2part_pkg};
+ $agent_signup_info->{'agent'} = $session->{'agent'};
+ return $agent_signup_info;
+ }
+ elsif ( exists $packet->{'keys'} ) {
+ my @keys = @{ $packet->{'keys'} };
+ return { map { $_ => $signup_info->{$_} } @keys };
+ }
+ else {
+ return $signup_info;
+ }