wait()ing on SIGCHLD causing hangs for some folks
[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 FS::SSH qw(sshopen2);
9 use FS::UID qw(adminsuidsetup);
10 use FS::Record qw( qsearch qsearchs );
11 use FS::cust_main_county;
12 use FS::cust_main;
13
14 use vars qw( $opt $Debug );
15
16 $Debug = 0;
17
18 my @payby = qw(CARD PREPAY);
19
20 my $user = shift or die &usage;
21 &adminsuidsetup( $user ); 
22
23 my $machine = shift or die &usage;
24
25 my $agentnum = shift or die &usage;
26 my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } ) or die &usage;
27 my $pkgpart = $agent->pkgpart_hashref;
28
29 my $refnum = shift or die &usage;
30
31 #causing trouble for some folks
32 #$SIG{CHLD} = sub { wait() };
33
34 my($fs_signupd)="/usr/local/sbin/fs_signupd";
35
36 while (1) {
37   my($reader,$writer)=(new IO::Handle, new IO::Handle);
38   $writer->autoflush(1);
39   warn "[fs_signup_server] Connecting to $machine...\n" if $Debug;
40   sshopen2($machine,$reader,$writer,$fs_signupd);
41
42   my $data;
43
44   warn "[fs_signup_server] Sending locales...\n" if $Debug;
45   my @cust_main_county = qsearch('cust_main_county', {} );
46   print $writer $data = join("\n",
47     ( scalar(@cust_main_county) || die "no tax rates (cust_main_county records)" ),
48     map {
49       $_->taxnum,
50       $_->state,
51       $_->county,
52       $_->country,
53     } @cust_main_county
54   ),"\n";
55   warn "[fs_signup_server] $data\n" if $Debug > 2;
56
57   warn "[fs_signup_server] Sending package definitions...\n" if $Debug;
58   my @part_pkg = grep { $_->svcpart('svc_acct') && $pkgpart->{ $_->pkgpart } }
59     qsearch( 'part_pkg', {} );
60   print $writer $data = join("\n",
61     ( scalar(@part_pkg) || die "no usable package definitions, agent $agentnum" ),
62     map {
63       $_->pkgpart,
64       $_->pkg,
65     } @part_pkg
66   ), "\n";
67   warn "[fs_signup_server] $data\n" if $Debug > 2;
68
69   warn "[fs_signup_server] Sending POPs...\n" if $Debug;
70   my @svc_acct_pop = qsearch ('svc_acct_pop',{} );
71   print $writer $data = join("\n",
72     ( scalar(@svc_acct_pop) || die "No points of presence (svc_acct_pop records)" ),
73     map {
74       $_->popnum,
75       $_->city,
76       $_->state,
77       $_->ac,
78       $_->exch,
79       $_->loc,
80     } @svc_acct_pop
81   ), "\n";
82   warn "[fs_signup_server] $data\n" if $Debug > 2;
83
84   warn "[fs_signup_server] Entering main loop...\n" if $Debug;
85   while (1) {
86     warn "[fs_signup_server] Reading (waiting for) signup data...\n" if $Debug;
87     chop( my(
88       $first, $last, $ss, $company, $address1, $address2, $city, $county,
89       $state, $zip, $country, $daytime, $night, $fax, $payby, $payinfo,
90       $paydate, $payname, $invoicing_list, $pkgpart, $username, $password,
91       $popnum,
92     ) = map { scalar(<$reader>) } ( 1 .. 23 ) );
93
94     warn "[fs_signup_server] Processing signup...\n" if $Debug;
95
96     my $error = '';
97
98     #shares some stuff with htdocs/edit/process/cust_main.cgi... take any
99     # common that are still here and library them.
100     my $cust_main = new FS::cust_main ( {
101       'custnum'  => '',
102       'agentnum' => $agentnum,
103       'refnum'   => $refnum,
104       'last'     => $last,
105       'first'    => $first,
106       'ss'       => $ss,
107       'company'  => $company,
108       'address1' => $address1,
109       'address2' => $address2,
110       'city'     => $city,
111       'county'   => $county,
112       'state'    => $state,
113       'zip'      => $zip,
114       'country'  => $country,
115       'daytime'  => $daytime,
116       'night'    => $night,
117       'fax'      => $fax,
118       'payby'    => $payby,
119       'payinfo'  => $payinfo,
120       'paydate'  => $paydate,
121       'payname'  => $payname,
122     } );
123
124     $error = "Illegal payment type" unless grep { $_ eq $payby } @payby;
125
126     my @invoicing_list = split( /\s*\,\s*/, $invoicing_list );
127
128     $error ||= $cust_main->check_invoicing_list( \@invoicing_list );
129
130     my $part_pkg = qsearchs( 'part_pkg', { 'pkgpart' => $pkgpart } )
131       or $error ||= "WARNING: unknown pkgpart $pkgpart";
132     my $svcpart = $part_pkg->svcpart unless $error;
133
134     # this should wind up in FS::cust_pkg!
135     my $agent = qsearchs( 'agent', { 'agentnum' => $agentnum } );
136     my $pkgpart_href = $agent->pkgpart_hashref;
137     $error ||= "WARNING: agent $agentnum can't purchase pkgpart $pkgpart"
138       unless $pkgpart_href->{ $pkgpart };
139
140     my $cust_pkg = new FS::cust_pkg ( {
141       #later#'custnum' => $custnum,
142       'pkgpart' => $pkgpart,
143     } );
144     $error ||= $cust_pkg->check;
145
146     my $svc_acct = new FS::svc_acct ( {
147       'svcpart'   => $svcpart,
148       'username'  => $username,
149       '_password' => $password,
150       'popnum'    => $popnum,
151     } );
152
153     my $y = $svc_acct->setdefault; # arguably should be in new method
154     $error ||= $y unless ref($y);
155     #and just in case you were silly
156     $svc_acct->svcpart($svcpart);
157     $svc_acct->username($username);
158     $svc_acct->_password($password);
159     $svc_acct->popnum($popnum);
160
161     $error ||= $svc_acct->check;
162
163     $error ||= $cust_main->insert;
164     if ( $cust_pkg && ! $error ) { #in this case, $cust_pkg should always
165                                    #be definied, but....
166       $cust_pkg->custnum( $cust_main->custnum );
167       $error ||= $cust_pkg->insert; 
168       warn "WARNING: $error on pre-checked cust_pkg record!" if $error;
169       $svc_acct->pkgnum( $cust_pkg->pkgnum );
170       $error ||= $svc_acct->insert;
171       warn "WARNING: $error on pre-checked svc_acct record!" if $error;
172     }
173
174     warn "[fs_signup_server] Sending results...\n" if $Debug;
175     print $writer $error, "\n";
176
177     $cust_main->invoicing_list( \@invoicing_list ) unless $error;
178
179   }
180   close $writer;
181   close $reader;
182   warn "connection to $machine lost!  waiting 60 seconds...\n";
183   sleep 60;
184   warn "reconnecting...\n";
185 }
186
187 sub usage {
188   die "Usage:\n\n  fs_signup_server user machine agentnum refnum\n";
189 }
190