8eeab1a47b0f3319332cad254d817416eedf1d92
[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::Msgcat qw(gettext);
8 use FS::Misc qw(card_types);
9 use FS::ClientAPI_SessionCache;
10 use FS::agent;
11 use FS::cust_main_county;
12 use FS::part_pkg;
13 use FS::svc_acct_pop;
14 use FS::cust_main;
15 use FS::cust_pkg;
16 use FS::svc_acct;
17 use FS::acct_snarf;
18 use FS::queue;
19 use FS::reg_code;
20
21 sub signup_info {
22   my $packet = shift;
23
24   my $conf = new FS::Conf;
25
26   use vars qw($signup_info); #cache for performance;
27   $signup_info ||= {
28     'cust_main_county' =>
29       [ map { $_->hashref } qsearch('cust_main_county', {}) ],
30
31     'agent' =>
32       [
33         map { $_->hashref }
34           qsearch('agent', dbdef->table('agent')->column('disabled')
35                              ? { 'disabled' => '' }
36                              : {}
37                  )
38       ],
39
40     'part_referral' =>
41       [
42         map { $_->hashref }
43           qsearch('part_referral',
44                     dbdef->table('part_referral')->column('disabled')
45                       ? { 'disabled' => '' }
46                       : {}
47                  )
48       ],
49
50     'agentnum2part_pkg' =>
51       {
52         map {
53           my $href = $_->pkgpart_hashref;
54           $_->agentnum =>
55             [
56               map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
57                 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
58                   qsearch( 'part_pkg', { 'disabled' => '' } )
59             ];
60         } qsearch('agent', dbdef->table('agent')->column('disabled')
61                              ? { 'disabled' => '' }
62                              : {}
63                  )
64       },
65
66     'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
67
68     'emailinvoiceonly' => $conf->exists('emailinvoiceonly'),
69
70     'security_phrase' => $conf->exists('security_phrase'),
71
72     'payby' => [ $conf->config('signup_server-payby') ],
73
74     'card_types' => card_types(),
75
76     'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
77
78     'ship_enabled' => defined dbdef->table('cust_main')->column('ship_last'),
79
80     'msgcat' => { map { $_=>gettext($_) } qw(
81       passwords_dont_match invalid_card unknown_card_type not_a empty_password illegal_or_empty_text
82     ) },
83
84     'statedefault' => $conf->config('statedefault') || 'CA',
85
86     'countrydefault' => $conf->config('countrydefault') || 'US',
87
88     'refnum' => $conf->config('signup_server-default_refnum'),
89
90     'default_pkgpart' => $conf->config('signup_server-default_pkgpart'),
91
92   };
93
94   my @addl = qw( signup_server-classnum2 signup_server-classnum3 );
95
96   if ( grep { $conf->exists($_) } @addl ) {
97   
98     $signup_info->{optional_packages} = [];
99
100     foreach my $addl ( @addl ) {
101       my $classnum = $conf->config($addl) or next;
102       my @pkgs = map { $_->hashref }
103                      qsearch( 'part_pkg', { classnum => $classnum } );
104       push @{$signup_info->{optional_packages}}, \@pkgs;
105     }
106
107   }
108
109   my $agentnum = $conf->config('signup_server-default_agentnum');
110
111   my $session = '';
112   if ( exists $packet->{'session_id'} ) {
113     my $cache = new FS::ClientAPI_SessionCache( {
114       'namespace' => 'FS::ClientAPI::Agent',
115     } );
116     $session = $cache->get($packet->{'session_id'});
117     if ( $session ) {
118       $agentnum = $session->{'agentnum'};
119     } else {
120       return { 'error' => "Can't resume session" }; #better error message
121     }
122   }elsif( exists $packet->{'customer_session_id'} ) {
123     my $cache = new FS::ClientAPI_SessionCache( {
124       'namespace' => 'FS::ClientAPI::MyAccount',
125     } );
126     $session = $cache->get($packet->{'customer_session_id'});
127     if ( $session ) {
128       my $custnum = $session->{'custnum'};
129       my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum });
130       return { 'error' => "Can't find your customer record" } unless $cust_main;
131       $agentnum = $cust_main->agentnum;
132     } else {
133       return { 'error' => "Can't resume session" }; #better error message
134     }
135   }
136
137   $signup_info->{'part_pkg'} = [];
138
139   if ( $packet->{'reg_code'} ) {
140     $signup_info->{'part_pkg'} = 
141       [ map { { 'payby'   => [ $_->payby ], %{$_->hashref} } }
142           grep { $_->svcpart('svc_acct') }
143           map { $_->part_pkg }
144             qsearchs( 'reg_code', { 'code'     => $packet->{'reg_code'},
145                                     'agentnum' => $agentnum,              } )
146
147       ];
148
149     $signup_info->{'error'} = 'Unknown registration code'
150       unless @{ $signup_info->{'part_pkg'} };
151
152   } elsif ( $packet->{'promo_code'} ) {
153
154     $signup_info->{'part_pkg'} =
155       [ map { { 'payby'   => [ $_->payby ], %{$_->hashref} } }
156           grep { $_->svcpart('svc_acct') }
157             qsearch( 'part_pkg', { 'promo_code' => {
158                                      op=>'ILIKE',
159                                      value=>$packet->{'promo_code'}
160                                    },
161                                    'disabled'   => '',                  } )
162       ];
163
164     $signup_info->{'error'} = 'Unknown promotional code'
165       unless @{ $signup_info->{'part_pkg'} };
166   }
167
168   if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
169     $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
170   }
171   # else {
172   # delete $signup_info->{'part_pkg'};
173   #}
174
175   if ( exists $packet->{'session_id'} ) {
176     my $agent_signup_info = { %$signup_info };
177     delete $agent_signup_info->{agentnum2part_pkg};
178     $agent_signup_info->{'agent'} = $session->{'agent'};
179     $agent_signup_info;
180   } else {
181     $signup_info;
182   }
183
184 }
185
186 sub new_customer {
187   my $packet = shift;
188
189   my $conf = new FS::Conf;
190   
191   #things that aren't necessary in base class, but are for signup server
192     #return "Passwords don't match"
193     #  if $hashref->{'_password'} ne $hashref->{'_password2'}
194   return { 'error' => gettext('empty_password') }
195     unless length($packet->{'_password'});
196   # a bit inefficient for large numbers of pops
197   return { 'error' => gettext('no_access_number_selected') }
198     unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
199
200   my $agentnum;
201   if ( exists $packet->{'session_id'} ) {
202     my $cache = new FS::ClientAPI_SessionCache( {
203       'namespace' => 'FS::ClientAPI::Agent',
204     } );
205     my $session = $cache->get($packet->{'session_id'});
206     if ( $session ) {
207       $agentnum = $session->{'agentnum'};
208     } else {
209       return { 'error' => "Can't resume session" }; #better error message
210     }
211   } else {
212     $agentnum = $packet->{agentnum}
213                 || $conf->config('signup_server-default_agentnum');
214   }
215
216   #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
217   # common that are still here and library them.
218   my $cust_main = new FS::cust_main ( {
219     #'custnum'          => '',
220     'agentnum'      => $agentnum,
221     'refnum'        => $packet->{refnum}
222                        || $conf->config('signup_server-default_refnum'),
223
224     map { $_ => $packet->{$_} } qw(
225
226       last first ss company address1 address2
227       city county state zip country
228       daytime night fax
229
230       ship_last ship_first ship_ss ship_company ship_address1 ship_address2
231       ship_city ship_county ship_state ship_zip ship_country
232       ship_daytime ship_night ship_fax
233
234       payby
235       payinfo paycvv paydate payname
236       paystart_month paystart_year payissue
237       payip
238
239       referral_custnum comments
240     )
241
242   } );
243
244   return { 'error' => "Illegal payment type" }
245     unless grep { $_ eq $packet->{'payby'} }
246                 $conf->config('signup_server-payby');
247
248   $cust_main->payinfo($cust_main->daytime)
249     if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
250
251   my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
252
253   $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
254   my $pkgpart = $1;
255   return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
256
257   my $part_pkg =
258     qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
259       or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
260   my $svcpart = $part_pkg->svcpart('svc_acct');
261
262   my $reg_code = '';
263   if ( $packet->{'reg_code'} ) {
264     $reg_code = qsearchs( 'reg_code', { 'code'     => $packet->{'reg_code'},
265                                         'agentnum' => $agentnum,             } )
266       or return { 'error' => 'Unknown registration code' };
267   }
268
269   my $cust_pkg = new FS::cust_pkg ( {
270     #later#'custnum' => $custnum,
271     'pkgpart'    => $packet->{'pkgpart'},
272     'promo_code' => $packet->{'promo_code'},
273     'reg_code'   => $packet->{'reg_code'},
274   } );
275   #my $error = $cust_pkg->check;
276   #return { 'error' => $error } if $error;
277
278   my $svc_acct = new FS::svc_acct ( {
279     'svcpart'   => $svcpart,
280     map { $_ => $packet->{$_} }
281       qw( username _password sec_phrase popnum ),
282   } );
283
284   my @acct_snarf;
285   my $snarfnum = 1;
286   while (    exists($packet->{"snarf_machine$snarfnum"})
287           && length($packet->{"snarf_machine$snarfnum"}) ) {
288     my $acct_snarf = new FS::acct_snarf ( {
289       'machine'   => $packet->{"snarf_machine$snarfnum"},
290       'protocol'  => $packet->{"snarf_protocol$snarfnum"},
291       'username'  => $packet->{"snarf_username$snarfnum"},
292       '_password' => $packet->{"snarf_password$snarfnum"},
293     } );
294     $snarfnum++;
295     push @acct_snarf, $acct_snarf;
296   }
297   $svc_acct->child_objects( \@acct_snarf );
298
299   my $y = $svc_acct->setdefault; # arguably should be in new method
300   return { 'error' => $y } if $y && !ref($y);
301
302   #$error = $svc_acct->check;
303   #return { 'error' => $error } if $error;
304
305   #setup a job dependancy to delay provisioning
306   my $placeholder = new FS::queue ( {
307     'job'    => 'FS::ClientAPI::Signup::__placeholder',
308     'status' => 'locked',
309   } );
310   my $error = $placeholder->insert;
311   return { 'error' => $error } if $error;
312
313   use Tie::RefHash;
314   tie my %hash, 'Tie::RefHash';
315   %hash = ( $cust_pkg => [ $svc_acct ] );
316   #msgcat
317   $error = $cust_main->insert(
318     \%hash,
319     \@invoicing_list,
320     'depend_jobnum' => $placeholder->jobnum,
321   );
322   if ( $error ) {
323     my $perror = $placeholder->delete;
324     $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
325     return { 'error' => $error };
326   }
327
328   if ( $conf->exists('signup_server-realtime') ) {
329
330     #warn "[fs_signup_server] Billing customer...\n" if $Debug;
331
332     my $bill_error = $cust_main->bill;
333     #warn "[fs_signup_server] error billing new customer: $bill_error"
334     #  if $bill_error;
335
336     $cust_main->apply_payments;
337     $cust_main->apply_credits;
338
339     $bill_error = $cust_main->collect('realtime' => 1);
340     #warn "[fs_signup_server] error collecting from new customer: $bill_error"
341     #  if $bill_error;
342
343     if ( $cust_main->balance > 0 ) {
344
345       #this makes sense.  credit is "un-doing" the invoice
346       $cust_main->credit( $cust_main->balance, 'signup server decline' );
347       $cust_main->apply_credits;
348
349       #should check list for errors...
350       #$cust_main->suspend;
351       local $FS::svc_Common::noexport_hack = 1;
352       $cust_main->cancel('quiet'=>1);
353
354       my $perror = $placeholder->depended_delete;
355       warn "error removing provisioning jobs after decline: $perror" if $perror;
356       unless ( $perror ) {
357         $perror = $placeholder->delete;
358         warn "error removing placeholder after decline: $perror" if $perror;
359       }
360
361       return { 'error' => '_decline' };
362     }
363
364   }
365
366   if ( $reg_code ) {
367     $error = $reg_code->delete;
368     return { 'error' => $error } if $error;
369   }
370
371   $error = $placeholder->delete;
372   return { 'error' => $error } if $error;
373
374   return { error => '' };
375
376 }
377
378 1;