60639b132fb5186753dd266a7dd9a4f61af33094
[freeside.git] / FS / FS / ClientAPI / Signup.pm
1 package FS::ClientAPI::Signup;
2
3 use strict;
4 use Tie::RefHash;
5 use FS::Conf;
6 use FS::Record qw(qsearch qsearchs dbdef);
7 use FS::agent;
8 use FS::cust_main_county;
9 use FS::part_pkg;
10 use FS::svc_acct_pop;
11 use FS::cust_main;
12 use FS::cust_pkg;
13 use FS::svc_acct;
14 use FS::acct_snarf;
15 use FS::Msgcat qw(gettext);
16
17 use FS::ClientAPI; #hmm
18 FS::ClientAPI->register_handlers(
19   'Signup/signup_info'  => \&signup_info,
20   'Signup/new_customer' => \&new_customer,
21 );
22
23 sub signup_info {
24   #my $packet = shift;
25
26   my $conf = new FS::Conf;
27
28   use vars qw($signup_info); #cache for performance;
29   $signup_info ||= {
30
31     'cust_main_county' =>
32       [ map { $_->hashref } qsearch('cust_main_county', {}) ],
33
34     'agent' =>
35       [
36         map { $_->hashref }
37           qsearch('agent', dbdef->table('agent')->column('disabled')
38                              ? { 'disabled' => '' }
39                              : {}
40                  )
41       ],
42
43     'agentnum2part_pkg' =>
44       {
45         map {
46           my $href = $_->pkgpart_hashref;
47           $_->agentnum =>
48             [
49               map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
50                 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
51                   qsearch( 'part_pkg', { 'disabled' => '' } )
52             ];
53         } qsearch('agent', dbdef->table('agent')->column('disabled')
54                              ? { 'disabled' => '' }
55                              : {}
56                  )
57       },
58
59     'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
60
61     'security_phrase' => $conf->exists('security_phrase'),
62
63     'payby' => [ $conf->config('signup_server-payby') ],
64
65     'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
66
67     'msgcat' => { map { $_=>gettext($_) } qw(
68       passwords_dont_match invalid_card unknown_card_type not_a
69     ) },
70
71     'statedefault' => $conf->config('statedefault') || 'CA',
72
73     'countrydefault' => $conf->config('countrydefault') || 'US',
74
75   };
76
77   if (
78     $conf->config('signup_server-default_agentnum')
79     && !exists $signup_info->{'part_pkg'} #cache for performance
80   ) {
81     my $agentnum = $conf->config('signup_server-default_agentnum');
82     my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
83       or die "fatal: signup_server-default_agentnum $agentnum not found\n";
84     my $pkgpart_href = $agent->pkgpart_hashref;
85
86     $signup_info->{'part_pkg'} = [
87       #map { $_->hashref }
88       map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
89         grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
90           qsearch( 'part_pkg', { 'disabled' => '' } )
91     ];
92   }
93
94   $signup_info;
95
96 }
97
98 sub new_customer {
99   my $packet = shift;
100
101   my $conf = new FS::Conf;
102   
103   #things that aren't necessary in base class, but are for signup server
104     #return "Passwords don't match"
105     #  if $hashref->{'_password'} ne $hashref->{'_password2'}
106   return { 'error' => gettext('empty_password') }
107     unless $packet->{'_password'};
108   # a bit inefficient for large numbers of pops
109   return { 'error' => gettext('no_access_number_selected') }
110     unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
111
112   #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
113   # common that are still here and library them.
114   my $cust_main = new FS::cust_main ( {
115     #'custnum'          => '',
116     'agentnum'      => $packet->{agentnum}
117                        || $conf->config('signup_server-default_agentnum'),
118     'refnum'        => $packet->{refnum}
119                        || $conf->config('signup_server-default_refnum'),
120
121     map { $_ => $packet->{$_} } qw(
122       last first ss company address1 address2 city county state zip country
123       daytime night fax payby payinfo paycvv paydate payname referral_custnum
124       comments
125     ),
126
127   } );
128
129   return { 'error' => "Illegal payment type" }
130     unless grep { $_ eq $packet->{'payby'} }
131                 $conf->config('signup_server-payby');
132
133   $cust_main->payinfo($cust_main->daytime)
134     if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
135
136   my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
137
138   $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
139   my $pkgpart = $1;
140   return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
141
142   my $part_pkg =
143     qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
144       or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
145   my $svcpart = $part_pkg->svcpart('svc_acct');
146
147   my $cust_pkg = new FS::cust_pkg ( {
148     #later#'custnum' => $custnum,
149     'pkgpart' => $packet->{'pkgpart'},
150   } );
151   my $error = $cust_pkg->check;
152   return { 'error' => $error } if $error;
153
154   my $svc_acct = new FS::svc_acct ( {
155     'svcpart'   => $svcpart,
156     map { $_ => $packet->{$_} }
157       qw( username _password sec_phrase popnum ),
158   } );
159
160   my @acct_snarf;
161   my $snarfnum = 1;
162   while ( length($packet->{"snarf_machine$snarfnum"}) ) {
163     my $acct_snarf = new FS::acct_snarf ( {
164       'machine'   => $packet->{"snarf_machine$snarfnum"},
165       'protocol'  => $packet->{"snarf_protocol$snarfnum"},
166       'username'  => $packet->{"snarf_username$snarfnum"},
167       '_password' => $packet->{"snarf_password$snarfnum"},
168     } );
169     $snarfnum++;
170     push @acct_snarf, $acct_snarf;
171   }
172   $svc_acct->child_objects( \@acct_snarf );
173
174   my $y = $svc_acct->setdefault; # arguably should be in new method
175   return { 'error' => $y } if $y && !ref($y);
176
177   $error = $svc_acct->check;
178   return { 'error' => $error } if $error;
179
180   use Tie::RefHash;
181   tie my %hash, 'Tie::RefHash';
182   %hash = ( $cust_pkg => [ $svc_acct ] );
183   #msgcat
184   $error = $cust_main->insert( \%hash, \@invoicing_list, 'noexport' => 1 );
185   return { 'error' => $error } if $error;
186
187   if ( $conf->exists('signup_server-realtime') ) {
188
189     #warn "[fs_signup_server] Billing customer...\n" if $Debug;
190
191     my $bill_error = $cust_main->bill;
192     #warn "[fs_signup_server] error billing new customer: $bill_error"
193     #  if $bill_error;
194
195     $cust_main->apply_payments;
196     $cust_main->apply_credits;
197
198     $bill_error = $cust_main->collect;
199     #warn "[fs_signup_server] error collecting from new customer: $bill_error"
200     #  if $bill_error;
201
202     if ( $cust_main->balance > 0 ) {
203
204       #this makes sense.  credit is "un-doing" the invoice
205       $cust_main->credit( $cust_main->balance, 'signup server decline' );
206       $cust_main->apply_credits;
207
208       #should check list for errors...
209       #$cust_main->suspend;
210       local $FS::svc_Common::noexport_hack = 1;
211       $cust_main->cancel('quiet'=>1);
212
213       return { 'error' => '_decline' };
214     }
215
216   }
217   $cust_main->reexport;
218
219   return { error => '' };
220
221 }
222
223 1;