This commit was generated by cvs2svn to compensate for changes in r3241,
[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
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   if (
91     $conf->config('signup_server-default_agentnum')
92     && !exists $signup_info->{'part_pkg'} #cache for performance
93   ) {
94     my $agentnum = $conf->config('signup_server-default_agentnum');
95     my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
96       or die "fatal: signup_server-default_agentnum $agentnum not found\n";
97     my $pkgpart_href = $agent->pkgpart_hashref;
98
99     $signup_info->{'part_pkg'} = [
100       #map { $_->hashref }
101       map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
102         grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
103           qsearch( 'part_pkg', { 'disabled' => '' } )
104     ];
105   }
106
107   $signup_info;
108
109 }
110
111 sub new_customer {
112   my $packet = shift;
113
114   my $conf = new FS::Conf;
115   
116   #things that aren't necessary in base class, but are for signup server
117     #return "Passwords don't match"
118     #  if $hashref->{'_password'} ne $hashref->{'_password2'}
119   return { 'error' => gettext('empty_password') }
120     unless $packet->{'_password'};
121   # a bit inefficient for large numbers of pops
122   return { 'error' => gettext('no_access_number_selected') }
123     unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
124
125   #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
126   # common that are still here and library them.
127   my $cust_main = new FS::cust_main ( {
128     #'custnum'          => '',
129     'agentnum'      => $packet->{agentnum}
130                        || $conf->config('signup_server-default_agentnum'),
131     'refnum'        => $packet->{refnum}
132                        || $conf->config('signup_server-default_refnum'),
133
134     map { $_ => $packet->{$_} } qw(
135       last first ss company address1 address2 city county state zip country
136       daytime night fax payby payinfo paycvv paydate payname referral_custnum
137       comments
138     ),
139
140   } );
141
142   return { 'error' => "Illegal payment type" }
143     unless grep { $_ eq $packet->{'payby'} }
144                 $conf->config('signup_server-payby');
145
146   $cust_main->payinfo($cust_main->daytime)
147     if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
148
149   my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
150
151   $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
152   my $pkgpart = $1;
153   return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
154
155   my $part_pkg =
156     qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
157       or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
158   my $svcpart = $part_pkg->svcpart('svc_acct');
159
160   my $cust_pkg = new FS::cust_pkg ( {
161     #later#'custnum' => $custnum,
162     'pkgpart' => $packet->{'pkgpart'},
163   } );
164   my $error = $cust_pkg->check;
165   return { 'error' => $error } if $error;
166
167   my $svc_acct = new FS::svc_acct ( {
168     'svcpart'   => $svcpart,
169     map { $_ => $packet->{$_} }
170       qw( username _password sec_phrase popnum ),
171   } );
172
173   my @acct_snarf;
174   my $snarfnum = 1;
175   while (    exists($packet->{"snarf_machine$snarfnum"})
176           && length($packet->{"snarf_machine$snarfnum"}) ) {
177     my $acct_snarf = new FS::acct_snarf ( {
178       'machine'   => $packet->{"snarf_machine$snarfnum"},
179       'protocol'  => $packet->{"snarf_protocol$snarfnum"},
180       'username'  => $packet->{"snarf_username$snarfnum"},
181       '_password' => $packet->{"snarf_password$snarfnum"},
182     } );
183     $snarfnum++;
184     push @acct_snarf, $acct_snarf;
185   }
186   $svc_acct->child_objects( \@acct_snarf );
187
188   my $y = $svc_acct->setdefault; # arguably should be in new method
189   return { 'error' => $y } if $y && !ref($y);
190
191   $error = $svc_acct->check;
192   return { 'error' => $error } if $error;
193
194   #setup a job dependancy to delay provisioning
195   my $placeholder = new FS::queue ( {
196     'job'    => 'FS::ClientAPI::Signup::__placeholder',
197     'status' => 'locked',
198   } );
199   $error = $placeholder->insert;
200   return { 'error' => $error } if $error;
201
202   use Tie::RefHash;
203   tie my %hash, 'Tie::RefHash';
204   %hash = ( $cust_pkg => [ $svc_acct ] );
205   #msgcat
206   $error = $cust_main->insert(
207     \%hash,
208     \@invoicing_list,
209     'depend_jobnum' => $placeholder->jobnum,
210   );
211   if ( $error ) {
212     my $perror = $placeholder->delete;
213     $error .= " (Additionally, error removing placeholder: $perror)" if $perror;
214     return { 'error' => $error };
215   }
216
217   if ( $conf->exists('signup_server-realtime') ) {
218
219     #warn "[fs_signup_server] Billing customer...\n" if $Debug;
220
221     my $bill_error = $cust_main->bill;
222     #warn "[fs_signup_server] error billing new customer: $bill_error"
223     #  if $bill_error;
224
225     $cust_main->apply_payments;
226     $cust_main->apply_credits;
227
228     $bill_error = $cust_main->collect;
229     #warn "[fs_signup_server] error collecting from new customer: $bill_error"
230     #  if $bill_error;
231
232     if ( $cust_main->balance > 0 ) {
233
234       #this makes sense.  credit is "un-doing" the invoice
235       $cust_main->credit( $cust_main->balance, 'signup server decline' );
236       $cust_main->apply_credits;
237
238       #should check list for errors...
239       #$cust_main->suspend;
240       local $FS::svc_Common::noexport_hack = 1;
241       $cust_main->cancel('quiet'=>1);
242
243       my $perror = $placeholder->depended_delete;
244       warn "error removing provisioning jobs after decline: $perror" if $perror;
245       unless ( $perror ) {
246         $perror = $placeholder->delete;
247         warn "error removing placeholder after decline: $perror" if $perror;
248       }
249
250       return { 'error' => '_decline' };
251     }
252
253   }
254
255   $error = $placeholder->delete;
256   return { 'error' => $error } if $error;
257
258   return { error => '' };
259
260 }
261
262 1;