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