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