separate state and country <SELECT> in signup server also, closes: Bug#353
[freeside.git] / fs_signup / fs_signup_server
1 #!/usr/bin/perl -Tw
2 #
3 # fs_signup_server
4 #
5
6 use strict;
7 use vars qw($pid);
8 use IO::Handle;
9 use Storable qw(nstore_fd fd_retrieve);
10 use Tie::RefHash;
11 use Net::SSH qw(sshopen2);
12 use FS::UID qw(adminsuidsetup);
13 use FS::Conf;
14 use FS::Record qw( qsearch qsearchs );
15 use FS::cust_main_county;
16 use FS::cust_main;
17 use FS::Msgcat qw(gettext);
18
19 use vars qw( $opt $Debug );
20
21 $Debug = 2;
22
23 my $user = shift or die &usage;
24 &adminsuidsetup( $user ); 
25
26 my $conf = new FS::Conf;
27
28 #my @payby = qw(CARD PREPAY);
29 my @payby = $conf->config('signup_server-payby');
30 my $smtpmachine = $conf->config('smtpmachine');
31
32 my $machine = shift or die &usage;
33
34 my $agentnum = shift or die &usage;
35 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } ) or die &usage;
36 my $pkgpart_href = $agent->pkgpart_hashref;
37
38 my $refnum = shift or die &usage;
39
40 #causing trouble for some folks
41 #$SIG{CHLD} = sub { wait() };
42
43 $SIG{HUP} = \&killssh;
44 $SIG{INT} = \&killssh;
45 $SIG{QUIT} = \&killssh;
46 $SIG{TERM} = \&killssh;
47 $SIG{PIPE} = \&killssh;
48 sub killssh { kill 'TERM', $pid if $pid; exit; };
49
50 my($fs_signupd)="/usr/local/sbin/fs_signupd";
51
52 while (1) {
53   my($reader,$writer)=(new IO::Handle, new IO::Handle);
54   #seems to be broken - calling ->flush explicitly# $writer->autoflush(1);
55   warn "[fs_signup_server] Connecting to $machine...\n" if $Debug;
56   $pid = sshopen2($machine,$reader,$writer,$fs_signupd);
57
58   my @pops = qsearch('svc_acct_pop',{} );
59   my $init_data = {
60
61     #'_protocol' => 'signup',
62     #'_version' => '0.1',
63     #'_packet' => 'init'
64   
65     'cust_main_county' =>
66       [ map { $_->hashref } qsearch('cust_main_county', {}) ],
67       
68     'part_pkg' =>
69       [
70         #map { $_->hashref }
71         map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
72           grep { $_->svcpart('svc_acct') && $pkgpart_href->{ $_->pkgpart } }
73             qsearch( 'part_pkg', { 'disabled' => '' } )
74       ],
75
76     'agentnum2part_pkg' =>
77       {
78         map {
79           my $href = $_->pkgpart_hashref;
80           $_->agentnum =>
81             [
82               map { { 'payby' => [ $_->payby ], %{$_->hashref} } }
83                 grep { $_->svcpart('svc_acct') && $href->{ $_->pkgpart } }
84                   qsearch( 'part_pkg', { 'disabled' => '' } )
85             ];
86         } qsearch('agent', {} )
87       },
88
89     'svc_acct_pop' => [ map { $_->hashref } @pops ],
90
91     'security_phrase' => $conf->exists('security_phrase'),
92
93     'payby' => [ $conf->config('signup_server-payby') ],
94
95     'msgcat' => { map { $_=>gettext($_) } qw(
96       passwords_dont_match invalid_card unknown_card_type not_a
97     ) },
98
99     'statedefault' => $conf->config('statedefault') || 'CA',
100
101     'countrydefault' => $conf->config('countrydefault') || 'US',
102
103   };
104
105   warn "[fs_signup_server] Sending init data...\n" if $Debug;
106   nstore_fd($init_data, $writer) or die "can't send init data: $!";
107   $writer->flush;
108
109   warn "[fs_signup_server] Entering main loop...\n" if $Debug;
110   while (1) {
111     warn "[fs_signup_server] Reading (waiting for) signup data...\n" if $Debug;
112     my $signup_data = fd_retrieve($reader);
113
114     if ( $Debug > 1 ) {
115       warn join('',
116         map { "  $_ => ". $signup_data->{$_}. "\n" } keys %$signup_data );
117     }
118
119     warn "[fs_signup_server] Processing signup...\n" if $Debug;
120
121     my $error = '';
122
123     #things that aren't necessary in base class, but are for signup server
124       #return "Passwords don't match"
125       #  if $hashref->{'_password'} ne $hashref->{'_password2'}
126     $error ||= gettext('empty_password') unless $signup_data->{'_password'};
127     $error ||= gettext('no_access_number_selected')
128       unless $signup_data->{'popnum'} || !scalar(@pops);
129
130     #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
131     # common that are still here and library them.
132     my $cust_main = new FS::cust_main ( {
133       #'custnum'          => '',
134       'agentnum'         => $signup_data->{agentnum} || $agentnum,
135       'refnum'           => $refnum,
136
137       map { $_ => $signup_data->{$_} } qw(
138         last first ss company address1 address2 city county state zip country
139         daytime night fax payby payinfo paydate payname referral_custnum
140       ),
141
142     } );
143
144     $error ||= "Illegal payment type"
145       unless grep { $_ eq $signup_data->{'payby'} } @payby;
146
147     $cust_main->payinfo($cust_main->daytime)
148       if $cust_main->payby eq 'LECB' && ! $cust_main->payinfo;
149
150     my @invoicing_list = split( /\s*\,\s*/, $signup_data->{'invoicing_list'} );
151
152     $signup_data->{'pkgpart'} =~ /^(\d+)$/ or '' =~ /^()$/;
153     my $pkgpart = $1;
154
155     my $part_pkg =
156       qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
157         or $error ||= "WARNING: unknown pkgpart: $pkgpart";
158     my $svcpart = $part_pkg->svcpart unless $error;
159
160     my $cust_pkg = new FS::cust_pkg ( {
161       #later#'custnum' => $custnum,
162       'pkgpart' => $signup_data->{'pkgpart'},
163     } );
164     $error ||= $cust_pkg->check;
165
166     my $svc_acct = new FS::svc_acct ( {
167       'svcpart'   => $svcpart,
168       map { $_ => $signup_data->{$_} }
169         qw( username _password sec_phrase popnum ),
170     } );
171
172     my $y = $svc_acct->setdefault; # arguably should be in new method
173     $error ||= $y unless ref($y);
174
175     $error ||= $svc_acct->check;
176
177     use Tie::RefHash;
178     tie my %hash, 'Tie::RefHash';
179     %hash = ( $cust_pkg => [ $svc_acct ] );
180     $error ||= $cust_main->insert( \%hash, \@invoicing_list ); #msgcat
181
182     if ( ! $error && $conf->exists('signup_server-realtime') ) {
183
184       warn "[fs_signup_server] Billing customer...\n" if $Debug;
185
186       my $bill_error = $cust_main->bill;
187       warn "[fs_signup_server] error billing new customer: $bill_error"
188         if $bill_error;
189
190       $cust_main->apply_payments;
191       $cust_main->apply_credits;
192
193       $bill_error = $cust_main->collect;
194       warn "[fs_signup_server] error collecting from new customer: $bill_error"
195         if $bill_error;
196
197       if ( $cust_main->balance > 0 ) {
198
199         #this makes sense.  credit is "un-doing" the invoice
200         $cust_main->credit( $cust_main->balance, 'signup server decline' );
201         $cust_main->apply_credits;
202
203         #should check list for errors...
204         #$cust_main->suspend;
205         $cust_main->cancel;
206
207         $error = '_decline';
208       }
209     }
210
211     warn "[fs_signup_server] Sending results...\n" if $Debug;
212     print $writer $error, "\n";
213
214     next if $error;
215
216     if ( $conf->config('signup_server-email') ) {
217       warn "[fs_signup_server] Sending email...\n" if $Debug;
218
219       #false laziness w/FS::cust_bill::send & FS::cust_pay::delete
220       use Mail::Header;
221       use Mail::Internet 1.44;
222       use Date::Format;
223       my $from = $conf->config('invoice_from'); #??? as good as any
224       $ENV{MAILADDRESS} = $from;
225       my $header = new Mail::Header ( [
226         "From: $from",
227         "To: ". $conf->config('signup_server-email'),
228         "Sender: $from",
229         "Reply-To: $from",
230         "Date: ". time2str("%a, %d %b %Y %X %z", time),
231         "Subject: FREESIDE NOTIFICATION: Signup Server",
232       ] );
233       my $body = [
234         "This is an automatic message from your Freeside installation\n",
235         "informing you a customer has signed up via the signup server:\n",
236         "\n",
237         'custnum     : '. $cust_main->custnum. "\n",
238         'Name        : '. $cust_main->last. ", ". $cust_main->first. "\n",
239         'Agent       : '. $cust_main->agent->agent. "\n",
240         'Package     : '. $part_pkg->pkg. ' - '. $part_pkg->comment. "\n",
241         'Signup Date : '. time2str('%C', time). "\n",
242         'Username    : '. $svc_acct->username. "\n",
243         #'Password    : '. # config file to turn this on if noment insists
244         'Day phone   : '. $cust_main->daytime. "\n",
245         'Night phone : '. $cust_main->night. "\n",
246         'Address     : '. $cust_main->address1. "\n",
247         ( $cust_main->address2
248             ? '              '. $cust_main->address2. "\n"
249             : ''                                           ),
250         '              '. $cust_main->city. ', '. $cust_main->state. '  '.
251                           $cust_main->zip. "\n",
252         ( $cust_main->country eq 'US'
253             ? ''
254             : '              '. $cust_main->country. "\n" ),
255         "\n",
256       ];
257       #if ( $cust_main->balance > 0 ) {
258       #  push @$body,
259       #    "This customer has an outstanding balance and has been suspended.\n";
260       #}
261       my $message = new Mail::Internet ( 'Header' => $header, 'Body' => $body );
262       $!=0;
263       $message->smtpsend( Host => $smtpmachine )
264         or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
265           or warn "[fs_signup_server] can't send email to ".
266                    $conf->config('signup_server-email').
267                    " via server $smtpmachine with SMTP: $!";
268       #end-of-send mail
269     }
270
271   }
272   close $writer;
273   close $reader;
274   warn "connection to $machine lost!  waiting 60 seconds...\n";
275   sleep 60;
276   warn "reconnecting...\n";
277 }
278
279 sub usage {
280   die "Usage:\n\n  fs_signup_server user machine agentnum refnum\n";
281 }
282