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