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