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