fix setContactField email
[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::part_export;
5
6 @ISA = qw(FS::part_export);
7
8 %infostreet2cust_main = (
9   'firstName'   => 'first',
10   'lastName'    => 'last',
11   'address1'    => 'address1',
12   'address2'    => 'address2',
13   'city'        => 'city',
14   'state'       => 'state',
15   'zipCode'     => 'zip',
16   'country'     => 'country',
17   'phoneNumber' => 'daytime',
18   'faxNumber'   => 'night',
19 );
20
21 sub rebless { shift; }
22
23 sub _export_insert {
24   my( $self, $svc_acct ) = (shift, shift);
25   my $cust_main = $svc_acct->cust_svc->cust_pkg->cust_main;
26   my $accountID = $self->infostreet_queue( $svc_acct->svcnum,
27     'createUser', $svc_acct->username, $svc_acct->_password );
28   foreach my $infostreet_field ( keys %infostreet2cust_main ) {
29     my $error = $self->infostreet_queue( $svc_acct->svcnum,
30       'setContactField', $accountID, $infostreet_field,
31         $cust_main->getfield( $infostreet2cust_main{$infostreet_field} ) );
32     return $error if $error;
33   }
34
35   my @emails = grep { $_ ne 'POST' } $cust_main->invoicing_list;
36   if ( @emails ) {
37     my $error = $self->infostreet_queue( $svc_acct->svcnum,
38       'setContactField', $accountID, 'email', $emails[0] );
39     return $error if $error;
40   }
41
42   #this one is kinda noment-specific
43   $self->infostreet_queue( $svc_acct->svcnum,
44     'setContactField', $accountID, 'title', $cust_main->agent->agent );
45
46 }
47
48 sub _export_replace {
49   my( $self, $new, $old ) = (shift, shift, shift);
50   return "can't change username with InfoStreet"
51     if $old->username ne $new->username;
52   return '' unless $old->_password ne $new->_password;
53   $self->infostreet_queue( $new->svcnum,
54     'passwd', $new->username, $new->_password );
55 }
56
57 sub _export_delete {
58   my( $self, $svc_acct ) = (shift, shift);
59   $self->infostreet_queue( $svc_acct->svcnum,
60     'purgeAccount,releaseUsername', $svc_acct->username );
61 }
62
63 sub _export_suspend {
64   my( $self, $svc_acct ) = (shift, shift);
65   $self->infostreet_queue( $svc_acct->svcnum,
66     'setStatus', $svc_acct->username, 'DISABLED' );
67 }
68
69 sub _export_unsuspend {
70   my( $self, $svc_acct ) = (shift, shift);
71   $self->infostreet_queue( $svc_acct->svcnum,
72     'setStatus', $svc_acct->username, 'ACTIVE' );
73 }
74
75 sub infostreet_queue {
76   my( $self, $svcnum, $method ) = (shift, shift, shift);
77   my $queue = new FS::queue {
78     'svcnum' => $svcnum,
79     'job'    => 'FS::part_export::infostreet::infostreet_command',
80   };
81   $queue->insert(
82     $self->option('url'),
83     $self->option('login'),
84     $self->option('password'),
85     $self->option('groupID'),
86     $method,
87     @_,
88   );
89 }
90
91 sub infostreet_command { #subroutine, not method
92   my($url, $username, $password, $groupID, $method, @args) = @_;
93
94   #quelle hack
95   if ( $method =~ /,/ ) {
96     foreach my $part ( split(/,\s*/, $method) ) {
97       infostreet_command($url, $username, $password, $groupID, $part, @args);
98     }
99     return;
100   }
101
102   eval "use Frontier::Client;";
103
104   my $conn = Frontier::Client->new( url => $url );
105   my $key_result = $conn->call( 'authenticate', $username, $password, $groupID);
106   my %key_result = _infostreet_parse($key_result);
107   die $key_result{error} unless $key_result{success};
108   my $key = $key_result{data};
109
110   #my $result = $conn->call($method, $key, @args);
111   my $result = $conn->call($method, $key, map { $conn->string($_) } @args);
112   my %result = _infostreet_parse($result);
113   die $result{error} unless $result{success};
114
115   $result->{data};
116
117 }
118
119 #sub infostreet_command_byid { #subroutine, not method;
120 #  my($url, $username, $password, $groupID, $method, @args ) = @_;
121 #
122 #  infostreet_command
123 #
124 #}
125
126 sub _infostreet_parse { #subroutine, not method
127   my $arg = shift;
128   map {
129     my $value = $arg->{$_};
130     #warn ref($value);
131     $value = $value->value()
132       if ref($value) && $value->isa('Frontier::RPC2::DataType');
133     $_=>$value;
134   } keys %$arg;
135 }
136
137