1 package FS::ClientAPI::Signup;
4 use vars qw($DEBUG $me);
8 use FS::Record qw(qsearch qsearchs dbdef);
9 use FS::Msgcat qw(gettext);
10 use FS::Misc qw(card_types);
11 use FS::ClientAPI_SessionCache;
13 use FS::cust_main_county;
24 $me = '[FS::ClientAPI::Signup]';
29 warn "$me signup_info called on $packet\n" if $DEBUG;
31 my $conf = new FS::Conf;
33 my $cache = new FS::ClientAPI_SessionCache( {
34 'namespace' => 'FS::ClientAPI::Signup',
36 my $signup_info_cache = $cache->get('signup_info_cache');
38 if ( $signup_info_cache ) {
40 warn "$me loading cached signup info\n" if $DEBUG > 1;
44 warn "$me populating signup info cache\n" if $DEBUG > 1;
46 my $agentnum2part_pkg =
49 my $href = $_->pkgpart_hashref;
52 map { { 'payby' => [ $_->payby ],
53 'freq_pretty' => $_->freq_pretty,
54 'options' => { $_->options },
57 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
58 qsearch( 'part_pkg', { 'disabled' => '' } )
60 } qsearch('agent', { 'disabled' => '' })
63 my $msgcat = { map { $_=>gettext($_) }
64 qw( passwords_dont_match invalid_card unknown_card_type
65 not_a empty_password illegal_or_empty_text )
67 warn "msgcat: ". Dumper($msgcat). "\n" if $DEBUG > 2;
69 my $label = { map { $_ => FS::Msgcat::_gettext($_) }
70 qw( stateid stateid_state )
72 warn "label: ". Dumper($label). "\n" if $DEBUG > 2;
74 $signup_info_cache = {
75 'cust_main_county' => [ map $_->hashref,
76 qsearch('cust_main_county', {} )
79 'agent' => [ map $_->hashref,
80 qsearch('agent', { 'disabled' => '' } )
83 'part_referral' => [ map $_->hashref,
84 qsearch('part_referral', { 'disabled' => '' } )
87 'agentnum2part_pkg' => $agentnum2part_pkg,
89 'svc_acct_pop' => [ map $_->hashref, qsearch('svc_acct_pop',{} ) ],
91 'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
93 'security_phrase' => $conf->exists('security_phrase'),
95 'payby' => [ $conf->config('signup_server-payby') ],
97 'card_types' => card_types(),
99 'paytypes' => [ @FS::cust_main::paytypes ],
103 'stateid_enabled' => $conf->exists('show_stateid'),
105 'paystate_enabled' => $conf->exists('show_bankstate'),
113 'statedefault' => scalar($conf->config('statedefault')) || 'CA',
115 'countrydefault' => scalar($conf->config('countrydefault')) || 'US',
117 'refnum' => scalar($conf->config('signup_server-default_refnum')),
119 'default_pkgpart' => scalar($conf->config('signup_server-default_pkgpart')),
123 $cache->set('signup_info_cache', $signup_info_cache);
127 my $signup_info = { %$signup_info_cache };
128 warn "$me signup info loaded\n" if $DEBUG > 1;
129 warn Dumper($signup_info). "\n" if $DEBUG > 2;
131 my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
133 if ( grep { $conf->exists($_) } @addl ) {
135 $signup_info->{optional_packages} = [];
137 foreach my $addl ( @addl ) {
139 warn "$me adding optional package info\n" if $DEBUG > 1;
141 my $classnum = $conf->config($addl) or next;
144 'freq_pretty' => $_->freq_pretty,
145 'options' => { $_->options },
149 qsearch( 'part_pkg', { classnum => $classnum } );
151 push @{$signup_info->{optional_packages}}, \@pkgs;
153 warn "$me done adding opt. package info for $classnum\n" if $DEBUG > 1;
159 my $agentnum = $packet->{'agentnum'}
160 || $conf->config('signup_server-default_agentnum');
161 $agentnum =~ /^(\d*)$/ or die "illegal agentnum";
165 if ( exists $packet->{'session_id'} ) {
167 warn "$me loading agent session\n" if $DEBUG > 1;
168 my $cache = new FS::ClientAPI_SessionCache( {
169 'namespace' => 'FS::ClientAPI::Agent',
171 $session = $cache->get($packet->{'session_id'});
173 $agentnum = $session->{'agentnum'};
175 return { 'error' => "Can't resume session" }; #better error message
177 warn "$me done loading agent session\n" if $DEBUG > 1;
179 } elsif ( exists $packet->{'customer_session_id'} ) {
181 warn "$me loading customer session\n" if $DEBUG > 1;
182 my $cache = new FS::ClientAPI_SessionCache( {
183 'namespace' => 'FS::ClientAPI::MyAccount',
185 $session = $cache->get($packet->{'customer_session_id'});
187 my $custnum = $session->{'custnum'};
188 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
189 return { 'error' => "Can't find your customer record" } unless $cust_main;
190 $agentnum = $cust_main->agentnum;
192 return { 'error' => "Can't resume session" }; #better error message
194 warn "$me done loading customer session\n" if $DEBUG > 1;
198 $signup_info->{'part_pkg'} = [];
200 if ( $packet->{'reg_code'} ) {
202 warn "$me setting package list via reg_code\n" if $DEBUG > 1;
204 $signup_info->{'part_pkg'} =
205 [ map { { 'payby' => [ $_->payby ],
206 'freq_pretty' => $_->freq_pretty,
207 'options' => { $_->options },
211 grep { $_->svcpart('svc_acct') }
213 qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
214 'agentnum' => $agentnum, } )
218 $signup_info->{'error'} = 'Unknown registration code'
219 unless @{ $signup_info->{'part_pkg'} };
221 warn "$me done setting package list via reg_code\n" if $DEBUG > 1;
223 } elsif ( $packet->{'promo_code'} ) {
225 warn "$me setting package list via promo_code\n" if $DEBUG > 1;
227 $signup_info->{'part_pkg'} =
228 [ map { { 'payby' => [ $_->payby ],
229 'freq_pretty' => $_->freq_pretty,
230 'options' => { $_->options },
233 grep { $_->svcpart('svc_acct') }
234 qsearch( 'part_pkg', { 'promo_code' => {
236 value=>$packet->{'promo_code'}
238 'disabled' => '', } )
241 $signup_info->{'error'} = 'Unknown promotional code'
242 unless @{ $signup_info->{'part_pkg'} };
244 warn "$me done setting package list via promo_code\n" if $DEBUG > 1;
249 warn "$me setting agent-specific package list\n" if $DEBUG > 1;
250 $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum}
251 unless @{ $signup_info->{'part_pkg'} };
252 warn "$me done setting agent-specific package list\n" if $DEBUG > 1;
254 warn "$me setting agent-specific adv. source list\n" if $DEBUG > 1;
255 $signup_info->{'part_referral'} =
259 'table' => 'part_referral',
260 'hashref' => { 'disabled' => '' },
261 'extra_sql' => "AND ( agentnum = $agentnum ".
262 " OR agentnum IS NULL ) ",
266 warn "$me done setting agent-specific adv. source list\n" if $DEBUG > 1;
270 # delete $signup_info->{'part_pkg'};
273 warn "$me sorting package list\n" if $DEBUG > 1;
274 $signup_info->{'part_pkg'} = [ sort { $a->{pkg} cmp $b->{pkg} } # case?
275 @{ $signup_info->{'part_pkg'} }
277 warn "$me done sorting package list\n" if $DEBUG > 1;
279 if ( exists $packet->{'session_id'} ) {
280 my $agent_signup_info = { %$signup_info };
281 delete $agent_signup_info->{agentnum2part_pkg};
282 $agent_signup_info->{'agent'} = $session->{'agent'};
293 my $conf = new FS::Conf;
295 #things that aren't necessary in base class, but are for signup server
296 #return "Passwords don't match"
297 # if $hashref->{'_password'} ne $hashref->{'_password2'}
298 return { 'error' => gettext('empty_password') }
299 unless length($packet->{'_password'});
300 # a bit inefficient for large numbers of pops
301 return { 'error' => gettext('no_access_number_selected') }
302 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
305 if ( exists $packet->{'session_id'} ) {
306 my $cache = new FS::ClientAPI_SessionCache( {
307 'namespace' => 'FS::ClientAPI::Agent',
309 my $session = $cache->get($packet->{'session_id'});
311 $agentnum = $session->{'agentnum'};
313 return { 'error' => "Can't resume session" }; #better error message
316 $agentnum = $packet->{agentnum}
317 || $conf->config('signup_server-default_agentnum');
320 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
321 # common that are still here and library them.
322 my $cust_main = new FS::cust_main ( {
324 'agentnum' => $agentnum,
325 'refnum' => $packet->{refnum}
326 || $conf->config('signup_server-default_refnum'),
328 map { $_ => $packet->{$_} } qw(
330 last first ss company address1 address2
331 city county state zip country
332 daytime night fax stateid stateid_state
334 ship_last ship_first ship_ss ship_company ship_address1 ship_address2
335 ship_city ship_county ship_state ship_zip ship_country
336 ship_daytime ship_night ship_fax
339 payinfo paycvv paydate payname paystate paytype
340 paystart_month paystart_year payissue
343 referral_custnum comments
348 return { 'error' => "Illegal payment type" }
349 unless grep { $_ eq $packet->{'payby'} }
350 $conf->config('signup_server-payby');
352 $cust_main->payinfo($cust_main->daytime)
353 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
355 my @invoicing_list = $packet->{'invoicing_list'}
356 ? split( /\s*\,\s*/, $packet->{'invoicing_list'} )
359 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
361 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
364 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
365 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
366 my $svcpart = $part_pkg->svcpart('svc_acct');
369 if ( $packet->{'reg_code'} ) {
370 $reg_code = qsearchs( 'reg_code', { 'code' => $packet->{'reg_code'},
371 'agentnum' => $agentnum, } )
372 or return { 'error' => 'Unknown registration code' };
375 my $cust_pkg = new FS::cust_pkg ( {
376 #later#'custnum' => $custnum,
377 'pkgpart' => $packet->{'pkgpart'},
378 'promo_code' => $packet->{'promo_code'},
379 'reg_code' => $packet->{'reg_code'},
381 #my $error = $cust_pkg->check;
382 #return { 'error' => $error } if $error;
384 my $svc_acct = new FS::svc_acct ( {
385 'svcpart' => $svcpart,
386 map { $_ => $packet->{$_} }
387 qw( username _password sec_phrase popnum ),
392 while ( exists($packet->{"snarf_machine$snarfnum"})
393 && length($packet->{"snarf_machine$snarfnum"}) ) {
394 my $acct_snarf = new FS::acct_snarf ( {
395 'machine' => $packet->{"snarf_machine$snarfnum"},
396 'protocol' => $packet->{"snarf_protocol$snarfnum"},
397 'username' => $packet->{"snarf_username$snarfnum"},
398 '_password' => $packet->{"snarf_password$snarfnum"},
401 push @acct_snarf, $acct_snarf;
403 $svc_acct->child_objects( \@acct_snarf );
405 my $y = $svc_acct->setdefault; # arguably should be in new method
406 return { 'error' => $y } if $y && !ref($y);
408 #$error = $svc_acct->check;
409 #return { 'error' => $error } if $error;
411 #setup a job dependancy to delay provisioning
412 my $placeholder = new FS::queue ( {
413 'job' => 'FS::ClientAPI::Signup::__placeholder',
414 'status' => 'locked',
416 my $error = $placeholder->insert;
417 return { 'error' => $error } if $error;
420 tie my %hash, 'Tie::RefHash';
421 %hash = ( $cust_pkg => [ $svc_acct ] );
423 $error = $cust_main->insert(
426 'depend_jobnum' => $placeholder->jobnum,
429 my $perror = $placeholder->delete;
430 $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
431 return { 'error' => $error };
434 if ( $conf->exists('signup_server-realtime') ) {
436 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
438 my $bill_error = $cust_main->bill;
439 #warn "[fs_signup_server] error billing new customer: $bill_error"
442 $cust_main->apply_payments_and_credits;
444 $bill_error = $cust_main->collect('realtime' => 1);
445 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
448 if ( $cust_main->balance > 0 ) {
450 #this makes sense. credit is "un-doing" the invoice
451 $cust_main->credit( $cust_main->balance, 'signup server decline' );
452 $cust_main->apply_credits;
454 #should check list for errors...
455 #$cust_main->suspend;
456 local $FS::svc_Common::noexport_hack = 1;
457 $cust_main->cancel('quiet'=>1);
459 my $perror = $placeholder->depended_delete;
460 warn "error removing provisioning jobs after decline: $perror" if $perror;
462 $perror = $placeholder->delete;
463 warn "error removing placeholder after decline: $perror" if $perror;
466 return { 'error' => '_decline' };
472 $error = $reg_code->delete;
473 return { 'error' => $error } if $error;
476 $error = $placeholder->delete;
477 return { 'error' => $error } if $error;
479 return { error => '' };