fix infostreet contact field foo
[freeside.git] / FS / FS / part_export / infostreet.pm
1 package FS::part_export::infostreet;
2
3 use vars qw(@ISA %infostreet2cust_main);
4 use FS::UID qw(dbh);
5 use FS::part_export;
6
7 @ISA = qw(FS::part_export);
8
9 %infostreet2cust_main = (
10   'firstName'   => 'first',
11   'lastName'    => 'last',
12   'address1'    => 'address1',
13   'address2'    => 'address2',
14   'city'        => 'city',
15   'state'       => 'state',
16   'zipCode'     => 'zip',
17   'country'     => 'country',
18   'phoneNumber' => 'daytime',
19   'faxNumber'   => 'night', #noment-request...
20 );
21
22 sub rebless { shift; }
23
24 sub _export_insert {
25   my( $self, $svc_acct ) = (shift, shift);
26   my $cust_main = $svc_acct->cust_svc->cust_pkg->cust_main;
27
28   local $SIG{HUP} = 'IGNORE';
29   local $SIG{INT} = 'IGNORE';
30   local $SIG{QUIT} = 'IGNORE';
31   local $SIG{TERM} = 'IGNORE';
32   local $SIG{TSTP} = 'IGNORE';
33   local $SIG{PIPE} = 'IGNORE';
34   my $oldAutoCommit = $FS::UID::AutoCommit;
35   local $FS::UID::AutoCommit = 0;
36   my $dbh = dbh;
37
38   my $err_or_queue = $self->infostreet_err_or_queue( $svc_acct->svcnum,
39     'createUser', $svc_acct->username, $svc_acct->_password );
40   return $err_or_queue unless ref($err_or_queue);
41   my $jobnum = $err_or_queue->jobnum;
42
43   my %contact_info = ( map {
44     $_ => $cust_main->getfield( $infostreet2cust_main{$_} );
45   } keys %infostreet2cust_main );
46
47   my @emails = grep { $_ ne 'POST' } $cust_main->invoicing_list;
48   $contact_info{'email'} = $emails[0] if @emails;
49
50   #this one is kinda noment-specific
51   $contact_info{'title'} = $cust_main->agent->agent;
52
53   $err_or_queue = $self->infostreet_queueContact( $svc_acct->svcnum,
54     $svc_acct->username, %contact_info );
55   return $err_or_queue unless ref($err_or_queue);
56   my $error = $err_or_queue->depend_insert( $jobnum );
57   return $error if $error;
58
59   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
60
61   '';
62
63 }
64
65 sub _export_replace {
66   my( $self, $new, $old ) = (shift, shift, shift);
67   return "can't change username with InfoStreet"
68     if $old->username ne $new->username;
69   return '' unless $old->_password ne $new->_password;
70   $self->infostreet_queue( $new->svcnum,
71     'passwd', $new->username, $new->_password );
72 }
73
74 sub _export_delete {
75   my( $self, $svc_acct ) = (shift, shift);
76   $self->infostreet_queue( $svc_acct->svcnum,
77     'purgeAccount,releaseUsername', $svc_acct->username );
78 }
79
80 sub _export_suspend {
81   my( $self, $svc_acct ) = (shift, shift);
82   $self->infostreet_queue( $svc_acct->svcnum,
83     'setStatus', $svc_acct->username, 'DISABLED' );
84 }
85
86 sub _export_unsuspend {
87   my( $self, $svc_acct ) = (shift, shift);
88   $self->infostreet_queue( $svc_acct->svcnum,
89     'setStatus', $svc_acct->username, 'ACTIVE' );
90 }
91
92 sub infostreet_queue {
93   my( $self, $svcnum, $method ) = (shift, shift, shift);
94   my $queue = new FS::queue {
95     'svcnum' => $svcnum,
96     'job'    => 'FS::part_export::infostreet::infostreet_command',
97   };
98   $queue->insert(
99     $self->option('url'),
100     $self->option('login'),
101     $self->option('password'),
102     $self->option('groupID'),
103     $method,
104     @_,
105   );
106 }
107
108 #ick false laziness
109 sub infostreet_err_or_queue {
110   my( $self, $svcnum, $method ) = (shift, shift, shift);
111   my $queue = new FS::queue {
112     'svcnum' => $svcnum,
113     'job'    => 'FS::part_export::infostreet::infostreet_command',
114   };
115   $queue->insert(
116     $self->option('url'),
117     $self->option('login'),
118     $self->option('password'),
119     $self->option('groupID'),
120     $method,
121     @_,
122   ) or $queue;
123 }
124
125 sub infostreet_queueContact {
126   my( $self, $svcnum ) = (shift, shift);
127   my $queue = new FS::queue {
128     'svcnum' => $svcnum,
129     'job'    => 'FS::part_export::infostreet::infostreet_setContact',
130   };
131   $queue->insert(
132     $self->option('url'),
133     $self->option('login'),
134     $self->option('password'),
135     $self->option('groupID'),
136     @_,
137   ) or $queue;
138 }
139
140 sub infostreet_setContact {
141   my($url, $is_username, $is_password, $groupID, $username, %contact_info) = @_;
142   my $accountID = infostreet_command($url, $is_username, $is_password, $groupID,
143     'getAccountID', $username);
144   foreach my $field ( %contact_info ) {
145     infostreet_command($url, $is_username, $is_password, $groupID,
146       'setContactField', $field, $contact_info{$field} );
147   }
148
149 }
150
151 sub infostreet_command { #subroutine, not method
152   my($url, $username, $password, $groupID, $method, @args) = @_;
153
154   #quelle hack
155   if ( $method =~ /,/ ) {
156     foreach my $part ( split(/,\s*/, $method) ) {
157       infostreet_command($url, $username, $password, $groupID, $part, @args);
158     }
159     return;
160   }
161
162   eval "use Frontier::Client;";
163
164   my $conn = Frontier::Client->new( url => $url );
165   my $key_result = $conn->call( 'authenticate', $username, $password, $groupID);
166   my %key_result = _infostreet_parse($key_result);
167   die $key_result{error} unless $key_result{success};
168   my $key = $key_result{data};
169
170   #my $result = $conn->call($method, $key, @args);
171   my $result = $conn->call($method, $key, map { $conn->string($_) } @args);
172   my %result = _infostreet_parse($result);
173   die $result{error} unless $result{success};
174
175   $result->{data};
176
177 }
178
179 #sub infostreet_command_byid { #subroutine, not method;
180 #  my($url, $username, $password, $groupID, $method, @args ) = @_;
181 #
182 #  infostreet_command
183 #
184 #}
185
186 sub _infostreet_parse { #subroutine, not method
187   my $arg = shift;
188   map {
189     my $value = $arg->{$_};
190     #warn ref($value);
191     $value = $value->value()
192       if ref($value) && $value->isa('Frontier::RPC2::DataType');
193     $_=>$value;
194   } keys %$arg;
195 }
196
197