fix infostreet contact field setting
[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{'title'} = $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   my $error = $err_or_queue->depend_insert( $jobnum );
59   return $error if $error;
60
61   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
62
63   '';
64
65 }
66
67 sub _export_replace {
68   my( $self, $new, $old ) = (shift, shift, shift);
69   return "can't change username with InfoStreet"
70     if $old->username ne $new->username;
71   return '' unless $old->_password ne $new->_password;
72   $self->infostreet_queue( $new->svcnum,
73     'passwd', $new->username, $new->_password );
74 }
75
76 sub _export_delete {
77   my( $self, $svc_acct ) = (shift, shift);
78   $self->infostreet_queue( $svc_acct->svcnum,
79     'purgeAccount,releaseUsername', $svc_acct->username );
80 }
81
82 sub _export_suspend {
83   my( $self, $svc_acct ) = (shift, shift);
84   $self->infostreet_queue( $svc_acct->svcnum,
85     'setStatus', $svc_acct->username, 'DISABLED' );
86 }
87
88 sub _export_unsuspend {
89   my( $self, $svc_acct ) = (shift, shift);
90   $self->infostreet_queue( $svc_acct->svcnum,
91     'setStatus', $svc_acct->username, 'ACTIVE' );
92 }
93
94 sub infostreet_queue {
95   my( $self, $svcnum, $method ) = (shift, shift, shift);
96   my $queue = new FS::queue {
97     'svcnum' => $svcnum,
98     'job'    => 'FS::part_export::infostreet::infostreet_command',
99   };
100   $queue->insert(
101     $self->option('url'),
102     $self->option('login'),
103     $self->option('password'),
104     $self->option('groupID'),
105     $method,
106     @_,
107   );
108 }
109
110 #ick false laziness
111 sub infostreet_err_or_queue {
112   my( $self, $svcnum, $method ) = (shift, shift, shift);
113   my $queue = new FS::queue {
114     'svcnum' => $svcnum,
115     'job'    => 'FS::part_export::infostreet::infostreet_command',
116   };
117   $queue->insert(
118     $self->option('url'),
119     $self->option('login'),
120     $self->option('password'),
121     $self->option('groupID'),
122     $method,
123     @_,
124   ) or $queue;
125 }
126
127 sub infostreet_queueContact {
128   my( $self, $svcnum ) = (shift, shift);
129   my $queue = new FS::queue {
130     'svcnum' => $svcnum,
131     'job'    => 'FS::part_export::infostreet::infostreet_setContact',
132   };
133   $queue->insert(
134     $self->option('url'),
135     $self->option('login'),
136     $self->option('password'),
137     $self->option('groupID'),
138     @_,
139   ) or $queue;
140 }
141
142 sub infostreet_setContact {
143   my($url, $is_username, $is_password, $groupID, $username, %contact_info) = @_;
144   my $accountID = infostreet_command($url, $is_username, $is_password, $groupID,
145     'getAccountID', $username);
146   foreach my $field ( keys %contact_info ) {
147     infostreet_command($url, $is_username, $is_password, $groupID,
148       'setContactField', $accountID, $field, $contact_info{$field} );
149   }
150
151 }
152
153 sub infostreet_command { #subroutine, not method
154   my($url, $username, $password, $groupID, $method, @args) = @_;
155
156   warn "[FS::part_export::infostreet] $method ".join(' ', @args)."\n" if $DEBUG;
157
158   #quelle hack
159   if ( $method =~ /,/ ) {
160     foreach my $part ( split(/,\s*/, $method) ) {
161       infostreet_command($url, $username, $password, $groupID, $part, @args);
162     }
163     return;
164   }
165
166   eval "use Frontier::Client;";
167
168   my $conn = Frontier::Client->new( url => $url );
169   my $key_result = $conn->call( 'authenticate', $username, $password, $groupID);
170   my %key_result = _infostreet_parse($key_result);
171   die $key_result{error} unless $key_result{success};
172   my $key = $key_result{data};
173
174   #my $result = $conn->call($method, $key, @args);
175   my $result = $conn->call($method, $key, map { $conn->string($_) } @args);
176   my %result = _infostreet_parse($result);
177   die $result{error} unless $result{success};
178
179   $result->{data};
180
181 }
182
183 #sub infostreet_command_byid { #subroutine, not method;
184 #  my($url, $username, $password, $groupID, $method, @args ) = @_;
185 #
186 #  infostreet_command
187 #
188 #}
189
190 sub _infostreet_parse { #subroutine, not method
191   my $arg = shift;
192   map {
193     my $value = $arg->{$_};
194     #warn ref($value);
195     $value = $value->value()
196       if ref($value) && $value->isa('Frontier::RPC2::DataType');
197     $_=>$value;
198   } keys %$arg;
199 }
200
201