X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FClientAPI%2FSignup.pm;h=00b4d445e2050a41622c16f32c49d31fc8ab4163;hb=9035034a53d60cb7a7687dfee899c1d0c775ea74;hp=dc627e9bd038c2d97f29da64810a21475cb32caa;hpb=a5a258c91c5dc78897d16627b8092385ceb4c4d2;p=freeside.git diff --git a/FS/FS/ClientAPI/Signup.pm b/FS/FS/ClientAPI/Signup.pm index dc627e9bd..00b4d445e 100644 --- a/FS/FS/ClientAPI/Signup.pm +++ b/FS/FS/ClientAPI/Signup.pm @@ -1,10 +1,14 @@ package FS::ClientAPI::Signup; use strict; +use vars qw($DEBUG $me); +use Data::Dumper; use Tie::RefHash; use FS::Conf; use FS::Record qw(qsearch qsearchs dbdef); use FS::Msgcat qw(gettext); +use FS::Misc qw(card_types); +use FS::ClientAPI_SessionCache; use FS::agent; use FS::cust_main_county; use FS::part_pkg; @@ -14,86 +18,154 @@ use FS::cust_pkg; use FS::svc_acct; use FS::acct_snarf; use FS::queue; +use FS::reg_code; -use FS::ClientAPI; #hmm -FS::ClientAPI->register_handlers( - 'Signup/signup_info' => \&signup_info, - 'Signup/new_customer' => \&new_customer, -); +$DEBUG = 0; +$me = '[FS::ClientAPI::Signup]'; sub signup_info { my $packet = shift; + warn "$me signup_info called on $packet\n" if $DEBUG; + my $conf = new FS::Conf; - use vars qw($signup_info); #cache for performance; - $signup_info ||= { + my $cache = new FS::ClientAPI_SessionCache( { + 'namespace' => 'FS::ClientAPI::Signup', + } ); + my $signup_info_cache = $cache->get('signup_info_cache'); - 'cust_main_county' => - [ map { $_->hashref } qsearch('cust_main_county', {}) ], + if ( $signup_info_cache ) { - 'agent' => - [ - map { $_->hashref } - qsearch('agent', dbdef->table('agent')->column('disabled') - ? { 'disabled' => '' } - : {} - ) - ], + warn "$me loading cached signup info\n" if $DEBUG > 1; - 'part_referral' => - [ - map { $_->hashref } - qsearch('part_referral', - dbdef->table('part_referral')->column('disabled') - ? { 'disabled' => '' } - : {} - ) - ], + } else { + + warn "$me populating signup info cache\n" if $DEBUG > 1; - 'agentnum2part_pkg' => + my $agentnum2part_pkg = { map { my $href = $_->pkgpart_hashref; $_->agentnum => [ - map { { 'payby' => [ $_->payby ], %{$_->hashref} } } + map { { 'payby' => [ $_->payby ], + 'freq_pretty' => $_->freq_pretty, + 'options' => { $_->options }, + %{$_->hashref} + } } grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } } qsearch( 'part_pkg', { 'disabled' => '' } ) ]; - } qsearch('agent', dbdef->table('agent')->column('disabled') - ? { 'disabled' => '' } - : {} - ) - }, + } qsearch('agent', { 'disabled' => '' }) + }; + + my $msgcat = { map { $_=>gettext($_) } + qw( passwords_dont_match invalid_card unknown_card_type + not_a empty_password illegal_or_empty_text ) + }; + warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2; + + my $label = { map { $_ => FS::Msgcat::_gettext($_) } + qw( stateid stateid_state ) + }; + warn "label: ". Dumper($label). "\n" if $DEBUG > 2; + + $signup_info_cache = { + 'cust_main_county' => [ map $_->hashref, + qsearch('cust_main_county', {} ) + ], + + 'agent' => [ map $_->hashref, + qsearch('agent', { 'disabled' => '' } ) + ], + + 'part_referral' => [ map $_->hashref, + qsearch('part_referral', { 'disabled' => '' } ) + ], + + 'agentnum2part_pkg' => $agentnum2part_pkg, + + 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ], + + 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'), + + 'security_phrase' => $conf->exists('security_phrase'), - 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ], + 'payby' => [ $conf->config('signup_server-payby') ], - 'security_phrase' => $conf->exists('security_phrase'), + 'card_types' => card_types(), - 'payby' => [ $conf->config('signup_server-payby') ], + 'paytypes' => [ @FS::cust_main::paytypes ], - 'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'), + 'cvv_enabled' => 1, - 'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'), + 'stateid_enabled' => $conf->exists('show_stateid'), - 'msgcat' => { map { $_=>gettext($_) } qw( - passwords_dont_match invalid_card unknown_card_type not_a empty_password - ) }, + 'paystate_enabled' => $conf->exists('show_bankstate'), - 'statedefault' => $conf->config('statedefault') || 'CA', + 'ship_enabled' => 1, - 'countrydefault' => $conf->config('countrydefault') || 'US', + 'msgcat' => $msgcat, - 'refnum' => $conf->config('signup_server-default_refnum'), + 'label' => $label, - }; + 'statedefault' => scalar($conf->config('statedefault')) || 'CA', - my $agentnum = $conf->config('signup_server-default_agentnum'); + 'countrydefault' => scalar($conf->config('countrydefault')) || 'US', + + 'refnum' => scalar($conf->config('signup_server-default_refnum')), + + 'default_pkgpart' => scalar($conf->config('signup_server-default_pkgpart')), + + }; + + $cache->set('signup_info_cache', $signup_info_cache); + + } + + 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'} ) { - my $cache = new Cache::SharedMemoryCache( { + + 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'}); @@ -102,12 +174,62 @@ sub signup_info { } 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->{'promo_code'} ) { + + 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_acct') } + 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 ], %{$_->hashref} } } + [ map { { 'payby' => [ $_->payby ], + 'freq_pretty' => $_->freq_pretty, + 'options' => { $_->options }, + %{$_->hashref} + } } grep { $_->svcpart('svc_acct') } qsearch( 'part_pkg', { 'promo_code' => { op=>'ILIKE', @@ -118,16 +240,43 @@ sub signup_info { $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 && ! @{ $signup_info->{'part_pkg'} } ) { - $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}; + if ( $agentnum ) { + + 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; + } # else { # delete $signup_info->{'part_pkg'}; #} - if ( $session ) { + 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'}; @@ -154,7 +303,7 @@ sub new_customer { my $agentnum; if ( exists $packet->{'session_id'} ) { - my $cache = new Cache::SharedMemoryCache( { + my $cache = new FS::ClientAPI_SessionCache( { 'namespace' => 'FS::ClientAPI::Agent', } ); my $session = $cache->get($packet->{'session_id'}); @@ -180,13 +329,18 @@ sub new_customer { last first ss company address1 address2 city county state zip country - daytime night fax + daytime night fax stateid stateid_state ship_last ship_first ship_ss ship_company ship_address1 ship_address2 ship_city ship_county ship_state ship_zip ship_country ship_daytime ship_night ship_fax - payby payinfo paycvv paydate payname referral_custnum comments + payby + payinfo paycvv paydate payname paystate paytype + paystart_month paystart_year payissue + payip + + referral_custnum comments ) } ); @@ -198,7 +352,9 @@ sub new_customer { $cust_main->payinfo($cust_main->daytime) if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo; - my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} ); + my @invoicing_list = $packet->{'invoicing_list'} + ? split( /\s*\,\s*/, $packet->{'invoicing_list'} ) + : (); $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/; my $pkgpart = $1; @@ -209,10 +365,18 @@ sub new_customer { or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" }; my $svcpart = $part_pkg->svcpart('svc_acct'); + my $reg_code = ''; + if ( $packet->{'reg_code'} ) { + $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'}, + 'agentnum' => $agentnum, } ) + or return { 'error' => 'Unknown registration code' }; + } + my $cust_pkg = new FS::cust_pkg ( { #later#'custnum' => $custnum, 'pkgpart' => $packet->{'pkgpart'}, 'promo_code' => $packet->{'promo_code'}, + 'reg_code' => $packet->{'reg_code'}, } ); #my $error = $cust_pkg->check; #return { 'error' => $error } if $error; @@ -275,10 +439,12 @@ sub new_customer { #warn "[fs_signup_server] error billing new customer: $bill_error" # if $bill_error; - $cust_main->apply_payments; - $cust_main->apply_credits; + $bill_error = $cust_main->apply_payments_and_credits; + #warn "[fs_signup_server] error applying payments and credits for". + # " new customer: $bill_error" + # if $bill_error; - $bill_error = $cust_main->collect; + $bill_error = $cust_main->collect('realtime' => 1); #warn "[fs_signup_server] error collecting from new customer: $bill_error" # if $bill_error; @@ -305,6 +471,11 @@ sub new_customer { } + if ( $reg_code ) { + $error = $reg_code->delete; + return { 'error' => $error } if $error; + } + $error = $placeholder->delete; return { 'error' => $error } if $error;