signup with agent selection
[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::agent;
8 use FS::cust_main_county;
9 use FS::part_pkg;
10 use FS::svc_acct_pop;
11 use FS::cust_main;
12 use FS::cust_pkg;
13 use FS::Msgcat qw(gettext);
14
15 use FS::ClientAPI; #hmm
16 FS::ClientAPI->register_handlers(
17   'Signup/signup_info'  => \&signup_info,
18   'Signup/new_customer' => \&new_customer,
19 );
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
29     'cust_main_county' =>
30       [ map { $_->hashref } qsearch('cust_main_county', {}) ],
31
32     'agent' =>
33       [
34         map { $_->hashref }
35           qsearch('agent', dbdef->table('agent')->column('disabled')
36                              ? { 'disabled' => '' }
37                              : {}
38                  )
39       ],
40
41     'agentnum2part_pkg' =>
42       {
43         map {
44           my $href = $_->pkgpart_hashref;
45           $_->agentnum =>
46             [
47               map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
48                 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
49                   qsearch( 'part_pkg', { 'disabled' => '' } )
50             ];
51         } qsearch('agent', dbdef->table('agent')->column('disabled')
52                              ? { 'disabled' => '' }
53                              : {}
54                  )
55       },
56
57     'svc_acct_pop' => [ map { $_->hashref } qsearch('svc_acct_pop',{} ) ],
58
59     'security_phrase' => $conf->exists('security_phrase'),
60
61     'payby' => [ $conf->config('signup_server-payby') ],
62
63     'msgcat' => { map { $_=>gettext($_) } qw(
64       passwords_dont_match invalid_card unknown_card_type not_a
65     ) },
66
67     'statedefault' => $conf->config('statedefault') || 'CA',
68
69     'countrydefault' => $conf->config('countrydefault') || 'US',
70
71   };
72
73   if (
74     $conf->config('signup_server-default_agentnum')
75     && !exists $signup_info->{'part_pkg'} #cache for performance
76   ) {
77     my $agentnum = $conf->config('signup_server-default_agentnum');
78     my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } )
79       or die "fatal: signup_server-default_agentnum $agentnum not found\n";
80     my $pkgpart_href = $agent->pkgpart_hashref;
81
82     $signup_info->{'part_pkg'} = [
83       #map { $_->hashref }
84       map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
85         grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
86           qsearch( 'part_pkg', { 'disabled' => '' } )
87     ];
88   }
89
90   $signup_info;
91
92 }
93
94 sub new_customer {
95   my $packet = shift;
96
97   my $conf = new FS::Conf;
98   
99   #things that aren't necessary in base class, but are for signup server
100     #return "Passwords don't match"
101     #  if $hashref->{'_password'} ne $hashref->{'_password2'}
102   return { 'error' => gettext('empty_password') }
103     unless $packet->{'_password'};
104   # a bit inefficient for large numbers of pops
105   return { 'error' => gettext('no_access_number_selected') }
106     unless $packet->{'popnum'} || !scalar(qsearch('svc_acct_pop',{} ));
107
108   #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
109   # common that are still here and library them.
110   my $cust_main = new FS::cust_main ( {
111     #'custnum'          => '',
112     'agentnum'      => $packet->{agentnum}
113                        || $conf->config('signup_server-default_agentnum'),
114     'refnum'        => $packet->{refnum}
115                        || $conf->config('signup_server-default_refnum'),
116
117     map { $_ => $packet->{$_} } qw(
118       last first ss company address1 address2 city county state zip country
119       daytime night fax payby payinfo paydate payname referral_custnum comments
120     ),
121
122   } );
123
124   return { 'error' => "Illegal payment type" }
125     unless grep { $_ eq $packet->{'payby'} }
126                 $conf->config('signup_server-payby');
127
128   $cust_main->payinfo($cust_main->daytime)
129     if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
130
131   my @invoicing_list = split( /\s*\,\s*/, $packet->{'invoicing_list'} );
132
133   $packet->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
134   my $pkgpart = $1;
135   return { 'error' => 'Please select a package' } unless $pkgpart; #msgcat
136
137   my $part_pkg =
138     qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
139       or return { 'error' => "WARNING: unknown pkgpart: $pkgpart" };
140   my $svcpart = $part_pkg->svcpart('svc_acct');
141
142   my $cust_pkg = new FS::cust_pkg ( {
143     #later#'custnum' => $custnum,
144     'pkgpart' => $packet->{'pkgpart'},
145   } );
146   my $error = $cust_pkg->check;
147   return { 'error' => $error } if $error;
148
149   my $svc_acct = new FS::svc_acct ( {
150     'svcpart'   => $svcpart,
151     map { $_ => $packet->{$_} }
152       qw( username _password sec_phrase popnum ),
153   } );
154
155   my $y = $svc_acct->setdefault; # arguably should be in new method
156   return { 'error' => $y } if $y && !ref($y);
157
158   $error = $svc_acct->check;
159   return { 'error' => $error } if $error;
160
161   use Tie::RefHash;
162   tie my %hash, 'Tie::RefHash';
163   %hash = ( $cust_pkg => [ $svc_acct ] );
164   #msgcat
165   $error = $cust_main->insert( \%hash, \@invoicing_list, 'noexport' => 1 );
166   return { 'error' => $error } if $error;
167
168   if ( $conf->exists('signup_server-realtime') ) {
169
170     #warn "[fs_signup_server] Billing customer...\n" if $Debug;
171
172     my $bill_error = $cust_main->bill;
173     #warn "[fs_signup_server] error billing new customer: $bill_error"
174     #  if $bill_error;
175
176     $cust_main->apply_payments;
177     $cust_main->apply_credits;
178
179     $bill_error = $cust_main->collect;
180     #warn "[fs_signup_server] error collecting from new customer: $bill_error"
181     #  if $bill_error;
182
183     if ( $cust_main->balance > 0 ) {
184
185       #this makes sense.  credit is "un-doing" the invoice
186       $cust_main->credit( $cust_main->balance, 'signup server decline' );
187       $cust_main->apply_credits;
188
189       #should check list for errors...
190       #$cust_main->suspend;
191       local $FS::svc_Common::noexport_hack = 1;
192       $cust_main->cancel('quiet'=>1);
193
194       return { 'error' => '_decline' };
195     }
196
197   }
198   $cust_main->reexport;
199
200   return { error => '' };
201
202 }
203
204 1;