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