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