1 package FS::ClientAPI::Signup;
6 use FS::Record qw(qsearch qsearchs dbdef);
8 use FS::cust_main_county;
13 use FS::Msgcat qw(gettext);
15 use FS::ClientAPI; #hmm
16 FS::ClientAPI->register_handlers(
17 'Signup/signup_info' => \&signup_info,
18 'Signup/new_customer' => \&new_customer,
24 my $conf = new FS::Conf;
26 use vars qw($signup_info); #cache for performance;
30 [ map { $_->hashref } qsearch('cust_main_county', {}) ],
35 qsearch('agent', dbdef->table('agent')->column('disabled')
36 ? { 'disabled' => '' }
41 'agentnum2part_pkg' =>
44 my $href = $_->pkgpart_hashref;
47 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
48 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
49 qsearch( 'part_pkg', { 'disabled' => '' } )
51 } qsearch('agent', dbdef->table('agent')->column('disabled')
52 ? { 'disabled' => '' }
57 'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
59 'security_phrase' => $conf->exists('security_phrase'),
61 'payby' => [ $conf->config('signup_server-payby') ],
63 'msgcat' => { map { $_=>gettext($_) } qw(
64 passwords_dont_match invalid_card unknown_card_type not_a
67 'statedefault' => $conf->config('statedefault') || 'CA',
69 'countrydefault' => $conf->config('countrydefault') || 'US',
74 $conf->config('signup_server-default_agentnum')
75 && !exists $signup_info->{'part_pkg'} #cache for performance
77 my $agentnum = $conf->config('signup_server-default_agentnum');
78 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
79 or die "fatal: signup_server-default_agentnum $agentnum not found\n";
80 my $pkgpart_href = $agent->pkgpart_hashref;
82 $signup_info->{'part_pkg'} = [
84 map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
85 grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
86 qsearch( 'part_pkg', { 'disabled' => '' } )
97 my $conf = new FS::Conf;
99 #things that aren't necessary in base class, but are for signup server
100 #return "Passwords don't match"
101 # if $hashref->{'_password'} ne $hashref->{'_password2'}
102 return { 'error' => gettext('empty_password') }
103 unless $packet->{'_password'};
104 # a bit inefficient for large numbers of pops
105 return { 'error' => gettext('no_access_number_selected') }
106 unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
108 #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
109 # common that are still here and library them.
110 my $cust_main = new FS::cust_main ( {
112 'agentnum' => $packet->{agentnum}
113 || $conf->config('signup_server-default_agentnum'),
114 'refnum' => $packet->{refnum}
115 || $conf->config('signup_server-default_refnum'),
117 map { $_ => $packet->{$_} } qw(
118 last first ss company address1 address2 city county state zip country
119 daytime night fax payby payinfo paydate payname referral_custnum comments
124 return { 'error' => "Illegal payment type" }
125 unless grep { $_ eq $packet->{'payby'} }
126 $conf->config('signup_server-payby');
128 $cust_main->payinfo($cust_main->daytime)
129 if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
131 my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
133 $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
135 return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
138 qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
139 or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
140 my $svcpart = $part_pkg->svcpart('svc_acct');
142 my $cust_pkg = new FS::cust_pkg ( {
143 #later#'custnum' => $custnum,
144 'pkgpart' => $packet->{'pkgpart'},
146 my $error = $cust_pkg->check;
147 return { 'error' => $error } if $error;
149 my $svc_acct = new FS::svc_acct ( {
150 'svcpart' => $svcpart,
151 map { $_ => $packet->{$_} }
152 qw( username _password sec_phrase popnum ),
155 my $y = $svc_acct->setdefault; # arguably should be in new method
156 return { 'error' => $y } if $y && !ref($y);
158 $error = $svc_acct->check;
159 return { 'error' => $error } if $error;
162 tie my %hash, 'Tie::RefHash';
163 %hash = ( $cust_pkg => [ $svc_acct ] );
165 $error = $cust_main->insert( \%hash, \@invoicing_list, 'noexport' => 1 );
166 return { 'error' => $error } if $error;
168 if ( $conf->exists('signup_server-realtime') ) {
170 #warn "[fs_signup_server] Billing customer...\n" if $Debug;
172 my $bill_error = $cust_main->bill;
173 #warn "[fs_signup_server] error billing new customer: $bill_error"
176 $cust_main->apply_payments;
177 $cust_main->apply_credits;
179 $bill_error = $cust_main->collect;
180 #warn "[fs_signup_server] error collecting from new customer: $bill_error"
183 if ( $cust_main->balance > 0 ) {
185 #this makes sense. credit is "un-doing" the invoice
186 $cust_main->credit( $cust_main->balance, 'signup server decline' );
187 $cust_main->apply_credits;
189 #should check list for errors...
190 #$cust_main->suspend;
191 local $FS::svc_Common::noexport_hack = 1;
192 $cust_main->cancel('quiet'=>1);
194 return { 'error' => '_decline' };
198 $cust_main->reexport;
200 return { error => '' };