import torrus 1.0.9
[freeside.git] / FS / FS / part_export / globalpops_voip.pm
index 13c263b..67b48bb 100644 (file)
@@ -2,13 +2,17 @@ package FS::part_export::globalpops_voip;
 
 use vars qw(@ISA %info);
 use Tie::IxHash;
+use FS::Record qw(qsearch dbh);
 use FS::part_export;
+use FS::phone_avail;
 
 @ISA = qw(FS::part_export);
 
 tie my %options, 'Tie::IxHash',
-  'login'    => { label=>'GlobalPOPs Media Services API login' },
-  'password' => { label=>'GlobalPOPs Media Services API password' },
+  'login'         => { label=>'GlobalPOPs Media Services API login' },
+  'password'      => { label=>'GlobalPOPs Media Services API password' },
+  'endpointgroup' => { label=>'GlobalPOPs endpoint group number' },
+  'dry_run'       => { label=>"Test mode - don't actually provision" },
 ;
 
 %info = (
@@ -28,7 +32,7 @@ sub get_dids {
   my $self = shift;
   my %opt = ref($_[0]) ? %{$_[0]} : @_;
 
-  my %search = ();
+  my %getdids = ();
   #  'orderby' => 'npa', #but it doesn't seem to work :/
 
   if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
@@ -38,7 +42,21 @@ sub get_dids {
   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
     %getdids = ( 'npa'   => $opt{'areacode'} );
   } elsif ( $opt{'state'} ) {
+
+    my @avail = qsearch({
+      'table'    => 'phone_avail',
+      'hashref'  => { 'exportnum'   => $self->exportnum,
+                      'countrycode' => '1', #don't hardcode me when gp goes int'l
+                      'state'       => $opt{'state'},
+                    },
+      'order_by' => 'ORDER BY npa',
+    });
+
+    return [ map $_->npa, @avail ] if @avail; #return cached area codes instead
+
+    #otherwise, search for em
     %getdids = ( 'state' => $opt{'state'} );
+
   }
 
   my $dids = $self->gp_command('getDIDs', %getdids);
@@ -48,8 +66,6 @@ sub get_dids {
 
   my $search = $dids->{'search'};
 
-  #warn Dumper($search);
-
   if ( $search->{'statuscode'} == 302200 ) {
     return [];
   } elsif ( $search->{'statuscode'} != 100 ) {
@@ -60,7 +76,6 @@ sub get_dids {
   my @return = ();
 
   #my $latas = $search->{state}{lata};
-
   my %latas;
   if ( grep $search->{state}{lata}{$_}, qw(name rate_center) ) {
     %latas = map $search->{state}{lata}{$_},
@@ -173,6 +188,44 @@ sub get_dids {
   } elsif ( $opt{'areacode'} ) { #return city (npa-nxx-XXXX)
     @return = sort { lc($a) cmp lc($b) } @return;
   } elsif ( $opt{'state'} ) { #and not other things, then return areacode
+
+    #populate cache
+
+    local $SIG{HUP} = 'IGNORE';
+    local $SIG{INT} = 'IGNORE';
+    local $SIG{QUIT} = 'IGNORE';
+    local $SIG{TERM} = 'IGNORE';
+    local $SIG{TSTP} = 'IGNORE';
+    local $SIG{PIPE} = 'IGNORE';
+
+    my $oldAutoCommit = $FS::UID::AutoCommit;
+    local $FS::UID::AutoCommit = 0;
+    my $dbh = dbh;
+
+    my $errmsg = 'WARNING: error populating phone availability cache: ';
+    my $error = '';
+    foreach my $return (@return) {
+      my $phone_avail = new FS::phone_avail {
+        'exportnum'   => $self->exportnum,
+        'countrycode' => '1', #don't hardcode me when gp goes int'l
+        'state'       => $opt{'state'},
+        'npa'         => $return,
+      };
+      $error = $phone_avail->insert();
+      if ( $error ) {
+        warn $errmsg.$error;
+        last;
+      }
+    }
+
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+    } else {
+      $dbh->commit or warn $errmsg.$dbh->errstr if $oldAutoCommit;
+    }
+
+    #end populate cache
+
     #@return = sort { (split(' ', $a))[0] <=> (split(' ', $b))[0] } @return;
     @return = sort { $a <=> $b } @return;
   } else {
@@ -201,44 +254,96 @@ sub gp_command {
 
 sub _export_insert {
   my( $self, $svc_phone ) = (shift, shift);
+
+  return '' if $self->option('dry_run');
+
   #we want to provision and catch errors now, not queue
+
+  my $r = $self->gp_command('reserveDID',
+    'did'           => $svc_phone->phonenum,
+    'minutes'       => 1,
+    'endpointgroup' => $self->option('endpointgroup'),
+  );
+
+  my $rdid = $r->{did};
+
+  if ( $rdid->{'statuscode'} != 100 ) {
+    return "Error running globalpop reserveDID: ".
+           $rdid->{'statuscode'}. ': '. $rdid->{'status'};
+  }
+
+  my $a = $self->gp_command('assignDID',
+    'did'           => $svc_phone->phonenum,
+    'endpointgroup' => $self->option('endpointgroup'),
+    #'rewrite'
+    #'cnam'
+  );
+
+  my $adid = $a->{did};
+
+  if ( $adid->{'statuscode'} != 100 ) {
+    return "Error running globalpop assignDID: ".
+           $adid->{'statuscode'}. ': '. $adid->{'status'};
+  }
+
+  '';
 }
 
 sub _export_replace {
   my( $self, $new, $old ) = (shift, shift, shift);
+
   #hmm, what's to change?
+  '';
 }
 
 sub _export_delete {
   my( $self, $svc_phone ) = (shift, shift);
-  #probably okay to queue the deletion...
+
+  return '' if $self->option('dry_run');
+
+  #probably okay to queue the deletion...?
+  #but hell, let's do it inline anyway, who wants phone numbers hanging around
+
+  my $r = $self->gp_command('releaseDID',
+    'did'           => $svc_phone->phonenum,
+  );
+
+  my $rdid = $r->{did};
+
+  if ( $rdid->{'statuscode'} != 100 ) {
+    return "Error running globalpop releaseDID: ".
+           $rdid->{'statuscode'}. ': '. $rdid->{'status'};
+  }
+
+  '';
 }
 
 sub _export_suspend {
   my( $self, $svc_phone ) = (shift, shift);
   #nop for now
+  '';
 }
 
 sub _export_unsuspend {
   my( $self, $svc_phone ) = (shift, shift);
   #nop for now
+  '';
 }
 
 #hmm, might forgo queueing entirely for most things, data is too much of a pita
-
-sub globalpops_voip_queue {
-  my( $self, $svcnum, $method ) = (shift, shift, shift);
-  my $queue = new FS::queue {
-    'svcnum' => $svcnum,
-    'job'    => 'FS::part_export::globalpops_voip::globalpops_voip_command',
-  };
-  $queue->insert(
-    $self->option('login'),
-    $self->option('password'),
-    $method,
-    @_,
-  );
-}
+#sub globalpops_voip_queue {
+#  my( $self, $svcnum, $method ) = (shift, shift, shift);
+#  my $queue = new FS::queue {
+#    'svcnum' => $svcnum,
+#    'job'    => 'FS::part_export::globalpops_voip::globalpops_voip_command',
+#  };
+#  $queue->insert(
+#    $self->option('login'),
+#    $self->option('password'),
+#    $method,
+#    @_,
+#  );
+#}
 
 sub globalpops_voip_command {
   my($login, $password, $method, @args) = @_;