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