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