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