This commit was generated by cvs2svn to compensate for changes in r2523,
[freeside.git] / FS / FS / part_export / infostreet.pm
1 package FS::part_export::infostreet;
2
3 use vars qw(@ISA %infostreet2cust_main $DEBUG);
4 use FS::UID qw(dbh);
5 use FS::part_export;
6
7 @ISA = qw(FS::part_export);
8
9 $DEBUG = 0;
10
11 %infostreet2cust_main = (
12   'firstName'   => 'first',
13   'lastName'    => 'last',
14   'address1'    => 'address1',
15   'address2'    => 'address2',
16   'city'        => 'city',
17   'state'       => 'state',
18   'zipCode'     => 'zip',
19   'country'     => 'country',
20   'phoneNumber' => 'daytime',
21   'faxNumber'   => 'night', #noment-request...
22 );
23
24 sub rebless { shift; }
25
26 sub _export_insert {
27   my( $self, $svc_acct ) = (shift, shift);
28   my $cust_main = $svc_acct->cust_svc->cust_pkg->cust_main;
29
30   local $SIG{HUP} = 'IGNORE';
31   local $SIG{INT} = 'IGNORE';
32   local $SIG{QUIT} = 'IGNORE';
33   local $SIG{TERM} = 'IGNORE';
34   local $SIG{TSTP} = 'IGNORE';
35   local $SIG{PIPE} = 'IGNORE';
36   my $oldAutoCommit = $FS::UID::AutoCommit;
37   local $FS::UID::AutoCommit = 0;
38   my $dbh = dbh;
39
40   my $err_or_queue = $self->infostreet_err_or_queue( $svc_acct->svcnum,
41     'createUser', $svc_acct->username, $svc_acct->_password );
42   return $err_or_queue unless ref($err_or_queue);
43   my $jobnum = $err_or_queue->jobnum;
44
45   my %contact_info = ( map {
46     $_ => $cust_main->getfield( $infostreet2cust_main{$_} );
47   } keys %infostreet2cust_main );
48
49   my @emails = grep { $_ ne 'POST' } $cust_main->invoicing_list;
50   $contact_info{'email'} = $emails[0] if @emails;
51
52   #this one is kinda noment-specific
53   $contact_info{'organization'} = $cust_main->agent->agent;
54
55   $err_or_queue = $self->infostreet_queueContact( $svc_acct->svcnum,
56     $svc_acct->username, %contact_info );
57   return $err_or_queue unless ref($err_or_queue);
58
59   # If a quota has been specified set the quota because it is not the default
60   $err_or_queue = $self->infostreet_queueSetQuota( $svc_acct->svcnum, 
61     $svc_acct->username, $svc_acct->quota ) if $svc_acct->quota;
62   return $err_or_queue unless ref($err_or_queue);
63
64   my $error = $err_or_queue->depend_insert( $jobnum );
65   return $error if $error;
66
67   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
68
69   '';
70
71 }
72
73 sub _export_replace {
74   my( $self, $new, $old ) = (shift, shift, shift);
75   return "can't change username with InfoStreet"
76     if $old->username ne $new->username;
77
78   # If the quota has changed then do the export to setQuota
79   my $err_or_queue = $self->infostreet_queueSetQuota( $new->svcnum, $new->username, $new->quota ) 
80         if ( $old->quota != $new->quota );  
81   return $err_or_queue unless ref($err_or_queue);
82
83
84   return '' unless $old->_password ne $new->_password;
85   $self->infostreet_queue( $new->svcnum,
86     'passwd', $new->username, $new->_password );
87 }
88
89 sub _export_delete {
90   my( $self, $svc_acct ) = (shift, shift);
91   $self->infostreet_queue( $svc_acct->svcnum,
92     'purgeAccount,releaseUsername', $svc_acct->username );
93 }
94
95 sub _export_suspend {
96   my( $self, $svc_acct ) = (shift, shift);
97   $self->infostreet_queue( $svc_acct->svcnum,
98     'setStatus', $svc_acct->username, 'DISABLED' );
99 }
100
101 sub _export_unsuspend {
102   my( $self, $svc_acct ) = (shift, shift);
103   $self->infostreet_queue( $svc_acct->svcnum,
104     'setStatus', $svc_acct->username, 'ACTIVE' );
105 }
106
107 sub infostreet_queue {
108   my( $self, $svcnum, $method ) = (shift, shift, shift);
109   my $queue = new FS::queue {
110     'svcnum' => $svcnum,
111     'job'    => 'FS::part_export::infostreet::infostreet_command',
112   };
113   $queue->insert(
114     $self->option('url'),
115     $self->option('login'),
116     $self->option('password'),
117     $self->option('groupID'),
118     $method,
119     @_,
120   );
121 }
122
123 #ick false laziness
124 sub infostreet_err_or_queue {
125   my( $self, $svcnum, $method ) = (shift, shift, shift);
126   my $queue = new FS::queue {
127     'svcnum' => $svcnum,
128     'job'    => 'FS::part_export::infostreet::infostreet_command',
129   };
130   $queue->insert(
131     $self->option('url'),
132     $self->option('login'),
133     $self->option('password'),
134     $self->option('groupID'),
135     $method,
136     @_,
137   ) or $queue;
138 }
139
140 sub infostreet_queueContact {
141   my( $self, $svcnum ) = (shift, shift);
142   my $queue = new FS::queue {
143     'svcnum' => $svcnum,
144     'job'    => 'FS::part_export::infostreet::infostreet_setContact',
145   };
146   $queue->insert(
147     $self->option('url'),
148     $self->option('login'),
149     $self->option('password'),
150     $self->option('groupID'),
151     @_,
152   ) or $queue;
153 }
154
155 sub infostreet_setContact {
156   my($url, $is_username, $is_password, $groupID, $username, %contact_info) = @_;
157   my $accountID = infostreet_command($url, $is_username, $is_password, $groupID,
158     'getAccountID', $username);
159   foreach my $field ( keys %contact_info ) {
160     infostreet_command($url, $is_username, $is_password, $groupID,
161       'setContactField', [ 'int'=>$accountID ], $field, $contact_info{$field} );
162   }
163
164 }
165
166 sub infostreet_queueSetQuota {
167
168  my( $self, $svcnum) = (shift, shift);
169  my $queue = new FS::queue {
170     'svcnum' => $svcnum,
171     'job'    => 'FS::part_export::infostreet::infostreet_setQuota',
172  };
173
174  $queue->insert(
175     $self->option('url'),
176     $self->option('login'),
177     $self->option('password'),
178     $self->option('groupID'),
179     @_,
180  ) or $queue;
181
182 }
183
184 sub infostreet_setQuota {
185   my($url, $is_username, $is_password, $groupID, $username, $quota) = @_;
186   infostreet_command($url, $is_username, $is_password, $groupID, 'setQuota', $username, [ 'int'=> $quota ]  );
187 }
188
189
190 sub infostreet_command { #subroutine, not method
191   my($url, $username, $password, $groupID, $method, @args) = @_;
192
193   warn "[FS::part_export::infostreet] $method ".join(' ', @args)."\n" if $DEBUG;
194
195   #quelle hack
196   if ( $method =~ /,/ ) {
197     foreach my $part ( split(/,\s*/, $method) ) {
198       infostreet_command($url, $username, $password, $groupID, $part, @args);
199     }
200     return;
201   }
202
203   eval "use Frontier::Client;";
204   die $@ if $@;
205
206   eval 'sub Frontier::RPC2::String::repr {
207     my $self = shift;
208     my $value = $$self;
209     $value =~ s/([&<>\"])/$Frontier::RPC2::char_entities{$1}/ge;
210     $value;
211   }';
212   die $@ if $@;
213
214   my $conn = Frontier::Client->new( url => $url );
215   my $key_result = $conn->call( 'authenticate', $username, $password, $groupID);
216   my %key_result = _infostreet_parse($key_result);
217   die $key_result{error} unless $key_result{success};
218   my $key = $key_result{data};
219
220   #my $result = $conn->call($method, $key, @args);
221   my $result = $conn->call( $method, $key,
222                             map {
223                                   if ( ref($_) ) {
224                                     my( $type, $value) = @{$_};
225                                     $conn->$type($value);
226                                   } else {
227                                     $conn->string($_);
228                                   }
229                                 } @args );
230   my %result = _infostreet_parse($result);
231   die $result{error} unless $result{success};
232
233   $result->{data};
234
235 }
236
237 #sub infostreet_command_byid { #subroutine, not method;
238 #  my($url, $username, $password, $groupID, $method, @args ) = @_;
239 #
240 #  infostreet_command
241 #
242 #}
243
244 sub _infostreet_parse { #subroutine, not method
245   my $arg = shift;
246   map {
247     my $value = $arg->{$_};
248     #warn ref($value);
249     $value = $value->value()
250       if ref($value) && $value->isa('Frontier::RPC2::DataType');
251     $_=>$value;
252   } keys %$arg;
253 }
254
255