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