This commit was generated by cvs2svn to compensate for changes in r3880,
[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
18 use FS::ClientAPI; #hmm
19 FS::ClientAPI->register_handlers(
20   'Signup/signup_info'  => \&signup_info,
21   'Signup/new_customer' => \&new_customer,
22 );
23
24 sub signup_info {
25   my $packet = shift;
26
27   my $conf = new FS::Conf;
28
29   use vars qw($signup_info); #cache for performance;
30   $signup_info ||= {
31
32     'cust_main_county' =>
33       [ map { $_->hashref } qsearch('cust_main_county', {}) ],
34
35     'agent' =>
36       [
37         map { $_->hashref }
38           qsearch('agent', dbdef->table('agent')->column('disabled')
39                              ? { 'disabled' => '' }
40                              : {}
41                  )
42       ],
43
44     'part_referral' =>
45       [
46         map { $_->hashref }
47           qsearch('part_referral',
48                     dbdef->table('part_referral')->column('disabled')
49                       ? { 'disabled' => '' }
50                       : {}
51                  )
52       ],
53
54     'agentnum2part_pkg' =>
55       {
56         map {
57           my $href = $_->pkgpart_hashref;
58           $_->agentnum =>
59             [
60               map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
61                 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
62                   qsearch( 'part_pkg', { 'disabled' => '' } )
63             ];
64         } qsearch('agent', dbdef->table('agent')->column('disabled')
65                              ? { 'disabled' => '' }
66                              : {}
67                  )
68       },
69
70     'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
71
72     'security_phrase' => $conf->exists('security_phrase'),
73
74     'payby' => [ $conf->config('signup_server-payby') ],
75
76     'cvv_enabled' => defined dbdef->table('cust_main')->column('paycvv'),
77
78     'msgcat' => { map { $_=>gettext($_) } qw(
79       passwords_dont_match invalid_card unknown_card_type not_a empty_password
80     ) },
81
82     'statedefault' => $conf->config('statedefault') || 'CA',
83
84     'countrydefault' => $conf->config('countrydefault') || 'US',
85
86     'refnum' => $conf->config('signup_server-default_refnum'),
87
88   };
89
90   my $agentnum = $conf->config('signup_server-default_agentnum');
91
92   my $session = '';
93   if ( exists $packet->{'session_id'} ) {
94     my $cache = new Cache::SharedMemoryCache( {
95       'namespace' => 'FS::ClientAPI::Agent',
96     } );
97     $session = $cache->get($packet->{'session_id'});
98     if ( $session ) {
99       $agentnum = $session->{'agentnum'};
100     } else {
101       return { 'error' => "Can't resume session" }; #better error message
102     }
103   }
104
105   if ( $agentnum ) {
106     $signup_info->{'part_pkg'} = $signup_info->{'agentnum2part_pkg'}{$agentnum};
107   } else {
108     delete $signup_info->{'part_pkg'};
109   }
110
111   if ( $session ) {
112     my $agent_signup_info = { %$signup_info };
113     delete $agent_signup_info->{agentnum2part_pkg};
114     $agent_signup_info->{'agent'} = $session->{'agent'};
115     $agent_signup_info;
116   } else {
117     $signup_info;
118   }
119
120 }
121
122 sub new_customer {
123   my $packet = shift;
124
125   my $conf = new FS::Conf;
126   
127   #things that aren't necessary in base class, but are for signup server
128     #return "Passwords don't match"
129     #  if $hashref->{'_password'} ne $hashref->{'_password2'}
130   return { 'error' => gettext('empty_password') }
131     unless length($packet->{'_password'});
132   # a bit inefficient for large numbers of pops
133   return { 'error' => gettext('no_access_number_selected') }
134     unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
135
136   my $agentnum;
137   if ( exists $packet->{'session_id'} ) {
138     my $cache = new Cache::SharedMemoryCache( {
139       'namespace' => 'FS::ClientAPI::Agent',
140     } );
141     my $session = $cache->get($packet->{'session_id'});
142     if ( $session ) {
143       $agentnum = $session->{'agentnum'};
144     } else {
145       return { 'error' => "Can't resume session" }; #better error message
146     }
147   } else {
148     $agentnum = $packet->{agentnum}
149                 || $conf->config('signup_server-default_agentnum');
150   }
151
152   #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
153   # common that are still here and library them.
154   my $cust_main = new FS::cust_main ( {
155     #'custnum'          => '',
156     'agentnum'      => $agentnum,
157     'refnum'        => $packet->{refnum}
158                        || $conf->config('signup_server-default_refnum'),
159
160     map { $_ => $packet->{$_} } qw(
161       last first ss company address1 address2 city county state zip country
162       daytime night fax payby payinfo paycvv paydate payname referral_custnum
163       comments
164     ),
165
166   } );
167
168   return { 'error' => "Illegal payment type" }
169     unless grep { $_ eq $packet->{'payby'} }
170                 $conf->config('signup_server-payby');
171
172   $cust_main->payinfo($cust_main->daytime)
173     if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
174
175   my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
176
177   $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
178   my $pkgpart = $1;
179   return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
180
181   my $part_pkg =
182     qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
183       or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
184   my $svcpart = $part_pkg->svcpart('svc_acct');
185
186   my $cust_pkg = new FS::cust_pkg ( {
187     #later#'custnum' => $custnum,
188     'pkgpart' => $packet->{'pkgpart'},
189   } );
190   my $error = $cust_pkg->check;
191   return { 'error' => $error } if $error;
192
193   my $svc_acct = new FS::svc_acct ( {
194     'svcpart'   => $svcpart,
195     map { $_ => $packet->{$_} }
196       qw( username _password sec_phrase popnum ),
197   } );
198
199   my @acct_snarf;
200   my $snarfnum = 1;
201   while (    exists($packet->{"snarf_machine$snarfnum"})
202           && length($packet->{"snarf_machine$snarfnum"}) ) {
203     my $acct_snarf = new FS::acct_snarf ( {
204       'machine'   => $packet->{"snarf_machine$snarfnum"},
205       'protocol'  => $packet->{"snarf_protocol$snarfnum"},
206       'username'  => $packet->{"snarf_username$snarfnum"},
207       '_password' => $packet->{"snarf_password$snarfnum"},
208     } );
209     $snarfnum++;
210     push @acct_snarf, $acct_snarf;
211   }
212   $svc_acct->child_objects( \@acct_snarf );
213
214   my $y = $svc_acct->setdefault; # arguably should be in new method
215   return { 'error' => $y } if $y && !ref($y);
216
217   $error = $svc_acct->check;
218   return { 'error' => $error } if $error;
219
220   #setup a job dependancy to delay provisioning
221   my $placeholder = new FS::queue ( {
222     'job'    => 'FS::ClientAPI::Signup::__placeholder',
223     'status' => 'locked',
224   } );
225   $error = $placeholder->insert;
226   return { 'error' => $error } if $error;
227
228   use Tie::RefHash;
229   tie my %hash, 'Tie::RefHash';
230   %hash = ( $cust_pkg => [ $svc_acct ] );
231   #msgcat
232   $error = $cust_main->insert(
233     \%hash,
234     \@invoicing_list,
235     'depend_jobnum' => $placeholder->jobnum,
236   );
237   if ( $error ) {
238     my $perror = $placeholder->delete;
239     $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
240     return { 'error' => $error };
241   }
242
243   if ( $conf->exists('signup_server-realtime') ) {
244
245     #warn "[fs_signup_server] Billing customer...\n" if $Debug;
246
247     my $bill_error = $cust_main->bill;
248     #warn "[fs_signup_server] error billing new customer: $bill_error"
249     #  if $bill_error;
250
251     $cust_main->apply_payments;
252     $cust_main->apply_credits;
253
254     $bill_error = $cust_main->collect;
255     #warn "[fs_signup_server] error collecting from new customer: $bill_error"
256     #  if $bill_error;
257
258     if ( $cust_main->balance > 0 ) {
259
260       #this makes sense.  credit is "un-doing" the invoice
261       $cust_main->credit( $cust_main->balance, 'signup server decline' );
262       $cust_main->apply_credits;
263
264       #should check list for errors...
265       #$cust_main->suspend;
266       local $FS::svc_Common::noexport_hack = 1;
267       $cust_main->cancel('quiet'=>1);
268
269       my $perror = $placeholder->depended_delete;
270       warn "error removing provisioning jobs after decline: $perror" if $perror;
271       unless ( $perror ) {
272         $perror = $placeholder->delete;
273         warn "error removing placeholder after decline: $perror" if $perror;
274       }
275
276       return { 'error' => '_decline' };
277     }
278
279   }
280
281   $error = $placeholder->delete;
282   return { 'error' => $error } if $error;
283
284   return { error => '' };
285
286 }
287
288 1;