import torrus 1.0.9
[freeside.git] / FS / FS / part_export / infostreet.pm
index fbf58bd..ef16c7c 100644 (file)
@@ -1,11 +1,32 @@
 package FS::part_export::infostreet;
 
-use vars qw(@ISA %infostreet2cust_main $DEBUG);
+use vars qw(@ISA %info %infostreet2cust_main $DEBUG);
+use Tie::IxHash;
 use FS::UID qw(dbh);
 use FS::part_export;
 
 @ISA = qw(FS::part_export);
 
+tie my %options, 'Tie::IxHash',
+  'url'      => { label=>'XML-RPC Access URL', },
+  'login'    => { label=>'InfoStreet login', },
+  'password' => { label=>'InfoStreet password', },
+  'groupID'  => { label=>'InfoStreet groupID', },
+;
+
+%info = (
+  'svc'      => 'svc_acct',
+  'desc'     => 'Real-time export to InfoStreet streetSmartAPI',
+  'options'  => \%options,
+  'nodomain' => 'Y',
+  'notes'    => <<'END'
+Real-time export to
+<a href="http://www.infostreet.com/">InfoStreet</a> streetSmartAPI.
+Requires installation of
+<a href="http://search.cpan.org/dist/Frontier-Client">Frontier::Client</a> from CPAN.
+END
+);
+
 $DEBUG = 0;
 
 %infostreet2cust_main = (
@@ -46,7 +67,7 @@ sub _export_insert {
     $_ => $cust_main->getfield( $infostreet2cust_main{$_} );
   } keys %infostreet2cust_main );
 
-  my @emails = grep { $_ ne 'POST' } $cust_main->invoicing_list;
+  my @emails = grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list;
   $contact_info{'email'} = $emails[0] if @emails;
 
   #this one is kinda noment-specific
@@ -55,6 +76,12 @@ sub _export_insert {
   $err_or_queue = $self->infostreet_queueContact( $svc_acct->svcnum,
     $svc_acct->username, %contact_info );
   return $err_or_queue unless ref($err_or_queue);
+
+  # If a quota has been specified set the quota because it is not the default
+  $err_or_queue = $self->infostreet_queueSetQuota( $svc_acct->svcnum, 
+    $svc_acct->username, $svc_acct->quota ) if $svc_acct->quota;
+  return $err_or_queue unless ref($err_or_queue);
+
   my $error = $err_or_queue->depend_insert( $jobnum );
   return $error if $error;
 
@@ -68,6 +95,13 @@ sub _export_replace {
   my( $self, $new, $old ) = (shift, shift, shift);
   return "can't change username with InfoStreet"
     if $old->username ne $new->username;
+
+  # If the quota has changed then do the export to setQuota
+  my $err_or_queue = $self->infostreet_queueSetQuota( $new->svcnum, $new->username, $new->quota ) 
+        if ( $old->quota != $new->quota );  
+  return $err_or_queue unless ref($err_or_queue);
+
+
   return '' unless $old->_password ne $new->_password;
   $self->infostreet_queue( $new->svcnum,
     'passwd', $new->username, $new->_password );
@@ -150,6 +184,30 @@ sub infostreet_setContact {
 
 }
 
+sub infostreet_queueSetQuota {
+
+ my( $self, $svcnum) = (shift, shift);
+ my $queue = new FS::queue {
+    'svcnum' => $svcnum,
+    'job'    => 'FS::part_export::infostreet::infostreet_setQuota',
+ };
+
+ $queue->insert(
+    $self->option('url'),
+    $self->option('login'),
+    $self->option('password'),
+    $self->option('groupID'),
+    @_,
+ ) or $queue;
+
+}
+
+sub infostreet_setQuota {
+  my($url, $is_username, $is_password, $groupID, $username, $quota) = @_;
+  infostreet_command($url, $is_username, $is_password, $groupID, 'setQuota', $username, [ 'int'=> $quota ]  );
+}
+
+
 sub infostreet_command { #subroutine, not method
   my($url, $username, $password, $groupID, $method, @args) = @_;
 
@@ -164,13 +222,15 @@ sub infostreet_command { #subroutine, not method
   }
 
   eval "use Frontier::Client;";
+  die $@ if $@;
 
   eval 'sub Frontier::RPC2::String::repr {
-    my $self = shift
+    my $self = shift;
     my $value = $$self;
     $value =~ s/([&<>\"])/$Frontier::RPC2::char_entities{$1}/ge;
     $value;
   }';
+  die $@ if $@;
 
   my $conn = Frontier::Client->new( url => $url );
   my $key_result = $conn->call( 'authenticate', $username, $password, $groupID);
@@ -213,4 +273,5 @@ sub _infostreet_parse { #subroutine, not method
   } keys %$arg;
 }
 
+1;