customer edit: abstract out billing info to billing.html, re-do payment type selectio...
[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
201       payinfo paycvv paydate payname
202       paystart_month paystart_year payissue
203       payip
204
205       referral_custnum comments
206     )
207
208   } );
209
210   return { 'error' => "Illegal payment type" }
211     unless grep { $_ eq $packet->{'payby'} }
212                 $conf->config('signup_server-payby');
213
214   $cust_main->payinfo($cust_main->daytime)
215     if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
216
217   my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
218
219   $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
220   my $pkgpart = $1;
221   return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
222
223   my $part_pkg =
224     qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
225       or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
226   my $svcpart = $part_pkg->svcpart('svc_acct');
227
228   my $reg_code = '';
229   if ( $packet->{'reg_code'} ) {
230     $reg_code = qsearchs( 'reg_code', { 'code'     => $packet->{'reg_code'},
231                                         'agentnum' => $agentnum,             } )
232       or return { 'error' => 'Unknown registration code' };
233   }
234
235   my $cust_pkg = new FS::cust_pkg ( {
236     #later#'custnum' => $custnum,
237     'pkgpart'    => $packet->{'pkgpart'},
238     'promo_code' => $packet->{'promo_code'},
239     'reg_code'   => $packet->{'reg_code'},
240   } );
241   #my $error = $cust_pkg->check;
242   #return { 'error' => $error } if $error;
243
244   my $svc_acct = new FS::svc_acct ( {
245     'svcpart'   => $svcpart,
246     map { $_ => $packet->{$_} }
247       qw( username _password sec_phrase popnum ),
248   } );
249
250   my @acct_snarf;
251   my $snarfnum = 1;
252   while (    exists($packet->{"snarf_machine$snarfnum"})
253           && length($packet->{"snarf_machine$snarfnum"}) ) {
254     my $acct_snarf = new FS::acct_snarf ( {
255       'machine'   => $packet->{"snarf_machine$snarfnum"},
256       'protocol'  => $packet->{"snarf_protocol$snarfnum"},
257       'username'  => $packet->{"snarf_username$snarfnum"},
258       '_password' => $packet->{"snarf_password$snarfnum"},
259     } );
260     $snarfnum++;
261     push @acct_snarf, $acct_snarf;
262   }
263   $svc_acct->child_objects( \@acct_snarf );
264
265   my $y = $svc_acct->setdefault; # arguably should be in new method
266   return { 'error' => $y } if $y && !ref($y);
267
268   #$error = $svc_acct->check;
269   #return { 'error' => $error } if $error;
270
271   #setup a job dependancy to delay provisioning
272   my $placeholder = new FS::queue ( {
273     'job'    => 'FS::ClientAPI::Signup::__placeholder',
274     'status' => 'locked',
275   } );
276   my $error = $placeholder->insert;
277   return { 'error' => $error } if $error;
278
279   use Tie::RefHash;
280   tie my %hash, 'Tie::RefHash';
281   %hash = ( $cust_pkg => [ $svc_acct ] );
282   #msgcat
283   $error = $cust_main->insert(
284     \%hash,
285     \@invoicing_list,
286     'depend_jobnum' => $placeholder->jobnum,
287   );
288   if ( $error ) {
289     my $perror = $placeholder->delete;
290     $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
291     return { 'error' => $error };
292   }
293
294   if ( $conf->exists('signup_server-realtime') ) {
295
296     #warn "[fs_signup_server] Billing customer...\n" if $Debug;
297
298     my $bill_error = $cust_main->bill;
299     #warn "[fs_signup_server] error billing new customer: $bill_error"
300     #  if $bill_error;
301
302     $cust_main->apply_payments;
303     $cust_main->apply_credits;
304
305     $bill_error = $cust_main->collect;
306     #warn "[fs_signup_server] error collecting from new customer: $bill_error"
307     #  if $bill_error;
308
309     if ( $cust_main->balance > 0 ) {
310
311       #this makes sense.  credit is "un-doing" the invoice
312       $cust_main->credit( $cust_main->balance, 'signup server decline' );
313       $cust_main->apply_credits;
314
315       #should check list for errors...
316       #$cust_main->suspend;
317       local $FS::svc_Common::noexport_hack = 1;
318       $cust_main->cancel('quiet'=>1);
319
320       my $perror = $placeholder->depended_delete;
321       warn "error removing provisioning jobs after decline: $perror" if $perror;
322       unless ( $perror ) {
323         $perror = $placeholder->delete;
324         warn "error removing placeholder after decline: $perror" if $perror;
325       }
326
327       return { 'error' => '_decline' };
328     }
329
330   }
331
332   if ( $reg_code ) {
333     $error = $reg_code->delete;
334     return { 'error' => $error } if $error;
335   }
336
337   $error = $placeholder->delete;
338   return { 'error' => $error } if $error;
339
340   return { error => '' };
341
342 }
343
344 1;