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