have signup page use card-types config too
[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   }
123
124   $signup_info->{'part_pkg'} = [];
125
126   if ( $packet->{'reg_code'} ) {
127     $signup_info->{'part_pkg'} = 
128       [ map { { 'payby'   => [ $_->payby ], %{$_->hashref} } }
129           grep { $_->svcpart('svc_acct') }
130           map { $_->part_pkg }
131             qsearchs( 'reg_code', { 'code'     => $packet->{'reg_code'},
132                                     'agentnum' => $agentnum,              } )
133
134       ];
135
136     $signup_info->{'error'} = 'Unknown registration code'
137       unless @{ $signup_info->{'part_pkg'} };
138
139   } elsif ( $packet->{'promo_code'} ) {
140
141     $signup_info->{'part_pkg'} =
142       [ map { { 'payby'   => [ $_->payby ], %{$_->hashref} } }
143           grep { $_->svcpart('svc_acct') }
144             qsearch( 'part_pkg', { 'promo_code' => {
145                                      op=>'ILIKE',
146                                      value=>$packet->{'promo_code'}
147                                    },
148                                    'disabled'   => '',                  } )
149       ];
150
151     $signup_info->{'error'} = 'Unknown promotional code'
152       unless @{ $signup_info->{'part_pkg'} };
153   }
154
155   if ( $agentnum && ! @{ $signup_info->{'part_pkg'} } ) {
156     $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
157   }
158   # else {
159   # delete $signup_info->{'part_pkg'};
160   #}
161
162   if ( $session ) {
163     my $agent_signup_info = { %$signup_info };
164     delete $agent_signup_info->{agentnum2part_pkg};
165     $agent_signup_info->{'agent'} = $session->{'agent'};
166     $agent_signup_info;
167   } else {
168     $signup_info;
169   }
170
171 }
172
173 sub new_customer {
174   my $packet = shift;
175
176   my $conf = new FS::Conf;
177   
178   #things that aren't necessary in base class, but are for signup server
179     #return "Passwords don't match"
180     #  if $hashref->{'_password'} ne $hashref->{'_password2'}
181   return { 'error' => gettext('empty_password') }
182     unless length($packet->{'_password'});
183   # a bit inefficient for large numbers of pops
184   return { 'error' => gettext('no_access_number_selected') }
185     unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
186
187   my $agentnum;
188   if ( exists $packet->{'session_id'} ) {
189     my $cache = new FS::ClientAPI_SessionCache( {
190       'namespace' => 'FS::ClientAPI::Agent',
191     } );
192     my $session = $cache->get($packet->{'session_id'});
193     if ( $session ) {
194       $agentnum = $session->{'agentnum'};
195     } else {
196       return { 'error' => "Can't resume session" }; #better error message
197     }
198   } else {
199     $agentnum = $packet->{agentnum}
200                 || $conf->config('signup_server-default_agentnum');
201   }
202
203   #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
204   # common that are still here and library them.
205   my $cust_main = new FS::cust_main ( {
206     #'custnum'          => '',
207     'agentnum'      => $agentnum,
208     'refnum'        => $packet->{refnum}
209                        || $conf->config('signup_server-default_refnum'),
210
211     map { $_ => $packet->{$_} } qw(
212
213       last first ss company address1 address2
214       city county state zip country
215       daytime night fax
216
217       ship_last ship_first ship_ss ship_company ship_address1 ship_address2
218       ship_city ship_county ship_state ship_zip ship_country
219       ship_daytime ship_night ship_fax
220
221       payby
222       payinfo paycvv paydate payname
223       paystart_month paystart_year payissue
224       payip
225
226       referral_custnum comments
227     )
228
229   } );
230
231   return { 'error' => "Illegal payment type" }
232     unless grep { $_ eq $packet->{'payby'} }
233                 $conf->config('signup_server-payby');
234
235   $cust_main->payinfo($cust_main->daytime)
236     if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
237
238   my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
239
240   $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
241   my $pkgpart = $1;
242   return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
243
244   my $part_pkg =
245     qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
246       or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
247   my $svcpart = $part_pkg->svcpart('svc_acct');
248
249   my $reg_code = '';
250   if ( $packet->{'reg_code'} ) {
251     $reg_code = qsearchs( 'reg_code', { 'code'     => $packet->{'reg_code'},
252                                         'agentnum' => $agentnum,             } )
253       or return { 'error' => 'Unknown registration code' };
254   }
255
256   my $cust_pkg = new FS::cust_pkg ( {
257     #later#'custnum' => $custnum,
258     'pkgpart'    => $packet->{'pkgpart'},
259     'promo_code' => $packet->{'promo_code'},
260     'reg_code'   => $packet->{'reg_code'},
261   } );
262   #my $error = $cust_pkg->check;
263   #return { 'error' => $error } if $error;
264
265   my $svc_acct = new FS::svc_acct ( {
266     'svcpart'   => $svcpart,
267     map { $_ => $packet->{$_} }
268       qw( username _password sec_phrase popnum ),
269   } );
270
271   my @acct_snarf;
272   my $snarfnum = 1;
273   while (    exists($packet->{"snarf_machine$snarfnum"})
274           && length($packet->{"snarf_machine$snarfnum"}) ) {
275     my $acct_snarf = new FS::acct_snarf ( {
276       'machine'   => $packet->{"snarf_machine$snarfnum"},
277       'protocol'  => $packet->{"snarf_protocol$snarfnum"},
278       'username'  => $packet->{"snarf_username$snarfnum"},
279       '_password' => $packet->{"snarf_password$snarfnum"},
280     } );
281     $snarfnum++;
282     push @acct_snarf, $acct_snarf;
283   }
284   $svc_acct->child_objects( \@acct_snarf );
285
286   my $y = $svc_acct->setdefault; # arguably should be in new method
287   return { 'error' => $y } if $y && !ref($y);
288
289   #$error = $svc_acct->check;
290   #return { 'error' => $error } if $error;
291
292   #setup a job dependancy to delay provisioning
293   my $placeholder = new FS::queue ( {
294     'job'    => 'FS::ClientAPI::Signup::__placeholder',
295     'status' => 'locked',
296   } );
297   my $error = $placeholder->insert;
298   return { 'error' => $error } if $error;
299
300   use Tie::RefHash;
301   tie my %hash, 'Tie::RefHash';
302   %hash = ( $cust_pkg => [ $svc_acct ] );
303   #msgcat
304   $error = $cust_main->insert(
305     \%hash,
306     \@invoicing_list,
307     'depend_jobnum' => $placeholder->jobnum,
308   );
309   if ( $error ) {
310     my $perror = $placeholder->delete;
311     $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
312     return { 'error' => $error };
313   }
314
315   if ( $conf->exists('signup_server-realtime') ) {
316
317     #warn "[fs_signup_server] Billing customer...\n" if $Debug;
318
319     my $bill_error = $cust_main->bill;
320     #warn "[fs_signup_server] error billing new customer: $bill_error"
321     #  if $bill_error;
322
323     $cust_main->apply_payments;
324     $cust_main->apply_credits;
325
326     $bill_error = $cust_main->collect('realtime' => 1);
327     #warn "[fs_signup_server] error collecting from new customer: $bill_error"
328     #  if $bill_error;
329
330     if ( $cust_main->balance > 0 ) {
331
332       #this makes sense.  credit is "un-doing" the invoice
333       $cust_main->credit( $cust_main->balance, 'signup server decline' );
334       $cust_main->apply_credits;
335
336       #should check list for errors...
337       #$cust_main->suspend;
338       local $FS::svc_Common::noexport_hack = 1;
339       $cust_main->cancel('quiet'=>1);
340
341       my $perror = $placeholder->depended_delete;
342       warn "error removing provisioning jobs after decline: $perror" if $perror;
343       unless ( $perror ) {
344         $perror = $placeholder->delete;
345         warn "error removing placeholder after decline: $perror" if $perror;
346       }
347
348       return { 'error' => '_decline' };
349     }
350
351   }
352
353   if ( $reg_code ) {
354     $error = $reg_code->delete;
355     return { 'error' => $error } if $error;
356   }
357
358   $error = $placeholder->delete;
359   return { 'error' => $error } if $error;
360
361   return { error => '' };
362
363 }
364
365 1;