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